mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-05 18:47:20 +01:00
kms: read provider settings as TOML gives them (#13294)
* kms: read provider settings as TOML gives them Provider settings were read with plain type assertions, so a setting of another type read as its zero value. TOML decodes integers as int64, which GetInt ignored, so max_cache_size from filer.toml never applied, and Viper lowercases TOML keys, so the local provider never saw enableOnDemandCreate. A cache_ttl that didn't parse fell back to an hour without a word. Settings are now read through one typed getter. An int64 or a whole float64 reads as an int, a key matches ignoring case when it isn't found as given, and a provider that reads a setting of the wrong type fails to load and is closed. The loader reads the cache settings the same way and fails a provider whose cache settings don't parse. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * kms: apply the cache defaults to a setting given as null A JSON null for cache_enabled or cache_ttl counted as set, so it turned caching off or failed the provider on an empty duration, where it used to read as the default. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Chris Lu <chrislusf@users.noreply.github.com>
This commit is contained in:
3 files changed
+217
-91
No files matched your search
+79
-40
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
@@ -41,6 +42,7 @@ type BucketKMSConfig struct {
|
||||
// configAdapter adapts KMSConfig.Config to util.Configuration interface
|
||||
type configAdapter struct {
|
||||
config map[string]interface{}
|
||||
errs []error
|
||||
}
|
||||
|
||||
// GetConfigMap returns the underlying configuration map for direct access
|
||||
@@ -48,59 +50,94 @@ func (c *configAdapter) GetConfigMap() map[string]interface{} {
|
||||
return c.config
|
||||
}
|
||||
|
||||
func (c *configAdapter) GetString(key string) string {
|
||||
// value matches the key ignoring case if it isn't found as given, as Viper lowercases the
|
||||
// keys of a TOML configuration. Of keys that differ only in case the first in sorted order
|
||||
// wins, so every load of the same configuration reads the same one.
|
||||
func (c *configAdapter) value(key string) (interface{}, bool) {
|
||||
if val, ok := c.config[key]; ok {
|
||||
if str, ok := val.(string); ok {
|
||||
return str
|
||||
return val, true
|
||||
}
|
||||
match, found := "", false
|
||||
for k := range c.config {
|
||||
if strings.EqualFold(k, key) && (!found || k < match) {
|
||||
match, found = k, true
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (c *configAdapter) GetBool(key string) bool {
|
||||
if val, ok := c.config[key]; ok {
|
||||
if b, ok := val.(bool); ok {
|
||||
return b
|
||||
}
|
||||
if !found {
|
||||
return nil, false
|
||||
}
|
||||
return false
|
||||
return c.config[match], true
|
||||
}
|
||||
|
||||
func (c *configAdapter) GetInt(key string) int {
|
||||
if val, ok := c.config[key]; ok {
|
||||
if i, ok := val.(int); ok {
|
||||
return i
|
||||
}
|
||||
if f, ok := val.(float64); ok {
|
||||
return int(f)
|
||||
}
|
||||
// typed reads key as a T. TOML decodes integers as int64 and JSON as float64, so a whole
|
||||
// number of either reads as an int.
|
||||
func typed[T any](c *configAdapter, key string) (T, error) {
|
||||
var zero T
|
||||
val, ok := c.value(key)
|
||||
if !ok || val == nil {
|
||||
return zero, nil
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (c *configAdapter) GetStringSlice(key string) []string {
|
||||
if val, ok := c.config[key]; ok {
|
||||
if slice, ok := val.([]string); ok {
|
||||
return slice
|
||||
if v, ok := val.(T); ok {
|
||||
return v, nil
|
||||
}
|
||||
switch v := val.(type) {
|
||||
case int64:
|
||||
if i, ok := any(int(v)).(T); ok && int64(int(v)) == v {
|
||||
return i, nil
|
||||
}
|
||||
if interfaceSlice, ok := val.([]interface{}); ok {
|
||||
result := make([]string, len(interfaceSlice))
|
||||
for i, v := range interfaceSlice {
|
||||
if str, ok := v.(string); ok {
|
||||
result[i] = str
|
||||
}
|
||||
case float64:
|
||||
if i, ok := any(int(v)).(T); ok && float64(int(v)) == v {
|
||||
return i, nil
|
||||
}
|
||||
case []interface{}:
|
||||
list := make([]string, 0, len(v))
|
||||
for _, item := range v {
|
||||
if s, ok := item.(string); ok {
|
||||
list = append(list, s)
|
||||
}
|
||||
return result
|
||||
}
|
||||
if l, ok := any(list).(T); ok && len(list) == len(v) {
|
||||
return l, nil
|
||||
}
|
||||
}
|
||||
return nil
|
||||
return zero, fmt.Errorf("%s: expected %T, got %T", key, zero, val)
|
||||
}
|
||||
|
||||
// get is typed for the util.Configuration getters, which return no error. It keeps the error
|
||||
// for checked, so a provider that read a setting of the wrong type fails to load rather than
|
||||
// running without it.
|
||||
func get[T any](c *configAdapter, key string) T {
|
||||
v, err := typed[T](c, key)
|
||||
if err != nil {
|
||||
c.errs = append(c.errs, err)
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
// checked fails a provider built from c if it read a setting of the wrong type. c may be nil.
|
||||
func (c *configAdapter) checked(provider KMSProvider, err error) (KMSProvider, error) {
|
||||
if c == nil || len(c.errs) == 0 {
|
||||
return provider, err
|
||||
}
|
||||
if err == nil && provider != nil {
|
||||
provider.Close()
|
||||
}
|
||||
return nil, errors.Join(append(c.errs, err)...)
|
||||
}
|
||||
|
||||
func (c *configAdapter) GetString(key string) string { return get[string](c, key) }
|
||||
|
||||
func (c *configAdapter) GetBool(key string) bool { return get[bool](c, key) }
|
||||
|
||||
func (c *configAdapter) GetInt(key string) int { return get[int](c, key) }
|
||||
|
||||
func (c *configAdapter) GetStringSlice(key string) []string { return get[[]string](c, key) }
|
||||
|
||||
func (c *configAdapter) SetDefault(key string, value interface{}) {
|
||||
if c.config == nil {
|
||||
c.config = make(map[string]interface{})
|
||||
}
|
||||
if _, exists := c.config[key]; !exists {
|
||||
if v, exists := c.value(key); !exists || v == nil {
|
||||
c.config[key] = value
|
||||
}
|
||||
}
|
||||
@@ -120,12 +157,14 @@ func InitializeGlobalKMS(config *KMSConfig) error {
|
||||
}
|
||||
|
||||
// Adapt the config to util.Configuration interface
|
||||
var settings *configAdapter
|
||||
var providerConfig util.Configuration
|
||||
if config.Config != nil {
|
||||
providerConfig = &configAdapter{config: config.Config}
|
||||
settings = &configAdapter{config: config.Config}
|
||||
providerConfig = settings
|
||||
}
|
||||
|
||||
provider, err := NewProvider(config.Provider, providerConfig)
|
||||
provider, err := settings.checked(NewProvider(config.Provider, providerConfig))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -213,8 +252,8 @@ func (km *KMSManager) AddKMSProvider(name string, config *KMSConfig) error {
|
||||
// one closed below and then handed back still in use. It is created before the lock is
|
||||
// taken and the current provider closed, so a slow configuration does not hold up other
|
||||
// lookups and one that fails leaves the current provider working.
|
||||
configAdapter := &configAdapter{config: config.Config}
|
||||
provider, err := NewProvider(config.Provider, configAdapter)
|
||||
settings := &configAdapter{config: config.Config}
|
||||
provider, err := settings.checked(NewProvider(config.Provider, settings))
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create KMS provider %s: %w", name, err)
|
||||
}
|
||||
|
||||
+39
-51
@@ -114,17 +114,21 @@ func (loader *ConfigLoader) loadKMSProviders(providers map[string]interface{}) e
|
||||
return nil
|
||||
}
|
||||
|
||||
// loadSingleKMSProvider loads a single KMS provider configuration
|
||||
func (loader *ConfigLoader) loadSingleKMSProvider(providerName string, config map[string]interface{}) error {
|
||||
func kmsProviderConfig(providerName string, raw interface{}) (*KMSConfig, error) {
|
||||
config, ok := raw.(map[string]interface{})
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("invalid configuration for KMS provider %s", providerName)
|
||||
}
|
||||
|
||||
// Get provider type
|
||||
providerType, exists := config["type"]
|
||||
if !exists {
|
||||
return fmt.Errorf("provider type not specified for %s", providerName)
|
||||
return nil, fmt.Errorf("provider type not specified for %s", providerName)
|
||||
}
|
||||
|
||||
providerTypeStr, ok := providerType.(string)
|
||||
if !ok {
|
||||
return fmt.Errorf("invalid provider type for %s", providerName)
|
||||
return nil, fmt.Errorf("invalid provider type for %s", providerName)
|
||||
}
|
||||
|
||||
// Get provider-specific configuration
|
||||
@@ -136,35 +140,42 @@ func (loader *ConfigLoader) loadSingleKMSProvider(providerName string, config ma
|
||||
}
|
||||
|
||||
// Set default cache settings if not specified
|
||||
if _, exists := providerConfig["cache_enabled"]; !exists {
|
||||
providerConfig["cache_enabled"] = true
|
||||
settings := &configAdapter{config: providerConfig}
|
||||
settings.SetDefault("cache_enabled", true)
|
||||
settings.SetDefault("cache_ttl", "1h")
|
||||
settings.SetDefault("max_cache_size", DefaultDataKeyCacheSize)
|
||||
|
||||
cacheEnabled, err := typed[bool](settings, "cache_enabled")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid configuration for KMS provider %s: %w", providerName, err)
|
||||
}
|
||||
ttl, err := typed[string](settings, "cache_ttl")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid configuration for KMS provider %s: %w", providerName, err)
|
||||
}
|
||||
cacheTTL, err := time.ParseDuration(ttl)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid configuration for KMS provider %s: cache_ttl: %w", providerName, err)
|
||||
}
|
||||
maxCacheSize, err := typed[int](settings, "max_cache_size")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid configuration for KMS provider %s: %w", providerName, err)
|
||||
}
|
||||
|
||||
if _, exists := providerConfig["cache_ttl"]; !exists {
|
||||
providerConfig["cache_ttl"] = "1h"
|
||||
}
|
||||
|
||||
if _, exists := providerConfig["max_cache_size"]; !exists {
|
||||
providerConfig["max_cache_size"] = 1000
|
||||
}
|
||||
|
||||
// Parse cache TTL
|
||||
cacheTTL := time.Hour // default
|
||||
if ttlStr, exists := providerConfig["cache_ttl"]; exists {
|
||||
if ttlStrValue, ok := ttlStr.(string); ok {
|
||||
if parsed, err := time.ParseDuration(ttlStrValue); err == nil {
|
||||
cacheTTL = parsed
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Create KMS configuration
|
||||
kmsConfig := &KMSConfig{
|
||||
return &KMSConfig{
|
||||
Provider: providerTypeStr,
|
||||
Config: providerConfig,
|
||||
CacheEnabled: getBoolFromConfig(providerConfig, "cache_enabled", true),
|
||||
CacheEnabled: cacheEnabled,
|
||||
CacheTTL: cacheTTL,
|
||||
MaxCacheSize: getIntFromConfig(providerConfig, "max_cache_size", 1000),
|
||||
MaxCacheSize: maxCacheSize,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// loadSingleKMSProvider loads a single KMS provider configuration
|
||||
func (loader *ConfigLoader) loadSingleKMSProvider(providerName string, config map[string]interface{}) error {
|
||||
kmsConfig, err := kmsProviderConfig(providerName, config)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Add the provider to the KMS manager
|
||||
@@ -383,26 +394,3 @@ func (d *directConfigAdapter) IsSet(key string) bool {
|
||||
_, exists := d.config[key]
|
||||
return exists
|
||||
}
|
||||
|
||||
// Helper functions
|
||||
|
||||
func getBoolFromConfig(config map[string]interface{}, key string, defaultValue bool) bool {
|
||||
if value, exists := config[key]; exists {
|
||||
if boolValue, ok := value.(bool); ok {
|
||||
return boolValue
|
||||
}
|
||||
}
|
||||
return defaultValue
|
||||
}
|
||||
|
||||
func getIntFromConfig(config map[string]interface{}, key string, defaultValue int) int {
|
||||
if value, exists := config[key]; exists {
|
||||
if intValue, ok := value.(int); ok {
|
||||
return intValue
|
||||
}
|
||||
if floatValue, ok := value.(float64); ok {
|
||||
return int(floatValue)
|
||||
}
|
||||
}
|
||||
return defaultValue
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package kms
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -112,6 +113,104 @@ func TestAddKMSProviderDoesNotBlockLookups(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigKeysDifferingInCaseReadTheSameValue(t *testing.T) {
|
||||
config := map[string]interface{}{"KEYID": "upper", "KeyId": "mixed", "keyId": "lower"}
|
||||
for i := 0; i < 50; i++ {
|
||||
if got := (&configAdapter{config: config}).GetString("keyid"); got != "upper" {
|
||||
t.Fatalf("read %q, want upper from KEYID, the first in sorted order", got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestTypedSettings(t *testing.T) {
|
||||
c := &configAdapter{config: map[string]interface{}{
|
||||
"toml_int": int64(5), "json_int": float64(5), "fraction": 1.5, "word": "five",
|
||||
"list": []interface{}{"a", "b"}, "mixed": []interface{}{"a", 1}, "unset": nil,
|
||||
}}
|
||||
for _, key := range []string{"toml_int", "json_int"} {
|
||||
if got, err := typed[int](c, key); err != nil || got != 5 {
|
||||
t.Errorf("%s = %d, %v, want 5", key, got, err)
|
||||
}
|
||||
}
|
||||
if got, err := typed[[]string](c, "list"); err != nil || strings.Join(got, ",") != "a,b" {
|
||||
t.Errorf("list = %v, %v", got, err)
|
||||
}
|
||||
for _, key := range []string{"missing", "unset"} {
|
||||
if got, err := typed[string](c, key); err != nil || got != "" {
|
||||
t.Errorf("%s = %q, %v, want no value and no error", key, got, err)
|
||||
}
|
||||
}
|
||||
if _, err := typed[int](c, "fraction"); err == nil {
|
||||
t.Error("a fraction read as an int")
|
||||
}
|
||||
if _, err := typed[int](c, "word"); err == nil {
|
||||
t.Error("a string read as an int")
|
||||
}
|
||||
if _, err := typed[[]string](c, "mixed"); err == nil {
|
||||
t.Error("a list holding a number read as strings")
|
||||
}
|
||||
if _, err := typed[bool](c, "word"); err == nil {
|
||||
t.Error("a string read as a bool")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSettingOfTheWrongTypeFailsTheProvider(t *testing.T) {
|
||||
var built []*closeTracker
|
||||
RegisterProvider("typed-test", func(c util.Configuration) (KMSProvider, error) {
|
||||
c.GetInt("size")
|
||||
p := &closeTracker{KMSProvider: newCountingKMSProvider()}
|
||||
built = append(built, p)
|
||||
return p, nil
|
||||
})
|
||||
mgr := &KMSManager{
|
||||
providers: make(map[string]KMSProvider),
|
||||
configs: make(map[string]*KMSConfig),
|
||||
bucketKMS: make(map[string]string),
|
||||
}
|
||||
add := func(size interface{}) error {
|
||||
return mgr.AddKMSProvider("typed", &KMSConfig{Provider: "typed-test", Config: map[string]interface{}{"size": size}})
|
||||
}
|
||||
|
||||
if err := add(int64(3)); err != nil {
|
||||
t.Fatalf("an int64 setting, as TOML gives, failed the provider: %v", err)
|
||||
}
|
||||
err := add("three")
|
||||
if err == nil || !strings.Contains(err.Error(), "size") {
|
||||
t.Fatalf("a string setting read as an int = %v, want an error naming size", err)
|
||||
}
|
||||
if !built[len(built)-1].closed {
|
||||
t.Fatal("the provider that read a setting of the wrong type wasn't closed")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCacheSettings(t *testing.T) {
|
||||
config, err := kmsProviderConfig("p", map[string]interface{}{"type": "t", "max_cache_size": int64(7), "cache_ttl": "5m"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !config.CacheEnabled || config.MaxCacheSize != 7 || config.CacheTTL != 5*time.Minute {
|
||||
t.Fatalf("cache settings = %v, %d, %s", config.CacheEnabled, config.MaxCacheSize, config.CacheTTL)
|
||||
}
|
||||
config, err = kmsProviderConfig("p", map[string]interface{}{"type": "t", "cache_enabled": nil, "Cache_TTL": nil, "max_cache_size": nil})
|
||||
if err != nil {
|
||||
t.Fatalf("null cache settings: %v", err)
|
||||
}
|
||||
if !config.CacheEnabled || config.MaxCacheSize != DefaultDataKeyCacheSize || config.CacheTTL != time.Hour {
|
||||
t.Fatalf("null cache settings = %v, %d, %s, want the defaults", config.CacheEnabled, config.MaxCacheSize, config.CacheTTL)
|
||||
}
|
||||
for name, setting := range map[string]map[string]interface{}{
|
||||
"cache_ttl not a duration": {"cache_ttl": "an hour"},
|
||||
"cache_ttl not a string": {"cache_ttl": 60},
|
||||
"cache_enabled not a bool": {"cache_enabled": "yes"},
|
||||
"max_cache_size a string": {"max_cache_size": "7"},
|
||||
} {
|
||||
setting["type"] = "t"
|
||||
if _, err := kmsProviderConfig("p", setting); err == nil {
|
||||
t.Errorf("%s: accepted", name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type stalledDescribe struct {
|
||||
KMSProvider
|
||||
started, release chan struct{}
|
||||
|
||||
Reference in new issue
Block a user