@@ -5,9 +5,11 @@ import (
5
5
"errors"
6
6
"fmt"
7
7
"log/slog"
8
+ "slices"
8
9
"strconv"
9
10
"strings"
10
11
12
+ "github.com/opentdf/platform/lib/identifier"
11
13
authz "github.com/opentdf/platform/protocol/go/authorization/v2"
12
14
entityresolutionV2 "github.com/opentdf/platform/protocol/go/entityresolution/v2"
13
15
"github.com/opentdf/platform/protocol/go/policy"
@@ -47,7 +49,7 @@ type EntitlementFailure struct {
47
49
type PolicyDecisionPoint struct {
48
50
logger * logger.Logger
49
51
allEntitleableAttributesByValueFQN map [string ]* attrs.GetAttributeValuesByFqnsResponse_AttributeAndValue
50
- // allRegisteredResourcesByValueFQN map[string]*policy.RegisteredResourceValue
52
+ allRegisteredResourceValuesByFQN map [string ]* policy.RegisteredResourceValue
51
53
}
52
54
53
55
var (
@@ -67,8 +69,7 @@ func NewPolicyDecisionPoint(
67
69
l * logger.Logger ,
68
70
allAttributeDefinitions []* policy.Attribute ,
69
71
allSubjectMappings []* policy.SubjectMapping ,
70
- // TODO: take in all registered resources and store them in memory by value FQN
71
- // allRegisteredResources []*policy.RegisteredResource,
72
+ allRegisteredResources []* policy.RegisteredResource ,
72
73
) (* PolicyDecisionPoint , error ) {
73
74
var err error
74
75
@@ -126,9 +127,26 @@ func NewPolicyDecisionPoint(
126
127
allEntitleableAttributesByValueFQN [mappedValueFQN ] = mapped
127
128
}
128
129
130
+ allRegisteredResourceValuesByFQN := make (map [string ]* policy.RegisteredResourceValue )
131
+ for _ , rr := range allRegisteredResources {
132
+ if err := validateRegisteredResource (rr ); err != nil {
133
+ return nil , fmt .Errorf ("invalid registered resource: %w" , err )
134
+ }
135
+ rrName := rr .GetName ()
136
+
137
+ for _ , v := range rr .GetValues () {
138
+ fullyQualifiedValue := identifier.FullyQualifiedRegisteredResourceValue {
139
+ Name : rrName ,
140
+ Value : v .GetValue (),
141
+ }
142
+ allRegisteredResourceValuesByFQN [fullyQualifiedValue .FQN ()] = v
143
+ }
144
+ }
145
+
129
146
pdp := & PolicyDecisionPoint {
130
147
l ,
131
148
allEntitleableAttributesByValueFQN ,
149
+ allRegisteredResourceValuesByFQN ,
132
150
}
133
151
return pdp , nil
134
152
}
@@ -299,3 +317,65 @@ func (p *PolicyDecisionPoint) GetEntitlements(
299
317
)
300
318
return result , nil
301
319
}
320
+
321
+ func (p * PolicyDecisionPoint ) GetEntitlementsRegisteredResource (
322
+ ctx context.Context ,
323
+ registeredResourceValueFQN string ,
324
+ withComprehensiveHierarchy bool ,
325
+ ) ([]* authz.EntityEntitlements , error ) {
326
+ l := p .logger .With ("withComprehensiveHierarchy" , strconv .FormatBool (withComprehensiveHierarchy ))
327
+ l .DebugContext (ctx , "getting entitlements for registered resource value" , slog .String ("fqn" , registeredResourceValueFQN ))
328
+
329
+ if _ , err := identifier.Parse [* identifier.FullyQualifiedRegisteredResourceValue ](registeredResourceValueFQN ); err != nil {
330
+ return nil , err
331
+ }
332
+
333
+ registeredResourceValue := p .allRegisteredResourceValuesByFQN [registeredResourceValueFQN ]
334
+ if err := validateRegisteredResourceValue (registeredResourceValue ); err != nil {
335
+ return nil , err
336
+ }
337
+
338
+ actionsPerAttributeValueFqn := make (map [string ]* authz.EntityEntitlements_ActionsList )
339
+
340
+ for _ , aav := range registeredResourceValue .GetActionAttributeValues () {
341
+ action := aav .GetAction ()
342
+ attrVal := aav .GetAttributeValue ()
343
+ attrValFQN := attrVal .GetFqn ()
344
+
345
+ actionsList , ok := actionsPerAttributeValueFqn [attrValFQN ]
346
+ if ! ok {
347
+ actionsList = & authz.EntityEntitlements_ActionsList {
348
+ Actions : make ([]* policy.Action , 0 ),
349
+ }
350
+ }
351
+
352
+ if ! slices .ContainsFunc (actionsList .GetActions (), func (a * policy.Action ) bool {
353
+ return a .GetName () == action .GetName ()
354
+ }) {
355
+ actionsList .Actions = append (actionsList .Actions , action )
356
+ }
357
+
358
+ actionsPerAttributeValueFqn [attrValFQN ] = actionsList
359
+
360
+ if withComprehensiveHierarchy {
361
+ err := populateLowerValuesIfHierarchy (attrValFQN , p .allEntitleableAttributesByValueFQN , actionsList , actionsPerAttributeValueFqn )
362
+ if err != nil {
363
+ return nil , fmt .Errorf ("error populating comprehensive lower hierarchy values for registered resource value FQN [%s]: %w" , attrValFQN , err )
364
+ }
365
+ }
366
+ }
367
+
368
+ result := []* authz.EntityEntitlements {
369
+ {
370
+ EphemeralId : registeredResourceValueFQN ,
371
+ ActionsPerAttributeValueFqn : actionsPerAttributeValueFqn ,
372
+ },
373
+ }
374
+ l .DebugContext (
375
+ ctx ,
376
+ "entitlement results for registered resource value" ,
377
+ slog .Any ("entitlements" , result ),
378
+ )
379
+
380
+ return result , nil
381
+ }
0 commit comments