Skip to content

Allow Custom CLI commands to be any (complex) shell commands - #260

Merged
Andriy Knysh (aknysh) merged 8 commits into
cloudposse:masterfrom
stoned:sh-as-custom-cmds
Nov 30, 2022
Merged

Andriy Knysh (aknysh) merged 8 commits into
cloudposse:masterfrom
stoned:sh-as-custom-cmds

Conversation

@stoned

@stoned stoned commented Nov 20, 2022

Copy link
Copy Markdown
Contributor

what

  • Allow Custom CLI commands to be any (complex) shell commands

why

references

Misc

  • With the proposed changes, the ExecuteShellCommandAndReturnOutput is no longer used and may be removed.
  • I am also pondering to modify executeWorkflowSteps in a similar way. Would that make sense ?

... using mvdan.cc/sh/v3's shell parser and interpreter.

Also execute "valueCommand commands" the same way when defining
environment variables for custom CLI commands.
Comment thread internal/exec/shell_utils.go Resolved
Comment thread internal/exec/shell_utils.go Resolved

@aknysh Andriy Knysh (aknysh) left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stoned thanks again.

Can you please explain what issues you are solving and what is not possible with the current implementation?

Note that currently command steps support complex scripts using https://yaml-multiline.info, e.g.

    steps:
    - touch ${KUBECONFIG}
    - >
      aws eksupdate-kubecfg 
      --cluster-name {{ .Flags.environment }}-{{ .Flags.stage }}-eks-cluster 
      --aws-profile xxx-gbl-{{ .Flags.stage }}-helm

If the library adds something what is not currently supported, we'll consider it.
We don't want to add a 3rd-party library just b/c it's fancy. Many issues with those 3rd-party libraries, they have bugs we we would encounter later in some cases, they change, they break, they go obsolete (we fixed a few issues like that recently). The more we have them, the more difficult to maintain the whole system.

@stoned

stoned commented Nov 20, 2022

Copy link
Copy Markdown
Contributor Author

The function ExecuteShellCommand() does not fork a shell to execute its provided command. Also executeCustomCommand() execute a simple word splitting with strings.Fields() on the steps values. Both these behaviors prevent using a complex shell command, which has nothing to do with a long or somewhat "hard" to read command.
The example command you provided above is still a simple shell command, as opposed to a complex one like for example date ; echo foo. If you try to specify the latter in a step, you will end up with an error like the following:

Executing command:
/usr/bin/date ; echo foo
date: extra operand ‘echo’
Try 'date --help' for more information.
exit status 1

The support of any shell command without using a library like mvdan/sh if of course possible, and I can provide an alternate implementation if you prefer.

@aknysh

Copy link
Copy Markdown
Member

The function ExecuteShellCommand() does not fork a shell to execute its provided command. Also executeCustomCommand() execute a simple word splitting with strings.Fields() on the steps values. Both these behaviors prevent using a complex shell command, which has nothing to do with a long or somewhat "hard" to read command.
The example command you provided above is still a simple shell command, as opposed to a complex one like for example date ; echo foo. If you try to specify the latter in a step, you will end up with an error like the following:

