Skip to content

Commit 15fee85

Browse files
committed
Adding additional logs for secret regenerations.
1 parent 682d3fb commit 15fee85

File tree

1 file changed

+30
-8
lines changed

1 file changed

+30
-8
lines changed

src/WebJobs.Script.WebHost/Security/KeyManagement/SecretManager.cs

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using Microsoft.Azure.WebJobs.Script.Diagnostics;
1717
using Microsoft.Azure.WebJobs.Script.WebHost.Properties;
1818
using Microsoft.Extensions.Logging;
19+
using DataProtectionCostants = Microsoft.Azure.Web.DataProtection.Constants;
1920

2021
namespace Microsoft.Azure.WebJobs.Script.WebHost
2122
{
@@ -459,14 +460,6 @@ private async Task PersistSecretsAsync<T>(T secrets, string keyScope = null, boo
459460
ScriptSecretsType secretsType = secrets.SecretsType;
460461
if (isNonDecryptable)
461462
{
462-
string decryptionKey = SystemEnvironment.Instance.GetEnvironmentVariable(EnvironmentSettingNames.WebSiteAuthEncryptionKey);
463-
if (!string.IsNullOrEmpty(decryptionKey))
464-
{
465-
SHA256Managed hash = new SHA256Managed();
466-
byte[] hashBytes = hash.ComputeHash(Encoding.UTF8.GetBytes(decryptionKey));
467-
secrets.DecryptionKeyId = Convert.ToBase64String(hashBytes);
468-
}
469-
470463
string[] secretBackups = await _repository.GetSecretSnapshots(secrets.SecretsType, keyScope);
471464

472465
if (secretBackups.Length >= ScriptConstants.MaximumSecretBackupCount)
@@ -479,6 +472,11 @@ private async Task PersistSecretsAsync<T>(T secrets, string keyScope = null, boo
479472
}
480473
else
481474
{
475+
// We want to store encryption keys hashes to investigate sudden regenerations
476+
string hashes = GetEncryptionKeysHashes();
477+
secrets.DecryptionKeyId = hashes;
478+
_logger?.LogInformation("Encription keys hashes: {0}", hashes);
479+
482480
await _repository.WriteAsync(secretsType, keyScope, secrets);
483481
}
484482
}
@@ -594,5 +592,29 @@ private string GetFunctionName(string keyScope, ScriptSecretsType secretsType)
594592
{
595593
return (secretsType == ScriptSecretsType.Function) ? keyScope : null;
596594
}
595+
596+
private string GetEncryptionKeysHashes()
597+
{
598+
string result = string.Empty;
599+
string azureWebsiteLocalEncryptionKey = SystemEnvironment.Instance.GetEnvironmentVariable(DataProtectionCostants.AzureWebsiteLocalEncryptionKey) ?? string.Empty;
600+
SHA256Managed hash = new SHA256Managed();
601+
602+
if (!string.IsNullOrEmpty(azureWebsiteLocalEncryptionKey))
603+
{
604+
byte[] hashBytes = hash.ComputeHash(Encoding.UTF8.GetBytes(azureWebsiteLocalEncryptionKey));
605+
string azureWebsiteLocalEncryptionKeyHash = Convert.ToBase64String(hashBytes);
606+
result += $"{DataProtectionCostants.AzureWebsiteLocalEncryptionKey}={azureWebsiteLocalEncryptionKeyHash};";
607+
}
608+
609+
string azureWebsiteEnvironmentMachineKey = SystemEnvironment.Instance.GetEnvironmentVariable(DataProtectionCostants.AzureWebsiteEnvironmentMachineKey) ?? string.Empty;
610+
if (!string.IsNullOrEmpty(azureWebsiteEnvironmentMachineKey))
611+
{
612+
byte[] hashBytes = hash.ComputeHash(Encoding.UTF8.GetBytes(azureWebsiteEnvironmentMachineKey));
613+
string azureWebsiteEnvironmentMachineKeyHash = Convert.ToBase64String(hashBytes);
614+
result += $"{DataProtectionCostants.AzureWebsiteEnvironmentMachineKey}={azureWebsiteEnvironmentMachineKeyHash};";
615+
}
616+
617+
return result;
618+
}
597619
}
598620
}

0 commit comments

Comments
 (0)