diff --git a/v1/bundle/store.go b/v1/bundle/store.go index 29efe208d2..1784c98f5f 100644 --- a/v1/bundle/store.go +++ b/v1/bundle/store.go @@ -780,7 +780,7 @@ func erasePolicies(ctx context.Context, store storage.Store, txn storage.Transac } getRegoVersion := func(modId string) (ast.RegoVersion, bool) { - info, ok := modulesInfo[modId] + info, ok := modulesInfo[strings.TrimPrefix(modId, "/")] if !ok { return ast.RegoUndefined, false } diff --git a/v1/bundle/store_test.go b/v1/bundle/store_test.go index 6d76d13294..4c6fa29d37 100644 --- a/v1/bundle/store_test.go +++ b/v1/bundle/store_test.go @@ -4278,6 +4278,38 @@ func TestActivate_DefaultRegoVersion(t *testing.T) { } } +// Regression test for https://github.com/open-policy-agent/opa/issues/8797. +func TestActivate_PreservesPerModuleRegoVersionWithLeadingSlashID(t *testing.T) { + ctx := t.Context() + store := mock.New() + + v1Module := `package test + +p contains 42` + + policyID := "/test/policy.rego" + txn := storage.NewTransactionOrDie(ctx, store, storage.WriteParams) + if err := store.UpsertPolicy(ctx, txn, policyID, []byte(v1Module)); err != nil { + t.Fatalf("UpsertPolicy failed: %v", err) + } + if err := write(ctx, store, txn, moduleRegoVersionPath(policyID), int64(ast.RegoV1.Int())); err != nil { + t.Fatalf("writing rego_version failed: %v", err) + } + if err := store.Commit(ctx, txn); err != nil { + t.Fatalf("commit failed: %v", err) + } + + mustActivate(t, store, &ActivateOpts{ + Compiler: ast.NewCompiler().WithDefaultRegoVersion(ast.RegoV0CompatV1), + ParserOptions: ast.ParserOptions{RegoVersion: ast.RegoV0}, + Bundles: map[string]*Bundle{ + "other": { + Manifest: Manifest{Roots: &[]string{"other"}}, + }, + }, + }) +} + func TestDeactivate_DefaultRegoVersion(t *testing.T) { tests := []struct { note string