Skip to content

Commit 4fbbab3

Browse files
committed
fix for relative rego paths when policy outside of cwd is provided
Signed-off-by: Sylwester Piskozub <sylwesterpiskozub@gmail.com>
1 parent a0623db commit 4fbbab3

1 file changed

Lines changed: 8 additions & 2 deletions

File tree

  • app/cli/internal/policydevel

app/cli/internal/policydevel/lint.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ func Lookup(absPath, config string, format bool) (*PolicyToLint, error) {
109109
}
110110

111111
// Load referenced rego files from all YAML files
112-
if err := policy.loadReferencedRegoFiles(); err != nil {
112+
if err := policy.loadReferencedRegoFiles(filepath.Dir(resolvedPath)); err != nil {
113113
return nil, err
114114
}
115115

@@ -122,7 +122,8 @@ func Lookup(absPath, config string, format bool) (*PolicyToLint, error) {
122122
}
123123

124124
// Loads referenced rego files from all YAML files in the policy
125-
func (p *PolicyToLint) loadReferencedRegoFiles() error {
125+
// Loads referenced rego files from all YAML files in the policy
126+
func (p *PolicyToLint) loadReferencedRegoFiles(baseDir string) error {
126127
seen := make(map[string]struct{})
127128
for _, yamlFile := range p.YAMLFiles {
128129
var parsed v1.Policy
@@ -133,6 +134,11 @@ func (p *PolicyToLint) loadReferencedRegoFiles() error {
133134
for _, spec := range parsed.Spec.Policies {
134135
regoPath := spec.GetPath()
135136
if regoPath != "" {
137+
// If path is relative, make it relative to the YAML file's directory
138+
if !filepath.IsAbs(regoPath) {
139+
regoPath = filepath.Join(baseDir, regoPath)
140+
}
141+
136142
resolvedPath, err := resourceloader.GetPathForResource(regoPath)
137143
if err != nil {
138144
return fmt.Errorf("failed to resolve rego file %q: %w", regoPath, err)

0 commit comments

Comments
 (0)