rego+bundle: Fix issues underlying #3209 (#3444)

* bundle: cleanup path before setting it as baseDir

GetBundleDirectoryLoader will do it again, but we need the cleaned-up
`path` to avoid "file:/" parts erroneously making it into the bundle's
ModuleFile structs. GetBundleDirectoryLoader is shared with the server,
so we'll keep that as-is.

* rego: avoid (*Bundle).ParsedModules(..)

Using this will prefix a path that is already complete. Using ModuleFile's
Path instead, we get the already-resolved path.

Signed-off-by: Stephan Renatus <stephan.renatus@gmail.com>
This commit is contained in:
Stephan Renatus
2021-05-13 20:25:45 +02:00
committed by GitHub
parent e2af1423c7
commit ee115dba41
5 changed files with 62 additions and 7 deletions
+4
View File
@@ -178,6 +178,10 @@ func (fl fileLoader) Filtered(paths []string, filter Filter) (*Result, error) {
// it will be treated as a normal tarball bundle. If a directory
// is supplied it will be loaded as an unzipped bundle tree.
func (fl fileLoader) AsBundle(path string) (*bundle.Bundle, error) {
path, err := fileurl.Clean(path)
if err != nil {
return nil, err
}
bundleLoader, isDir, err := GetBundleDirectoryLoader(path)
if err != nil {
return nil, err
+15 -2
View File
@@ -411,8 +411,9 @@ func TestAsBundleWithDir(t *testing.T) {
func TestAsBundleWithFileURLDir(t *testing.T) {
files := map[string]string{
"/foo/data.json": "[1,2,3]",
"/.manifest": `{"roots": ["foo"]}`,
"/foo/data.json": "[1,2,3]",
"/foo/policy.rego": "package foo.bar\np = 1",
"/.manifest": `{"roots": ["foo"]}`,
}
test.WithTempFS(files, func(rootDir string) {
@@ -425,6 +426,18 @@ func TestAsBundleWithFileURLDir(t *testing.T) {
t.Fatalf("Expected bundle to be non-nil")
}
if len(b.Modules) != 1 {
t.Fatalf("expected 1 modules, got %d", len(b.Modules))
}
expectedModulePaths := map[string]struct{}{
filepath.Join(rootDir, "/foo/policy.rego"): {},
}
for _, mf := range b.Modules {
if _, found := expectedModulePaths[mf.Path]; !found {
t.Errorf("Unexpected module file with path %s in bundle modules", mf.Path)
}
}
expectedData := util.MustUnmarshalJSON([]byte(`{"foo": [1,2,3]}`))
if !reflect.DeepEqual(b.Data, expectedData) {
t.Fatalf("expected data %+v, got %+v", expectedData, b.Data)