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

Allow aws profile setting from metadata to be overridden. #1659

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
17 changes: 17 additions & 0 deletions cmd/sops/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ type GenericDecryptOpts struct {
IgnoreMAC bool
KeyServices []keyservice.KeyServiceClient
DecryptionOrder []string
UseAwsProfile string
}

// LoadEncryptedFileWithBugFixes is a wrapper around LoadEncryptedFile which includes
Expand All @@ -251,6 +252,22 @@ func LoadEncryptedFileWithBugFixes(opts GenericDecryptOpts) (*sops.Tree, error)
}
}

awsProfile := os.Getenv("AWS_PROFILE")
if opts.UseAwsProfile != "" {
awsProfile = opts.UseAwsProfile
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think this is the right place to look at environment variables. This is a library function deep down the call tree.

Maybe adding EnvVar: "AWS_PROFILE", to the CLI option would be a better place to handle this? This might have other unintended consequences, though.

Copy link
Author

Choose a reason for hiding this comment

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

Agreed. I can move this logic up in the stack.

}

if awsProfile != "" {
for _, keyGroup := range tree.Metadata.KeyGroups {
for _, masterKey := range keyGroup {
kmsMasterKey, ok := (masterKey).(*kms.MasterKey)
if ok {
kmsMasterKey.AwsProfile = awsProfile
}
}
}
}

return tree, nil
}

Expand Down
12 changes: 7 additions & 5 deletions cmd/sops/decrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,17 @@ type decryptOpts struct {
Extract []interface{}
KeyServices []keyservice.KeyServiceClient
DecryptionOrder []string
UseAwsProfile string
}

func decryptTree(opts decryptOpts) (tree *sops.Tree, err error) {
tree, err = common.LoadEncryptedFileWithBugFixes(common.GenericDecryptOpts{
Cipher: opts.Cipher,
InputStore: opts.InputStore,
InputPath: opts.InputPath,
IgnoreMAC: opts.IgnoreMAC,
KeyServices: opts.KeyServices,
Cipher: opts.Cipher,
InputStore: opts.InputStore,
InputPath: opts.InputPath,
IgnoreMAC: opts.IgnoreMAC,
KeyServices: opts.KeyServices,
UseAwsProfile: opts.UseAwsProfile,
})
if err != nil {
return nil, err
Expand Down
5 changes: 5 additions & 0 deletions cmd/sops/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -743,6 +743,10 @@ func main() {
Usage: "comma separated list of decryption key types",
EnvVar: "SOPS_DECRYPTION_ORDER",
},
cli.StringFlag{
Name: "aws-profile",
Usage: "The AWS profile to use for requests to AWS",
},
}, keyserviceFlags...),
Action: func(c *cli.Context) error {
if c.Bool("verbose") {
Expand Down Expand Up @@ -796,6 +800,7 @@ func main() {
KeyServices: svcs,
DecryptionOrder: order,
IgnoreMAC: c.Bool("ignore-mac"),
UseAwsProfile: c.String("aws-profile"),
Copy link
Contributor

Choose a reason for hiding this comment

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

There are four instances of decryptOpts creations in this file. You need to change all of them, not just one.

})
if err != nil {
return toExitError(err)
Expand Down
Loading