From 13619ee47976018b8ea37a81185e5b477e2dd556 Mon Sep 17 00:00:00 2001 From: Punith C K Date: Thu, 5 Sep 2024 16:21:26 +0530 Subject: [PATCH] fix: Conftest can now successfully load files using a file URL (e.g., 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 --- internal/commands/push.go | 2 +- policy/engine.go | 30 +++++------------------------- 2 files changed, 6 insertions(+), 26 deletions(-) diff --git a/internal/commands/push.go b/internal/commands/push.go index e84f126469..7213b3df6b 100644 --- a/internal/commands/push.go +++ b/internal/commands/push.go @@ -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, diff --git a/policy/engine.go b/policy/engine.go index 84b9500835..997db16d23 100644 --- a/policy/engine.go +++ b/policy/engine.go @@ -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 { @@ -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) } @@ -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 } @@ -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 }