Files
seaweedFS/weed/iam/sts/provider_factory.go
Chris Lu 995dfc4d5d chore: remove ~50k lines of unreachable dead code (#8913)
* chore: remove unreachable dead code across the codebase

Remove ~50,000 lines of unreachable code identified by static analysis.

Major removals:
- weed/filer/redis_lua: entire unused Redis Lua filer store implementation
- weed/wdclient/net2, resource_pool: unused connection/resource pool packages
- weed/plugin/worker/lifecycle: unused lifecycle plugin worker
- weed/s3api: unused S3 policy templates, presigned URL IAM, streaming copy,
  multipart IAM, key rotation, and various SSE helper functions
- weed/mq/kafka: unused partition mapping, compression, schema, and protocol functions
- weed/mq/offset: unused SQL storage and migration code
- weed/worker: unused registry, task, and monitoring functions
- weed/query: unused SQL engine, parquet scanner, and type functions
- weed/shell: unused EC proportional rebalance functions
- weed/storage/erasure_coding/distribution: unused distribution analysis functions
- Individual unreachable functions removed from 150+ files across admin,
  credential, filer, iam, kms, mount, mq, operation, pb, s3api, server,
  shell, storage, topology, and util packages

* fix(s3): reset shared memory store in IAM test to prevent flaky failure

TestLoadIAMManagerFromConfig_EmptyConfigWithFallbackKey was flaky because
the MemoryStore credential backend is a singleton registered via init().
Earlier tests that create anonymous identities pollute the shared store,
causing LookupAnonymous() to unexpectedly return true.

Fix by calling Reset() on the memory store before the test runs.

* style: run gofmt on changed files

* fix: restore KMS functions used by integration tests

* fix(plugin): prevent panic on send to closed worker session channel

The Plugin.sendToWorker method could panic with "send on closed channel"
when a worker disconnected while a message was being sent. The race was
between streamSession.close() closing the outgoing channel and sendToWorker
writing to it concurrently.

Add a done channel to streamSession that is closed before the outgoing
channel, and check it in sendToWorker's select to safely detect closed
sessions without panicking.
2026-04-03 16:04:27 -07:00

277 lines
7.9 KiB
Go

package sts
import (
"fmt"
"github.com/seaweedfs/seaweedfs/weed/glog"
"github.com/seaweedfs/seaweedfs/weed/iam/ldap"
"github.com/seaweedfs/seaweedfs/weed/iam/oidc"
"github.com/seaweedfs/seaweedfs/weed/iam/providers"
)
// ProviderFactory creates identity providers from configuration
type ProviderFactory struct{}
// NewProviderFactory creates a new provider factory
func NewProviderFactory() *ProviderFactory {
return &ProviderFactory{}
}
// CreateProvider creates an identity provider from configuration
func (f *ProviderFactory) CreateProvider(config *ProviderConfig) (providers.IdentityProvider, error) {
if config == nil {
return nil, fmt.Errorf(ErrConfigCannotBeNil)
}
if config.Name == "" {
return nil, fmt.Errorf(ErrProviderNameEmpty)
}
if config.Type == "" {
return nil, fmt.Errorf(ErrProviderTypeEmpty)
}
if !config.Enabled {
glog.V(2).Infof("Provider %s is disabled, skipping", config.Name)
return nil, nil
}
glog.V(2).Infof("Creating provider: name=%s, type=%s", config.Name, config.Type)
switch config.Type {
case ProviderTypeOIDC:
return f.createOIDCProvider(config)
case ProviderTypeLDAP:
return f.createLDAPProvider(config)
case ProviderTypeSAML:
return f.createSAMLProvider(config)
default:
return nil, fmt.Errorf(ErrUnsupportedProviderType, config.Type)
}
}
// createOIDCProvider creates an OIDC provider from configuration
func (f *ProviderFactory) createOIDCProvider(config *ProviderConfig) (providers.IdentityProvider, error) {
oidcConfig, err := f.convertToOIDCConfig(config.Config)
if err != nil {
return nil, fmt.Errorf("failed to convert OIDC config: %w", err)
}
provider := oidc.NewOIDCProvider(config.Name)
if err := provider.Initialize(oidcConfig); err != nil {
return nil, fmt.Errorf("failed to initialize OIDC provider: %w", err)
}
return provider, nil
}
// createLDAPProvider creates an LDAP provider from configuration
func (f *ProviderFactory) createLDAPProvider(config *ProviderConfig) (providers.IdentityProvider, error) {
provider := ldap.NewLDAPProvider(config.Name)
if err := provider.Initialize(config.Config); err != nil {
return nil, fmt.Errorf("failed to initialize LDAP provider: %w", err)
}
return provider, nil
}
// createSAMLProvider creates a SAML provider from configuration
func (f *ProviderFactory) createSAMLProvider(config *ProviderConfig) (providers.IdentityProvider, error) {
// TODO: Implement SAML provider when available
return nil, fmt.Errorf("SAML provider not implemented yet")
}
// convertToOIDCConfig converts generic config map to OIDC config struct
func (f *ProviderFactory) convertToOIDCConfig(configMap map[string]interface{}) (*oidc.OIDCConfig, error) {
config := &oidc.OIDCConfig{}
// Required fields
if issuer, ok := configMap[ConfigFieldIssuer].(string); ok {
config.Issuer = issuer
} else {
return nil, fmt.Errorf(ErrIssuerRequired)
}
if clientID, ok := configMap[ConfigFieldClientID].(string); ok {
config.ClientID = clientID
} else {
return nil, fmt.Errorf(ErrClientIDRequired)
}
// Optional fields
if clientSecret, ok := configMap[ConfigFieldClientSecret].(string); ok {
config.ClientSecret = clientSecret
}
if jwksUri, ok := configMap[ConfigFieldJWKSUri].(string); ok {
config.JWKSUri = jwksUri
}
if userInfoUri, ok := configMap[ConfigFieldUserInfoUri].(string); ok {
config.UserInfoUri = userInfoUri
}
// Convert scopes array
if scopesInterface, ok := configMap[ConfigFieldScopes]; ok {
scopes, err := f.convertToStringSlice(scopesInterface)
if err != nil {
return nil, fmt.Errorf("failed to convert scopes: %w", err)
}
config.Scopes = scopes
}
if tlsCaCert, ok := configMap[ConfigFieldTLSCACert].(string); ok {
config.TLSCACert = tlsCaCert
}
if tlsInsecureSkipVerify, ok := configMap[ConfigFieldTLSInsecureSkipVerify].(bool); ok {
config.TLSInsecureSkipVerify = tlsInsecureSkipVerify
}
// Convert claims mapping
if claimsMapInterface, ok := configMap["claimsMapping"]; ok {
claimsMap, err := f.convertToStringMap(claimsMapInterface)
if err != nil {
return nil, fmt.Errorf("failed to convert claimsMapping: %w", err)
}
config.ClaimsMapping = claimsMap
}
// Convert role mapping
if roleMappingInterface, ok := configMap["roleMapping"]; ok {
roleMapping, err := f.convertToRoleMapping(roleMappingInterface)
if err != nil {
return nil, fmt.Errorf("failed to convert roleMapping: %w", err)
}
config.RoleMapping = roleMapping
}
glog.V(3).Infof("Converted OIDC config: issuer=%s, clientId=%s, jwksUri=%s",
config.Issuer, config.ClientID, config.JWKSUri)
return config, nil
}
// convertToStringSlice converts interface{} to []string
func (f *ProviderFactory) convertToStringSlice(value interface{}) ([]string, error) {
switch v := value.(type) {
case []string:
return v, nil
case []interface{}:
result := make([]string, len(v))
for i, item := range v {
if str, ok := item.(string); ok {
result[i] = str
} else {
return nil, fmt.Errorf("non-string item in slice: %v", item)
}
}
return result, nil
default:
return nil, fmt.Errorf("cannot convert %T to []string", value)
}
}
// convertToStringMap converts interface{} to map[string]string
func (f *ProviderFactory) convertToStringMap(value interface{}) (map[string]string, error) {
switch v := value.(type) {
case map[string]string:
return v, nil
case map[string]interface{}:
result := make(map[string]string)
for key, val := range v {
if str, ok := val.(string); ok {
result[key] = str
} else {
return nil, fmt.Errorf("non-string value for key %s: %v", key, val)
}
}
return result, nil
default:
return nil, fmt.Errorf("cannot convert %T to map[string]string", value)
}
}
// LoadProvidersFromConfig creates providers from configuration
func (f *ProviderFactory) LoadProvidersFromConfig(configs []*ProviderConfig) (map[string]providers.IdentityProvider, error) {
providersMap := make(map[string]providers.IdentityProvider)
for _, config := range configs {
if config == nil {
glog.V(1).Infof("Skipping nil provider config")
continue
}
glog.V(2).Infof("Loading provider: %s (type: %s, enabled: %t)",
config.Name, config.Type, config.Enabled)
if !config.Enabled {
glog.V(2).Infof("Provider %s is disabled, skipping", config.Name)
continue
}
provider, err := f.CreateProvider(config)
if err != nil {
glog.Errorf("Failed to create provider %s: %v", config.Name, err)
return nil, fmt.Errorf("failed to create provider %s: %w", config.Name, err)
}
if provider != nil {
providersMap[config.Name] = provider
glog.V(1).Infof("Successfully loaded provider: %s", config.Name)
}
}
glog.V(1).Infof("Loaded %d identity providers from configuration", len(providersMap))
return providersMap, nil
}
// convertToRoleMapping converts interface{} to *providers.RoleMapping
func (f *ProviderFactory) convertToRoleMapping(value interface{}) (*providers.RoleMapping, error) {
roleMappingMap, ok := value.(map[string]interface{})
if !ok {
return nil, fmt.Errorf("roleMapping must be an object")
}
roleMapping := &providers.RoleMapping{}
// Convert rules
if rulesInterface, ok := roleMappingMap["rules"]; ok {
rulesSlice, ok := rulesInterface.([]interface{})
if !ok {
return nil, fmt.Errorf("rules must be an array")
}
rules := make([]providers.MappingRule, len(rulesSlice))
for i, ruleInterface := range rulesSlice {
ruleMap, ok := ruleInterface.(map[string]interface{})
if !ok {
return nil, fmt.Errorf("rule must be an object")
}
rule := providers.MappingRule{}
if claim, ok := ruleMap["claim"].(string); ok {
rule.Claim = claim
}
if value, ok := ruleMap["value"].(string); ok {
rule.Value = value
}
if role, ok := ruleMap["role"].(string); ok {
rule.Role = role
}
if condition, ok := ruleMap["condition"].(string); ok {
rule.Condition = condition
}
rules[i] = rule
}
roleMapping.Rules = rules
}
// Convert default role
if defaultRole, ok := roleMappingMap["defaultRole"].(string); ok {
roleMapping.DefaultRole = defaultRole
}
return roleMapping, nil
}