-
Notifications
You must be signed in to change notification settings - Fork 24
feat(opa): Adding jq OPA builtin for selection #527
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 10 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
76958f8
adding jq builtin
elizabethhealy 40a2746
updated go mod and sum
elizabethhealy 3c3a174
linting
elizabethhealy ca1cb5a
remove extra files
elizabethhealy 4f60fce
get the builtin working with keycloak attributes
elizabethhealy 3ac3668
linting
elizabethhealy eb8e179
Merge branch 'main' into get-subject-selector-with-jq
elizabethhealy 9047076
fix linting
elizabethhealy 1ff3af1
Merge branch 'get-subject-selector-with-jq' of https://github.com/ope…
elizabethhealy e8c48a8
use keycloak rego by default
elizabethhealy 13ed41e
Merge branch 'main' into get-subject-selector-with-jq
elizabethhealy 45a325c
unescape strings as failsafe
elizabethhealy 62380f3
fix comment
elizabethhealy 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
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,51 @@ | ||
| ## Testing an OPA builtin with a rego query | ||
|
|
||
| 1. Set up your main.go to be the following | ||
| ``` | ||
| func main() { | ||
| logLevel := &slog.LevelVar{} | ||
| logLevel.Set(slog.LevelDebug) | ||
|
|
||
| opts := &slog.HandlerOptions{ | ||
| Level: logLevel, | ||
| } | ||
| logger := slog.New(slog.NewJSONHandler(os.Stdout, opts)) | ||
|
|
||
| slog.SetDefault(logger) | ||
|
|
||
| jqbuiltin.JQBuiltin() | ||
|
|
||
| if err := cmd.RootCommand.Execute(); err != nil { | ||
| fmt.Println(err) | ||
| os.Exit(1) | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| 2. Build the executable | ||
| ``` | ||
| cd service | ||
| go build -o opa++ | ||
| ``` | ||
|
|
||
| 3. Create an example rego file | ||
| ``` | ||
| package sample | ||
|
|
||
| my_json = { | ||
| "testing1": { | ||
| "testing2": { | ||
| "testing3": ["helloworld"] | ||
| } | ||
| } | ||
| } | ||
| req = ".testing1.testing2.testing3[]" | ||
|
|
||
| res := jq.evaluate(my_json, req) | ||
| ``` | ||
|
|
||
| 4. Perform the query | ||
| ``` | ||
| ./opa++ eval -d example.rego 'data.sample.res' | ||
| ``` | ||
|
|
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,76 @@ | ||
| package jqbuiltin | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "encoding/json" | ||
| "log/slog" | ||
|
|
||
| "github.com/itchyny/gojq" | ||
| "github.com/open-policy-agent/opa/ast" | ||
| "github.com/open-policy-agent/opa/rego" | ||
| "github.com/open-policy-agent/opa/types" | ||
| ) | ||
|
|
||
| func JQBuiltin() { | ||
| rego.RegisterBuiltin2(®o.Function{ | ||
| Name: "jq.evaluate", | ||
| Decl: types.NewFunction(types.Args(types.A, types.S), types.A), | ||
| Memoize: true, | ||
| Nondeterministic: true, | ||
| }, func(_ rego.BuiltinContext, a, b *ast.Term) (*ast.Term, error) { | ||
| slog.Debug("JQ plugin invoked") | ||
| var input map[string]any | ||
| var query string | ||
|
|
||
| if err := ast.As(a.Value, &input); err != nil { | ||
| return nil, err | ||
| } else if err := ast.As(b.Value, &query); err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| res, err := ExecuteQuery(input, query) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| respBytes, err := json.Marshal(res) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| reader := bytes.NewReader(respBytes) | ||
| v, err := ast.ValueFromReader(reader) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return ast.NewTerm(v), nil | ||
| }, | ||
| ) | ||
| } | ||
|
|
||
| func ExecuteQuery(inputJSON map[string]any, queryString string) ([]any, error) { | ||
| query, err := gojq.Parse(queryString) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| iter := query.Run(inputJSON) | ||
| found := []any{} | ||
| for { | ||
| v, ok := iter.Next() | ||
| if !ok { | ||
| break | ||
| } | ||
| if err, ok2 := v.(error); ok2 { | ||
| //nolint:errorlint // temp following gojq example | ||
| if err, ok3 := err.(*gojq.HaltError); ok3 && err.Value() == nil { | ||
| break | ||
| } | ||
| // ignore error: we don't have a match but that is not an error state in this case | ||
| } else { | ||
| if v != nil { | ||
| found = append(found, v) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return found, nil | ||
| } | ||
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,68 @@ | ||
| package jqbuiltin_test | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/opentdf/platform/service/internal/jqbuiltin" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| const testResult1 string = "helloworld" | ||
|
|
||
| var testInput1 = map[string]interface{}{ | ||
| "testing1": testResult1, | ||
| } | ||
| var testQuery1 = ".testing1" | ||
|
|
||
| func Test_JQSuccessSimple(t *testing.T) { | ||
| res, err := jqbuiltin.ExecuteQuery(testInput1, testQuery1) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, []any{testResult1}, res) | ||
| } | ||
|
|
||
| var testInput2 = map[string]interface{}{ | ||
| "testing1": map[string]interface{}{"testing2": testResult1}, | ||
| } | ||
| var testQuery2 = ".testing1.testing2" | ||
|
|
||
| func Test_JQSuccessTwoDeep(t *testing.T) { | ||
| res, err := jqbuiltin.ExecuteQuery(testInput2, testQuery2) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, []any{testResult1}, res) | ||
| } | ||
|
|
||
| var testInput3 = map[string]interface{}{ | ||
| "testing1": map[string]interface{}{"testing2": []any{testResult1}}, | ||
| } | ||
| var testQuery3 = ".testing1.testing2[0]" | ||
|
|
||
| func Test_JQSuccessTwoDeepInArray(t *testing.T) { | ||
| res, err := jqbuiltin.ExecuteQuery(testInput3, testQuery3) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, []any{testResult1}, res) | ||
| } | ||
|
|
||
| const testResult2 string = "whatsup" | ||
|
|
||
| var testInput4 = map[string]interface{}{ | ||
| "testing1": map[string]interface{}{"testing2": []any{testResult1, testResult2}}, | ||
| } | ||
| var testQuery4 = ".testing1.testing2[]" | ||
|
|
||
| func Test_JQSuccessTwoDeepAllInArray(t *testing.T) { | ||
| res, err := jqbuiltin.ExecuteQuery(testInput4, testQuery4) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, []any{testResult1, testResult2}, res) | ||
| } | ||
|
|
||
| var testInput5 = map[string]interface{}{ | ||
| "testing1": map[string]interface{}{"testing2": testResult1}, | ||
| } | ||
| var testQuery5 = ".testing1.testing3" | ||
|
|
||
| func Test_JQSuccessTwoDeepAllNoMatch(t *testing.T) { | ||
| res, err := jqbuiltin.ExecuteQuery(testInput5, testQuery5) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, []any{}, res) | ||
| } |
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
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.