diff --git a/internal/bundle/utils.go b/internal/bundle/utils.go index 8c70e8ab2c9..5a1d5c30bf6 100644 --- a/internal/bundle/utils.go +++ b/internal/bundle/utils.go @@ -166,8 +166,8 @@ func LoadWasmResolversFromStore(ctx context.Context, store storage.Store, txn st return resolvers, nil } -// LoadBundleFromDisk loads a previously persisted activated bundle from disk -func LoadBundleFromDisk(path string, opts *LoadOptions) (*bundle.Bundle, error) { +// LoadBundleFromDiskWithOptions loads a previously persisted activated bundle from disk +func LoadBundleFromDiskWithOptions(path string, opts *LoadOptions) (*bundle.Bundle, error) { // if a bundle package exists, use that as it might contain the bundle etag which // is not stored in the legacy bundle file. This can help avoid unnecessary bundle // downloads. @@ -230,9 +230,9 @@ func LoadBundleFromDisk(path string, opts *LoadOptions) (*bundle.Bundle, error) } } -// SaveBundleToDisk persists a bundle to disk. Bundles are wrapped in a 'bundlePackage' in order +// SaveBundleToDiskWithOptions persists a bundle to disk. Bundles are wrapped in a 'bundlePackage' in order // to support additional metadata (e.g. etag) that is not part of the original bundle. -func SaveBundleToDisk(path string, rawBundle io.Reader, opts *SaveOptions) error { +func SaveBundleToDiskWithOptions(path string, rawBundle io.Reader, opts *SaveOptions) error { if _, err := os.Stat(path); os.IsNotExist(err) { err = os.MkdirAll(path, os.ModePerm) if err != nil { diff --git a/internal/bundle/utils_test.go b/internal/bundle/utils_test.go index fa7f3ef89b8..01ff6e8c6a9 100644 --- a/internal/bundle/utils_test.go +++ b/internal/bundle/utils_test.go @@ -41,7 +41,7 @@ func TestLoadBundleFromDiskLegacy(t *testing.T) { t.Fatal("unexpected error:", err) } - loadedBundle, err := LoadBundleFromDisk(tempDir, &LoadOptions{}) + loadedBundle, err := LoadBundleFromDiskWithOptions(tempDir, &LoadOptions{}) if err != nil { t.Fatal("unexpected error:", err) } @@ -71,12 +71,12 @@ func TestLoadBundleFromDiskBundlePackage(t *testing.T) { t.Fatal("unexpected error:", err) } - err = SaveBundleToDisk(tempDir, &buf, &SaveOptions{Etag: "123"}) + err = SaveBundleToDiskWithOptions(tempDir, &buf, &SaveOptions{Etag: "123"}) if err != nil { t.Fatal("unexpected error:", err) } - loadedBundle, err := LoadBundleFromDisk(tempDir, &LoadOptions{}) + loadedBundle, err := LoadBundleFromDiskWithOptions(tempDir, &LoadOptions{}) if err != nil { t.Fatal("unexpected error:", err) } @@ -110,12 +110,12 @@ func TestSaveBundleToDisk(t *testing.T) { t.Fatal("unexpected error:", err) } - err = SaveBundleToDisk(tempDir, &buf, &SaveOptions{Etag: "123"}) + err = SaveBundleToDiskWithOptions(tempDir, &buf, &SaveOptions{Etag: "123"}) if err != nil { t.Fatal("unexpected error:", err) } - loadedBundle, err := LoadBundleFromDisk(tempDir, &LoadOptions{}) + loadedBundle, err := LoadBundleFromDiskWithOptions(tempDir, &LoadOptions{}) if err != nil { t.Fatal("unexpected error:", err) } @@ -167,13 +167,13 @@ func TestSaveBundleToDiskOverwrite(t *testing.T) { sourceBundle2Etag := "456" // write the first version of the bundle to disk - err = SaveBundleToDisk(tempDir, &buf1, &SaveOptions{Etag: sourceBundle1ETag}) + err = SaveBundleToDiskWithOptions(tempDir, &buf1, &SaveOptions{Etag: sourceBundle1ETag}) if err != nil { t.Fatal("unexpected error:", err) } // load the bundle and validate it - loadedBundle1, err := LoadBundleFromDisk(tempDir, &LoadOptions{}) + loadedBundle1, err := LoadBundleFromDiskWithOptions(tempDir, &LoadOptions{}) if err != nil { t.Fatal("unexpected error:", err) } @@ -187,13 +187,13 @@ func TestSaveBundleToDiskOverwrite(t *testing.T) { } // overwrite the bundle - err = SaveBundleToDisk(tempDir, &buf2, &SaveOptions{Etag: sourceBundle2Etag}) + err = SaveBundleToDiskWithOptions(tempDir, &buf2, &SaveOptions{Etag: sourceBundle2Etag}) if err != nil { t.Fatal("unexpected error:", err) } // load the new bundle and validate it - loadedBundle2, err := LoadBundleFromDisk(tempDir, &LoadOptions{}) + loadedBundle2, err := LoadBundleFromDiskWithOptions(tempDir, &LoadOptions{}) if err != nil { t.Fatal("unexpected error:", err) } @@ -229,12 +229,12 @@ func TestSaveBundleToDiskNewPath(t *testing.T) { bundlePath := filepath.Join(tempDir, "foo", "bar", "example1") - err = SaveBundleToDisk(bundlePath, &buf, &SaveOptions{Etag: "123"}) + err = SaveBundleToDiskWithOptions(bundlePath, &buf, &SaveOptions{Etag: "123"}) if err != nil { t.Fatal("unexpected error:", err) } - loadedBundle, err := LoadBundleFromDisk(bundlePath, &LoadOptions{}) + loadedBundle, err := LoadBundleFromDiskWithOptions(bundlePath, &LoadOptions{}) if err != nil { t.Fatal("unexpected error:", err) } @@ -252,7 +252,7 @@ func TestSaveBundleToDiskNil(t *testing.T) { var err error srcDir := t.TempDir() - err = SaveBundleToDisk(srcDir, nil, &SaveOptions{}) + err = SaveBundleToDiskWithOptions(srcDir, nil, &SaveOptions{}) if err == nil { t.Fatal("expected error but got nil") } diff --git a/plugins/bundle/plugin.go b/plugins/bundle/plugin.go index 245bca2fa4b..5c551d7cc4f 100644 --- a/plugins/bundle/plugin.go +++ b/plugins/bundle/plugin.go @@ -351,7 +351,7 @@ func (p *Plugin) loadAndActivateBundlesFromDisk(ctx context.Context) { for name, src := range p.config.Bundles { if p.persistBundle(name) { - b, err := bundleUtils.LoadBundleFromDisk(filepath.Join(p.bundlePersistPath, name), &bundleUtils.LoadOptions{VerificationConfig: src.Signing}) + b, err := bundleUtils.LoadBundleFromDiskWithOptions(filepath.Join(p.bundlePersistPath, name), &bundleUtils.LoadOptions{VerificationConfig: src.Signing}) if err != nil { p.log(name).Error("Failed to load bundle from disk: %v", err) p.status[name].SetError(err) @@ -506,7 +506,7 @@ func (p *Plugin) process(ctx context.Context, name string, u download.Update) { if u.Bundle.Type() == bundle.SnapshotBundleType && p.persistBundle(name) { p.log(name).Debug("Persisting bundle to disk in progress.") - err := bundleUtils.SaveBundleToDisk( + err := bundleUtils.SaveBundleToDiskWithOptions( filepath.Join(p.bundlePersistPath, name), u.Raw, &bundleUtils.SaveOptions{Etag: u.ETag}, diff --git a/plugins/bundle/plugin_test.go b/plugins/bundle/plugin_test.go index b35c9c66543..9d5511c8327 100644 --- a/plugins/bundle/plugin_test.go +++ b/plugins/bundle/plugin_test.go @@ -672,7 +672,7 @@ func TestPluginOneShotBundlePersistence(t *testing.T) { ensurePluginState(t, plugin, plugins.StateOK) - result, err := bundleUtils.LoadBundleFromDisk(filepath.Join(plugin.bundlePersistPath, bundleName), &bundleUtils.LoadOptions{}) + result, err := bundleUtils.LoadBundleFromDiskWithOptions(filepath.Join(plugin.bundlePersistPath, bundleName), &bundleUtils.LoadOptions{}) if err != nil { t.Fatal("unexpected error:", err) } @@ -774,7 +774,7 @@ func TestPluginOneShotSignedBundlePersistence(t *testing.T) { ensurePluginState(t, plugin, plugins.StateOK) // load signed bundle from disk - result, err := bundleUtils.LoadBundleFromDisk(filepath.Join(plugin.bundlePersistPath, bundleName), &bundleUtils.LoadOptions{VerificationConfig: bundles[bundleName].Signing}) + result, err := bundleUtils.LoadBundleFromDiskWithOptions(filepath.Join(plugin.bundlePersistPath, bundleName), &bundleUtils.LoadOptions{VerificationConfig: bundles[bundleName].Signing}) if err != nil { t.Fatal("unexpected error:", err) } @@ -858,7 +858,7 @@ func TestLoadAndActivateBundlesFromDisk(t *testing.T) { t.Fatal("unexpected error:", err) } - err = bundleUtils.SaveBundleToDisk( + err = bundleUtils.SaveBundleToDiskWithOptions( filepath.Join(bundlePersistPath, bundleName), &buf, &bundleUtils.SaveOptions{}, @@ -974,7 +974,7 @@ is_one(x) { t.Fatal("unexpected error:", err) } - err = bundleUtils.SaveBundleToDisk( + err = bundleUtils.SaveBundleToDiskWithOptions( filepath.Join(bundlePersistPath, bundleName), &buf1, &bundleUtils.SaveOptions{}, @@ -988,7 +988,7 @@ is_one(x) { t.Fatal("unexpected error:", err) } - err = bundleUtils.SaveBundleToDisk( + err = bundleUtils.SaveBundleToDiskWithOptions( filepath.Join(bundlePersistPath, bundleNameOther), &buf2, &bundleUtils.SaveOptions{}, @@ -1061,7 +1061,7 @@ allow { t.Fatal("unexpected error:", err) } - err = bundleUtils.SaveBundleToDisk( + err = bundleUtils.SaveBundleToDiskWithOptions( filepath.Join(bundlePersistPath, bundleName), &buf, &bundleUtils.SaveOptions{}, @@ -2502,7 +2502,7 @@ func TestUpgradeLegacyBundleToMuiltiBundleNewBundles(t *testing.T) { func TestLoadBundleFromDisk(t *testing.T) { // no bundle on disk - _, err := bundleUtils.LoadBundleFromDisk(filepath.Join("foo", "bar"), nil) + _, err := bundleUtils.LoadBundleFromDiskWithOptions(filepath.Join("foo", "bar"), nil) if err != nil { t.Fatalf("unexpected error %v", err) } @@ -2519,7 +2519,7 @@ func TestLoadBundleFromDisk(t *testing.T) { b := writeTestBundleToDisk(t, bundleDir, false) - result, err := bundleUtils.LoadBundleFromDisk(bundleDir, &bundleUtils.LoadOptions{}) + result, err := bundleUtils.LoadBundleFromDiskWithOptions(bundleDir, &bundleUtils.LoadOptions{}) if err != nil { t.Fatal("unexpected error:", err) } @@ -2532,7 +2532,7 @@ func TestLoadBundleFromDisk(t *testing.T) { func TestLoadSignedBundleFromDisk(t *testing.T) { // no bundle on disk - _, err := bundleUtils.LoadBundleFromDisk("foo/bar", nil) + _, err := bundleUtils.LoadBundleFromDiskWithOptions("foo/bar", nil) if err != nil { t.Fatalf("unexpected error %v", err) } @@ -2553,7 +2553,7 @@ func TestLoadSignedBundleFromDisk(t *testing.T) { Signing: bundle.NewVerificationConfig(map[string]*keys.Config{"foo": {Key: "secret", Algorithm: "HS256"}}, "foo", "", nil), } - result, err := bundleUtils.LoadBundleFromDisk(bundleDir, &bundleUtils.LoadOptions{VerificationConfig: src.Signing}) + result, err := bundleUtils.LoadBundleFromDiskWithOptions(bundleDir, &bundleUtils.LoadOptions{VerificationConfig: src.Signing}) if err != nil { t.Fatal("unexpected error:", err) } @@ -2911,7 +2911,7 @@ func TestPluginReadBundleEtagFromPersistenceDir(t *testing.T) { t.Fatal("unexpected error:", err) } - err = bundleUtils.SaveBundleToDisk( + err = bundleUtils.SaveBundleToDiskWithOptions( path.Join(persistenceDir, "bundles", "test"), &buf, &bundleUtils.SaveOptions{Etag: "foo"}, diff --git a/plugins/discovery/discovery.go b/plugins/discovery/discovery.go index 871de035d3c..a5b64b3795b 100644 --- a/plugins/discovery/discovery.go +++ b/plugins/discovery/discovery.go @@ -215,7 +215,7 @@ func (c *Discovery) getBundlePersistPath() (string, error) { func (c *Discovery) loadAndActivateBundleFromDisk(ctx context.Context) { if c.config != nil && c.config.Persist { - b, err := bundleUtils.LoadBundleFromDisk( + b, err := bundleUtils.LoadBundleFromDiskWithOptions( filepath.Join(c.bundlePersistPath, c.discoveryBundleDirName()), &bundleUtils.LoadOptions{VerificationConfig: c.config.Signing}, ) @@ -309,7 +309,7 @@ func (c *Discovery) processUpdate(ctx context.Context, u download.Update) { if c.config != nil && c.config.Persist { c.logger.Debug("Persisting discovery bundle to disk in progress.") - err := bundleUtils.SaveBundleToDisk( + err := bundleUtils.SaveBundleToDiskWithOptions( filepath.Join(c.bundlePersistPath, c.discoveryBundleDirName()), u.Raw, &bundleUtils.SaveOptions{Etag: u.ETag}, diff --git a/plugins/discovery/discovery_test.go b/plugins/discovery/discovery_test.go index ddd42e77c7a..7c1dd23198d 100644 --- a/plugins/discovery/discovery_test.go +++ b/plugins/discovery/discovery_test.go @@ -20,8 +20,6 @@ import ( "testing" "time" - bundleUtils "github.com/open-policy-agent/opa/internal/bundle" - "github.com/open-policy-agent/opa/ast" bundleApi "github.com/open-policy-agent/opa/bundle" "github.com/open-policy-agent/opa/download" @@ -533,7 +531,7 @@ func TestOneShotWithBundlePersistence(t *testing.T) { ensurePluginState(t, disco, plugins.StateOK) - result, err := bundleUtils.LoadBundleFromDisk( + result, err := bundleUtils.LoadBundleFromDiskWithOptions( filepath.Join(disco.bundlePersistPath, disco.discoveryBundleDirName()), &bundleUtils.LoadOptions{VerificationConfig: disco.config.Signing}, ) @@ -623,7 +621,7 @@ func TestLoadAndActivateBundleFromDisk(t *testing.T) { t.Fatal("unexpected error:", err) } - err = bundleUtils.SaveBundleToDisk( + err = bundleUtils.SaveBundleToDiskWithOptions( filepath.Join(bundlePersistPath, "config"), &buf, &bundleUtils.SaveOptions{}, @@ -724,7 +722,7 @@ func TestLoadAndActivateSignedBundleFromDisk(t *testing.T) { t.Fatal("unexpected error:", err) } - err = bundleUtils.SaveBundleToDisk( + err = bundleUtils.SaveBundleToDiskWithOptions( filepath.Join(bundlePersistPath, "config"), &buf, &bundleUtils.SaveOptions{}, @@ -819,7 +817,7 @@ func TestLoadAndActivateBundleFromDiskMaxAttempts(t *testing.T) { t.Fatal("unexpected error:", err) } - err = bundleUtils.SaveBundleToDisk( + err = bundleUtils.SaveBundleToDiskWithOptions( filepath.Join(bundlePersistPath, Name), &buf, &bundleUtils.SaveOptions{},