Skip to content

Commit

Permalink
feat: add master key as param
Browse files Browse the repository at this point in the history
  • Loading branch information
skynet2 committed Oct 1, 2024
1 parent 21b117a commit a39f2eb
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 15 deletions.
10 changes: 10 additions & 0 deletions cmd/vc-rest/startcmd/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ const (
aliasPrefixFlagUsage = "alias prefix" +
commonEnvVarUsageText + aliasPrefixEnvKey

masterKeyFlagName = "local-kms-master-key"
masterKeyEnvKey = "VC_REST_LOCAL_KMS_MASTER_KEY"
masterKeyFlagUsage = "Local KMS master key" +
commonEnvVarUsageText + masterKeyEnvKey

// Linter gosec flags these as "potential hardcoded credentials". They are not, hence the nolint annotations.
kmsSecretsDatabaseTypeFlagName = "default-kms-secrets-database-type" //nolint: gosec
kmsSecretsDatabaseTypeEnvKey = "VC_REST_DEFAULT_KMS_SECRETS_DATABASE_TYPE" //nolint: gosec
Expand Down Expand Up @@ -490,6 +495,7 @@ type kmsParameters struct {
kmsSecretsDatabasePrefix string
secretLockKeyPath string
aliasPrefix string
masterKey string
}

// nolint: gocyclo,funlen
Expand Down Expand Up @@ -953,6 +959,8 @@ func getKMSParameters(cmd *cobra.Command) (*kmsParameters, error) {
secretLockKeyPath := cmdutils.GetUserSetOptionalVarFromString(cmd, secretLockKeyPathFlagName, secretLockKeyPathEnvKey)
aliasPrefix := cmdutils.GetUserSetOptionalVarFromString(cmd, aliasPrefixFlagName, aliasPrefixEnvKey)

masterKey := cmdutils.GetUserSetOptionalVarFromString(cmd, masterKeyFlagName, masterKeyEnvKey)

keyDatabaseType, err := cmdutils.GetUserSetVarFromString(cmd, kmsSecretsDatabaseTypeFlagName,
kmsSecretsDatabaseTypeEnvKey, kmsType != kms.Local)
if err != nil {
Expand All @@ -972,6 +980,7 @@ func getKMSParameters(cmd *cobra.Command) (*kmsParameters, error) {
kmsSecretsDatabaseURL: keyDatabaseURL,
kmsSecretsDatabasePrefix: keyDatabasePrefix,
aliasPrefix: aliasPrefix,
masterKey: masterKey,
}, nil
}

Expand Down Expand Up @@ -1150,6 +1159,7 @@ func createFlags(startCmd *cobra.Command) {
startCmd.Flags().String(kmsEndpointFlagName, "", kmsEndpointFlagUsage)
startCmd.Flags().String(secretLockKeyPathFlagName, "", secretLockKeyPathFlagUsage)
startCmd.Flags().String(aliasPrefixFlagName, "", aliasPrefixFlagUsage)
startCmd.Flags().String(masterKeyFlagName, "", masterKeyFlagUsage)
startCmd.Flags().String(kmsRegionFlagName, "", kmsRegionFlagUsage)
startCmd.Flags().StringP(tlsCertificateFlagName, "", "", tlsCertificateFlagUsage)
startCmd.Flags().StringP(tlsKeyFlagName, "", "", tlsKeyFlagUsage)
Expand Down
1 change: 1 addition & 0 deletions cmd/vc-rest/startcmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,7 @@ func buildEchoHandler(
DBURL: conf.StartupParameters.dbParameters.databaseURL,
DBPrefix: conf.StartupParameters.dbParameters.databasePrefix,
AliasPrefix: conf.StartupParameters.kmsParameters.aliasPrefix,
MasterKey: conf.StartupParameters.kmsParameters.masterKey,
}

defaultVCSKeyManager, err := kms.NewAriesKeyManager(&defaultKmsConfig, metrics)
Expand Down
24 changes: 12 additions & 12 deletions pkg/kms/arieskms.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ package kms

import (
"context"
"errors"
"fmt"
"io"
"os"
"strings"
"time"

Expand Down Expand Up @@ -128,7 +126,10 @@ func NewAriesKeyManager(cfg *Config, metrics metricsProvider) (*KeyManager, erro
}

func createLocalKMS(cfg *Config) (api.Suite, error) {
secretLockService, err := createLocalSecretLock(cfg.SecretLockKeyPath)
secretLockService, err := createLocalSecretLock(
cfg.SecretLockKeyPath,
cfg.MasterKey,
)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -190,17 +191,16 @@ func (km *KeyManager) NewVCSigner(
return signer.NewKMSSigner(fks, signatureType, km.metrics), nil
}

func createLocalSecretLock(keyPath string) (secretlock.Service, error) {
var primaryKeyReader io.Reader
func createLocalSecretLock(
keyPath string,
kmsMasterKey string,
) (secretlock.Service, error) {
var err error
if os.Getenv("VCS_LOCAL_KMS_MASTER_KEY") != "" {
primaryKeyReader, err = local.MasterKeyFromEnv("VCS_LOCAL_KMS_", "MASTER_KEY")
if err != nil {
return nil, errors.Join(err, errors.New("failed to create MasterKeyFromEnv"))
}
}
var primaryKeyReader io.Reader

if primaryKeyReader == nil {
if kmsMasterKey != "" {
primaryKeyReader = strings.NewReader(kmsMasterKey)
} else {
if keyPath == "" {
return nil, fmt.Errorf("no key defined for local secret lock")

Check warning on line 205 in pkg/kms/arieskms.go

View check run for this annotation

Codecov / codecov/patch

pkg/kms/arieskms.go#L205

Added line #L205 was not covered by tests
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/kms/arieskms_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,6 @@ func TestNewLocalKeyManager(t *testing.T) {
t.Run("Success env", func(t *testing.T) {
pool, mongoDBResource := startMongoDBContainer(t)

t.Setenv("VCS_LOCAL_KMS_MASTER_KEY", "00kIMo3wwfp1r8OOR8QMSkyIByY8ZHBKJy4l0u2i9f4=")

defer func() {
require.NoError(t, pool.Purge(mongoDBResource), "failed to purge MongoDB resource")
}()
Expand All @@ -103,6 +101,7 @@ func TestNewLocalKeyManager(t *testing.T) {
DBType: "mongodb",
DBURL: mongoDBConnString,
DBPrefix: "test",
MasterKey: "00kIMo3wwfp1r8OOR8QMSkyIByY8ZHBKJy4l0u2i9f4=",
}, nil)

require.NoError(t, err)
Expand Down
1 change: 1 addition & 0 deletions pkg/kms/kms.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type Config struct {
DBType string
DBURL string
DBPrefix string
MasterKey string
}

type VCSKeyManager interface {
Expand Down
3 changes: 3 additions & 0 deletions pkg/kms/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ func (r *Registry) GetKeyManager(config *Config) (VCSKeyManager, error) {

cfgCopy := r.defaultConfig
cfgCopy.KMSType = config.KMSType
if config.MasterKey != "" {
cfgCopy.MasterKey = config.MasterKey

Check warning on line 35 in pkg/kms/registry.go

View check run for this annotation

Codecov / codecov/patch

pkg/kms/registry.go#L35

Added line #L35 was not covered by tests
}

return NewAriesKeyManager(&cfgCopy, r.defaultMetricProvider)
}
2 changes: 1 addition & 1 deletion test/bdd/fixtures/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ services:
- VC_TRANSIENT_DATA_STORE_TYPE=redis
- VC_REDIS_URL=redis.example.com:6379
- VC_REDIS_DISABLE_TLS=true
- VCS_LOCAL_KMS_MASTER_KEY=00kIMo3wwfp1r8OOR8QMSkyIByY8ZHBKJy4l0u2i9f4=
- VC_REST_LOCAL_KMS_MASTER_KEY=00kIMo3wwfp1r8OOR8QMSkyIByY8ZHBKJy4l0u2i9f4=
ports:
- "8075:8075"
- "48127:48127"
Expand Down

0 comments on commit a39f2eb

Please sign in to comment.