config_loader.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package credential
  2. import (
  3. "fmt"
  4. "github.com/seaweedfs/seaweedfs/weed/glog"
  5. "github.com/seaweedfs/seaweedfs/weed/util"
  6. )
  7. // CredentialConfig represents the credential configuration from credential.toml
  8. type CredentialConfig struct {
  9. Store string
  10. Config util.Configuration
  11. Prefix string
  12. }
  13. // LoadCredentialConfiguration loads credential configuration from credential.toml
  14. // Returns the store type, configuration, and prefix for credential management
  15. func LoadCredentialConfiguration() (*CredentialConfig, error) {
  16. // Try to load credential.toml configuration
  17. loaded := util.LoadConfiguration("credential", false)
  18. if !loaded {
  19. glog.V(1).Info("No credential.toml found, credential store disabled")
  20. return nil, nil
  21. }
  22. viper := util.GetViper()
  23. // Find which credential store is enabled
  24. var enabledStore string
  25. var storePrefix string
  26. // Get available store types from registered stores
  27. storeTypes := GetAvailableStores()
  28. for _, storeType := range storeTypes {
  29. key := fmt.Sprintf("credential.%s.enabled", string(storeType))
  30. if viper.GetBool(key) {
  31. if enabledStore != "" {
  32. return nil, fmt.Errorf("multiple credential stores enabled: %s and %s. Only one store can be enabled", enabledStore, string(storeType))
  33. }
  34. enabledStore = string(storeType)
  35. storePrefix = fmt.Sprintf("credential.%s.", string(storeType))
  36. }
  37. }
  38. if enabledStore == "" {
  39. glog.V(1).Info("No credential store enabled in credential.toml")
  40. return nil, nil
  41. }
  42. glog.V(0).Infof("Loaded credential configuration: store=%s", enabledStore)
  43. return &CredentialConfig{
  44. Store: enabledStore,
  45. Config: viper,
  46. Prefix: storePrefix,
  47. }, nil
  48. }
  49. // GetCredentialStoreConfig extracts credential store configuration from command line flags
  50. // This is used when credential store is configured via command line instead of credential.toml
  51. func GetCredentialStoreConfig(store string, config util.Configuration, prefix string) *CredentialConfig {
  52. if store == "" {
  53. return nil
  54. }
  55. return &CredentialConfig{
  56. Store: store,
  57. Config: config,
  58. Prefix: prefix,
  59. }
  60. }
  61. // MergeCredentialConfig merges command line credential config with credential.toml config
  62. // Command line flags take priority over credential.toml
  63. func MergeCredentialConfig(cmdLineStore string, cmdLineConfig util.Configuration, cmdLinePrefix string) (*CredentialConfig, error) {
  64. // If command line credential store is specified, use it
  65. if cmdLineStore != "" {
  66. glog.V(0).Infof("Using command line credential configuration: store=%s", cmdLineStore)
  67. return GetCredentialStoreConfig(cmdLineStore, cmdLineConfig, cmdLinePrefix), nil
  68. }
  69. // Otherwise, try to load from credential.toml
  70. config, err := LoadCredentialConfiguration()
  71. if err != nil {
  72. return nil, err
  73. }
  74. if config == nil {
  75. glog.V(1).Info("No credential store configured")
  76. }
  77. return config, nil
  78. }
  79. // NewCredentialManagerWithDefaults creates a credential manager with fallback to defaults
  80. // If explicitStore is provided, it will be used regardless of credential.toml
  81. // If explicitStore is empty, it tries credential.toml first, then defaults to "filer_etc"
  82. func NewCredentialManagerWithDefaults(explicitStore CredentialStoreTypeName) (*CredentialManager, error) {
  83. var storeName CredentialStoreTypeName
  84. var config util.Configuration
  85. var prefix string
  86. // If explicit store is provided, use it
  87. if explicitStore != "" {
  88. storeName = explicitStore
  89. config = nil
  90. prefix = ""
  91. glog.V(0).Infof("Using explicit credential store: %s", storeName)
  92. } else {
  93. // Try to load from credential.toml first
  94. if credConfig, err := LoadCredentialConfiguration(); err == nil && credConfig != nil {
  95. storeName = CredentialStoreTypeName(credConfig.Store)
  96. config = credConfig.Config
  97. prefix = credConfig.Prefix
  98. glog.V(0).Infof("Loaded credential configuration from credential.toml: store=%s", storeName)
  99. } else {
  100. // Default to filer_etc store
  101. storeName = StoreTypeFilerEtc
  102. config = nil
  103. prefix = ""
  104. glog.V(1).Info("No credential.toml found, defaulting to filer_etc store")
  105. }
  106. }
  107. // Create the credential manager
  108. credentialManager, err := NewCredentialManager(storeName, config, prefix)
  109. if err != nil {
  110. return nil, fmt.Errorf("failed to initialize credential manager with store '%s': %v", storeName, err)
  111. }
  112. return credentialManager, nil
  113. }