chore(rbac): update default behavior for tenant RBAC configuration

Refactor the tenant RBAC configuration to change the default value from false to true, enabling role enforcement by default. This change allows operators to opt into a logging-only rollout window by explicitly setting the configuration to false.

Updates include:
- Modifications to .env.example and docker-compose.yml to reflect the new default.
- Adjustments in rbac.md documentation to clarify the new default behavior and the opt-in process.
- Code changes across various files to utilize the new pointer-based configuration for EnableRBAC, ensuring nil safety and clearer intent.

No functional changes were introduced; the adjustments primarily enhance clarity and maintainability of the RBAC feature.
This commit is contained in:
wizardchen
2026-05-18 21:23:51 +08:00
committed by lyingbug
parent 633106c5ef
commit 7444c2190d
9 changed files with 62 additions and 30 deletions
+34 -7
View File
@@ -178,10 +178,15 @@ type TenantConfig struct {
// EnableCrossTenantAccess enables cross-tenant access for users with permission
EnableCrossTenantAccess bool `yaml:"enable_cross_tenant_access" json:"enable_cross_tenant_access"`
// EnableRBAC turns on tenant-level role enforcement (issue #1303).
// Default false — the schema and tenant_members rows ship in a release
// where role lookups are observed but not enforced; flip to true once
// role assignments have been audited.
EnableRBAC bool `yaml:"enable_rbac" json:"enable_rbac"`
// Pointer so we can distinguish "unset" from "explicit false":
// nil — fall back to the built-in default (true) applied
// by applyAuthAndTenantDefaults.
// pointer false — operators opted into the logging-only rollout
// window (set via config.yaml `enable_rbac: false`
// or env `WEKNORA_TENANT_ENABLE_RBAC=false`).
// pointer true — enforcement on (the new default).
// Read through IsRBACEnforced so callers stay nil-safe.
EnableRBAC *bool `yaml:"enable_rbac" json:"enable_rbac"`
// MaxOwnedPerUser caps how many tenants a single non-superuser can
// create (and Own) via self-service POST /tenants. Counts only Owner
// memberships so being invited as Admin/Editor/Viewer in another
@@ -198,6 +203,19 @@ type TenantConfig struct {
MaxOwnedPerUser int `yaml:"max_owned_per_user" json:"max_owned_per_user" mapstructure:"max_owned_per_user"`
}
// IsRBACEnforced reports whether tenant-level role enforcement is
// active. Nil receiver or nil EnableRBAC pointer means "operator did
// not opt out", which after applyAuthAndTenantDefaults is the new
// default (true). Callers that need to treat a nil *Config as
// fail-open (legacy behaviour) should keep their own `cfg != nil`
// short-circuit before invoking this helper.
func (t *TenantConfig) IsRBACEnforced() bool {
if t == nil || t.EnableRBAC == nil {
return true
}
return *t.EnableRBAC
}
// AuditConfig governs durable audit log behaviour. Writes happen on
// every member-management mutation and on RBAC denials (when
// EnableRBAC is true); the table grows monotonically unless this
@@ -516,7 +534,7 @@ func LoadConfig() (*Config, error) {
// "I edited .env but the gates still aren't firing" trap obvious
// from the first console line. Printf rather than logger because
// LoadConfig runs before the logger sink is wired in the dig graph.
rbacOn := cfg.Tenant != nil && cfg.Tenant.EnableRBAC
rbacOn := cfg.Tenant.IsRBACEnforced()
xtAccess := cfg.Tenant != nil && cfg.Tenant.EnableCrossTenantAccess
fmt.Printf(
"[config] tenant RBAC enforcement: enable_rbac=%v cross_tenant_access=%v "+
@@ -704,7 +722,9 @@ func applyAgentEnvOverrides(cfg *Config) {
//
// Defaults:
// - auth.registration_mode -> "self_serve" (preserves pre-RBAC behaviour)
// - tenant.enable_rbac -> false (rolled out in two steps; flip later)
// - tenant.enable_rbac -> true (enforce role checks unless an
// operator explicitly opts into the logging-only rollout window via
// config.yaml `enable_rbac: false` or `WEKNORA_TENANT_ENABLE_RBAC=false`).
//
// Env overrides (when set and non-empty):
// - WEKNORA_TENANT_ENABLE_RBAC ("true"/"false", case-insensitive)
@@ -743,7 +763,14 @@ func applyAuthAndTenantDefaults(cfg *Config) {
}
if value := strings.TrimSpace(os.Getenv("WEKNORA_TENANT_ENABLE_RBAC")); value != "" {
cfg.Tenant.EnableRBAC = strings.EqualFold(value, "true")
v := strings.EqualFold(value, "true")
cfg.Tenant.EnableRBAC = &v
}
if cfg.Tenant.EnableRBAC == nil {
// Default: enforce. Operators opt out of enforcement explicitly
// via config.yaml `enable_rbac: false` or the env override.
on := true
cfg.Tenant.EnableRBAC = &on
}
if value := strings.TrimSpace(os.Getenv("WEKNORA_TENANT_MAX_OWNED_PER_USER")); value != "" {
+1 -1
View File
@@ -553,7 +553,7 @@ func (h *Handler) AgentQA(c *gin.Context) {
if agentModeEnabled && reqCtx.customAgent != nil && !reqCtx.customAgent.RunnableByViewer {
role := types.TenantRoleFromContext(reqCtx.ctx)
if !role.HasPermission(types.TenantRoleContributor) {
if h.config != nil && h.config.Tenant != nil && h.config.Tenant.EnableRBAC {
if h.config != nil && h.config.Tenant.IsRBACEnforced() {
logger.Warnf(reqCtx.ctx,
"[rbac] agent run blocked: viewer cannot run runnable_by_viewer=false agent: agent=%s role=%s",
reqCtx.customAgent.ID, role)
+1 -1
View File
@@ -345,7 +345,7 @@ func resolveTenantRole(
}
// 4. 兜底:根据 EnableRBAC 决定 fail-closed 还是 fail-open
if cfg != nil && cfg.Tenant != nil && cfg.Tenant.EnableRBAC {
if cfg != nil && cfg.Tenant.IsRBACEnforced() {
logger.Warnf(ctx,
"[auth] resolveTenantRole step4 fail-closed (EnableRBAC=true): user=%s tenant=%d",
user.ID, targetTenantID)
+1 -1
View File
@@ -135,7 +135,7 @@ func (f *fakeMemberService) RemoveMember(ctx context.Context, userID string, ten
var _ interfaces.TenantMemberService = (*fakeMemberService)(nil)
func cfgWithRBAC(enabled bool) *config.Config {
return &config.Config{Tenant: &config.TenantConfig{EnableRBAC: enabled}}
return &config.Config{Tenant: &config.TenantConfig{EnableRBAC: &enabled}}
}
func TestResolveTenantRole_ActiveMembershipWins(t *testing.T) {
+1 -1
View File
@@ -207,7 +207,7 @@ func RequireOwnershipOrRole(min types.TenantRole, lookup CreatorLookup, cfg *con
// to a warning and ownership lookups are skipped entirely so the dormant
// rollout window incurs no per-request DB cost.
func rbacEnforcementEnabled(cfg *config.Config) bool {
return cfg != nil && cfg.Tenant != nil && cfg.Tenant.EnableRBAC
return cfg != nil && cfg.Tenant.IsRBACEnforced()
}
// isCrossTenantSuperuser was moved to access.go (renamed to
+2 -2
View File
@@ -40,7 +40,7 @@ func rbacTestHarness(role types.TenantRole, userID string, mw gin.HandlerFunc) *
}
func cfgRBAC(enabled bool) *config.Config {
return &config.Config{Tenant: &config.TenantConfig{EnableRBAC: enabled}}
return &config.Config{Tenant: &config.TenantConfig{EnableRBAC: &enabled}}
}
// cfgRBACWithCrossTenant returns a config with both per-tenant RBAC
@@ -49,7 +49,7 @@ func cfgRBAC(enabled bool) *config.Config {
// so cross-tenant superuser tests need this rather than plain cfgRBAC.
func cfgRBACWithCrossTenant(enabled bool) *config.Config {
return &config.Config{Tenant: &config.TenantConfig{
EnableRBAC: enabled,
EnableRBAC: &enabled,
EnableCrossTenantAccess: true,
}}
}