- 
                Notifications
    
You must be signed in to change notification settings  - Fork 24
 
feat(authz): protovalidate tests for new authz obligations fields #2747
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Merged
      
      
    
  
     Merged
                    Changes from 2 commits
      Commits
    
    
            Show all changes
          
          
            4 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      
    File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| 
          
            
          
           | 
    @@ -7,6 +7,7 @@ import ( | |
| "testing" | ||
| 
     | 
||
| "buf.build/go/protovalidate" | ||
| "github.com/opentdf/platform/lib/identifier" | ||
| authzV2 "github.com/opentdf/platform/protocol/go/authorization/v2" | ||
| "github.com/opentdf/platform/protocol/go/entity" | ||
| "github.com/opentdf/platform/protocol/go/policy" | ||
| 
        
          
        
         | 
    @@ -23,6 +24,7 @@ var ( | |
| sampleResourceFQN = "https://example.com/attr/hier/value/highest" | ||
| sampleResourceFQN2 = "https://example.com/attr/hier/value/lowest" | ||
| sampleRegisteredResourceFQN = "https://example.com/reg_res/system/value/internal" | ||
| sampleObligationValueFQN = "https://example.com/obl/drm/value/prevent_print" | ||
| 
     | 
||
| // Good multi-resource requests that should pass validation | ||
| goodMultiResourceRequests = []struct { | ||
| 
          
            
          
           | 
    @@ -353,6 +355,62 @@ var ( | |
| }, | ||
| expectedValidationError: "registered_resource_value_fqn", | ||
| }, | ||
| { | ||
| name: "too many obligations", | ||
| request: &authzV2.GetDecisionMultiResourceRequest{ | ||
| EntityIdentifier: &authzV2.EntityIdentifier{ | ||
| Identifier: &authzV2.EntityIdentifier_EntityChain{ | ||
| EntityChain: &entity.EntityChain{ | ||
| EphemeralId: "1234", | ||
| Entities: []*entity.Entity{ | ||
| { | ||
| EphemeralId: "chained-1", | ||
| EntityType: &entity.Entity_EmailAddress{EmailAddress: "[email protected]"}, | ||
| Category: entity.Entity_CATEGORY_SUBJECT, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| Action: sampleActionCreate, | ||
| Resources: []*authzV2.Resource{ | ||
| { | ||
| Resource: &authzV2.Resource_AttributeValues_{ | ||
| AttributeValues: &authzV2.Resource_AttributeValues{ | ||
| Fqns: []string{sampleResourceFQN}, | ||
| }, | ||
| }, | ||
| }, | ||
| { | ||
| Resource: &authzV2.Resource_RegisteredResourceValueFqn{ | ||
| RegisteredResourceValueFqn: sampleRegisteredResourceFQN, | ||
| }, | ||
| }, | ||
| }, | ||
| FulfillableObligationFqns: getTooManyObligations(), | ||
| }, | ||
| expectedValidationError: "obligation_value_fqns_valid", | ||
| }, | ||
| { | ||
| name: "invalid obligation", | ||
| request: &authzV2.GetDecisionMultiResourceRequest{ | ||
| EntityIdentifier: &authzV2.EntityIdentifier{ | ||
| Identifier: &authzV2.EntityIdentifier_RegisteredResourceValueFqn{ | ||
| RegisteredResourceValueFqn: sampleRegisteredResourceFQN, | ||
| }, | ||
| }, | ||
| Action: sampleActionCreate, | ||
| Resources: []*authzV2.Resource{ | ||
| { | ||
| Resource: &authzV2.Resource_RegisteredResourceValueFqn{ | ||
| RegisteredResourceValueFqn: sampleRegisteredResourceFQN, | ||
| }, | ||
| }, | ||
| }, | ||
| FulfillableObligationFqns: []string{"missing.scheme/obl/name/value/val"}, | ||
| }, | ||
| expectedValidationError: "obligation_value_fqns_valid", | ||
| }, | ||
| } | ||
| ) | ||
| 
     | 
||
| 
          
            
          
           | 
    @@ -418,6 +476,10 @@ func Test_Resource_ManyAttributeValues(t *testing.T) { | |
| 
     | 
||
| func Test_GetDecisionRequest_Succeeds(t *testing.T) { | ||
| v := getValidator() | ||
| fiftyObligations := make([]string, 50) | ||
| for i := range 50 { | ||
| fiftyObligations[i] = sampleObligationValueFQN | ||
| } | ||
| 
     | 
||
| cases := []struct { | ||
| name string | ||
| 
          
            
          
           | 
    @@ -558,6 +620,40 @@ func Test_GetDecisionRequest_Succeeds(t *testing.T) { | |
| }, | ||
| }, | ||
| }, | ||
| { | ||
| name: "entity: registered resource, action: create, resource: registered, obligations - 1", | ||
| request: &authzV2.GetDecisionRequest{ | ||
| EntityIdentifier: &authzV2.EntityIdentifier{ | ||
| Identifier: &authzV2.EntityIdentifier_RegisteredResourceValueFqn{ | ||
| RegisteredResourceValueFqn: sampleRegisteredResourceFQN, | ||
| }, | ||
| }, | ||
| Action: sampleActionCreate, | ||
| Resource: &authzV2.Resource{ | ||
| Resource: &authzV2.Resource_RegisteredResourceValueFqn{ | ||
| RegisteredResourceValueFqn: sampleRegisteredResourceFQN, | ||
| }, | ||
| }, | ||
| FulfillableObligationFqns: []string{sampleObligationValueFQN}, | ||
| }, | ||
| }, | ||
| { | ||
| name: "entity: registered resource, action: create, resource: registered, obligations - 50", | ||
| request: &authzV2.GetDecisionRequest{ | ||
| EntityIdentifier: &authzV2.EntityIdentifier{ | ||
| Identifier: &authzV2.EntityIdentifier_RegisteredResourceValueFqn{ | ||
| RegisteredResourceValueFqn: sampleRegisteredResourceFQN, | ||
| }, | ||
| }, | ||
| Action: sampleActionCreate, | ||
| Resource: &authzV2.Resource{ | ||
| Resource: &authzV2.Resource_RegisteredResourceValueFqn{ | ||
| RegisteredResourceValueFqn: sampleRegisteredResourceFQN, | ||
| }, | ||
| }, | ||
| FulfillableObligationFqns: fiftyObligations, | ||
| }, | ||
| }, | ||
| } | ||
| 
     | 
||
| for _, tc := range cases { | ||
| 
        
          
        
         | 
    @@ -570,6 +666,7 @@ func Test_GetDecisionRequest_Succeeds(t *testing.T) { | |
| 
     | 
||
| func Test_GetDecisionRequest_Fails(t *testing.T) { | ||
| v := getValidator() | ||
| 
     | 
||
| cases := []struct { | ||
| name string | ||
| request *authzV2.GetDecisionRequest | ||
| 
          
            
          
           | 
    @@ -771,6 +868,40 @@ func Test_GetDecisionRequest_Fails(t *testing.T) { | |
| }, | ||
| expectedValidationError: "entities", | ||
| }, | ||
| { | ||
| name: "too many obligations", | ||
| request: &authzV2.GetDecisionRequest{ | ||
| EntityIdentifier: &authzV2.EntityIdentifier{ | ||
| Identifier: &authzV2.EntityIdentifier_RegisteredResourceValueFqn{ | ||
| RegisteredResourceValueFqn: sampleRegisteredResourceFQN, | ||
| }, | ||
| }, | ||
| Action: sampleActionCreate, | ||
| Resource: &authzV2.Resource{ | ||
| Resource: &authzV2.Resource_RegisteredResourceValueFqn{ | ||
| RegisteredResourceValueFqn: sampleRegisteredResourceFQN, | ||
| }, | ||
| }, | ||
| FulfillableObligationFqns: getTooManyObligations(), | ||
| }, | ||
| }, | ||
| { | ||
| name: "invalid obligation format", | ||
| request: &authzV2.GetDecisionRequest{ | ||
| EntityIdentifier: &authzV2.EntityIdentifier{ | ||
| Identifier: &authzV2.EntityIdentifier_RegisteredResourceValueFqn{ | ||
| RegisteredResourceValueFqn: sampleRegisteredResourceFQN, | ||
| }, | ||
| }, | ||
| Action: sampleActionCreate, | ||
| Resource: &authzV2.Resource{ | ||
| Resource: &authzV2.Resource_RegisteredResourceValueFqn{ | ||
| RegisteredResourceValueFqn: sampleRegisteredResourceFQN, | ||
| }, | ||
| }, | ||
| FulfillableObligationFqns: []string{"invalid-format"}, | ||
| }, | ||
| }, | ||
| } | ||
| 
     | 
||
| for _, tc := range cases { | ||
| 
        
          
        
         | 
    @@ -791,6 +922,7 @@ func Test_GetDecisionMultiResourceRequest_Succeeds(t *testing.T) { | |
| // All known good cases should pass | ||
| for _, tc := range goodMultiResourceRequests { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| tc.request.FulfillableObligationFqns = getRandomValidObligationValueFQNsList() | ||
| err := v.Validate(tc.request) | ||
| require.NoError(t, err, "validation should succeed for request: %s", tc.name) | ||
                
      
                  jakedoublev marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| }) | ||
| 
          
            
          
           | 
    @@ -874,6 +1006,7 @@ func Test_GetDecisionBulkRequest_Succeeds(t *testing.T) { | |
| clonedReq.Action = &policy.Action{ | ||
| Name: actions[rand.Intn(len(actions))], | ||
| } | ||
| clonedReq.FulfillableObligationFqns = getRandomValidObligationValueFQNsList() | ||
| reqs[j] = clonedReq | ||
| } | ||
| for j := firstCount; j < firstCount+secondCount; j++ { | ||
| 
        
          
        
         | 
    @@ -882,6 +1015,7 @@ func Test_GetDecisionBulkRequest_Succeeds(t *testing.T) { | |
| clonedReq.Action = &policy.Action{ | ||
| Name: actions[rand.Intn(len(actions))], | ||
| } | ||
| clonedReq.FulfillableObligationFqns = getRandomValidObligationValueFQNsList() | ||
| reqs[j] = clonedReq | ||
| } | ||
| 
     | 
||
| 
          
            
          
           | 
    @@ -1503,3 +1637,41 @@ func Test_RollupSingleResourceDecision_WithNilChecks(t *testing.T) { | |
| assert.Contains(t, err.Error(), "no decision results returned") | ||
| }) | ||
| } | ||
| 
     | 
||
| // Helpers | ||
| 
     | 
||
| func getRandomValidObligationValueFQNsList() []string { | ||
| count := rand.Intn(50) | ||
                
      
                  jakedoublev marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| randomList := make([]string, count) | ||
| for i := range count { | ||
| randomList[i] = getRandomObligationValueFQN() | ||
| } | ||
| return randomList | ||
| } | ||
| 
     | 
||
| func getTooManyObligations() []string { | ||
| tooMany := make([]string, 51) | ||
| for i := range tooMany { | ||
| tooMany[i] = getRandomObligationValueFQN() | ||
| } | ||
| return tooMany | ||
| } | ||
| 
     | 
||
| const charset = "abcdefghijklmnopqrstuvwxyz" | ||
| 
     | 
||
| func randString(length int) string { | ||
| b := make([]byte, length) | ||
| for i := range b { | ||
| b[i] = charset[rand.Intn(len(charset))] | ||
| } | ||
| return string(b) | ||
| } | ||
| 
     | 
||
| // builds a random FQN for an obligation value that matches | ||
| // https://<namespace>.com/obl/<name>/value/<value> | ||
| func getRandomObligationValueFQN() string { | ||
| namespace := "https://" + randString(5) + ".com" | ||
| name := randString(5) | ||
| value := randString(5) | ||
| return identifier.BuildOblValFQN(namespace, name, value) | ||
| } | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.