That's correct, commands separated by ; (date ; echo foo) are not supported (it's Go limitations) (for that we have steps. but it's a separate topic).

I'll review the PR little bit later.

Did you test it by running on the command-line? For example, all the currently defined commands in https://github.com/cloudposse/atmos/blob/master/atmos.yaml#L69 should work.

Thanks

@stoned

stoned commented Nov 20, 2022

Copy link
Copy Markdown
Contributor Author

The commands mentioned do work, albeit some defined stacks or component do have problems, like for example:

$ atmos tf plan -s tenant1-ue2-dev vpc

Found duplicate config for the component 'vpc' for the stack 'tenant1-ue2-dev' in the files: orgs/cp/tenant1/dev/us-east-2-extras, orgs/cp/tenant1/dev/us-east-2.
Check that all context variables in the stack name pattern '{tenant}-{environment}-{stage}' are correctly defined in the files and not duplicated.
Check that all imports are valid.

exit status 1

@stoned

stoned commented Nov 20, 2022

Copy link
Copy Markdown
Contributor Author

The commands mentioned do work, albeit some defined stacks or component do have problems, like for example:

And the example could now use a bit of quoting as a shell process is now involved in the steps execution...

@stoned

stoned commented Nov 20, 2022

Copy link
Copy Markdown
Contributor Author

The support of any shell command without using a library like mvdan/sh if of course possible, and I can provide an alternate implementation if you prefer.

A simpler and incomplete (there is no Windows support for example, unless "Git Bash"/MKSYS/WSL/whatever is used) alternative implementation could be along these lines. Would you prefer something like that ?

@aknysh

Copy link
Copy Markdown
Member

The commands mentioned do work, albeit some defined stacks or component do have problems, like for example:

$ atmos tf plan -s tenant1-ue2-dev vpc

Found duplicate config for the component 'vpc' for the stack 'tenant1-ue2-dev' in the files: orgs/cp/tenant1/dev/us-east-2-extras, orgs/cp/tenant1/dev/us-east-2.
Check that all context variables in the stack name pattern '{tenant}-{environment}-{stage}' are correctly defined in the files and not duplicated.
Check that all imports are valid.

exit status 1

the component vpc shows that behavior on purpose, it was done to detect this exact error.

Please use infra/vpc component for testing

Comment thread internal/exec/shell_utils.go Outdated

runner, err := interp.New(
interp.Dir(dir),
interp.Env(expand.ListEnviron(append(os.Environ(), env...)...)),

@aknysh Andriy Knysh (aknysh) Nov 28, 2022 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stoned you removed

commandToRun := os.ExpandEnv(commandTmpl)

which is used to replace ${var} or $var in the command steps at runtime according to the values of the current environment variables.

Does interp.Env(expand do the same thing?

Also, we prob need to simplify

interp.Env(expand.ListEnviron(append(os.Environ(), env...)...)),

by using a local var b/c it's difficult to read

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly, mvdan.cc/sh/v3/interp.Env() will setup the environment to use: there is no need to have os.ExpandEnv() making its own substitutions.
And, yes, I will rewrite the call to interp.Env() to be, hopefully, simpler to read.

Comment thread internal/exec/shell_utils.go Resolved

@aknysh Andriy Knysh (aknysh) left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stoned thanks again for the PR, LGTM, a few nitpicks, please see comments

@stoned

stoned commented Nov 28, 2022

Copy link
Copy Markdown
Contributor Author

the component vpc shows that behavior on purpose, it was done to detect this exact error.

Please use infra/vpc component for testing

OK, got it.

@stoned

stoned commented Nov 30, 2022

Copy link
Copy Markdown
Contributor Author

stoned thanks again for the PR, LGTM, a few nitpicks, please see comments

Andriy Knysh (@aknysh) I pushed a new commit in the branch/PR with some refactoring.

@aknysh Andriy Knysh (aknysh) left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks stoned

@stoned
stoned deleted the sh-as-custom-cmds branch January 1, 2023 13:56
Erik Osterman (Cloud Posse) (osterman) added a commit that referenced this pull request Jul 24, 2026
Dependabot alert #260 (high severity): postcss <= 8.5.17 is vulnerable to
path traversal via sourceMappingURL auto-loading, allowing arbitrary
.map file disclosure. Bumps the existing pnpm.overrides pin from
^8.5.10 to ^8.5.18 (patched), resolving to 8.5.23. Regenerates
pnpm-lock.yaml and NOTICE accordingly; website build verified clean.

Not addressed here: Dependabot alert #259 (github.com/google/cel-go,
medium severity, GHSA-gcjh-h69q-9w9g) already has a dedicated open
remediation PR (#2800) bumping to the patched v0.29.0 — duplicating
that bump on this unrelated branch would risk a go.sum conflict with
that PR, so it's left to land there.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
Andriy Knysh (aknysh) added a commit that referenced this pull request Jul 26, 2026
* fix: restore auth for evaluated list commands

* fix(auth): correct describe-stacks skip-auth tests, add fix-log record

The describe-stacks auth-manager gate now also considers ProcessTemplates
(fixed in f1839ee), so the pre-existing skip-auth tests that only
disabled --process-functions no longer matched the intended behavior.
Updates them to disable both templates and functions, and adds a
regression test proving auth is still resolved when templates remain
enabled. Also adds the docs/fixes record for the nested-auth-inheritance
fix.

Co-Authored-By: Claude Sonnet 5 <[email protected]>

* [autocommit] formatting fixes

* fix(security): bump postcss to remediate GHSA-r28c-9q8g-f849

Dependabot alert #260 (high severity): postcss <= 8.5.17 is vulnerable to
path traversal via sourceMappingURL auto-loading, allowing arbitrary
.map file disclosure. Bumps the existing pnpm.overrides pin from
^8.5.10 to ^8.5.18 (patched), resolving to 8.5.23. Regenerates
pnpm-lock.yaml and NOTICE accordingly; website build verified clean.

Not addressed here: Dependabot alert #259 (github.com/google/cel-go,
medium severity, GHSA-gcjh-h69q-9w9g) already has a dedicated open
remediation PR (#2800) bumping to the patched v0.29.0 — duplicating
that bump on this unrelated branch would risk a go.sum conflict with
that PR, so it's left to land there.

Co-Authored-By: Claude Sonnet 5 <[email protected]>

* chore: regenerate NOTICE to match current go.sum after rebase

Rebasing onto the atmos-pro[bot] autocommit left NOTICE referencing
grpc-go v1.82.1, which doesn't match this branch's actual go.sum
(v1.81.1). Regenerating via scripts/generate-notice.sh restores the
accurate, go.sum-derived URL.

Co-Authored-By: Claude Sonnet 5 <[email protected]>

* fix(auth): address CodeRabbit feedback on PR #2801

- cmd/list/dependencies.go: only treat --identity=false as AuthDisabled;
  an empty/default identity must still let a newly created auth manager
  run (matches the pattern already used in cmd/list/instances.go).
- cmd/list/settings.go: derive authDisabled from authManager == nil
  instead of hardcoding false, so an explicit --identity=false is
  actually honored by ExecuteDescribeStacksWithOptions instead of
  silently attempting per-component auth resolution.
- cmd/describe_skip_auth_test.go: strengthen the three new
  describe-stacks auth-guard tests to assert ProcessTemplates directly
  and document why the injected setCliArgsForDescribeStackCli stub is
  never invoked (getRunnableDescribeStacksCmd always calls the
  package-level function against the real cmd.Flags()), so the
  assertions are proven to exercise real behavior rather than pass
  coincidentally.
- docs/fixes/2026-07-24-nested-auth-inheritance.md: fix the Validation
  section's run count (3 targeted runs, not 4).

Co-Authored-By: Claude Sonnet 5 <[email protected]>

* refactor(auth): replace mutable-func-var test seams with generated-mock interfaces

CodeRabbit flagged cmd/list/utils.go's createAuthManagerWithStackScan and
internal/exec/template_funcs_component.go's executeComponentFuncTerraformOutputs
as bare package-level func vars swapped by hand-written stubs in tests, instead
of the project's mandated interface + mockgen pattern. Rather than deferring to
a follow-up issue, convert both now, and fold in a third pre-existing instance
of the same anti-pattern (internal/exec/terraform_query.go's authManagerFactory)
so all three auth-manager-factory seams share one consistent, generated-mock
style, matching the existing TerraformOutputGetter convention.

Co-Authored-By: Claude Sonnet 5 <[email protected]>

---------

Co-authored-by: Claude Sonnet 5 <[email protected]>
Co-authored-by: atmos-pro[bot] <173522224+atmos-pro[bot]@users.noreply.github.com>
Co-authored-by: Andriy Knysh <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants