From 66b7437b924a76e309b55b2c24258a45eae58557 Mon Sep 17 00:00:00 2001 From: Punith C K Date: Mon, 23 Sep 2024 23:18:00 +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 --- internal/commands/push.go | 2 +- policy/engine.go | 24 +++--------------------- 2 files changed, 4 insertions(+), 22 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 dc2f016fa2..62e40c608f 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]interface{} } type compilerOptions struct { @@ -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 } @@ -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 }