-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Alex Snaps <[email protected]>
- Loading branch information
Showing
8 changed files
with
150 additions
and
0 deletions.
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
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,90 @@ | ||
package response | ||
|
||
import ( | ||
"context" | ||
"reflect" | ||
"strings" | ||
|
||
"github.com/golang/protobuf/jsonpb" | ||
"github.com/google/cel-go/cel" | ||
"github.com/google/cel-go/checker/decls" | ||
"github.com/google/cel-go/common/types/ref" | ||
"github.com/kuadrant/authorino/pkg/auth" | ||
"google.golang.org/protobuf/types/known/structpb" | ||
|
||
"google.golang.org/protobuf/encoding/protojson" | ||
"google.golang.org/protobuf/proto" | ||
) | ||
|
||
const rootBinding = "auth" | ||
|
||
func NewDynamicCelResponse(expression string) (*DynamicCEL, error) { | ||
|
||
cel_exp := DynamicCEL{} | ||
|
||
env, err := cel.NewEnv(cel.Declarations( | ||
decls.NewConst(rootBinding, decls.NewObjectType("google.protobuf.Struct"), nil), | ||
)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
ast, issues := env.Parse(expression) | ||
if issues.Err() != nil { | ||
return nil, issues.Err() | ||
} | ||
|
||
checked, issues := env.Check(ast) | ||
if issues.Err() != nil { | ||
return nil, issues.Err() | ||
} | ||
|
||
program, err := env.Program(checked) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
cel_exp.program = program | ||
|
||
return &cel_exp, nil | ||
} | ||
|
||
type DynamicCEL struct { | ||
program cel.Program | ||
} | ||
|
||
func (c *DynamicCEL) Call(pipeline auth.AuthPipeline, ctx context.Context) (interface{}, error) { | ||
|
||
auth_json := pipeline.GetAuthorizationJSON() | ||
data := structpb.Struct{} | ||
if err := jsonpb.Unmarshal(strings.NewReader(auth_json), &data); err != nil { | ||
return nil, err | ||
} | ||
|
||
value := data.GetFields()["auth"] | ||
result, _, err := c.program.Eval(map[string]interface{}{ | ||
rootBinding: value, | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if jsonVal, err := valueToJSON(result); err != nil { | ||
return nil, err | ||
} else { | ||
return jsonVal, nil | ||
} | ||
} | ||
|
||
func valueToJSON(val ref.Val) (string, error) { | ||
v, err := val.ConvertToNative(reflect.TypeOf(&structpb.Value{})) | ||
if err != nil { | ||
return "", err | ||
} | ||
marshaller := protojson.MarshalOptions{Multiline: false} | ||
bytes, err := marshaller.Marshal(v.(proto.Message)) | ||
if err != nil { | ||
return "", err | ||
} | ||
return string(bytes), nil | ||
} |
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,35 @@ | ||
package response | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"testing" | ||
|
||
mock_auth "github.com/kuadrant/authorino/pkg/auth/mocks" | ||
"gotest.tools/assert" | ||
|
||
"github.com/golang/mock/gomock" | ||
) | ||
|
||
func TestDynamicCELCall(t *testing.T) { | ||
ctrl := gomock.NewController(t) | ||
defer ctrl.Finish() | ||
|
||
celResponseEvaluator, _ := NewDynamicCelResponse(`{"prop1": "value1", "prop2": auth.identity.username}`) | ||
|
||
pipelineMock := mock_auth.NewMockAuthPipeline(ctrl) | ||
pipelineMock.EXPECT().GetAuthorizationJSON().Return(`{"auth":{"identity":{"username":"john","evil": false}}}`) | ||
|
||
response, err := celResponseEvaluator.Call(pipelineMock, context.TODO()) | ||
assert.NilError(t, err) | ||
|
||
// We need to parse this response: https://protobuf.dev/reference/go/faq/#unstable-json | ||
result := struct { | ||
Prop1 string `json:"prop1"` | ||
Prop2 string `json:"prop2"` | ||
}{} | ||
assert.NilError(t, json.Unmarshal([]byte(response.(string)), &result)) | ||
|
||
assert.Equal(t, result.Prop1, "value1") | ||
assert.Equal(t, result.Prop2, "john") | ||
} |
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