Skip to content

Commit

Permalink
sdk: forward SDK config to runtime config (#4121)
Browse files Browse the repository at this point in the history
Updates runtime generation to include the opa config in `opa.runtime()`.

Signed-off-by: Christian Schuetz <christian.schuetz@chime.com>
  • Loading branch information
cmschuetz committed Dec 11, 2021
1 parent edf5f25 commit d817166
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
2 changes: 1 addition & 1 deletion sdk/opa.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func (opa *OPA) Configure(ctx context.Context, opts ConfigOptions) error {
}

func (opa *OPA) configure(ctx context.Context, bs []byte, ready chan struct{}, block bool) error {
info, err := runtime.Term(runtime.Params{})
info, err := runtime.Term(runtime.Params{Config: opa.config})
if err != nil {
return err
}
Expand Down
61 changes: 61 additions & 0 deletions sdk/opa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -603,3 +603,64 @@ func TestOpaVersion(t *testing.T) {
t.Fatalf("expected %v but got %v", exp, result.Result)
}
}

func TestOpaRuntimeConfig(t *testing.T) {
ctx := context.Background()

server := sdktest.MustNewServer(
sdktest.MockBundle("/bundles/bundle.tar.gz", map[string]string{
"main.rego": `
package system
rt := opa.runtime()
result := {
"service_url": rt.config.services.test.url,
"bundle_resource": rt.config.bundles.test.resource,
"test_label": rt.config.labels.test
}
`,
}),
)

defer server.Stop()

testBundleResource := "/bundles/bundle.tar.gz"
testLabel := "a label"

config := []byte(fmt.Sprintf(`{
"services": {
"test": {
"url": %q
}
},
"bundles": {
"test": {
"resource": %q
}
},
"labels": {
"test": %q
}
}`, server.URL(), testBundleResource, testLabel))

opa, err := sdk.New(ctx, sdk.Options{
Config: bytes.NewReader(config),
})
if err != nil {
t.Fatal(err)
}

defer opa.Stop(ctx)

exp := map[string]interface{}{
"service_url": server.URL(),
"bundle_resource": testBundleResource,
"test_label": testLabel,
}

if result, err := opa.Decision(ctx, sdk.DecisionOptions{Path: "/system/result"}); err != nil {
t.Fatal(err)
} else if !reflect.DeepEqual(result.Result, exp) {
t.Fatalf("expected %v but got %v", exp, result.Result)
}
}

0 comments on commit d817166

Please sign in to comment.