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

feat: Support Relative Path Imports #891

Open
wants to merge 33 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
c50d071
Handle relative paths for stack imports
milldr Dec 24, 2024
f79e16a
Tests for relative paths
milldr Dec 24, 2024
74ab1d8
Refactor handling of relative paths in ProcessImportSection
milldr Dec 24, 2024
e2af2db
Handle relative paths in StackImport.Path and string imports
milldr Dec 24, 2024
9d39012
Add new test2/us-west-1.yaml file and update test counts
milldr Dec 24, 2024
12358b0
Update import paths in us-west-1.yaml file
milldr Dec 24, 2024
093ac5e
Merge branch 'main' into feat/DEV-1856_relative-paths
milldr Dec 24, 2024
b9856ff
Merge branch 'main' into feat/DEV-1856_relative-paths
osterman Dec 26, 2024
71eaca0
Merge branch 'main' into feat/DEV-1856_relative-paths
milldr Dec 26, 2024
4f6170c
Merge branch 'feat/DEV-1856_relative-paths' of github.com:cloudposse/…
milldr Dec 26, 2024
362e979
Merge branch 'main' into feat/DEV-1856_relative-paths
milldr Dec 27, 2024
8d76059
Refactor handling of import paths in ProcessImportSection
milldr Dec 27, 2024
1beeb8a
Resolve relative paths in ProcessImportSection function
milldr Dec 27, 2024
dd55c0f
Update internal/exec/stack_processor_utils.go
milldr Dec 27, 2024
dfa5a84
Refactor resolveRelativePath for improved path handling
milldr Dec 27, 2024
507a5dd
Refactor filepath handling for better readability
milldr Dec 27, 2024
cafdfdd
Remove unnecessary path normalization code and simplify logic
milldr Dec 27, 2024
ddde3d5
Improve path resolution handling in stack_processor_utils
milldr Dec 27, 2024
4eeef33
Merge branch 'main' into feat/DEV-1856_relative-paths
milldr Jan 2, 2025
7fa809e
relative path handling improvements for windows; added security check…
milldr Jan 3, 2025
3be0d81
Validate import paths for security and base path inclusion
milldr Jan 3, 2025
2f89d4a
Update path.Join calls to use filepath.Join for consistency
milldr Jan 3, 2025
2c80a89
Update stack_processor_utils.go to use filepath package
milldr Jan 3, 2025
062cf05
validate stack paths to check against the global base path
milldr Jan 3, 2025
3d98cd4
Refactor path resolution and validation logic
milldr Jan 3, 2025
558c324
Refactor resolving relative path logic for clarity
milldr Jan 3, 2025
88740a5
Revert changes
milldr Jan 3, 2025
94886b6
Refactor path resolution for consistency
milldr Jan 3, 2025
1bc256a
Refactor resolveRelativePath function for path consistency
milldr Jan 3, 2025
037e0ba
Convert relative path to base path if starts with "." or ".."
milldr Jan 3, 2025
358128c
Simplify resolving relative paths in stack_processor_utils
milldr Jan 3, 2025
7bf9208
added tests for atmos stacks with relative paths
milldr Jan 3, 2025
29b390d
Merge branch 'main' into feat/DEV-1856_relative-paths
milldr Jan 3, 2025
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
8 changes: 8 additions & 0 deletions examples/tests/stacks/mixins/stage/test2.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# yaml-language-server: $schema=https://atmos.tools/schemas/atmos/atmos-manifest/1.0/atmos-manifest.json

vars:
stage: test-2

settings:
config:
is_prod: false
5 changes: 5 additions & 0 deletions examples/tests/stacks/orgs/cp/tenant1/test2/_defaults.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# yaml-language-server: $schema=https://atmos.tools/schemas/atmos/atmos-manifest/1.0/atmos-manifest.json

