public static string SaveSecret(string keyVaultName, string secretName, string secretValue) { if (string.IsNullOrEmpty(secretValue)) { //log.LogWarning($"Secret value for {secretName} was blank. Will not save to Key Vault {keyVaultName} "); return(null); } try { var secret = KeyVaultHelper.SecretClient(keyVaultName).SetSecret(new KeyVaultSecret(secretName, secretValue)); log.LogDebug($"Saved value for {secretName} in Key Vault {keyVaultName} "); return(secret.Value.Name); } catch (Exception exe) { log.LogError($"Unable to save secret '{secretName}' to vault {keyVaultName}: {exe.ToString()}"); return(null); } }
public static string GetSecret(string keyVaultName, string secretName) { try { var pollyRetrySecrets = Policy.Handle <Azure.Identity.AuthenticationFailedException>().WaitAndRetry(3, retryAttempt => TimeSpan.FromSeconds(Math.Pow(1.3, retryAttempt))); var secret = pollyRetrySecrets.Execute(() => KeyVaultHelper.SecretClient(keyVaultName).GetSecret(secretName)); return(secret.Value.Value); } catch (Azure.RequestFailedException rfe) { log.LogError($"Unable to get secret '{secretName}' from vault {keyVaultName}: [RequestFailedException] {rfe.ErrorCode}"); return(null); } catch (AuthenticationFailedException afe) { log.LogError($"Unable to get secret '{secretName}' from vault {keyVaultName}: [AuthenticationFailedException] {afe.Message}"); return(null); } catch (Exception exe) { log.LogError($"Unable to get secret '{secretName}' from vault {keyVaultName}:[{exe.GetType()}] {exe.Message}"); return(null); } }