-
Notifications
You must be signed in to change notification settings - Fork 4
/
authorization.go
422 lines (394 loc) · 15.8 KB
/
authorization.go
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
package authorization
import (
"context"
"errors"
"fmt"
"log/slog"
"strconv"
"strings"
"time"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"github.com/open-policy-agent/opa/metrics"
"github.com/open-policy-agent/opa/profiler"
opaSdk "github.com/open-policy-agent/opa/sdk"
"github.com/opentdf/platform/protocol/go/authorization"
"github.com/opentdf/platform/protocol/go/entityresolution"
"github.com/opentdf/platform/protocol/go/policy"
attr "github.com/opentdf/platform/protocol/go/policy/attributes"
otdf "github.com/opentdf/platform/sdk"
"github.com/opentdf/platform/service/internal/access"
"github.com/opentdf/platform/service/internal/entitlements"
"github.com/opentdf/platform/service/internal/logger"
"github.com/opentdf/platform/service/internal/opa"
"github.com/opentdf/platform/service/pkg/db"
"github.com/opentdf/platform/service/pkg/serviceregistry"
"golang.org/x/oauth2"
"golang.org/x/oauth2/clientcredentials"
)
type AuthorizationService struct { //nolint:revive // AuthorizationService is a valid name for this struct
authorization.UnimplementedAuthorizationServiceServer
eng *opa.Engine
sdk *otdf.SDK
ersURL string
logger *logger.Logger
tokenSource *oauth2.TokenSource
}
const tokenExpiryDelay = 100
func NewRegistration() serviceregistry.Registration {
return serviceregistry.Registration{
Namespace: "authorization",
ServiceDesc: &authorization.AuthorizationService_ServiceDesc,
RegisterFunc: func(srp serviceregistry.RegistrationParams) (any, serviceregistry.HandlerServer) {
// default ERS endpoint
var ersURL = "http://localhost:8080/entityresolution/resolve"
var clientID = "tdf-authorization-svc"
var clientSecret = "secret"
var tokenEndpoint = "http://localhost:8888/auth/realms/opentdf/protocol/openid-connect/token" //nolint:gosec // default token endpoint
as := &AuthorizationService{eng: srp.Engine, sdk: srp.SDK, logger: srp.Logger}
if err := srp.RegisterReadinessCheck("authorization", as.IsReady); err != nil {
slog.Error("failed to register authorization readiness check", slog.String("error", err.Error()))
}
// if its passed in the config use that
val, ok := srp.Config.ExtraProps["ersurl"]
if ok {
ersURL, ok = val.(string)
if !ok {
panic("Error casting ersURL to string")
}
}
val, ok = srp.Config.ExtraProps["clientid"]
if ok {
clientID, ok = val.(string)
if !ok {
panic("Error casting clientID to string")
}
}
val, ok = srp.Config.ExtraProps["clientsecret"]
if ok {
clientSecret, ok = val.(string)
if !ok {
panic("Error casting clientSecret to string")
}
}
val, ok = srp.Config.ExtraProps["tokenendpoint"]
if ok {
tokenEndpoint, ok = val.(string)
if !ok {
panic("Error casting tokenendpoint to string")
}
}
config := clientcredentials.Config{ClientID: clientID, ClientSecret: clientSecret, TokenURL: tokenEndpoint}
slog.Debug("authorization service client config", slog.Any("config", config))
newTokenSource := oauth2.ReuseTokenSourceWithExpiry(nil, config.TokenSource(context.Background()), tokenExpiryDelay)
slog.Debug("authorization service token source created", slog.Any("token_source", newTokenSource))
as.ersURL = ersURL
as.tokenSource = &newTokenSource
return as, func(ctx context.Context, mux *runtime.ServeMux, server any) error {
authServer, okAuth := server.(authorization.AuthorizationServiceServer)
if !okAuth {
return fmt.Errorf("failed to assert server type to authorization.AuthorizationServiceServer")
}
return authorization.RegisterAuthorizationServiceHandlerServer(ctx, mux, authServer)
}
},
}
}
// TODO: Not sure what we want to check here?
func (as AuthorizationService) IsReady(ctx context.Context) error {
slog.DebugContext(ctx, "checking readiness of authorization service")
return nil
}
// abstracted into variable for mocking in tests
var retrieveAttributeDefinitions = func(ctx context.Context, ra *authorization.ResourceAttribute, sdk *otdf.SDK) (map[string]*attr.GetAttributeValuesByFqnsResponse_AttributeAndValue, error) {
attrFqns := ra.GetAttributeValueFqns()
if len(attrFqns) == 0 {
return make(map[string]*attr.GetAttributeValuesByFqnsResponse_AttributeAndValue), nil
}
resp, err := sdk.Attributes.GetAttributeValuesByFqns(ctx, &attr.GetAttributeValuesByFqnsRequest{
WithValue: &policy.AttributeValueSelector{
WithSubjectMaps: false,
},
Fqns: attrFqns,
})
if err != nil {
return nil, err
}
return resp.GetFqnAttributeValues(), nil
}
// abstracted into variable for mocking in tests
var retrieveEntitlements = func(ctx context.Context, req *authorization.GetEntitlementsRequest, as *AuthorizationService) (*authorization.GetEntitlementsResponse, error) {
return as.GetEntitlements(ctx, req)
}
func (as *AuthorizationService) GetDecisionsByToken(ctx context.Context, req *authorization.GetDecisionsByTokenRequest) (*authorization.GetDecisionsByTokenResponse, error) {
var decisionsRequests = []*authorization.DecisionRequest{}
// for each token decision request
for _, tdr := range req.GetDecisionRequests() {
ecResp, err := as.sdk.EntityResoution.CreateEntityChainFromJwt(ctx, &entityresolution.CreateEntityChainFromJwtRequest{Tokens: tdr.GetTokens()})
if err != nil {
slog.Error("Error calling ERS to get entity chains from jwts")
return nil, err
}
// form a decision request for the token decision request
decisionsRequests = append(decisionsRequests, &authorization.DecisionRequest{
Actions: tdr.GetActions(),
EntityChains: ecResp.GetEntityChains(),
ResourceAttributes: tdr.GetResourceAttributes(),
})
}
// slog.Debug("Calling GetDecisions from GetDecisionsByToken")
// slog.Debug("GetDecisions Input", "input", decisionsRequests)
resp, err := as.GetDecisions(ctx, &authorization.GetDecisionsRequest{
DecisionRequests: decisionsRequests,
})
if err != nil {
return nil, err
}
return &authorization.GetDecisionsByTokenResponse{DecisionResponses: resp.GetDecisionResponses()}, err
}
func (as *AuthorizationService) GetDecisions(ctx context.Context, req *authorization.GetDecisionsRequest) (*authorization.GetDecisionsResponse, error) {
as.logger.DebugContext(ctx, "getting decisions")
// Temporary canned echo response with permit decision for all requested decision/entity/ra combos
rsp := &authorization.GetDecisionsResponse{
DecisionResponses: make([]*authorization.DecisionResponse, 0),
}
for _, dr := range req.GetDecisionRequests() {
for _, ra := range dr.GetResourceAttributes() {
as.logger.DebugContext(ctx, "getting resource attributes", slog.String("FQNs", strings.Join(ra.GetAttributeValueFqns(), ", ")))
// get attribute definition/value combinations
dataAttrDefsAndVals, err := retrieveAttributeDefinitions(ctx, ra, as.sdk)
if err != nil {
// TODO: should all decisions in a request fail if one FQN lookup fails?
return nil, db.StatusifyError(err, db.ErrTextGetRetrievalFailed, slog.String("fqns", strings.Join(ra.GetAttributeValueFqns(), ", ")))
}
var attrDefs []*policy.Attribute
var attrVals []*policy.Value
for fqn, v := range dataAttrDefsAndVals {
attrDefs = append(attrDefs, v.GetAttribute())
attrVal := v.GetValue()
attrVal.Fqn = fqn
attrVals = append(attrVals, attrVal)
}
attrDefs, err = populateAttrDefValueFqns(attrDefs)
if err != nil {
return nil, err
}
// get the relevant resource attribute fqns
allPertinentFqnsRA := authorization.ResourceAttribute{
AttributeValueFqns: ra.GetAttributeValueFqns(),
}
for _, attrDef := range attrDefs {
if attrDef.GetRule() == policy.AttributeRuleTypeEnum_ATTRIBUTE_RULE_TYPE_ENUM_HIERARCHY {
for _, value := range attrDef.GetValues() {
allPertinentFqnsRA.AttributeValueFqns = append(allPertinentFqnsRA.AttributeValueFqns, value.GetFqn())
}
}
}
for _, ec := range dr.GetEntityChains() {
//
// TODO: we should already have the subject mappings here and be able to just use OPA to trim down the known data attr values to the ones matched up with the entities
//
entities := ec.GetEntities()
req := authorization.GetEntitlementsRequest{
Entities: entities,
Scope: &allPertinentFqnsRA,
}
entityAttrValues := make(map[string][]string)
if len(entities) == 0 || len(allPertinentFqnsRA.GetAttributeValueFqns()) == 0 {
slog.WarnContext(ctx, "Empty entity list and/or entity data attribute list")
} else {
ecEntitlements, err := retrieveEntitlements(ctx, &req, as)
if err != nil {
// TODO: should all decisions in a request fail if one entity entitlement lookup fails?
return nil, db.StatusifyError(err, db.ErrTextGetRetrievalFailed, slog.String("extra", "getEntitlements request failed"))
}
// TODO this might cause errors if multiple entities dont have ids
// currently just adding each entity returned to same list
for _, e := range ecEntitlements.GetEntitlements() {
entityAttrValues[e.GetEntityId()] = e.GetAttributeValueFqns()
}
}
// call access-pdp
accessPDP := access.NewPdp()
decisions, err := accessPDP.DetermineAccess(
ctx,
attrVals,
entityAttrValues,
attrDefs,
)
if err != nil {
// TODO: should all decisions in a request fail if one entity entitlement lookup fails?
return nil, db.StatusifyError(err, db.ErrTextGetRetrievalFailed, slog.String("extra", "DetermineAccess request to Access PDP failed"))
}
// check the decisions
decision := authorization.DecisionResponse_DECISION_PERMIT
for _, d := range decisions {
if !d.Access {
decision = authorization.DecisionResponse_DECISION_DENY
}
}
decisionResp := &authorization.DecisionResponse{
Decision: decision,
EntityChainId: ec.GetId(),
Action: &policy.Action{
Value: &policy.Action_Standard{
Standard: policy.Action_STANDARD_ACTION_TRANSMIT,
},
},
}
if ra.GetResourceAttributesId() != "" {
decisionResp.ResourceAttributesId = ra.GetResourceAttributesId()
} else if len(ra.GetAttributeValueFqns()) > 0 {
decisionResp.ResourceAttributesId = ra.GetAttributeValueFqns()[0]
}
rsp.DecisionResponses = append(rsp.DecisionResponses, decisionResp)
}
}
}
return rsp, nil
}
func (as *AuthorizationService) GetEntitlements(ctx context.Context, req *authorization.GetEntitlementsRequest) (*authorization.GetEntitlementsResponse, error) {
as.logger.DebugContext(ctx, "getting entitlements")
request := attr.GetAttributeValuesByFqnsRequest{
WithValue: &policy.AttributeValueSelector{
WithSubjectMaps: true,
},
}
// Lack of scope has impacts on performance
// https://github.com/opentdf/platform/issues/365
if req.GetScope() == nil {
// TODO: Reomve and use MatchSubjectMappings instead later in the flow
listAttributeResp, err := as.sdk.Attributes.ListAttributes(ctx, &attr.ListAttributesRequest{})
if err != nil {
return nil, err
}
var attributeFqns []string
for _, attr := range listAttributeResp.GetAttributes() {
ns := attr.GetNamespace().GetName()
an := attr.GetName()
for _, val := range attr.GetValues() {
fqn, err := fqnBuilder(ns, an, val.GetValue())
if err != nil {
slog.Error("Error building attribute fqn for ", "attr", attr, "value", val)
return nil, err
}
attributeFqns = append(attributeFqns, fqn)
}
}
request.Fqns = attributeFqns
} else {
// get subject mappings
request.Fqns = req.GetScope().GetAttributeValueFqns()
}
avf, err := as.sdk.Attributes.GetAttributeValuesByFqns(ctx, &request)
if err != nil {
return nil, err
}
subjectMappings := avf.GetFqnAttributeValues()
as.logger.DebugContext(ctx, "retrieved from subject mappings service", slog.Any("subject_mappings: ", subjectMappings))
if req.Entities == nil {
as.logger.ErrorContext(ctx, "requires entities")
return nil, errors.New("entity chain is required")
}
rsp := &authorization.GetEntitlementsResponse{
Entitlements: make([]*authorization.EntityEntitlements, len(req.GetEntities())),
}
for i, entity := range req.GetEntities() {
// TODO: change this and the opa to take a bulk request and not have to call opa for each entity
// get the client auth token
authToken, err := (*as.tokenSource).Token()
if err != nil {
slog.Error("failed to get client auth token in GetEntitlements", slog.String("error", err.Error()))
return nil, fmt.Errorf("failed to get client auth token in GetEntitlements: %w", err)
}
// OPA
in, err := entitlements.OpaInput(entity, subjectMappings, as.ersURL, authToken.AccessToken)
if err != nil {
slog.Error("failed to build OPA input", slog.Any("entity", entity), slog.String("error", err.Error()))
slog.Debug("authToken", "authToken", authToken) // only log token in debug mode
return nil, fmt.Errorf("failed to build OPA input in GetEntitlements: %w", err)
}
as.logger.DebugContext(ctx, "entitlements", "entity_id", entity.GetId(), "input", fmt.Sprintf("%+v", in))
// uncomment for debugging
// if slog.Default().Enabled(ctx, slog.LevelDebug) {
// _ = json.NewEncoder(os.Stdout).Encode(in)
// }
options := opaSdk.DecisionOptions{
Now: time.Now(),
Path: "opentdf/entitlements/attributes", // change to /resolve_entities to get output of idp_plugin
Input: in,
NDBCache: nil,
StrictBuiltinErrors: true,
Tracer: nil,
Metrics: metrics.New(),
Profiler: profiler.New(),
Instrument: true,
DecisionID: fmt.Sprintf("%-v", req.String()),
}
decision, err := as.eng.Decision(ctx, options)
if err != nil {
return nil, fmt.Errorf("failed to get decision from OPA Engine in GetEntitlements: %w", err)
}
// uncomment for debugging
// if slog.Default().Enabled(ctx, slog.LevelDebug) {
// _ = json.NewEncoder(os.Stdout).Encode(decision.Result)
// }
// if the output is a string, it is an error
resultError, ok := decision.Result.(string)
if ok {
as.logger.DebugContext(ctx, "not ok", "entity_id", entity.GetId(), "decision.Result", fmt.Sprintf("%+v", resultError))
return nil, errors.New(resultError)
}
results, ok := decision.Result.([]interface{})
if !ok {
as.logger.DebugContext(ctx, "not ok", "entity_id", entity.GetId(), "decision.Result", fmt.Sprintf("%+v", decision.Result))
return nil, err
}
as.logger.DebugContext(ctx, "opa results", "entity_id", entity.GetId(), "results", fmt.Sprintf("%+v", results))
saa := make([]string, len(results))
for k, v := range results {
str, okk := v.(string)
if !okk {
as.logger.DebugContext(ctx, "not ok", slog.String("entity_id", entity.GetId()), slog.String(strconv.Itoa(k), fmt.Sprintf("%+v", v)))
}
saa[k] = str
}
rsp.Entitlements[i] = &authorization.EntityEntitlements{
EntityId: entity.GetId(),
AttributeValueFqns: saa,
}
}
as.logger.DebugContext(ctx, "opa", "rsp", fmt.Sprintf("%+v", rsp))
return rsp, nil
}
// Build an fqn from a namespace, attribute name, and value
func fqnBuilder(n string, a string, v string) (string, error) {
fqn := "https://"
switch {
case n != "" && a != "" && v != "":
return fqn + n + "/attr/" + a + "/value/" + v, nil
case n != "" && a != "" && v == "":
return fqn + n + "/attr/" + a, nil
case n != "" && a == "":
return fqn + n, nil
default:
return "", errors.New("invalid FQN, unable to build fqn")
}
}
// If there are missing fqns in the attribute definition fill them in using
// information from the attr definition
func populateAttrDefValueFqns(attrDefs []*policy.Attribute) ([]*policy.Attribute, error) {
for i, attrDef := range attrDefs {
ns := attrDef.GetNamespace().GetName()
attr := attrDef.GetName()
for j, value := range attrDef.GetValues() {
if value.GetFqn() == "" {
fqn, err := fqnBuilder(ns, attr, value.GetValue())
if err != nil {
return nil, err
}
attrDefs[i].Values[j].Fqn = fqn
}
}
}
return attrDefs, nil
}