Skip to content

Regenerate credentials+secrets on missing secret(s) #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 50 additions & 2 deletions controllers/manageddatabase_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"crypto/rand"
"encoding/hex"
"fmt"
"strings"
"time"

"github.com/go-logr/logr"
Expand Down Expand Up @@ -329,8 +330,51 @@ func (c *ManagedDatabaseController) reconcileCredentialsForVersion(oneMigration
secretsToAdd.Remove(newSecretName)
}

// TODO: handle the case of regenerating any database users for which we've
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another way to handle this would be to just remove the DB user and then let it go through the normal rectification flow, right?

// lost the secret (secretsToAdd remainder)
// Regenerate any missing secrets whose credentials are in the database
for secretToAddItem := range secretsToAdd.Iterator().C {
secretToAdd := secretToAddItem.(string)
oneMigration.log.Info("Missing database secret", "secret", secretToAdd)

secretMigration := migrationFromSecret(oneMigration.db.Name, secretToAdd)

// Renew the database user
dbUserToRenew := migrationDBUsername(secretMigration)
newPassword, err := randPassword()
if err != nil {
return nil
}

oneMigration.log.Info("Renewing user account", "username", dbUserToRenew)
if err := admin.VerifyUnusedAndDeleteCredentials(dbUserToRenew); err != nil {
return err
}

c.metrics.CredentialsRevoked.Inc()

if err := admin.WriteCredentials(dbUserToRenew, newPassword); err != nil {
return err
}

c.metrics.CredentialsCreated.Inc()

// Write the missing secret
secretLabels := getStandardLabels(oneMigration.db, oneMigration.version)
if err := writeCredentialsSecret(
oneMigration.ctx,
c.Client,
oneMigration.db.Namespace,
secretToAdd,
dbUserToRenew,
newPassword,
secretLabels,
oneMigration.db,
c.Scheme,
); err != nil {
return err
}

}

return nil
}

Expand Down Expand Up @@ -395,6 +439,10 @@ func migrationDBUsername(migrationName string) string {
return fmt.Sprintf("%s%s", DBUsernamePrefix, migrationName)
}

func migrationFromSecret(dbName, secretName string) string {
return strings.TrimPrefix(secretName, dbName+"-")
}

func randPassword() (string, error) {
identBytes := make([]byte, 16)
if _, err := rand.Read(identBytes); err != nil {
Expand Down