fix(config): 补齐 WEKNORA_TENANT_ENABLE_CROSS_TENANT_ACCESS 的 env 读取

main
sunmh 2026-08-07 15:22:40 +08:00 committed by lyingbug
parent 58ee0b7d38
commit bcc54353e3
2 changed files with 65 additions and 0 deletions

View File

@ -102,3 +102,55 @@ func TestApplyAuthAndTenantDefaults_DefaultTenantMode(t *testing.T) {
}
})
}
// TestApplyAuthAndTenantDefaults_CrossTenantAccess is a regression test for the
// env-binding gap: viper.AutomaticEnv has no SetEnvPrefix, so
// WEKNORA_TENANT_ENABLE_CROSS_TENANT_ACCESS is never bound to the nested struct
// automatically. applyAuthAndTenantDefaults must read it explicitly (like RBAC);
// without that, only config.yaml's enable_cross_tenant_access would take effect
// and the documented env override would be silently ignored.
func TestApplyAuthAndTenantDefaults_CrossTenantAccess(t *testing.T) {
t.Run("environment true enables cross-tenant access", func(t *testing.T) {
t.Setenv("WEKNORA_TENANT_ENABLE_CROSS_TENANT_ACCESS", "true")
cfg := &Config{Tenant: &TenantConfig{EnableCrossTenantAccess: false}}
applyAuthAndTenantDefaults(cfg)
if !cfg.Tenant.EnableCrossTenantAccess {
t.Fatal("WEKNORA_TENANT_ENABLE_CROSS_TENANT_ACCESS=true should enable cross-tenant access")
}
})
t.Run("environment false overrides yaml true", func(t *testing.T) {
t.Setenv("WEKNORA_TENANT_ENABLE_CROSS_TENANT_ACCESS", "false")
cfg := &Config{Tenant: &TenantConfig{EnableCrossTenantAccess: true}}
applyAuthAndTenantDefaults(cfg)
if cfg.Tenant.EnableCrossTenantAccess {
t.Fatal("WEKNORA_TENANT_ENABLE_CROSS_TENANT_ACCESS=false should disable cross-tenant access")
}
})
t.Run("case-insensitive TRUE also enables", func(t *testing.T) {
t.Setenv("WEKNORA_TENANT_ENABLE_CROSS_TENANT_ACCESS", "TRUE")
cfg := &Config{Tenant: &TenantConfig{EnableCrossTenantAccess: false}}
applyAuthAndTenantDefaults(cfg)
if !cfg.Tenant.EnableCrossTenantAccess {
t.Fatal("WEKNORA_TENANT_ENABLE_CROSS_TENANT_ACCESS=TRUE should enable cross-tenant access (case-insensitive)")
}
})
t.Run("unset leaves yaml value untouched", func(t *testing.T) {
t.Setenv("WEKNORA_TENANT_ENABLE_CROSS_TENANT_ACCESS", "")
cfg := &Config{Tenant: &TenantConfig{EnableCrossTenantAccess: true}}
applyAuthAndTenantDefaults(cfg)
if !cfg.Tenant.EnableCrossTenantAccess {
t.Fatal("empty env should leave the YAML-provided cross-tenant access value untouched")
}
})
}

View File

@ -808,6 +808,9 @@ func applyAgentEnvOverrides(cfg *Config) {
// - WEKNORA_AUTH_DEFAULT_TENANT_MODE ("create_personal"/"tenantless")
// - WEKNORA_TENANT_SELF_SERVICE_CREATION_ENABLED (boolean)
// - WEKNORA_TENANT_ENABLE_RBAC ("true"/"false", case-insensitive)
// - WEKNORA_TENANT_ENABLE_CROSS_TENANT_ACCESS ("true"/"false", case-insensitive).
// Read explicitly because viper.AutomaticEnv has no SetEnvPrefix, so the
// WEKNORA_-prefixed var is not bound to the nested struct automatically.
// - WEKNORA_TENANT_MAX_OWNED_PER_USER (integer; <0 disables the cap,
// 0 falls back to the handler default, >0 enforces that exact cap).
// Unparseable / empty values are ignored so a stale shell variable
@ -859,6 +862,16 @@ func applyAuthAndTenantDefaults(cfg *Config) {
cfg.Tenant.EnableRBAC = &on
}
// WEKNORA_TENANT_ENABLE_CROSS_TENANT_ACCESS mirrors the RBAC switch above.
// It must be read explicitly: viper.AutomaticEnv has no SetEnvPrefix, so the
// WEKNORA_-prefixed env var is never bound to the nested struct field —
// without this block, only config.yaml's enable_cross_tenant_access takes
// effect and the documented env override is silently ignored. The default
// stays whatever config.yaml provides (false unless set there).
if value := strings.TrimSpace(os.Getenv("WEKNORA_TENANT_ENABLE_CROSS_TENANT_ACCESS")); value != "" {
cfg.Tenant.EnableCrossTenantAccess = strings.EqualFold(value, "true")
}
if value := strings.TrimSpace(os.Getenv("WEKNORA_TENANT_SELF_SERVICE_CREATION_ENABLED")); value != "" {
if enabled, err := strconv.ParseBool(value); err == nil {
cfg.Tenant.SelfServiceCreationEnabled = &enabled