-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathauthz_server.go
More file actions
389 lines (337 loc) · 12.1 KB
/
authz_server.go
File metadata and controls
389 lines (337 loc) · 12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/*
SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
SPDX-License-Identifier: Apache-2.0
*/
package server
import (
"context"
"fmt"
"log/slog"
"strings"
"time"
envoy_api_v3_core "github.com/envoyproxy/go-control-plane/envoy/config/core/v3"
envoy_service_auth_v3 "github.com/envoyproxy/go-control-plane/envoy/service/auth/v3"
envoy_type_v3 "github.com/envoyproxy/go-control-plane/envoy/type/v3"
"google.golang.org/genproto/googleapis/rpc/status"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"go.corp.nvidia.com/osmo/utils/postgres"
"go.corp.nvidia.com/osmo/utils/roles"
)
const (
// Header names
headerOsmoUser = "x-osmo-user"
headerOsmoRoles = "x-osmo-roles"
headerOsmoTokenName = "x-osmo-token-name"
headerOsmoWorkflowID = "x-osmo-workflow-id"
headerOsmoAllowedPools = "x-osmo-allowed-pools"
// Default role added to all users
defaultRole = "osmo-default"
)
// AuthzServer implements Envoy External Authorization service
type AuthzServer struct {
envoy_service_auth_v3.UnimplementedAuthorizationServer
pgClient *postgres.PostgresClient
roleCache *roles.RoleCache
poolNameCache *roles.PoolNameCache
logger *slog.Logger
}
// NewAuthzServer creates a new authorization server
func NewAuthzServer(
pgClient *postgres.PostgresClient,
roleCache *roles.RoleCache,
poolNameCache *roles.PoolNameCache,
logger *slog.Logger,
) *AuthzServer {
return &AuthzServer{
pgClient: pgClient,
roleCache: roleCache,
poolNameCache: poolNameCache,
logger: logger,
}
}
// MigrateRoles converts all legacy roles to semantic format and updates the database.
// This should be called at startup to ensure all roles are in semantic format.
func (s *AuthzServer) MigrateRoles(ctx context.Context) error {
// Get all role names from the database
allRoleNames, err := roles.GetAllRoleNames(ctx, s.pgClient)
if err != nil {
return fmt.Errorf("failed to get all role names: %w", err)
}
if len(allRoleNames) == 0 {
s.logger.Warn("no roles found in database")
return nil
}
// Fetch all roles from database
allRoles, err := roles.GetRoles(ctx, s.pgClient, allRoleNames, s.logger)
if err != nil {
return fmt.Errorf("failed to get roles: %w", err)
}
// Convert all roles to semantic format
convertedRoles := roles.ConvertRolesToSemantic(allRoles)
// Update each role in the database with converted policies
for _, role := range convertedRoles {
if err := roles.UpdateRolePolicies(ctx, s.pgClient, role, s.logger); err != nil {
return fmt.Errorf("failed to update role %s: %w", role.Name, err)
}
}
s.logger.Info("migrated roles to semantic format",
slog.Int("total_roles", len(convertedRoles)),
)
return nil
}
// RegisterAuthzService registers the authorization service with gRPC server
func RegisterAuthzService(grpcServer *grpc.Server, authzServer *AuthzServer) {
envoy_service_auth_v3.RegisterAuthorizationServer(grpcServer, authzServer)
}
// Check implements the Envoy External Authorization Check RPC
func (s *AuthzServer) Check(ctx context.Context, req *envoy_service_auth_v3.CheckRequest) (*envoy_service_auth_v3.CheckResponse, error) {
checkStart := time.Now()
// Extract request attributes
attrs := req.GetAttributes()
if attrs == nil {
s.logger.Error("missing attributes in check request")
return s.denyResponse(codes.InvalidArgument, "missing request attributes"), nil
}
httpAttrs := attrs.GetRequest().GetHttp()
if httpAttrs == nil {
s.logger.Error("missing HTTP attributes in check request")
return s.denyResponse(codes.InvalidArgument, "missing HTTP attributes"), nil
}
// Extract path, method, and headers
path := httpAttrs.GetPath()
method := httpAttrs.GetMethod()
headers := httpAttrs.GetHeaders()
// Extract user, roles, and token/workflow identifiers from headers
user := headers[headerOsmoUser]
rolesHeader := headers[headerOsmoRoles]
tokenName := headers[headerOsmoTokenName]
workflowID := headers[headerOsmoWorkflowID]
// Parse roles (comma-separated)
var roleNames []string
if rolesHeader != "" {
roleNames = strings.Split(rolesHeader, ",")
for i := range roleNames {
roleNames[i] = strings.TrimSpace(roleNames[i])
}
}
parseDone := time.Now()
// Sync user_roles table from external IDP roles and retrieve the user's
// complete set of assigned OSMO roles in a single atomic operation.
// This maps external role names (from the JWT) to OSMO roles via
// role_external_mappings and applies sync_mode logic (import/force).
// Skip sync for access tokens and workflow-originated requests, as their
// roles are already resolved from the access_token_roles table.
if user != "" && tokenName == "" && workflowID == "" {
dbRoleNames, err := roles.SyncUserRoles(ctx, s.pgClient, user, roleNames, s.logger)
if err != nil {
s.logger.Error("failed to sync user roles",
slog.String("user", user),
slog.String("error", err.Error()),
)
}
roleNames = append(roleNames, dbRoleNames...)
}
// Add default role and deduplicate
roleNames = deduplicateRoles(append(roleNames, defaultRole))
syncDone := time.Now()
s.logger.Debug("authorization check request",
slog.String("user", user),
slog.String("path", path),
slog.String("method", method),
slog.String("token_name", tokenName),
slog.String("workflow_id", workflowID),
slog.Any("roles", roleNames),
)
// Fetch user roles from cache/DB
userRoles, err := s.resolveRoles(ctx, roleNames)
if err != nil {
s.logger.Error("error resolving roles",
slog.String("user", user),
slog.String("token_name", tokenName),
slog.String("workflow_id", workflowID),
slog.String("error", err.Error()),
slog.Any("roles", roleNames),
)
return s.denyResponse(codes.Internal, "internal error resolving roles"), nil
}
resolveDone := time.Now()
// Check access
result := s.checkAccess(ctx, user, path, method, userRoles)
accessDone := time.Now()
if !result.Allowed {
s.logger.Info("access denied",
slog.String("user", user),
slog.String("path", path),
slog.String("method", method),
slog.String("token_name", tokenName),
slog.String("workflow_id", workflowID),
slog.Any("roles", roleNames),
slog.Duration("parse", parseDone.Sub(checkStart)),
slog.Duration("sync_roles", syncDone.Sub(parseDone)),
slog.Duration("resolve_roles", resolveDone.Sub(syncDone)),
slog.Duration("check_access", accessDone.Sub(resolveDone)),
slog.Duration("total", accessDone.Sub(checkStart)),
)
return s.denyResponse(codes.PermissionDenied, "access denied"), nil
}
s.logger.Info("access allowed",
slog.String("user", user),
slog.String("path", path),
slog.String("method", method),
slog.String("token_name", tokenName),
slog.String("workflow_id", workflowID),
)
responseHeaders := map[string]string{}
var computePoolsDur time.Duration
switch result.MatchedAction {
case roles.ActionProfileRead, roles.ActionResourcesRead:
poolsStart := time.Now()
responseHeaders[headerOsmoAllowedPools] = strings.Join(
s.computeAllowedPools(ctx, user, userRoles), ",")
computePoolsDur = time.Since(poolsStart)
}
s.logger.Info("check timing",
slog.String("user", user),
slog.String("path", path),
slog.String("method", method),
slog.String("token_name", tokenName),
slog.String("workflow_id", workflowID),
slog.Duration("parse", parseDone.Sub(checkStart)),
slog.Duration("sync_roles", syncDone.Sub(parseDone)),
slog.Duration("resolve_roles", resolveDone.Sub(syncDone)),
slog.Duration("check_access", accessDone.Sub(resolveDone)),
slog.Duration("compute_pools", computePoolsDur),
slog.Duration("total", time.Since(checkStart)),
)
return s.allowResponse(responseHeaders), nil
}
// resolveRoles fetches role objects from cache/DB for the given role names.
func (s *AuthzServer) resolveRoles(ctx context.Context, roleNames []string) ([]*roles.Role, error) {
cachedRoles, missingNames := s.roleCache.Get(roleNames)
if len(missingNames) > 0 {
dbRoles, err := roles.GetRoles(ctx, s.pgClient, missingNames, s.logger)
if err != nil {
return nil, fmt.Errorf("failed to fetch roles: %w", err)
}
if len(dbRoles) > 0 {
s.roleCache.Set(dbRoles)
cachedRoles = append(cachedRoles, dbRoles...)
}
}
return cachedRoles, nil
}
// checkAccess verifies if the given roles have access to the path and method.
func (s *AuthzServer) checkAccess(
ctx context.Context, user, path, method string, userRoles []*roles.Role) roles.AccessResult {
result := roles.CheckRolesAccess(ctx, userRoles, path, method, s.pgClient)
s.logAccessResult(result, user, path, method)
return result
}
// computeAllowedPools evaluates role policies to determine which pools the
// user can access, respecting deny rules. Uses the pool name cache to avoid
// hitting the database on every request.
func (s *AuthzServer) computeAllowedPools(
ctx context.Context, user string, userRoles []*roles.Role) []string {
allPoolNames, ok := s.poolNameCache.Get()
if !ok {
var err error
allPoolNames, err = roles.GetAllPoolNames(ctx, s.pgClient)
if err != nil {
s.logger.Error("failed to get pool names for allowed pools computation",
slog.String("user", user),
slog.String("error", err.Error()))
return []string{}
}
s.poolNameCache.Set(allPoolNames)
}
allowed := roles.GetAllowedPools(userRoles, allPoolNames)
s.logger.Info("computed allowed pools",
slog.String("user", user),
slog.Any("allowed_pools", allowed),
)
return allowed
}
// logAccessResult logs the result of an access check with appropriate details
func (s *AuthzServer) logAccessResult(result roles.AccessResult, user, path, method string) {
if result.ActionType == roles.ActionTypeSemantic && result.Allowed {
s.logger.Debug("access granted by semantic action",
slog.String("user", user),
slog.String("role", result.RoleName),
slog.String("action", result.MatchedAction),
slog.String("resource", result.MatchedResource),
slog.String("path", path),
slog.String("method", method),
)
}
}
// allowResponse creates a successful authorization response with optional
// headers injected into the upstream request.
func (s *AuthzServer) allowResponse(headers map[string]string) *envoy_service_auth_v3.CheckResponse {
var headerOpts []*envoy_api_v3_core.HeaderValueOption
for key, value := range headers {
headerOpts = append(headerOpts, &envoy_api_v3_core.HeaderValueOption{
Header: &envoy_api_v3_core.HeaderValue{
Key: key,
Value: value,
},
})
}
return &envoy_service_auth_v3.CheckResponse{
Status: &status.Status{
Code: int32(codes.OK),
},
HttpResponse: &envoy_service_auth_v3.CheckResponse_OkResponse{
OkResponse: &envoy_service_auth_v3.OkHttpResponse{
Headers: headerOpts,
},
},
}
}
// deduplicateRoles returns a new slice with duplicate role names removed,
// preserving the order of first occurrence.
func deduplicateRoles(names []string) []string {
seen := make(map[string]struct{}, len(names))
out := make([]string, 0, len(names))
for _, n := range names {
if _, ok := seen[n]; !ok {
seen[n] = struct{}{}
out = append(out, n)
}
}
return out
}
// denyResponse creates a denial authorization response
func (s *AuthzServer) denyResponse(code codes.Code, message string) *envoy_service_auth_v3.CheckResponse {
return &envoy_service_auth_v3.CheckResponse{
Status: &status.Status{
Code: int32(code),
Message: message,
},
HttpResponse: &envoy_service_auth_v3.CheckResponse_DeniedResponse{
DeniedResponse: &envoy_service_auth_v3.DeniedHttpResponse{
Status: &envoy_type_v3.HttpStatus{
Code: envoy_type_v3.StatusCode_Forbidden,
},
Body: message,
Headers: []*envoy_api_v3_core.HeaderValueOption{
{
Header: &envoy_api_v3_core.HeaderValue{
Key: "content-type",
Value: "text/plain",
},
},
},
},
},
}
}