Skip to content

Commit e99011a

Browse files
committed
Allow skipping per-query namespace inference via config
This is a way to open the current behaviour of Loki "meta-data" queries to other use cases where the list of namespaces cannot be inferred from queries. NetObserv will use this flag because its queries are not namespaced. This allows to effectively use the fine-grained SAR feature with netobserv. Related JIRA: NETOBSERV-1324
1 parent ecdeca0 commit e99011a

File tree

5 files changed

+20
-17
lines changed

5 files changed

+20
-17
lines changed

internal/authorizer/authorizer.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,15 @@ func (a *Authorizer) Authorize(
5353
token,
5454
user string, groups []string,
5555
verb, resource, resourceName, apiGroup string,
56-
namespaces []string, metadataOnly bool,
56+
namespaces []string, allowSkipNamespaceInference bool,
5757
) (types.DataResponseV1, error) {
5858
switch verb {
5959
case CreateVerb, GetVerb:
6060
default:
6161
return types.DataResponseV1{}, &StatusCodeError{fmt.Errorf("unexpected verb: %s", verb), http.StatusBadRequest}
6262
}
6363

64-
cacheKey := generateCacheKey(token, user, groups, verb, resource, resourceName, apiGroup, namespaces, metadataOnly, a.matcher)
64+
cacheKey := generateCacheKey(token, user, groups, verb, resource, resourceName, apiGroup, namespaces, a.matcher, allowSkipNamespaceInference)
6565

6666
level.Debug(a.logger).Log("msg", "looking up in cache", "cachekey", cacheKey) //nolint:errcheck
6767
res, ok, err := a.cache.Get(cacheKey)
@@ -75,7 +75,7 @@ func (a *Authorizer) Authorize(
7575
return res, nil
7676
}
7777

78-
res, err = a.authorizeInner(user, groups, verb, resource, resourceName, apiGroup, namespaces, metadataOnly)
78+
res, err = a.authorizeInner(user, groups, verb, resource, resourceName, apiGroup, namespaces, allowSkipNamespaceInference)
7979
if err != nil {
8080
return types.DataResponseV1{}, err
8181
}
@@ -88,7 +88,7 @@ func (a *Authorizer) Authorize(
8888
return res, nil
8989
}
9090

91-
func (a *Authorizer) authorizeInner(user string, groups []string, verb, resource, resourceName, apiGroup string, namespaces []string, metadataOnly bool) (types.DataResponseV1, error) {
91+
func (a *Authorizer) authorizeInner(user string, groups []string, verb, resource, resourceName, apiGroup string, namespaces []string, allowSkipNamespaceInference bool) (types.DataResponseV1, error) {
9292
// check if user has cluster-wide access
9393
clusterAllow, err := a.client.AccessReview(user, groups, verb, resource, resourceName, apiGroup, "")
9494
if err != nil {
@@ -113,7 +113,7 @@ func (a *Authorizer) authorizeInner(user string, groups []string, verb, resource
113113
return a.authorizeClusterWide(namespaces)
114114
}
115115

116-
if metadataOnly && len(namespaces) == 0 {
116+
if allowSkipNamespaceInference && len(namespaces) == 0 {
117117
// Only a metadata request and no namespaces provided -> populate with API list
118118
nsList, err := a.client.ListNamespaces()
119119
if err != nil {

internal/authorizer/cache.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ import (
1313
func generateCacheKey(
1414
token, user string, groups []string,
1515
verb, resource, resourceName, apiGroup string, namespaces []string,
16-
metadataOnly bool, matcher *config.Matcher,
16+
matcher *config.Matcher, allowSkipNamespaceInference bool,
1717
) string {
1818
userHash := hashUserinfo(token, user, groups)
1919
matcherHash := hashMatcher(matcher)
2020

2121
return strings.Join([]string{
22-
verb, fmt.Sprintf("%v", metadataOnly),
22+
verb, fmt.Sprintf("%v", allowSkipNamespaceInference),
2323
apiGroup, resourceName, resource, strings.Join(namespaces, ":"),
2424
userHash, matcherHash,
2525
}, ",")

internal/authorizer/cache_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ func TestGenerateCacheKey(t *testing.T) {
166166

167167
for _, tc := range tt {
168168
t.Run(tc.desc, func(t *testing.T) {
169-
got := generateCacheKey(tc.token, tc.user, tc.groups, tc.verb, tc.resource, tc.resourceName, tc.apiGroup, tc.namespaces, tc.metadataOnly, tc.matcher)
169+
got := generateCacheKey(tc.token, tc.user, tc.groups, tc.verb, tc.resource, tc.resourceName, tc.apiGroup, tc.namespaces, tc.matcher, tc.metadataOnly)
170170

171171
if got != tc.wantKey {
172172
t.Errorf("got cache key %q, want %q", got, tc.wantKey)

internal/config/config.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,15 @@ type Config struct {
3131
}
3232

3333
type OPAConfig struct {
34-
Pkg string
35-
Rule string
36-
Matcher string
37-
MatcherOp string
38-
MatcherSkipTenants string
39-
MatcherAdminGroups string
40-
SSAR bool
41-
ViaQToOTELMigration bool
34+
Pkg string
35+
Rule string
36+
Matcher string
37+
MatcherOp string
38+
MatcherSkipTenants string
39+
MatcherAdminGroups string
40+
SSAR bool
41+
ViaQToOTELMigration bool
42+
AllowSkipNamespaceInference bool
4243
}
4344

4445
type ServerConfig struct {
@@ -112,6 +113,7 @@ func ParseFlags() (*Config, error) {
112113
flag.StringVar(&cfg.Opa.MatcherAdminGroups, "opa.admin-groups", "", "Groups which should be treated as admins and cause the matcher to be omitted.")
113114
flag.BoolVar(&cfg.Opa.SSAR, "opa.ssar", false, "Use SelftSubjectAccessReview instead of SubjectAccessReview.")
114115
flag.BoolVar(&cfg.Opa.ViaQToOTELMigration, "opa.viaq-to-otel-migration", false, "Enable the ViaQ to OTel migration.")
116+
flag.BoolVar(&cfg.Opa.AllowSkipNamespaceInference, "opa.skip-namespace-inference", false, "Set true when namespaces cannot be inferred from query. This results in doing SARs for each user accessible namespace.")
115117

116118
// Memcached flags
117119
flag.StringSliceVar(&cfg.Memcached.Servers, "memcached", nil, "One or more Memcached server addresses.")

internal/handler/handler.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,9 @@ func New(l log.Logger, c cache.Cacher, wt transport.WrapperFunc, cfg *config.Con
150150
}
151151

152152
a := authorizer.New(oc, l, c, matcherForRequest)
153+
allowSkipNamespaceInference := cfg.Opa.AllowSkipNamespaceInference || extras.MetadataOnly
153154

154-
res, err := a.Authorize(token, req.Input.Subject, req.Input.Groups, verb, req.Input.Tenant, req.Input.Resource, apiGroup, namespaces.UnsortedList(), extras.MetadataOnly)
155+
res, err := a.Authorize(token, req.Input.Subject, req.Input.Groups, verb, req.Input.Tenant, req.Input.Resource, apiGroup, namespaces.UnsortedList(), allowSkipNamespaceInference)
155156
if err != nil {
156157
statusCode := http.StatusInternalServerError
157158
//nolint:errorlint

0 commit comments

Comments
 (0)