Skip to content

Commit 9a73de9

Browse files
committed
sdk: options store takes precedence over ext store
Rationale is that if the user has passed a store into the SDK, it probably has something in it already, like data and policies. Signed-off-by: Stephan Renatus <stephan@styra.com>
1 parent 4c13c6c commit 9a73de9

2 files changed

Lines changed: 46 additions & 4 deletions

File tree

v1/sdk/opa.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,12 +179,14 @@ func (opa *OPA) configure(ctx context.Context, bs []byte, ready chan struct{}, b
179179
}
180180
opts = append(opts, opa.managerOpts...)
181181

182-
// Plumb in storage for external bundle activation plugin, if registered with bundle.RegisterStore.
182+
// Plumb in storage for external bundle activation plugin, if registered with bundle.RegisterStore,
183+
// unless the user has passed their own store already.
183184
var store storage.Store
184-
if bundle.BundleExtStore != nil {
185-
store = bundle.BundleExtStore()
186-
} else {
185+
switch {
186+
case opa.store != nil:
187187
store = opa.store
188+
case bundle.BundleExtStore != nil:
189+
store = bundle.BundleExtStore()
188190
}
189191
manager, err := plugins.New(
190192
bs,

v1/sdk/opa_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727

2828
"github.com/open-policy-agent/opa/internal/file/archive"
2929
"github.com/open-policy-agent/opa/v1/ast"
30+
"github.com/open-policy-agent/opa/v1/bundle"
3031
"github.com/open-policy-agent/opa/v1/config"
3132
"github.com/open-policy-agent/opa/v1/hooks"
3233
"github.com/open-policy-agent/opa/v1/logging"
@@ -38,6 +39,8 @@ import (
3839
"github.com/open-policy-agent/opa/v1/sdk"
3940
sdktest "github.com/open-policy-agent/opa/v1/sdk/test"
4041
"github.com/open-policy-agent/opa/v1/server/types"
42+
"github.com/open-policy-agent/opa/v1/storage"
43+
"github.com/open-policy-agent/opa/v1/storage/inmem"
4144
"github.com/open-policy-agent/opa/v1/topdown"
4245
"github.com/open-policy-agent/opa/v1/topdown/builtins"
4346
"github.com/open-policy-agent/opa/v1/topdown/lineage"
@@ -2980,3 +2983,40 @@ func TestActivateV1Bundles(t *testing.T) {
29802983
t.Errorf("expected result to be true, got %v", d.Result)
29812984
}
29822985
}
2986+
2987+
// TestWithOwnStoreVSExtStore asserts that in the SDK setup, a provided
2988+
// store always takes precedence over the extensions store.
2989+
func TestWithOwnStoreVSExtStore(t *testing.T) {
2990+
bundle.RegisterStoreFunc(inmem.New)
2991+
t.Cleanup(func() { bundle.RegisterStoreFunc(nil) })
2992+
ctx := context.Background()
2993+
opts := sdk.Options{
2994+
Store: inmem.New(),
2995+
}
2996+
store := opts.Store
2997+
if err := storage.Txn(ctx, store, storage.WriteParams, func(txn storage.Transaction) error {
2998+
return store.UpsertPolicy(ctx, txn, "pkg", []byte(`package pkg
2999+
p := true
3000+
`))
3001+
}); err != nil {
3002+
panic(err)
3003+
}
3004+
3005+
o, err := sdk.New(ctx, opts)
3006+
if err != nil {
3007+
panic(err)
3008+
}
3009+
3010+
res, err := o.Decision(ctx, sdk.DecisionOptions{
3011+
Path: "pkg/p",
3012+
})
3013+
if err != nil {
3014+
t.Fatal(err)
3015+
}
3016+
exp := true
3017+
act := res.Result
3018+
if diff := cmp.Diff(exp, act); diff != "" {
3019+
t.Errorf("unexpected result (-want, +got):\n%s", diff)
3020+
}
3021+
3022+
}

0 commit comments

Comments
 (0)