-
Notifications
You must be signed in to change notification settings - Fork 1.7k
[feature] Add policy filtering #78
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 1 commit
Commits
Show all changes
2 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
Next
Next commit
[feature] Implement Policy Filtering
A new interface, `FilteredAdapter`, extends the `Adapter` interface with the ability to load a filtered subset of the backend policy. This allows Casbin to more effectively scale when enforcing a very large number of policies, such as a busy multi-tenant system. There is also a second built-in file adapter supporting this feature. To prevent accidental data loss, the `SavePolicy` method is disabled when a filtered policy is loaded. For maximum compatibility, whether a given `Adapter` implements the new feature is checked at runtime.
- Loading branch information
commit e9895b6cc3f0eb8e95dc9cc6bb29875acf48c6f6
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
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
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 |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| // Copyright 2017 The casbin Authors. 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. | ||
|
|
||
| package persist | ||
|
|
||
| import ( | ||
| "github.com/casbin/casbin/model" | ||
| ) | ||
|
|
||
| // FilteredAdapter is the interface for Casbin adapters supporting filtered policies. | ||
| type FilteredAdapter interface { | ||
| Adapter | ||
|
|
||
| // LoadFilteredPolicy loads only policy rules that match the filter. | ||
| LoadFilteredPolicy(model model.Model, filter interface{}) error | ||
| // IsFiltered returns true if the loaded policy has been filtered. | ||
| IsFiltered() bool | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| // Copyright 2017 The casbin Authors. 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. | ||
|
|
||
| package fileadapter | ||
|
|
||
| import ( | ||
| "bufio" | ||
| "errors" | ||
| "os" | ||
| "strings" | ||
|
|
||
| "github.com/casbin/casbin/model" | ||
| "github.com/casbin/casbin/persist" | ||
| ) | ||
|
|
||
| // FilteredAdapter is the filtered file adapter for Casbin. It can load policy | ||
| // from file or save policy to file and supports loading of filtered policies. | ||
| type FilteredAdapter struct { | ||
| *Adapter | ||
| filtered bool | ||
| } | ||
|
|
||
| // Filter defines the filtering rules for a FilteredAdapter's policy. Empty values | ||
| // are ignored, but all others must match the filter. | ||
| type Filter struct { | ||
| P []string | ||
| G []string | ||
| } | ||
|
|
||
| // NewFilteredAdapter is the constructor for FilteredAdapter. | ||
| func NewFilteredAdapter(filePath string) *FilteredAdapter { | ||
| a := FilteredAdapter{} | ||
| a.Adapter = NewAdapter(filePath) | ||
| return &a | ||
| } | ||
|
|
||
| func (a *FilteredAdapter) LoadPolicy(model model.Model) error { | ||
| a.filtered = false | ||
| return a.Adapter.LoadPolicy(model) | ||
| } | ||
|
|
||
| // LoadPolicy loads all policy rules from the storage. | ||
| func (a *FilteredAdapter) LoadFilteredPolicy(model model.Model, filter interface{}) error { | ||
| if filter == nil { | ||
| return a.LoadPolicy(model) | ||
| } | ||
| if a.filePath == "" { | ||
| return errors.New("invalid file path, file path cannot be empty") | ||
| } | ||
|
|
||
| filterValue, ok := filter.(*Filter) | ||
| if !ok { | ||
| return errors.New("invalid filter type") | ||
| } | ||
| err := a.loadFilteredPolicyFile(model, filterValue, persist.LoadPolicyLine) | ||
| if err == nil { | ||
| a.filtered = true | ||
| } | ||
| return err | ||
| } | ||
|
|
||
| func (a *FilteredAdapter) loadFilteredPolicyFile(model model.Model, filter *Filter, handler func(string, model.Model)) error { | ||
| f, err := os.Open(a.filePath) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| defer f.Close() | ||
|
|
||
| scanner := bufio.NewScanner(f) | ||
| for scanner.Scan() { | ||
| line := strings.TrimSpace(scanner.Text()) | ||
|
|
||
| if filterLine(line, filter) { | ||
| continue | ||
| } | ||
|
|
||
| handler(line, model) | ||
| } | ||
| return scanner.Err() | ||
| } | ||
|
|
||
| // IsFiltered returns true if the loaded policy has been filtered. | ||
| func (a *FilteredAdapter) IsFiltered() bool { | ||
| return a.filtered | ||
| } | ||
|
|
||
| // SavePolicy saves all policy rules to the storage. | ||
| func (a *FilteredAdapter) SavePolicy(model model.Model) error { | ||
| if a.filtered == true { | ||
| return errors.New("cannot save a filtered policy") | ||
| } | ||
| return a.Adapter.SavePolicy(model) | ||
| } | ||
|
|
||
| func filterLine(line string, filter *Filter) bool { | ||
| if filter == nil { | ||
| return false | ||
| } | ||
| p := strings.Split(line, ",") | ||
| if len(p) == 0 { | ||
| return true | ||
| } | ||
| var filterSlice []string | ||
| switch strings.TrimSpace(p[0]) { | ||
| case "p": | ||
| filterSlice = filter.P | ||
| case "g": | ||
| filterSlice = filter.G | ||
| } | ||
| return filterWords(p, filterSlice) | ||
| } | ||
|
|
||
| func filterWords(line []string, filter []string) bool { | ||
| if len(line) < len(filter)+1 { | ||
| return true | ||
| } | ||
| var skipLine bool | ||
| for i, v := range filter { | ||
| if len(v) > 0 && strings.TrimSpace(v) != strings.TrimSpace(line[i+1]) { | ||
| skipLine = true | ||
| break | ||
| } | ||
| } | ||
| return skipLine | ||
| } |
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.
I took the opportunity to integrate my streamlined handler into the base file adapter, as it will be simpler for others to use as a reference IMO.