Skip to content

Commit

Permalink
updated credential encryption (#2233)
Browse files Browse the repository at this point in the history
  • Loading branch information
mekilis authored Feb 3, 2025
1 parent 38890b1 commit 9608d7e
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 40 deletions.
16 changes: 15 additions & 1 deletion cmd/ingest/ingest.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package ingest

import (
"context"
"errors"
"fmt"

"github.com/frain-dev/convoy/config"
"github.com/frain-dev/convoy/database/postgres"
"github.com/frain-dev/convoy/internal/pkg/cli"
"github.com/frain-dev/convoy/internal/pkg/keys"
"github.com/frain-dev/convoy/internal/pkg/limiter"
"github.com/frain-dev/convoy/internal/pkg/memorystore"
"github.com/frain-dev/convoy/internal/pkg/metrics"
Expand Down Expand Up @@ -47,6 +48,19 @@ func AddIngestCommand(a *cli.App) *cobra.Command {
return err
}

km := keys.NewHCPVaultKeyManagerFromConfig(cfg.HCPVault, a.Licenser, a.Cache)
if km.IsSet() {
if _, err = km.GetCurrentKeyFromCache(); err != nil {
if !errors.Is(err, keys.ErrCredentialEncryptionFeatureUnavailable) {
return err
}
km.Unset()
}
}
if err = keys.Set(km); err != nil {
return err
}

err = StartIngest(cmd.Context(), a, cfg, interval)
if err != nil {
return err
Expand Down
24 changes: 14 additions & 10 deletions cmd/utils/init_encryption.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,10 @@ var (

func AddInitEncryptionCommand(a *cli.App) *cobra.Command {
cmd := &cobra.Command{
Use: "init-encryption <encryption-key>",
Short: "Initializes encryption for the specified table columns with the provided encryption key",
Args: cobra.ExactArgs(1),
Use: "init-encryption",
Short: "Initializes encryption for the specified table columns with the encryption key fetched from HCP Vault",
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
encryptionKey := args[0]

if encryptionKey == "" {
return ErrEncryptionKeyCannotBeEmpty
}
timeout, err := cmd.Flags().GetInt("timeout")
if err != nil {
log.WithError(err).Errorln("failed to get timeout")
Expand All @@ -56,7 +51,16 @@ func AddInitEncryptionCommand(a *cli.App) *cobra.Command {
return ErrMissingHCPVaultConfig
}

log.Infof("Initializing encryption with the provided key...")
currentKey, err := km.GetCurrentKey()
if err != nil {
return err
}

if currentKey == "" {
return ErrEncryptionKeyCannotBeEmpty
}

log.Infof("Initializing encryption with the current encryption key...")

db, err := postgres.NewDB(cfg)
if err != nil {
Expand All @@ -65,7 +69,7 @@ func AddInitEncryptionCommand(a *cli.App) *cobra.Command {
}
defer db.Close()

err = keys.InitEncryption(a.Logger, db, km, encryptionKey, timeout)
err = keys.InitEncryption(a.Logger, db, km, currentKey, timeout)
if err != nil {
log.WithError(err).Error("Error initializing encryption key.")
}
Expand Down
34 changes: 12 additions & 22 deletions cmd/utils/revert_encryption.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package utils

import (
"errors"
"github.com/frain-dev/convoy/config"
"github.com/frain-dev/convoy/database/postgres"
"github.com/frain-dev/convoy/internal/pkg/cli"
Expand All @@ -13,23 +12,17 @@ import (

func AddRevertEncryptionCommand(a *cli.App) *cobra.Command {
cmd := &cobra.Command{
Use: "revert-encryption <encryption-key>",
Short: "Reverts the encryption initialization for the specified table columns with the provided encryption key",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
encryptionKey := args[0]
Use: "revert-encryption",
Short: "Reverts the encryption initialization for the specified table columns with the encryption key fetched from HCP Vaults",
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, _ []string) error {

if encryptionKey == "" {
return ErrEncryptionKeyCannotBeEmpty
}
timeout, err := cmd.Flags().GetInt("timeout")
if err != nil {
log.WithError(err).Errorln("failed to get timeout")
return err
}

log.Infof("Reverting encryption with the provided key...")

cfg, err := config.Get()
if err != nil {
log.WithError(err).Error("Error fetching the config.")
Expand All @@ -46,28 +39,25 @@ func AddRevertEncryptionCommand(a *cli.App) *cobra.Command {
return ErrMissingHCPVaultConfig
}

// Ensure the encryption key matches the current key
currentKey, err := km.GetCurrentKey()
currentKey, err := km.GetHCPSecretKey()
if err != nil {
if !errors.Is(err, keys.ErrCredentialEncryptionFeatureUnavailable) {
return err
}
return err
}
if encryptionKey != currentKey {
if !errors.Is(err, keys.ErrCredentialEncryptionFeatureUnavailable) {
return ErrEncryptionKeyMismatch
}
// allow any key if downgraded

if currentKey == "" {
return ErrEncryptionKeyCannotBeEmpty
}

log.Infof("Reverting encryption with the current encryption key...")

db, err := postgres.NewDB(cfg)
if err != nil {
log.WithError(err).Error("Error connecting to database.")
return err
}
defer db.Close()

err = keys.RevertEncryption(a.Logger, db, encryptionKey, timeout)
err = keys.RevertEncryption(a.Logger, db, currentKey, timeout)
if err != nil {
log.WithError(err).Error("Error reverting the encryption key.")
}
Expand Down
7 changes: 0 additions & 7 deletions internal/pkg/keys/encrypter_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,6 @@ func InitEncryption(lo log.StdLogger, db database.Database, km KeyManager, encry
}
}

err = km.SetKey(encryptionKey)
if err != nil {
rollback(lo, tx)
lo.WithError(err).Error("failed to set encryption key")
return fmt.Errorf("failed to update encryption key: %w", err)
}

// Commit the transaction
if err := tx.Commit(); err != nil {
lo.WithError(err).Error("failed to commit transaction")
Expand Down
4 changes: 4 additions & 0 deletions internal/pkg/keys/hcpvault.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,11 @@ func (k *HCPVaultKeyManager) GetCurrentKey() (string, error) {
if !k.licenser.CredentialEncryption() {
return "", ErrCredentialEncryptionFeatureUnavailable
}
return k.GetHCPSecretKey()
}

// GetHCPSecretKey retrieves the current key from HCP Vault API.
func (k *HCPVaultKeyManager) GetHCPSecretKey() (string, error) {
retryCount := 1
for {
if err := k.ensureValidToken(); err != nil {
Expand Down

0 comments on commit 9608d7e

Please sign in to comment.