flagr
flagr copied to clipboard
Customizable validation rules for enabled flags
Description
This changes adds customizable validation rules for enabled flags using https://github.com/Knetic/govaluate. The rules are specified via new environment variables in the env.go
; a list of govaluate boolean expressions to be evaluated for flags (a []string
), and a connecting operation (either OR
or AND
). When someone uses the API operations setFlagEnabled
or putFlag
and the flag is enabled=true
, we convert the flag into a map[string]interface{}
so that the properties of the flag are parameters for the govaluate expression. Then, we iterate through the govaluate expressions in order; if the boolean result of the govaluate expression is true
then the flag has passed that rule. Depending on the operation, at least one or all the rules must pass. If the flag doesn't meet this criteria, a 400 request is returned to the client and the flag update is not saved.
Motivation and Context
Our team is trying to keep more aware of how flags are being used on our project. Specifically we want to associated a flag with an owning project in our JIRA system, so we can provide the teams some info about how the flag is being used (e.g. if the flag in practice always returns the same segment for a period of time). We landed on using a tag to track this relationship, and we wanted to automate user feedback making sure they assign a tag when the flag is active. Doing this validation only when a flag is set to enabled seemed like the best way; the user can create and configure the flag without validation up until they enable it for actual use.
I mentioned this change in this issue at the end: https://github.com/checkr/flagr/issues/428
How Has This Been Tested?
I initially prototyped using govaluate for this usecase here: https://github.com/sesquipedalian-dev/govaluate-experiment/blob/main/main.go
In addition to adding unit tests covering the new validation rules, I followed a manual test procedure: 0) environment: Macbook Pro, OSX 10.15.5, chrome browser, go 1.15.6
-
make run_ui
-
FLAGR_ENABLED_FLAG_VALIDATION_RULES='any("regexMatch(Value, \"^JIRA:[a-zA-Z]{3}[a-zA-Z]*$\")", Tags)' make run
- in a browser at
localhost:8080
create a new flag with description 'New flag' - in the browser open developer tools to follow along with the network calls
- Click the toggle button to enable the flag, observe you get an error message and the network tab indicates a 400
- Reload the page (verify the flag in the DB is not actually enabled) - small UI bug here in that the toggle remains 'enabled' in the UI even though the network call failed
- Add a tag to the flag matching the critera (e.g.
JIRA:EPLT
) - Click the toggle button to enable, observe no error message this time and the network call has a 200 status
- Remove the tag from the flag
- change the flag description by adding some characters, and click 'save'. Observe you again get an error message as in step (5) 11)Add the tag back in and save again, observe no error message
I didn't test other areas of the code too much manually.
Types of changes
- [ ] Bug fix (non-breaking change which fixes an issue)
- [X] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to change)
Checklist:
- [X] My code follows the code style of this project.
- [ ] My change requires a change to the documentation.
- [ ] I have updated the documentation accordingly.
- [X] I have added tests to cover my changes.
- [X] All new and existing tests passed.
Codecov Report
Merging #431 (805972c) into master (c3dc3c0) will decrease coverage by
1.20%
. The diff coverage is58.62%
.
@@ Coverage Diff @@
## master #431 +/- ##
==========================================
- Coverage 81.75% 80.54% -1.21%
==========================================
Files 27 28 +1
Lines 1611 1696 +85
==========================================
+ Hits 1317 1366 +49
- Misses 214 235 +21
- Partials 80 95 +15
Impacted Files | Coverage Δ | |
---|---|---|
pkg/handler/crud.go | 86.09% <33.33%> (-0.94%) |
:arrow_down: |
pkg/handler/enabled_flag_validation.go | 60.49% <60.49%> (ø) |
Continue to review full report at Codecov.
Legend - Click here to learn more
Δ = absolute <relative> (impact)
,ø = not affected
,? = missing data
Powered by Codecov. Last update c3dc3c0...805972c. Read the comment docs.
Stale pull request message