Skip to content

Commit

Permalink
fix: Conftest can now successfully load files using a file URL (e.g.,…
Browse files Browse the repository at this point in the history
… file:///C:/path/to/data.yaml) on windows

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.
We opted for file URLs(e.g., file:///C:/path/to/data.yaml) instead of paths with drive letters (e.g., C:/path/to/data.yaml) because OPA does not support file paths with drive letters.
Resolves #979
  • Loading branch information
Punith C K authored and Punith C K committed Sep 23, 2024
1 parent b188737 commit 66b7437
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 22 deletions.
2 changes: 1 addition & 1 deletion internal/commands/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ func pushLayers(ctx context.Context, pusher content.Pusher, policyPath, dataPath
}

for path, contents := range engine.Documents() {
data := []byte(contents)
data := []byte(contents.(string))
desc := content.NewDescriptorFromBytes(openPolicyAgentDataLayerMediaType, data)
desc.Annotations = map[string]string{
ocispec.AnnotationTitle: path,
Expand Down
24 changes: 3 additions & 21 deletions policy/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type Engine struct {
compiler *ast.Compiler
store storage.Store
policies map[string]string
docs map[string]string
docs map[string]interface{}
}

type compilerOptions struct {
Expand Down Expand Up @@ -137,26 +137,8 @@ func LoadWithData(policyPaths []string, dataPaths []string, capabilities string,
return nil, fmt.Errorf("get documents store: %w", err)
}

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

documentPath = filepath.Clean(documentPath)
documentPath = filepath.ToSlash(documentPath)
documentContents[documentPath] = string(contents)
}

engine.store = store
engine.docs = documentContents
engine.docs = documents.Documents

return engine, nil
}
Expand Down Expand Up @@ -242,7 +224,7 @@ func (e *Engine) Namespaces() []string {
// Documents returns all of the documents loaded into the engine.
// The result is a map where the key is the filepath of the document
// and its value is the raw contents of the loaded document.
func (e *Engine) Documents() map[string]string {
func (e *Engine) Documents() map[string]interface{} {
return e.docs
}

Expand Down

0 comments on commit 66b7437

Please sign in to comment.