github.com/grafana/pyroscope@v1.18.0/pkg/tenant/allowed_tenants.go (about)

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