@@ -28,6 +28,7 @@ type azureConfig struct {
2828 // IgnoreVersion will ignore any key version, even if given, and always use the latest version.
2929 IgnoreVersion bool `mapstructure:"ignore_version"`
3030 // The Azure Vault KeyId to use for encryption and decryption
31+ // KeyId is the full key string, example: https://secretkeyvault.vault.azure.net/keys/secrets-key/1111231232312111
3132 KeyId string `mapstructure:"key_id"`
3233
3334 VaultName string
@@ -90,7 +91,20 @@ func (driver *Azure) Encrypt(input string) (string, error) {
9091 return base64 .RawStdEncoding .EncodeToString (res .Result ), nil
9192}
9293
93- func (driver * Azure ) Decrypt (input string ) (string , error ) {
94+ // Decrypt decrypts an input either using the key configured in the driver or if the key parameter isn't empty it will use that one.
95+ func (driver * Azure ) Decrypt (input string , key string ) (string , error ) {
96+ var err error
97+ keyName := driver .config .KeyName
98+ keyVersion := driver .config .KeyVersion
99+
100+ // if we hand over a key to this func, make sure to use this key for the decryption, otherwise use the key configured in the driver
101+ if len (key ) > 0 {
102+ _ , keyName , keyVersion , err = parseAzureKeyVaultKeyId (key )
103+ if err != nil {
104+ return "" , fmt .Errorf ("the key we handed over to Decrypt() could not be parsed" )
105+ }
106+ }
107+
94108 decoded , err := base64 .RawStdEncoding .DecodeString (input )
95109 if err != nil {
96110 return "" , err
@@ -101,11 +115,10 @@ func (driver *Azure) Decrypt(input string) (string, error) {
101115 Value : []byte (decoded ),
102116 }
103117
104- version := driver .config .KeyVersion
105118 if driver .config .IgnoreVersion {
106- version = ""
119+ keyVersion = ""
107120 }
108- res , err := driver .client .Decrypt (context .TODO (), driver . config . KeyName , version , encryptParams , nil )
121+ res , err := driver .client .Decrypt (context .TODO (), keyName , keyVersion , encryptParams , nil )
109122 if err != nil {
110123 return "" , err
111124 }
@@ -157,3 +170,7 @@ func parseAzureKeyVaultKeyId(key string) (vaultName string, keyName string, keyV
157170func (driver * Azure ) Type () string {
158171 return "azurekv"
159172}
173+
174+ func (driver Azure ) GetKey () string {
175+ return driver .config .KeyId
176+ }
0 commit comments