Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Conftest can now successfully load files using a file URL (e.g., file:///C:/path/to/data.yaml) on windows #1008

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
28 changes: 5 additions & 23 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 @@ -121,20 +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().All(allDocumentPaths)
documents, err := loader.NewFileLoader().Filtered(dataPaths, filter)
if err != nil {
return nil, fmt.Errorf("load documents: %w", err)
}
Expand All @@ -143,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 @@ -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
Loading