From a96d8b937336fed928b003a6d1474c3fc6a002b6 Mon Sep 17 00:00:00 2001 From: Todd Knight Date: Tue, 16 Nov 2021 14:51:00 -0700 Subject: [PATCH] Check no unrecongized fields are passed in. Add back in the helper methods for rotating credentials. --- plugin/auth.go | 117 +++++++++++++++++++++++++++++++++++++++--- plugin/plugin.go | 16 +++++- plugin/plugin_test.go | 11 ++++ 3 files changed, 135 insertions(+), 9 deletions(-) diff --git a/plugin/auth.go b/plugin/auth.go index f90ef59..50cfada 100644 --- a/plugin/auth.go +++ b/plugin/auth.go @@ -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 +} diff --git a/plugin/plugin.go b/plugin/plugin.go index 45ac042..8fc6543 100644 --- a/plugin/plugin.go +++ b/plugin/plugin.go @@ -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) } diff --git a/plugin/plugin_test.go b/plugin/plugin_test.go index cfde8b2..a505b3c 100644 --- a/plugin/plugin_test.go +++ b/plugin/plugin_test.go @@ -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 {