Skip to content

Commit 401d4dd

Browse files
author
Leandro Martelli
committed
Allow aws profile setting from metadata to be overridden.
When decrypting, sops uses the AWS profile setting stored in the encrypted file metadata. This is a problem as the profile can change from user to user. This change will allow the AWS profile setting to be overridden by the '--aws-profile' flag and the AWS_PROFILE environment variable, in that order of precedence. The metadata value is used as a last resort only.
1 parent e0c970a commit 401d4dd

File tree

3 files changed

+29
-5
lines changed

3 files changed

+29
-5
lines changed

cmd/sops/common/common.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ type GenericDecryptOpts struct {
230230
IgnoreMAC bool
231231
KeyServices []keyservice.KeyServiceClient
232232
DecryptionOrder []string
233+
UseAwsProfile string
233234
}
234235

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

255+
awsProfile := os.Getenv("AWS_PROFILE")
256+
if opts.UseAwsProfile != "" {
257+
awsProfile = opts.UseAwsProfile
258+
}
259+
260+
if awsProfile != "" {
261+
for _, keyGroup := range tree.Metadata.KeyGroups {
262+
for _, masterKey := range keyGroup {
263+
kmsMasterKey, ok := (masterKey).(*kms.MasterKey)
264+
if ok {
265+
kmsMasterKey.AwsProfile = awsProfile
266+
}
267+
}
268+
}
269+
}
270+
254271
return tree, nil
255272
}
256273

cmd/sops/decrypt.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,17 @@ type decryptOpts struct {
2323
Extract []interface{}
2424
KeyServices []keyservice.KeyServiceClient
2525
DecryptionOrder []string
26+
UseAwsProfile string
2627
}
2728

2829
func decryptTree(opts decryptOpts) (tree *sops.Tree, err error) {
2930
tree, err = common.LoadEncryptedFileWithBugFixes(common.GenericDecryptOpts{
30-
Cipher: opts.Cipher,
31-
InputStore: opts.InputStore,
32-
InputPath: opts.InputPath,
33-
IgnoreMAC: opts.IgnoreMAC,
34-
KeyServices: opts.KeyServices,
31+
Cipher: opts.Cipher,
32+
InputStore: opts.InputStore,
33+
InputPath: opts.InputPath,
34+
IgnoreMAC: opts.IgnoreMAC,
35+
KeyServices: opts.KeyServices,
36+
UseAwsProfile: opts.UseAwsProfile,
3537
})
3638
if err != nil {
3739
return nil, err

cmd/sops/main.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -743,6 +743,10 @@ func main() {
743743
Usage: "comma separated list of decryption key types",
744744
EnvVar: "SOPS_DECRYPTION_ORDER",
745745
},
746+
cli.StringFlag{
747+
Name: "aws-profile",
748+
Usage: "The AWS profile to use for requests to AWS",
749+
},
746750
}, keyserviceFlags...),
747751
Action: func(c *cli.Context) error {
748752
if c.Bool("verbose") {
@@ -796,6 +800,7 @@ func main() {
796800
KeyServices: svcs,
797801
DecryptionOrder: order,
798802
IgnoreMAC: c.Bool("ignore-mac"),
803+
UseAwsProfile: c.String("aws-profile"),
799804
})
800805
if err != nil {
801806
return toExitError(err)

0 commit comments

Comments
 (0)