Skip to content

Commit 4435c65

Browse files
pckvcodePunith C K
and
Punith C K
authored
fix: Conftest can now successfully load files using a file URL (e.g., file:///C:/path/to/data.yaml) on windows (#999)
* fix: Conftest encounters errors on Windows when loading file paths that include drive letters (e.g., `C:/path/to/data.yaml`). Even when using a file URL (e.g., `file:///C:/path/to/data.yaml`), we still face issues. With these code changes, Conftest can now successfully load files using a file URL (e.g., `file:///C:/path/to/data.yaml`). We opted for file URLs instead of paths with drive letters (e.g., `C:/path/to/data.yaml`) because OPA does not support file paths with drive letters. For more details, see [this issue comment](open-policy-agent/opa#6922 (comment)). Resolves: #979 Signed-off-by: Punith C K <pck@veracode.com> * fix: Removing WithProcessAnnotation(true) which is not needed for loading data files Signed-off-by: Punith C K <punithck@itsupports-Laptop.local> * fix: Conftest can now successfully load files using a file URL (e.g., file:///C:/path/to/data.yaml) on windows Removing duplicate code Signed-off-by: Punith C K <pck@veracode.com> --------- Signed-off-by: Punith C K <punithck@itsupports-Laptop.local> Signed-off-by: Punith C K <pck@veracode.com> Co-authored-by: Punith C K <punithck@itsupports-Laptop.local>
1 parent 5ae180f commit 4435c65

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

policy/engine.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -125,20 +125,14 @@ func LoadWithData(policyPaths []string, dataPaths []string, opts CompilerOptions
125125
return nil, fmt.Errorf("loading policies: %w", err)
126126
}
127127
}
128-
129-
// FilteredPaths will recursively find all file paths that contain a valid document
130-
// extension from the given list of data paths.
131-
allDocumentPaths, err := loader.FilteredPaths(dataPaths, func(_ string, info os.FileInfo, _ int) bool {
128+
filter := func(_ string, info os.FileInfo, _ int) bool {
132129
if info.IsDir() {
133130
return false
134131
}
135132
return !contains([]string{".yaml", ".yml", ".json"}, filepath.Ext(info.Name()))
136-
})
137-
if err != nil {
138-
return nil, fmt.Errorf("filter data paths: %w", err)
139133
}
140134

141-
documents, err := loader.NewFileLoader().All(allDocumentPaths)
135+
documents, err := loader.NewFileLoader().Filtered(dataPaths, filter)
142136
if err != nil {
143137
return nil, fmt.Errorf("load documents: %w", err)
144138
}
@@ -147,6 +141,12 @@ func LoadWithData(policyPaths []string, dataPaths []string, opts CompilerOptions
147141
return nil, fmt.Errorf("get documents store: %w", err)
148142
}
149143

144+
// FilteredPaths will recursively find all file paths that contain a valid document
145+
// extension from the given list of data paths.
146+
allDocumentPaths, err := loader.FilteredPaths(dataPaths, filter)
147+
if err != nil {
148+
return nil, fmt.Errorf("filter data paths: %w", err)
149+
}
150150
documentContents := make(map[string]string)
151151
for _, documentPath := range allDocumentPaths {
152152
contents, err := os.ReadFile(documentPath)

0 commit comments

Comments
 (0)