mirror of
https://github.com/open-policy-agent/opa.git
synced 2026-08-13 03:42:35 -06:00
SDK: Allow configurable ID (#4736)
When calling the sdk the user can specify an opa id so that repeat calls to the sdk don't have a unique opa id Signed-off-by: PrahathessRaghavan <prahathess@gmail.com>
This commit is contained in:
+8
-3
@@ -53,9 +53,14 @@ type state struct {
|
||||
// options that specify an OPA configuration file.
|
||||
func New(ctx context.Context, opts Options) (*OPA, error) {
|
||||
|
||||
id, err := uuid.New(rand.Reader)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
var err error
|
||||
|
||||
id := opts.ID
|
||||
if id == "" {
|
||||
id, err = uuid.New(rand.Reader)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
if err := opts.init(); err != nil {
|
||||
|
||||
@@ -97,6 +97,59 @@ func TestPluginPanic(t *testing.T) {
|
||||
opa.Stop(ctx)
|
||||
}
|
||||
|
||||
func TestSDKConfigurableID(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
server := sdktest.MustNewServer(
|
||||
sdktest.MockBundle("/bundles/bundle.tar.gz", map[string]string{
|
||||
"main.rego": "package system\nmain = time.now_ns()",
|
||||
}),
|
||||
)
|
||||
|
||||
defer server.Stop()
|
||||
|
||||
config := fmt.Sprintf(`{
|
||||
"services": {
|
||||
"test": {
|
||||
"url": %q
|
||||
}
|
||||
},
|
||||
"bundles": {
|
||||
"test": {
|
||||
"resource": "/bundles/bundle.tar.gz"
|
||||
}
|
||||
},
|
||||
"decision_logs": {
|
||||
"console": true
|
||||
}
|
||||
}`, server.URL())
|
||||
|
||||
testLogger := loggingtest.New()
|
||||
opa, err := sdk.New(ctx, sdk.Options{
|
||||
Config: strings.NewReader(config),
|
||||
ConsoleLogger: testLogger,
|
||||
ID: "164031de-e511-11ec-8fea-0242ac120002"})
|
||||
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
defer opa.Stop(ctx)
|
||||
|
||||
if _, err := opa.Decision(ctx, sdk.DecisionOptions{
|
||||
Now: time.Unix(0, 1619868194450288000).UTC(),
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
entries := testLogger.Entries()
|
||||
|
||||
if entries[0].Fields["labels"].(map[string]interface{})["id"] != "164031de-e511-11ec-8fea-0242ac120002" {
|
||||
t.Fatalf("expected %v but got %v", "164031de-e511-11ec-8fea-0242ac120002", entries[0].Fields["labels"].(map[string]interface{})["id"])
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestDecision(t *testing.T) {
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
@@ -40,6 +40,9 @@ type Options struct {
|
||||
// registered with the OPA SDK instance.
|
||||
Plugins map[string]plugins.Factory
|
||||
|
||||
// When calling the sdk the user can specify an opa id so that repeat calls to the sdk don't have a unique opa id
|
||||
ID string
|
||||
|
||||
config []byte
|
||||
block bool
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user