Skip to content

Commit

Permalink
Use pkg helper to allow default MINIO_KMS_KEY_CACHE_INTERVAL as a tim…
Browse files Browse the repository at this point in the history
…e.Duration
  • Loading branch information
allanrogerr committed Apr 15, 2024
1 parent 0cf3d93 commit a9833c0
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 23 deletions.
3 changes: 1 addition & 2 deletions cmd/common-main.go
Original file line number Diff line number Diff line change
Expand Up @@ -958,8 +958,7 @@ func handleKMSConfig() {
}
}

kmsLogger := Logger{}
KMS, err := kms.NewWithConfig(kmsConf, kmsLogger)
KMS, err := kms.NewWithConfig(kmsConf, KMSLogger{})
if err != nil {
logger.Fatal(err, "Unable to initialize a connection to KES as specified by the shell environment")
}
Expand Down
13 changes: 9 additions & 4 deletions cmd/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,15 @@ func kmsLogIf(ctx context.Context, err error, errKind ...interface{}) {
logger.LogIf(ctx, "kms", err, errKind...)
}

// Logger permits access to module specific logging
type Logger struct{}
// KMSLogger permits access to kms module specific logging
type KMSLogger struct{}

// LogOnceIf is the implementation of LogOnceIf, accessible using the Logger interface
func (l Logger) LogOnceIf(ctx context.Context, subsystem string, err error, id string, errKind ...interface{}) {
logger.LogOnceIf(ctx, subsystem, err, id, errKind...)
func (kmsLogger KMSLogger) LogOnceIf(ctx context.Context, err error, id string, errKind ...interface{}) {
logger.LogOnceIf(ctx, "kms", err, id, errKind...)
}

// LogIf is the implementation of LogIf, accessible using the Logger interface
func (kmsLogger KMSLogger) LogIf(ctx context.Context, err error, errKind ...interface{}) {
logger.LogIf(ctx, "kms", err, errKind...)
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ require (
github.com/minio/madmin-go/v3 v3.0.50
github.com/minio/minio-go/v7 v7.0.69
github.com/minio/mux v1.9.0
github.com/minio/pkg/v2 v2.0.16
github.com/minio/pkg/v2 v2.0.17
github.com/minio/selfupdate v0.6.0
github.com/minio/sha256-simd v1.0.1
github.com/minio/simdjson-go v0.4.5
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -455,8 +455,8 @@ github.com/minio/minio-go/v7 v7.0.69 h1:l8AnsQFyY1xiwa/DaQskY4NXSLA2yrGsW5iD9nRP
github.com/minio/minio-go/v7 v7.0.69/go.mod h1:XAvOPJQ5Xlzk5o3o/ArO2NMbhSGkimC+bpW/ngRKDmQ=
github.com/minio/mux v1.9.0 h1:dWafQFyEfGhJvK6AwLOt83bIG5bxKxKJnKMCi0XAaoA=
github.com/minio/mux v1.9.0/go.mod h1:1pAare17ZRL5GpmNL+9YmqHoWnLmMZF9C/ioUCfy0BQ=
github.com/minio/pkg/v2 v2.0.16 h1:qBw2D08JE7fu4UORIxx0O4L09NM0wtMrw9sJRU5R1u0=
github.com/minio/pkg/v2 v2.0.16/go.mod h1:V+OP/fKRD/qhJMQpdXXrCXcLYjGMpHKEE26zslthm5k=
github.com/minio/pkg/v2 v2.0.17 h1:ndmGlitUj/eCVRPmfsAw3KlbtVNxqk0lQIvDXlcTHiQ=
github.com/minio/pkg/v2 v2.0.17/go.mod h1:V+OP/fKRD/qhJMQpdXXrCXcLYjGMpHKEE26zslthm5k=
github.com/minio/selfupdate v0.6.0 h1:i76PgT0K5xO9+hjzKcacQtO7+MjJ4JKA8Ak8XQ9DDwU=
github.com/minio/selfupdate v0.6.0/go.mod h1:bO02GTIPCMQFTEvE5h4DjYB58bCoZ35XLeBf0buTDdM=
github.com/minio/sha256-simd v0.1.1/go.mod h1:B5e1o+1/KgNmWrSQK08Y6Z1Vb5pwIktudl0J58iy0KM=
Expand Down
30 changes: 16 additions & 14 deletions internal/kms/kes.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ type Config struct {

// NewWithConfig returns a new KMS using the given
// configuration.
func NewWithConfig(config Config, kmsLogger Logger) (KMS, error) {
func NewWithConfig(config Config, kmsLogger KMSLogger) (KMS, error) {
if len(config.Endpoints) == 0 {
return nil, errors.New("kms: no server endpoints")
}
Expand Down Expand Up @@ -147,27 +147,28 @@ func NewWithConfig(config Config, kmsLogger Logger) (KMS, error) {

// Request KES keep an up-to-date copy of the KMS master key to allow minio to start up even if KMS is down. The
// cached key may still be evicted if the period of this function is longer than that of KES .cache.expiry.unused
func (c *kesClient) refreshKMSMasterKeyCache(logger Logger) {
func (c *kesClient) refreshKMSMasterKeyCache(kmsLogger KMSLogger) {
ctx := context.Background()

defaultCacheInterval := 10
cacheInterval, err := env.GetInt("EnvKESKeyCacheInterval", defaultCacheInterval)
defaultCacheDuration := time.Duration(10)
cacheDuration, err := env.GetDuration(EnvKESKeyCacheInterval, defaultCacheDuration)
if err != nil {
cacheInterval = defaultCacheInterval
kmsLogger.LogOnceIf(ctx, err, "refresh-kms-master-key")
cacheDuration = defaultCacheDuration
}

timer := time.NewTimer(time.Duration(cacheInterval) * time.Second)
timer := time.NewTimer(cacheDuration * time.Second)

Check failure on line 160 in internal/kms/kes.go

View workflow job for this annotation

GitHub Actions / Go 1.21.x on ubuntu-latest

Multiplication of durations: `cacheDuration * time.Second` (durationcheck)
defer timer.Stop()

for {
select {
case <-ctx.Done():
return
case <-timer.C:
c.RefreshKey(ctx, logger)
c.RefreshKey(ctx, kmsLogger)

// Reset for the next interval
timer.Reset(time.Duration(cacheInterval) * time.Second)
timer.Reset(cacheDuration * time.Second)

Check failure on line 171 in internal/kms/kes.go

View workflow job for this annotation

GitHub Actions / Go 1.21.x on ubuntu-latest

Multiplication of durations: `cacheDuration * time.Second` (durationcheck)
}
}
}
Expand Down Expand Up @@ -482,13 +483,14 @@ func (c *kesClient) Verify(ctx context.Context) []VerifyResult {
return results
}

// Logger interface permits access to module specific logging, in this case, for KMS
type Logger interface {
LogOnceIf(ctx context.Context, subsystem string, err error, id string, errKind ...interface{})
// KMSLogger interface permits access to module specific logging, in this case, for KMS
type KMSLogger interface {

Check failure on line 487 in internal/kms/kes.go

View workflow job for this annotation

GitHub Actions / Go 1.21.x on ubuntu-latest

exported: type name will be used as kms.KMSLogger by other packages, and that stutters; consider calling this Logger (revive)
LogOnceIf(ctx context.Context, err error, id string, errKind ...interface{})
LogIf(ctx context.Context, err error, errKind ...interface{})
}

// RefreshKey checks the validity of the KMS Master Key
func (c *kesClient) RefreshKey(ctx context.Context, logger Logger) bool {
func (c *kesClient) RefreshKey(ctx context.Context, kmsLogger KMSLogger) bool {
c.lock.RLock()
defer c.lock.RUnlock()

Expand All @@ -503,13 +505,13 @@ func (c *kesClient) RefreshKey(ctx context.Context, logger Logger) bool {
// 1. Generate a new key using the KMS.
kmsCtx, err := kmsContext.MarshalText()
if err != nil {
logger.LogOnceIf(ctx, "kms", err, "refresh-kms-master-key")
kmsLogger.LogOnceIf(ctx, err, "refresh-kms-master-key")
validKey = false
break
}
_, err = client.GenerateKey(ctx, env.Get(EnvKESKeyName, ""), kmsCtx)
if err != nil {
logger.LogOnceIf(ctx, "kms", err, "refresh-kms-master-key")
kmsLogger.LogOnceIf(ctx, err, "refresh-kms-master-key")
validKey = false
break
}
Expand Down

0 comments on commit a9833c0

Please sign in to comment.