Skip to content

Commit ba65a34

Browse files
authored
feat: add error throwing in case of absolut path for stack (#4055)
* Added throwing of error if path is absolute path * Tests update * CLI options update * Add stack dir check * Add checking of relative path * Markdown update
1 parent b5e1260 commit ba65a34

10 files changed

Lines changed: 135 additions & 102 deletions

File tree

config/stack.go

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -273,10 +273,29 @@ func processComponent(ctx context.Context, opts *options.TerragruntOptions, cmp
273273
return errors.Errorf("failed to adjust source %s: %v", cmp.source, err)
274274
}
275275

276-
dest := cmp.path
277-
// if destination is not an absolute path, join with target directory
278-
if !filepath.IsAbs(cmp.path) {
279-
dest = filepath.Join(cmp.targetDir, cmp.path)
276+
if filepath.IsAbs(cmp.path) {
277+
return errors.Errorf("path %s must be relative", cmp.path)
278+
}
279+
280+
// building destination path based on target directory
281+
dest := filepath.Join(cmp.targetDir, cmp.path)
282+
283+
// validate destination path is within the stack directory
284+
// get the absolute path of the destination directory
285+
absDest, err := filepath.Abs(dest)
286+
if err != nil {
287+
return errors.Errorf("failed to get absolute path for destination '%s': %v", cmp.name, err)
288+
}
289+
290+
// get the absolute path of the stack directory
291+
absStackDir, err := filepath.Abs(cmp.targetDir)
292+
if err != nil {
293+
return errors.Errorf("failed to get absolute path for stack directory '%s': %v", cmp.name, err)
294+
}
295+
296+
// validate that the destination path is within the stack directory
297+
if !strings.HasPrefix(absDest, absStackDir) {
298+
return errors.Errorf("destination path '%s' is outside of the stack directory '%s'", absDest, absStackDir)
280299
}
281300

282301
if cmp.noStack {

docs-starlight/src/data/commands/stack/generate.mdx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,9 @@ terragrunt stack generate --parallelism 4
7676
```
7777

7878
Automatic Discovery: The command automatically discovers all `terragrunt.stack.hcl` files within the directory structure and generates them in parallel.
79+
80+
</Aside>
81+
82+
<Aside type="caution">
83+
Path Restrictions: If an absolute path is provided as an argument, `generate` will throw an error. Only relative paths within the working directory are supported.
7984
</Aside>

docs/_docs/04_reference/02-cli-options.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,11 +343,13 @@ Will create the following directory structure:
343343
- Parallel Execution: Stack generation runs concurrently to improve performance. The number of parallel tasks is determined by the `GOMAXPROCS` environment variable and can be explicitly controlled using the `--parallelism` flag:
344344

345345
```bash
346-
terragrunt stack generate --parallelism 4
346+
terragrunt stack generate --parallelism 4
347347
```
348348

349349
- Automatic Discovery: The command automatically discovers all `terragrunt.stack.hcl` files within the directory structure and generates them in parallel.
350350

351+
- Path Restrictions: If an absolute path is provided as an argument, the command will throw an error. Only relative paths within the working directory are supported.
352+
351353
#### stack run
352354

353355
The `stack run *` command allows users to execute IaC commands across all units defined in a `terragrunt.stack.hcl` file.

test/fixtures/stacks/absolute-path/live/terragrunt.stack.hcl renamed to test/fixtures/stacks/errors/absolute-path/live/terragrunt.stack.hcl

File renamed without changes.

test/fixtures/stacks/absolute-path/units/app/main.tf renamed to test/fixtures/stacks/errors/absolute-path/units/app/main.tf

File renamed without changes.

test/fixtures/stacks/absolute-path/units/app/terragrunt.hcl renamed to test/fixtures/stacks/errors/absolute-path/units/app/terragrunt.hcl

File renamed without changes.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
unit "app1" {
3+
source = "../units/app1"
4+
path = "../project1/app1"
5+
}

test/fixtures/stacks/errors/relative-path-outside-of-stack/units/app1/main.tf

Whitespace-only changes.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
terraform {
2+
source = "."
3+
}

test/integration_stacks_test.go

Lines changed: 96 additions & 97 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)