Skip to content

Commit 74ff4dd

Browse files
authored
feat: Use better parsing for --auth-provider-cmd (#4881)
* feat: Use better parsing for `--auth-provider-cmd` * Apply suggestion from @yhakbar
1 parent ec83cb1 commit 74ff4dd

3 files changed

Lines changed: 19 additions & 6 deletions

File tree

cli/commands/run/creds/providers/externalcmd/provider.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/gruntwork-io/terragrunt/options"
1515
"github.com/gruntwork-io/terragrunt/pkg/log"
1616
"github.com/gruntwork-io/terragrunt/shell"
17+
"github.com/mattn/go-shellwords"
1718
)
1819

1920
// Provider runs external command that returns a json string with credentials.
@@ -35,15 +36,21 @@ func (provider *Provider) Name() string {
3536

3637
// GetCredentials implements providers.GetCredentials
3738
func (provider *Provider) GetCredentials(ctx context.Context, l log.Logger) (*providers.Credentials, error) {
38-
command := provider.terragruntOptions.AuthProviderCmd
39-
if command == "" {
39+
if provider.terragruntOptions.AuthProviderCmd == "" {
4040
return nil, nil
4141
}
4242

43-
var args []string
43+
parser := shellwords.NewParser()
4444

45-
if parts := strings.Fields(command); len(parts) > 1 {
46-
command = parts[0]
45+
parts, err := parser.Parse(provider.terragruntOptions.AuthProviderCmd)
46+
if err != nil {
47+
return nil, errors.Errorf("failed to parse auth provider command: %w", err)
48+
}
49+
50+
command := parts[0]
51+
52+
args := []string{}
53+
if len(parts) > 1 {
4754
args = parts[1:]
4855
}
4956

@@ -53,7 +60,10 @@ func (provider *Provider) GetCredentials(ctx context.Context, l log.Logger) (*pr
5360
}
5461

5562
if output.Stdout.String() == "" {
56-
return nil, errors.Errorf("command %s completed successfully, but the response does not contain JSON string", command)
63+
return nil, errors.Errorf(
64+
"command %s completed successfully, but the response does not contain JSON string",
65+
provider.terragruntOptions.AuthProviderCmd,
66+
)
5767
}
5868

5969
resp := &Response{Envs: make(map[string]string)}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ require (
241241
github.com/mattn/go-colorable v0.1.14 // indirect
242242
github.com/mattn/go-localereader v0.0.1 // indirect
243243
github.com/mattn/go-runewidth v0.0.16 // indirect
244+
github.com/mattn/go-shellwords v1.0.12 // indirect
244245
github.com/microcosm-cc/bluemonday v1.0.27 // indirect
245246
github.com/mitchellh/copystructure v1.2.0 // indirect
246247
github.com/mitchellh/go-testing-interface v1.14.1 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -786,6 +786,8 @@ github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh
786786
github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc=
787787
github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
788788
github.com/mattn/go-shellwords v1.0.4/go.mod h1:3xCvwCdWdlDJUrvuMn7Wuy9eWs4pE8vqg+NOMyg4B2o=
789+
github.com/mattn/go-shellwords v1.0.12 h1:M2zGm7EW6UQJvDeQxo4T51eKPurbeFbe8WtebGE2xrk=
790+
github.com/mattn/go-shellwords v1.0.12/go.mod h1:EZzvwXDESEeg03EKmM+RmDnNOPKG4lLtQsUlTZDWQ8Y=
789791
github.com/mattn/go-zglob v0.0.6 h1:mP8RnmCgho4oaUYDIDn6GNxYk+qJGUs8fJLn+twYj2A=
790792
github.com/mattn/go-zglob v0.0.6/go.mod h1:MxxjyoXXnMxfIpxTK2GAkw1w8glPsQILx3N5wrKakiY=
791793
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=

0 commit comments

Comments
 (0)