Skip to content

Commit b032e3b

Browse files
authored
Fixing issue where bundle plugin could panic on reconfiguration (SDK use) (#7300)
Fixing issue where bundle plugin would panic on reconfiguration if module rego-version is missing in bundle manifest. * Passing runtime rego-version to deactivation options * Preferring to pull rego-version from parsed modules if present This solves an edge case when using the OPA SDK, and should not affect standalone OPA. Fixes: #7297 Signed-off-by: Johan Fylling <johan.dev@fylling.se>
1 parent e47bd4f commit b032e3b

File tree

5 files changed

+367
-57
lines changed

5 files changed

+367
-57
lines changed

v1/bundle/store.go

+19-5
Original file line numberDiff line numberDiff line change
@@ -817,9 +817,23 @@ func writeEtagToStore(opts *ActivateOpts, name, etag string) error {
817817
}
818818

819819
func writeModuleRegoVersionToStore(ctx context.Context, store storage.Store, txn storage.Transaction, b *Bundle,
820-
bundlePath string, storagePath string, runtimeRegoVersion ast.RegoVersion) error {
821-
if v, err := b.RegoVersionForFile(bundlePath, ast.RegoUndefined); err == nil && v != ast.RegoUndefined && v != runtimeRegoVersion {
822-
if err := write(ctx, store, txn, moduleRegoVersionPath(storagePath), v.Int()); err != nil {
820+
mf ModuleFile, storagePath string, runtimeRegoVersion ast.RegoVersion) error {
821+
822+
var regoVersion ast.RegoVersion
823+
if mf.Parsed != nil {
824+
regoVersion = mf.Parsed.RegoVersion()
825+
}
826+
827+
if regoVersion == ast.RegoUndefined {
828+
var err error
829+
regoVersion, err = b.RegoVersionForFile(mf.Path, ast.RegoUndefined)
830+
if err != nil {
831+
return fmt.Errorf("failed to get rego version for module '%s' in bundle: %w", mf.Path, err)
832+
}
833+
}
834+
835+
if regoVersion != ast.RegoUndefined && regoVersion != runtimeRegoVersion {
836+
if err := write(ctx, store, txn, moduleRegoVersionPath(storagePath), regoVersion.Int()); err != nil {
823837
return fmt.Errorf("failed to write rego version for module '%s': %w", storagePath, err)
824838
}
825839
}
@@ -853,7 +867,7 @@ func writeDataAndModules(ctx context.Context, store storage.Store, txn storage.T
853867
return err
854868
}
855869

856-
if err := writeModuleRegoVersionToStore(ctx, store, txn, b, mf.Path, path, runtimeRegoVersion); err != nil {
870+
if err := writeModuleRegoVersionToStore(ctx, store, txn, b, mf, path, runtimeRegoVersion); err != nil {
857871
return err
858872
}
859873
}
@@ -875,7 +889,7 @@ func writeDataAndModules(ctx context.Context, store storage.Store, txn storage.T
875889
if m := f.module; m != nil {
876890
// 'f.module.Path' contains the module's path as it relates to the bundle root, and can be used for looking up the rego-version.
877891
// 'f.Path' can differ, based on how the bundle reader was initialized.
878-
if err := writeModuleRegoVersionToStore(ctx, store, txn, b, f.module.Path, p.String(), runtimeRegoVersion); err != nil {
892+
if err := writeModuleRegoVersionToStore(ctx, store, txn, b, *m, p.String(), runtimeRegoVersion); err != nil {
879893
return err
880894
}
881895
}

v1/bundle/store_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1702,7 +1702,7 @@ func TestBundleLifecycle_ModuleRegoVersions(t *testing.T) {
17021702
t.Fatalf("unexpected error: %s", err)
17031703
}
17041704

1705-
expectedRaw := deact.expData // `{"system": {"bundles": {}, "modules": {}}}`
1705+
expectedRaw := deact.expData
17061706
expected := loadExpectedSortedResult(expectedRaw)
17071707
if !reflect.DeepEqual(expected, actual) {
17081708
t.Errorf("expected:\n\n%s\n\ngot:\n\n%s", expectedRaw, string(util.MustMarshalJSON(actual)))

v1/plugins/bundle/plugin.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -197,10 +197,11 @@ func (p *Plugin) Reconfigure(ctx context.Context, config interface{}) {
197197
params.Context = storage.NewContext() // TODO(sr): metrics?
198198
err := storage.Txn(ctx, p.manager.Store, params, func(txn storage.Transaction) error {
199199
opts := &bundle.DeactivateOpts{
200-
Ctx: ctx,
201-
Store: p.manager.Store,
202-
Txn: txn,
203-
BundleNames: deletedBundles,
200+
Ctx: ctx,
201+
Store: p.manager.Store,
202+
Txn: txn,
203+
BundleNames: deletedBundles,
204+
ParserOptions: p.manager.ParserOptions(),
204205
}
205206
err := bundle.Deactivate(opts)
206207
if err != nil {

0 commit comments

Comments
 (0)