Skip to content

Commit

Permalink
use password in favor of self_managed_password
Browse files Browse the repository at this point in the history
  • Loading branch information
fairclothjm committed Dec 9, 2024
1 parent 0f69018 commit 63daa27
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
2 changes: 1 addition & 1 deletion builtin/logical/database/backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ func TestBackend_basic(t *testing.T) {
defer b.Cleanup(context.Background())

cleanup, connURL := postgreshelper.PrepareTestContainer(t)
defer cleanup()
t.Cleanup(cleanup)

// Configure a connection
data := map[string]interface{}{
Expand Down
28 changes: 22 additions & 6 deletions builtin/logical/database/path_roles.go
Original file line number Diff line number Diff line change
Expand Up @@ -636,15 +636,33 @@ func (b *databaseBackend) pathStaticRoleCreateUpdate(ctx context.Context, req *l
}
}

if smPasswordRaw, ok := data.GetOk("self_managed_password"); ok && createRole {
role.StaticAccount.SelfManagedPassword = smPasswordRaw.(string)
}

dbConfig, err := b.DatabaseConfig(ctx, req.Storage, role.DBName)
if err != nil {
return nil, err
}

lastVaultRotation := role.StaticAccount.LastVaultRotation
if passwordRaw, ok := data.GetOk("password"); ok {
// We will allow users to update the password until the point where
// Vault assumes management of the account so that we don't break the
// promise of Vault being the source of truth.
updateAllowed := lastVaultRotation.IsZero() || lastVaultRotation.After(time.Now())
if updateAllowed {
role.StaticAccount.Password = passwordRaw.(string)
if dbConfig.ConnectionDetails["self_managed"].(bool) {
// continue to support the deprecated self_managed_password field
role.StaticAccount.SelfManagedPassword = passwordRaw.(string)
}
} else {
return logical.ErrorResponse("update not allowed after rotation", "role", name, "lastVaultRotation", lastVaultRotation), nil
}
}

if smPasswordRaw, ok := data.GetOk("self_managed_password"); ok && createRole {
role.StaticAccount.SelfManagedPassword = smPasswordRaw.(string)
role.StaticAccount.Password = smPasswordRaw.(string)
}

if skipImportRotationRaw, ok := data.GetOk("skip_import_rotation"); ok {
if !createRole {
response.AddWarning("skip_import_rotation has no effect on updates")
Expand All @@ -665,8 +683,6 @@ func (b *databaseBackend) pathStaticRoleCreateUpdate(ctx context.Context, req *l
return logical.ErrorResponse("credential_config validation failed: %s", err), nil
}

lastVaultRotation := role.StaticAccount.LastVaultRotation

// Only call setStaticAccount if we're creating the role for the
// first time
var item *queue.Item
Expand Down
2 changes: 1 addition & 1 deletion builtin/logical/database/rotation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func TestBackend_StaticRole_Rotation_basic(t *testing.T) {
b.schedule = &TestSchedule{}

cleanup, connURL := postgreshelper.PrepareTestContainer(t)
defer cleanup()
t.Cleanup(cleanup)

// create the database user
createTestPGUser(t, connURL, dbUser, dbUserDefaultPassword, testRoleStaticCreate)
Expand Down

0 comments on commit 63daa27

Please sign in to comment.