import:
- mixins/stage/test2
- ../_defaults # validate relative paths
5 changes: 5 additions & 0 deletions examples/tests/stacks/orgs/cp/tenant1/test2/us-east-2.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# yaml-language-server: $schema=https://atmos.tools/schemas/atmos/atmos-manifest/1.0/atmos-manifest.json

import:
- mixins/region/us-east-2
- ./_defaults # validate relative paths
5 changes: 5 additions & 0 deletions examples/tests/stacks/orgs/cp/tenant1/test2/us-west-1.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# yaml-language-server: $schema=https://atmos.tools/schemas/atmos/atmos-manifest/1.0/atmos-manifest.json

import:
- path: mixins/region/us-west-1
- path: ./_defaults # validate relative paths using the `path` field
16 changes: 16 additions & 0 deletions internal/exec/stack_processor_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -1773,6 +1773,15 @@ func ProcessImportSection(stackMap map[string]any, filePath string) ([]schema.St
var importObj schema.StackImport
err := mapstructure.Decode(imp, &importObj)
if err == nil {
// Handle relative paths in StackImport.Path
if strings.HasPrefix(importObj.Path, "./") || strings.HasPrefix(importObj.Path, "../") {
// Get the directory of the current file
baseDir := filepath.Dir(filePath)
// Join the base directory with the relative path
importObj.Path = filepath.Join(baseDir, importObj.Path)
// Clean the path to resolve any .. segments
importObj.Path = filepath.Clean(importObj.Path)
}
result = append(result, importObj)
continue
}
Expand All @@ -1786,6 +1795,13 @@ func ProcessImportSection(stackMap map[string]any, filePath string) ([]schema.St
return nil, fmt.Errorf("invalid empty import in the file '%s'", filePath)
}

// Handle relative paths in string imports
if strings.HasPrefix(s, "./") || strings.HasPrefix(s, "../") {
osterman marked this conversation as resolved.
Show resolved Hide resolved
baseDir := filepath.Dir(filePath)
s = filepath.Join(baseDir, s)
s = filepath.Clean(s)
}

result = append(result, schema.StackImport{Path: s})
}

Expand Down
9 changes: 6 additions & 3 deletions pkg/stack/stack_processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ func TestStackProcessor(t *testing.T) {
"../../examples/tests/stacks/orgs/cp/tenant1/prod/us-east-2.yaml",
"../../examples/tests/stacks/orgs/cp/tenant1/staging/us-east-2.yaml",
"../../examples/tests/stacks/orgs/cp/tenant1/test1/us-east-2.yaml",
"../../examples/tests/stacks/orgs/cp/tenant1/test2/us-east-2.yaml",
"../../examples/tests/stacks/orgs/cp/tenant1/test2/us-west-1.yaml",
}

processStackDeps := true
Expand Down Expand Up @@ -50,15 +52,16 @@ func TestStackProcessor(t *testing.T) {
)

assert.Nil(t, err)
assert.Equal(t, 4, len(listResult))
assert.Equal(t, 4, len(mapResult))
assert.Equal(t, 6, len(listResult))
assert.Equal(t, 6, len(mapResult))

mapResultKeys := u.StringKeysFromMap(mapResult)
assert.Equal(t, "orgs/cp/tenant1/dev/us-east-2", mapResultKeys[0])
assert.Equal(t, "orgs/cp/tenant1/prod/us-east-2", mapResultKeys[1])
assert.Equal(t, "orgs/cp/tenant1/staging/us-east-2", mapResultKeys[2])
assert.Equal(t, "orgs/cp/tenant1/test1/us-east-2", mapResultKeys[3])

assert.Equal(t, "orgs/cp/tenant1/test2/us-east-2", mapResultKeys[4])
assert.Equal(t, "orgs/cp/tenant1/test2/us-west-1", mapResultKeys[5])
mapConfig1, err := u.UnmarshalYAML[schema.AtmosSectionMapType](listResult[0])
assert.Nil(t, err)

Expand Down
Loading