@@ -235,8 +235,16 @@ func (c CredentialsProvider) ApplyToMasterKey(key *MasterKey) {
235235
236236// Encrypt takes a SOPS data key, encrypts it with KMS and stores the result
237237// in the EncryptedKey field.
238+ //
239+ // Consider using EncryptContext instead.
238240func (key * MasterKey ) Encrypt (dataKey []byte ) error {
239- cfg , err := key .createKMSConfig ()
241+ return key .EncryptContext (context .Background (), dataKey )
242+ }
243+
244+ // EncryptContext takes a SOPS data key, encrypts it with KMS and stores the result
245+ // in the EncryptedKey field.
246+ func (key * MasterKey ) EncryptContext (ctx context.Context , dataKey []byte ) error {
247+ cfg , err := key .createKMSConfig (ctx )
240248 if err != nil {
241249 log .WithField ("arn" , key .Arn ).Info ("Encryption failed" )
242250 return err
@@ -247,7 +255,7 @@ func (key *MasterKey) Encrypt(dataKey []byte) error {
247255 Plaintext : dataKey ,
248256 EncryptionContext : stringPointerToStringMap (key .EncryptionContext ),
249257 }
250- out , err := client .Encrypt (context . TODO () , input )
258+ out , err := client .Encrypt (ctx , input )
251259 if err != nil {
252260 log .WithField ("arn" , key .Arn ).Info ("Encryption failed" )
253261 return fmt .Errorf ("failed to encrypt sops data key with AWS KMS: %w" , err )
@@ -278,13 +286,21 @@ func (key *MasterKey) SetEncryptedDataKey(enc []byte) {
278286
279287// Decrypt decrypts the EncryptedKey with a newly created AWS KMS config, and
280288// returns the result.
289+ //
290+ // Consider using DecryptContext instead.
281291func (key * MasterKey ) Decrypt () ([]byte , error ) {
292+ return key .DecryptContext (context .Background ())
293+ }
294+
295+ // DecryptContext decrypts the EncryptedKey with a newly created AWS KMS config, and
296+ // returns the result.
297+ func (key * MasterKey ) DecryptContext (ctx context.Context ) ([]byte , error ) {
282298 k , err := base64 .StdEncoding .DecodeString (key .EncryptedKey )
283299 if err != nil {
284300 log .WithField ("arn" , key .Arn ).Info ("Decryption failed" )
285301 return nil , fmt .Errorf ("error base64-decoding encrypted data key: %s" , err )
286302 }
287- cfg , err := key .createKMSConfig ()
303+ cfg , err := key .createKMSConfig (ctx )
288304 if err != nil {
289305 log .WithField ("arn" , key .Arn ).Info ("Decryption failed" )
290306 return nil , err
@@ -295,7 +311,7 @@ func (key *MasterKey) Decrypt() ([]byte, error) {
295311 CiphertextBlob : k ,
296312 EncryptionContext : stringPointerToStringMap (key .EncryptionContext ),
297313 }
298- decrypted , err := client .Decrypt (context . TODO () , input )
314+ decrypted , err := client .Decrypt (ctx , input )
299315 if err != nil {
300316 log .WithField ("arn" , key .Arn ).Info ("Decryption failed" )
301317 return nil , fmt .Errorf ("failed to decrypt sops data key with AWS KMS: %w" , err )
@@ -351,15 +367,15 @@ func (key *MasterKey) TypeToIdentifier() string {
351367
352368// createKMSConfig returns an AWS config with the credentialsProvider of the
353369// MasterKey, or the default configuration sources.
354- func (key MasterKey ) createKMSConfig () (* aws.Config , error ) {
370+ func (key MasterKey ) createKMSConfig (ctx context. Context ) (* aws.Config , error ) {
355371 re := regexp .MustCompile (arnRegex )
356372 matches := re .FindStringSubmatch (key .Arn )
357373 if matches == nil {
358374 return nil , fmt .Errorf ("no valid ARN found in '%s'" , key .Arn )
359375 }
360376 region := matches [1 ]
361377
362- cfg , err := config .LoadDefaultConfig (context . TODO () , func (lo * config.LoadOptions ) error {
378+ cfg , err := config .LoadDefaultConfig (ctx , func (lo * config.LoadOptions ) error {
363379 // Use the credentialsProvider if present, otherwise default to reading credentials
364380 // from the environment.
365381 if key .credentialsProvider != nil {
@@ -376,7 +392,7 @@ func (key MasterKey) createKMSConfig() (*aws.Config, error) {
376392 }
377393
378394 if key .Role != "" {
379- return key .createSTSConfig (& cfg )
395+ return key .createSTSConfig (ctx , & cfg )
380396 }
381397 return & cfg , nil
382398}
@@ -393,7 +409,7 @@ func (key MasterKey) createClient(config *aws.Config) *kms.Client {
393409// createSTSConfig uses AWS STS to assume a role and returns a config
394410// configured with that role's credentials. It returns an error if
395411// it fails to construct a session name, or assume the role.
396- func (key MasterKey ) createSTSConfig (config * aws.Config ) (* aws.Config , error ) {
412+ func (key MasterKey ) createSTSConfig (ctx context. Context , config * aws.Config ) (* aws.Config , error ) {
397413 name , err := stsSessionName ()
398414 if err != nil {
399415 return nil , err
@@ -404,7 +420,7 @@ func (key MasterKey) createSTSConfig(config *aws.Config) (*aws.Config, error) {
404420 }
405421
406422 client := sts .NewFromConfig (* config )
407- out , err := client .AssumeRole (context . TODO () , input )
423+ out , err := client .AssumeRole (ctx , input )
408424 if err != nil {
409425 return nil , fmt .Errorf ("failed to assume role '%s': %w" , key .Role , err )
410426 }
0 commit comments