Skip to content

Commit

Permalink
Merge pull request #1295 from robnester-rh/EC-285
Browse files Browse the repository at this point in the history
Provide better error messaging around policy URLs
  • Loading branch information
robnester-rh authored Jan 22, 2024
2 parents 00d216c + 20b0c1c commit db9b8cb
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
7 changes: 7 additions & 0 deletions cmd/inspect/inspect_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package inspect

import (
"bytes"
"fmt"
"testing"

"github.com/spf13/afero"
Expand Down Expand Up @@ -56,6 +57,9 @@ func TestFetchSourcesFromPolicy(t *testing.T) {
if err := fs.MkdirAll(dir, 0755); err != nil {
panic(err)
}
if err := afero.WriteFile(fs, fmt.Sprintf("%s/foo.rego", args.String(0)), []byte("package foo\n\nbar = 1"), 0644); err != nil {
panic(err)
}
}

downloader.On("Download", mock.Anything, "one", false).Return(nil).Run(createDir)
Expand Down Expand Up @@ -95,6 +99,9 @@ func TestFetchSources(t *testing.T) {
if err := fs.MkdirAll(dir, 0755); err != nil {
panic(err)
}
if err := afero.WriteFile(fs, fmt.Sprintf("%s/foo.rego", args.String(0)), []byte("package foo\n\nbar = 1"), 0644); err != nil {
panic(err)
}
}

downloader.On("Download", mock.Anything, "one", false).Return(nil).Run(createDir)
Expand Down
30 changes: 27 additions & 3 deletions internal/evaluator/conftest_evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"encoding/json"
"fmt"
"net/url"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -332,10 +333,33 @@ func (c conftestEvaluator) Evaluate(ctx context.Context, inputs []string) ([]Out
return nil, nil, err
}

annotations := []*ast.AnnotationsRef{}
fs := utils.FS(ctx)
annotations, err := opa.InspectDir(fs, dir)
if err != nil {
return nil, nil, err
// We only want to inspect the directory of policy subdirs, not config or data subdirs.
if s.Subdir() == "policy" {
annotations, err = opa.InspectDir(fs, dir)
if err != nil {
errMsg := err
if err.Error() == "no rego files found in policy subdirectory" {
// Let's try to give some more robust messaging to the user.
policyURL, err := url.Parse(s.PolicyUrl())
if err != nil {
return nil, nil, errMsg
}
// Do we have a prefix at the end of the URL path?
// If not, this means we aren't trying to access a specific file.
// TODO: Determine if we want to check for a .git suffix as well?
pos := strings.LastIndex(policyURL.Path, ".")
if pos == -1 {
// Are we accessing a GitHub or GitLab URL? If so, are we beginning with 'https' or 'http'?
if (policyURL.Host == "github.com" || policyURL.Host == "gitlab.com") && (policyURL.Scheme == "https" || policyURL.Scheme == "http") {
log.Debug("Git Hub or GitLab, http transport, and no file extension, this could be a problem.")
errMsg = fmt.Errorf("%s.\nYou've specified a %s URL with an %s:// scheme.\nDid you mean: %s instead?", errMsg, policyURL.Hostname(), policyURL.Scheme, fmt.Sprint(policyURL.Host+policyURL.RequestURI()))
}
}
}
return nil, nil, errMsg
}
}

for _, a := range annotations {
Expand Down
7 changes: 7 additions & 0 deletions internal/opa/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (

"github.com/open-policy-agent/opa/ast"
"github.com/open-policy-agent/opa/ast/json"
log "github.com/sirupsen/logrus"
"github.com/spf13/afero"
)

Expand Down Expand Up @@ -198,6 +199,12 @@ func InspectDir(afs afero.Fs, dir string) ([]*ast.AnnotationsRef, error) {
return nil, err
}

// Ensure that we have actual rules, and a directory without rego files.
if len(regoPaths) == 0 {
log.Debug("No rego files found after cloning policy url.")
return nil, errors.New("no rego files found in policy subdirectory")
}

// Inspect all rego files found
allAnnotations, err := inspectMultiple(regoPaths, regoContents)
if err != nil {
Expand Down

0 comments on commit db9b8cb

Please sign in to comment.