Check no unrecongized fields are passed in. Add back in the helper methods for rotating credentials.

This commit is contained in:
Todd Knight committed 2021-11-16 14:51:00 -07:00
1 parent d8471243e3
commit a96d8b9373
3 files changed
+135 -9

No files matched your search

+109 -8
View File
@@ -11,6 +11,7 @@ import (
"github.com/hashicorp/boundary/sdk/pbs/controller/api/resources/hostcatalogs"
hauth "github.com/manicminer/hamilton/auth"
"github.com/manicminer/hamilton/environments"
"github.com/manicminer/hamilton/msgraph"
"github.com/manicminer/hamilton/odata"
"github.com/mitchellh/mapstructure"
)
@@ -20,7 +21,6 @@ type AuthorizationInfo struct {
AuthParams AuthParams
}
// The allowed attribute fields from the controller to this plugin
type Attributes struct {
SubscriptionId string `mapstructure:"subscription_id"`
ClientId string `mapstructure:"client_id"`
@@ -28,7 +28,6 @@ type Attributes struct {
DisableCredentialRotation bool `mapstructure:"disable_credential_rotation"`
}
// The allowed secret fields passed in from the controller to this plugin
type SecretData struct {
SecretValue string `mapstructure:"secret_value"`
SecretId string `mapstructure:"secret_id"`
@@ -36,11 +35,10 @@ type SecretData struct {
}
type AuthParams struct {
SubscriptionId string
ClientId string
ClientObjectId string
TenantId string
SubscriptionId string
ClientId string
ClientObjectId string
TenantId string
SecretValue string
SecretId string
CredsLastRotatedTime time.Time
@@ -120,7 +118,7 @@ func (a *AuthorizationInfo) autorestAuthorizer() (autorest.Authorizer, error) {
return authorizer, nil
}
func (a *AuthorizationInfo) populateObjectId(ctx context.Context, _ ...Option) error {
func (a *AuthorizationInfo) populateObjectId(ctx context.Context, opt ...Option) error {
if a.AuthParams.ClientObjectId != "" {
return nil
}
@@ -152,3 +150,106 @@ func (a *AuthorizationInfo) populateObjectId(ctx context.Context, _ ...Option) e
return nil
}
// rotateCredentials creates a new password, then uses a client with that new
// password to revoke the old.
func rotateCredential(ctx context.Context, authzInfo *AuthorizationInfo, opt ...Option) (*msgraph.PasswordCredential, error) {
if authzInfo == nil {
return nil, errors.New("empty authz info")
}
if authzInfo.AuthParams.SecretId == "" {
return nil, errors.New("missing original secret id")
}
// Ensure the object ID is set
if err := authzInfo.populateObjectId(ctx); err != nil {
return nil, fmt.Errorf("error fetching object id: %w", err)
}
newCred, err := addCredential(ctx, authzInfo)
if err != nil {
return nil, fmt.Errorf("error adding password: %w", err)
}
if newCred == nil {
return nil, errors.New("new credential is nil after adding")
}
if newCred.SecretText == nil {
return nil, errors.New("new credential secret text is nil after adding")
}
if err := removeCredential(ctx, authzInfo); err != nil {
return nil, fmt.Errorf("error removing previous credential: %w", err)
}
return newCred, nil
}
func addCredential(ctx context.Context, authzInfo *AuthorizationInfo, opt ...Option) (*msgraph.PasswordCredential, error) {
if authzInfo == nil {
return nil, errors.New("empty authz info")
}
// Ensure the object ID is set
if err := authzInfo.populateObjectId(ctx); err != nil {
return nil, fmt.Errorf("error fetching object id: %w", err)
}
aClient, err := getApplicationsClient(ctx, authzInfo)
if err != nil {
return nil, fmt.Errorf("error getting application client: %w", err)
}
if aClient == nil {
return nil, errors.New("applications client is nil when adding credential")
}
// Create the new password
displayName := fmt.Sprintf("boundary-rotated-%s", time.Now().Format(time.RFC3339))
newPass, _, err := aClient.AddPassword(ctx, authzInfo.AuthParams.ClientObjectId, msgraph.PasswordCredential{
DisplayName: &displayName,
})
if err != nil {
return nil, fmt.Errorf("error creating new password: %w", err)
}
if newPass == nil {
return nil, errors.New("nil new password returned")
}
if newPass.KeyId == nil {
return nil, errors.New("nil key ID for new password")
}
if newPass.SecretText == nil {
return nil, errors.New("nil secret text for new password")
}
return newPass, nil
}
func removeCredential(ctx context.Context, authzInfo *AuthorizationInfo, opt ...Option) error {
// Ensure the object ID is set
if err := authzInfo.populateObjectId(ctx); err != nil {
return fmt.Errorf("error fetching object id: %w", err)
}
opts, err := getOpts(opt...)
if err != nil {
return fmt.Errorf("error parsing ops: %w", err)
}
secretId := authzInfo.AuthParams.SecretId
if opts.withSecretId != "" {
secretId = opts.withSecretId
}
aClient, err := getApplicationsClient(ctx, authzInfo)
if err != nil {
return fmt.Errorf("error getting application client: %w", err)
}
if aClient == nil {
return errors.New("applications client is nil when removing credential")
}
_, err = aClient.RemovePassword(ctx, authzInfo.AuthParams.ClientObjectId, secretId)
if err != nil {
return fmt.Errorf("error removing old password: %w", err)
}
return nil
}
+15 -1
View File
@@ -403,12 +403,20 @@ func validateSecrets(s *structpb.Struct) error {
return nil
}
var allowedFields = map[string]struct{}{
constDisableCredentialRotation: {},
constSubscriptionId: {},
constTenantId: {},
constClientId: {},
}
func validateCatalog(catalog *hostcatalogs.HostCatalog) error {
if catalog == nil {
return status.Error(codes.InvalidArgument, "catalog is nil")
}
var attrs Attributes
if err := mapstructure.Decode(catalog.GetAttributes().AsMap(), &attrs); err != nil {
attrMap := catalog.GetAttributes().AsMap()
if err := mapstructure.Decode(attrMap, &attrs); err != nil {
return status.Errorf(codes.InvalidArgument, "error decoding catalog attributes: %s", err)
}
badFields := make(map[string]string)
@@ -425,6 +433,12 @@ func validateCatalog(catalog *hostcatalogs.HostCatalog) error {
badFields["attributes.tenant_id"] = "This is a required field."
}
for f := range attrMap {
if _, ok := allowedFields[f]; !ok {
badFields[fmt.Sprintf("attributes.%s", f)] = "Unrecognized field."
}
}
if len(badFields) > 0 {
return invalidArgumentError("Invalid arguments in the new catalog", badFields)
}
+11
View File
@@ -100,6 +100,17 @@ func TestValidateCatalog(t *testing.T) {
}),
wantError: true,
},
{
name: "unrecognized field",
catalogAttrs: wrapMap(t, map[string]interface{}{
constDisableCredentialRotation: true,
constClientId: "client_id",
constTenantId: "tenant_id",
constSubscriptionId: "sub_id",
"unrecognized": "unrecognized",
}),
wantError: true,
},
}
for _, tc := range cases {