Skip to content

Commit

Permalink
downloader: log message on unexpected bundle content type
Browse files Browse the repository at this point in the history
Fixes open-policy-agent#4278

Signed-off-by: Anders Eknert <anders@eknert.com>
  • Loading branch information
anderseknert committed Mar 22, 2022
1 parent 32c613a commit 01d82bc
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
27 changes: 27 additions & 0 deletions download/download.go
Expand Up @@ -311,6 +311,24 @@ func (d *Downloader) download(ctx context.Context, m metrics.Metrics) (*download
if d.sizeLimitBytes != nil {
reader = reader.WithSizeLimitBytes(*d.sizeLimitBytes)
}

if d.logger.GetLevel() >= logging.Debug {
expectedBundleContentType := []string{
"application/gzip",
"application/octet-stream",
"application/vnd.openpolicyagent.bundles",
}

contentType := resp.Header.Get("content-type")
if !contains(contentType, expectedBundleContentType) {
d.logger.Debug("Content-Type response header set to %v. Expected one of %v. "+
"Possibly not a bundle being downloaded.",
contentType,
expectedBundleContentType,
)
}
}

b, err := reader.Read()
if err != nil {
return nil, err
Expand Down Expand Up @@ -358,3 +376,12 @@ type HTTPError struct {
func (e HTTPError) Error() string {
return fmt.Sprintf("server replied with %s", http.StatusText(e.StatusCode))
}

func contains(s string, strings []string) bool {
for _, str := range strings {
if s == str {
return true
}
}
return false
}
45 changes: 44 additions & 1 deletion download/download_test.go
Expand Up @@ -23,6 +23,8 @@ import (

"github.com/open-policy-agent/opa/bundle"
"github.com/open-policy-agent/opa/keys"
"github.com/open-policy-agent/opa/logging"
"github.com/open-policy-agent/opa/logging/test"
"github.com/open-policy-agent/opa/metrics"
"github.com/open-policy-agent/opa/plugins"
"github.com/open-policy-agent/opa/plugins/rest"
Expand Down Expand Up @@ -687,6 +689,43 @@ func TestOneShotNotLongPollingSwitch(t *testing.T) {
}
}

func TestWarnOnNonBundleContentType(t *testing.T) {
ctx := context.Background()
fixture := newTestFixture(t)
fixture.server.bundles["not-a-bundle"] = bundle.Bundle{}

config := Config{}
if err := config.ValidateAndInjectDefaults(); err != nil {
t.Fatal(err)
}

d := New(config, fixture.client, "/bundles/not-a-bundle")
logger := test.New()
logger.SetLevel(logging.Debug)
d.logger = logger

d.Start(ctx)

time.Sleep(1 * time.Second)

d.Stop(ctx)

expectLogged := "Content-Type response header set to text/html. " +
"Expected one of [application/gzip application/octet-stream application/vnd.openpolicyagent.bundles]. " +
"Possibly not a bundle being downloaded."
var found bool
for _, entry := range logger.Entries() {
if entry.Message == expectLogged {
found = true
break
}
}

if !found {
t.Errorf("Expected log entry: %s", expectLogged)
}
}

type testFixture struct {
d *Downloader
client rest.Client
Expand Down Expand Up @@ -864,7 +903,11 @@ func (t *testServer) handle(w http.ResponseWriter, r *http.Request) {
// in 304 Content-Type is not send according https://datatracker.ietf.org/doc/html/rfc7232#section-4.1
w.Header().Add("Content-Type", "application/vnd.openpolicyagent.bundles")
} else {
w.Header().Add("Content-Type", "application/gzip")
if r.URL.Path == "/bundles/not-a-bundle" {
w.Header().Add("Content-Type", "text/html")
} else {
w.Header().Add("Content-Type", "application/gzip")
}
}

if t.expEtag != "" {
Expand Down

0 comments on commit 01d82bc

Please sign in to comment.