Persist activated bundle etag to store

Currently etag from the HTTP response of activated bundles is not
persisted to store. Hence if OPA restarts and an activated bundle
loaded from the disk store is up-to-date, OPA may still download
the same version of the bundle and activate it. With this change,
OPA should include the right etag in the bundle download request
thereby avoiding unnecessary bundle download and activation.

Fixes: #4544

Signed-off-by: Ashutosh Narkar <anarkar4387@gmail.com>
This commit is contained in:
Ashutosh Narkar
2022-04-12 17:36:39 -07:00
parent 492aeb3cbb
commit ccba4a63d2
8 changed files with 372 additions and 23 deletions
+4 -2
View File
@@ -307,7 +307,9 @@ func (d *Downloader) download(ctx context.Context, m metrics.Metrics) (*download
loader = bundle.NewTarballLoaderWithBaseURL(resp.Body, baseURL)
}
reader := bundle.NewCustomReader(loader).WithMetrics(m).WithBundleVerificationConfig(d.bvc)
etag := resp.Header.Get("ETag")
reader := bundle.NewCustomReader(loader).WithMetrics(m).WithBundleVerificationConfig(d.bvc).
WithBundleEtag(etag)
if d.sizeLimitBytes != nil {
reader = reader.WithSizeLimitBytes(*d.sizeLimitBytes)
}
@@ -337,7 +339,7 @@ func (d *Downloader) download(ctx context.Context, m metrics.Metrics) (*download
return &downloaderResponse{
b: &b,
raw: &buf,
etag: resp.Header.Get("ETag"),
etag: etag,
longPoll: isLongPollSupported(resp.Header),
}, nil
}
+34
View File
@@ -408,6 +408,40 @@ func TestEtagCachingLifecycle(t *testing.T) {
}
}
func TestOneShotWithBundleEtag(t *testing.T) {
ctx := context.Background()
fixture := newTestFixture(t)
fixture.d = New(Config{}, fixture.client, "/bundles/test/bundle1").WithCallback(fixture.oneShot)
fixture.server.expEtag = "some etag value"
defer fixture.server.stop()
// check etag on the downloader is empty
if fixture.d.etag != "" {
t.Fatalf("Expected empty downloader ETag but got %v", fixture.d.etag)
}
// simulate successful bundle activation and check updated etag on the downloader
fixture.server.expCode = 0
err := fixture.d.oneShot(ctx)
if err != nil {
t.Fatal("Unexpected:", err)
}
if fixture.d.etag != fixture.server.expEtag {
t.Fatalf("Expected downloader ETag %v but got %v", fixture.server.expEtag, fixture.d.etag)
}
if fixture.updates[0].Bundle == nil {
// 200 response on first request, bundle should be present
t.Errorf("Expected bundle in response")
}
if fixture.updates[0].Bundle.Etag != fixture.server.expEtag {
t.Fatalf("Expected bundle ETag %v but got %v", fixture.server.expEtag, fixture.updates[0].Bundle.Etag)
}
}
func TestFailureAuthn(t *testing.T) {
ctx := context.Background()