github.com/yankunsam/loki/v2@v2.6.3-0.20220817130409-389df5235c27/pkg/util/allowed_tenants.go (about) 1 package util 2 3 // AllowedTenants that can answer whether tenant is allowed or not based on configuration. 4 // Default value (nil) allows all tenants. 5 type AllowedTenants struct { 6 // If empty, all tenants are enabled. If not empty, only tenants in the map are enabled. 7 enabled map[string]struct{} 8 9 // If empty, no tenants are disabled. If not empty, tenants in the map are disabled. 10 disabled map[string]struct{} 11 } 12 13 // NewAllowedTenants builds new allowed tenants based on enabled and disabled tenants. 14 // If there are any enabled tenants, then only those tenants are allowed. 15 // If there are any disabled tenants, then tenant from that list, that would normally be allowed, is disabled instead. 16 func NewAllowedTenants(enabled []string, disabled []string) *AllowedTenants { 17 a := &AllowedTenants{} 18 19 if len(enabled) > 0 { 20 a.enabled = make(map[string]struct{}, len(enabled)) 21 for _, u := range enabled { 22 a.enabled[u] = struct{}{} 23 } 24 } 25 26 if len(disabled) > 0 { 27 a.disabled = make(map[string]struct{}, len(disabled)) 28 for _, u := range disabled { 29 a.disabled[u] = struct{}{} 30 } 31 } 32 33 return a 34 } 35 36 func (a *AllowedTenants) IsAllowed(tenantID string) bool { 37 if a == nil { 38 return true 39 } 40 41 if len(a.enabled) > 0 { 42 if _, ok := a.enabled[tenantID]; !ok { 43 return false 44 } 45 } 46 47 if len(a.disabled) > 0 { 48 if _, ok := a.disabled[tenantID]; ok { 49 return false 50 } 51 } 52 53 return true 54 }