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

Signed-off-by: Punith C K <[email protected]>
  • Loading branch information
Punith C K authored and Punith C K committed Oct 17, 2024
1 parent db75f9e commit 13619ee
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 26 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
30 changes: 5 additions & 25 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]any
}

type compilerOptions struct {
Expand Down Expand Up @@ -121,22 +121,14 @@ func LoadWithData(policyPaths []string, dataPaths []string, capabilities string,
return nil, fmt.Errorf("loading policies: %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, func(_ string, info os.FileInfo, _ int) bool {
filter := func(_ string, info os.FileInfo, _ int) bool {
if info.IsDir() {
return false
}
return !contains([]string{".yaml", ".yml", ".json"}, filepath.Ext(info.Name()))
})
if err != nil {
return nil, fmt.Errorf("filter data paths: %w", err)
}

documents, err := loader.NewFileLoader().WithProcessAnnotation(true).Filtered(dataPaths, func(_ string, info os.FileInfo, _ int) bool {
return !info.IsDir() && !contains([]string{".yaml", ".yml", ".json"}, filepath.Ext(info.Name()))
})
documents, err := loader.NewFileLoader().Filtered(dataPaths, filter)
if err != nil {
return nil, fmt.Errorf("load documents: %w", err)
}
Expand All @@ -145,20 +137,8 @@ func LoadWithData(policyPaths []string, dataPaths []string, capabilities string,
return nil, fmt.Errorf("get documents store: %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 @@ -244,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 13619ee

Please sign in to comment.