-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
[TT-12897] Merge path based permissions when combining policies #6597
Open
titpetric
wants to merge
1
commit into
master
Choose a base branch
from
update/merge-allowed-urls
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+251
−132
Open
Changes from all commits
Commits
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 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 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 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 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 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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package policy | ||
|
||
import ( | ||
"github.com/TykTechnologies/tyk/user" | ||
) | ||
|
||
// StoreMap is same as Store, but doesn't preserve order. | ||
type StoreMap struct { | ||
policies map[string]user.Policy | ||
} | ||
|
||
// NewStoreMap returns a new policy.StoreMap. | ||
func NewStoreMap(policies map[string]user.Policy) *StoreMap { | ||
if len(policies) == 0 { | ||
policies = make(map[string]user.Policy) | ||
} | ||
|
||
return &StoreMap{ | ||
policies: policies, | ||
} | ||
} | ||
|
||
// PolicyIDs returns a list policy IDs in the store. | ||
// It will return nil if no policies exist. | ||
func (s *StoreMap) PolicyIDs() []string { | ||
if len(s.policies) == 0 { | ||
return nil | ||
} | ||
|
||
policyIDs := make([]string, 0, len(s.policies)) | ||
for _, val := range s.policies { | ||
policyIDs = append(policyIDs, val.ID) | ||
} | ||
return policyIDs | ||
} | ||
|
||
// PolicyByID returns a policy by ID. | ||
func (s *StoreMap) PolicyByID(id string) (user.Policy, bool) { | ||
v, ok := s.policies[id] | ||
return v, ok | ||
} | ||
|
||
// PolicyCount returns the number of policies in the store. | ||
func (s *StoreMap) PolicyCount() int { | ||
return len(s.policies) | ||
} |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why is this changed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Custom policies doesn't preserve order (map policyID => policy) and would fail tests due to the order changes. The PolicyIDs() doesn't return ids always in the same order, meaning Methods can be returned out of order failing the assertion(s), requiring a deeper assertion to account for the typical go range over map behaviour.
Store{} preserves order.
StoreMap{} doesn't.
This change ensures a deterministic order for applying policies, and in turn the merged access rights
Methods
are sorted with the order the policies are applied in, simplifying the assertion in tests.No other uses in gw. If the implementing ticket wasn't so bad with detail, I could easily consider custom policies functions internal scope. The underlying storage is a []any inside a map[string]any MetaData.
Considered but rejected a deeper assertion in tests for methods (ElementsEqual...). Not preserving custom policy order was a code smell, the added GetCustomPolicies works around the restriction.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sidenote:
CustomPolicies []Policy
on SessionState, not sure why it needs the API around a map[string]any;