Skip to content
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

Add extra debug logging for DB connection in minder-server #3478

Merged
merged 5 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
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
8 changes: 8 additions & 0 deletions cmd/server/app/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ import (
// This file contains logic shared between different commands.

func wireUpDB(ctx context.Context, cfg *serverconfig.Config) (db.Store, func(), error) {
zerolog.Ctx(ctx).Debug().
Str("name", cfg.Database.Name).
Str("host", cfg.Database.Host).
Str("user", cfg.Database.User).
Str("ssl_mode", cfg.Database.SSLMode).
Int("port", cfg.Database.Port).
Msg("connecting to minder database")

dbConn, _, err := cfg.Database.GetDBConnection(ctx)
if err != nil {
return nil, nil, fmt.Errorf("unable to connect to database: %w", err)
Expand Down
27 changes: 19 additions & 8 deletions cmd/server/app/encryption_rotate.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ var rotateCmd = &cobra.Command{

ctx := logger.FromFlags(cfg.LoggingConfig).WithContext(context.Background())

zerolog.Ctx(ctx).Debug().
Str("default_key_id", cfg.Crypto.Default.KeyID).
Str("default_algorithm", string(crypto.DefaultAlgorithm)).
Msg("default encryption settings")

// instantiate `db.Store` so we can run queries
store, closer, err := wireUpDB(ctx, cfg)
if err != nil {
Expand Down Expand Up @@ -151,32 +156,38 @@ func runRotationBatch(
if token.EncryptedAccessToken.Valid {
deserialized, err := crypto.DeserializeEncryptedData(token.EncryptedAccessToken.RawMessage)
if err != nil {
return 0, tokenError(token.ID, err)
return 0, tokenError(token.ID, "secret deserialization", err)
}
oldSecret = deserialized
} else if token.EncryptedToken.Valid {
oldSecret = crypto.NewBackwardsCompatibleEncryptedData(token.EncryptedToken.String)
} else {
// this should never happen
return 0, tokenError(token.ID, errors.New("no encrypted secret found"))
return 0, tokenError(token.ID, "secret retrieval", errors.New("no encrypted secret found"))
}

zerolog.Ctx(ctx).Debug().
Int32("token_id", token.ID).
Str("key_version", oldSecret.KeyVersion).
Str("algorithm", string(oldSecret.Algorithm)).
Msg("re-encrypting old secret")

// decrypt the secret
decrypted, err := engine.DecryptOAuthToken(oldSecret)
if err != nil {
return 0, tokenError(token.ID, err)
return 0, tokenError(token.ID, "decryption", err)
}

// re-encrypt it with new key/algorithm
encrypted, err := engine.EncryptOAuthToken(&decrypted)
if err != nil {
return 0, tokenError(token.ID, err)
return 0, tokenError(token.ID, "encryption", err)
}

// update DB
serialized, err := encrypted.Serialize()
if err != nil {
return 0, tokenError(token.ID, err)
return 0, tokenError(token.ID, "secret serialization", err)
}

zerolog.Ctx(ctx).
Expand All @@ -188,15 +199,15 @@ func runRotationBatch(
Secret: serialized,
})
if err != nil {
return 0, tokenError(token.ID, err)
return 0, tokenError(token.ID, "secret update in database", err)
}
}

return int64(len(batch)), nil
}

func tokenError(tokenID int32, err error) error {
return fmt.Errorf("unable to re-encrypt provider token %d: %s", tokenID, err)
func tokenError(tokenID int32, action string, err error) error {
return fmt.Errorf("unable to re-encrypt provider token %d during %s: %s", tokenID, action, err)
}

func init() {
Expand Down