Skip to content

Commit af16383

Browse files
committed
[Port V2]: Adding additional logs for secrets regen. Fixes #4879
1 parent bc029aa commit af16383

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

src/WebJobs.Script.WebHost/Security/ScriptSecrets.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ protected ScriptSecrets()
3636
[JsonProperty(PropertyName = "source")]
3737
public string Source { get; set; }
3838

39+
[JsonProperty(PropertyName = "decryptionKeyId")]
40+
public string DecryptionKeyId { get; set; }
41+
3942
protected abstract ICollection<Key> GetKeys(string keyScope);
4043

4144
public abstract ScriptSecrets Refresh(IKeyValueConverterFactory factory);

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

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@
88
using System.IO;
99
using System.Linq;
1010
using System.Security.Cryptography;
11+
using System.Text;
1112
using System.Threading;
1213
using System.Threading.Tasks;
1314
using Microsoft.Azure.WebJobs.Host;
1415
using Microsoft.Azure.WebJobs.Script.Config;
1516
using Microsoft.Azure.WebJobs.Script.WebHost.Properties;
1617
using Microsoft.Extensions.Logging;
18+
using DataProtectionCostants = Microsoft.Azure.Web.DataProtection.Constants;
1719

1820
namespace Microsoft.Azure.WebJobs.Script.WebHost
1921
{
@@ -466,7 +468,6 @@ private async Task PersistSecretsAsync<T>(T secrets, string keyScope = null, boo
466468
}
467469

468470
ScriptSecretsType secretsType = secrets.SecretsType;
469-
string secretsContent = ScriptSecretSerializer.SerializeSecrets<T>(secrets);
470471
if (isNonDecryptable)
471472
{
472473
string[] secretBackups = await _repository.GetSecretSnapshots(secrets.SecretsType, keyScope);
@@ -479,11 +480,18 @@ private async Task PersistSecretsAsync<T>(T secrets, string keyScope = null, boo
479480
_logger?.LogInformation(message);
480481
throw new InvalidOperationException(message);
481482
}
482-
await _repository.WriteSnapshotAsync(secretsType, keyScope, secretsContent);
483+
await _repository.WriteSnapshotAsync(secretsType, keyScope, ScriptSecretSerializer.SerializeSecrets<T>(secrets));
483484
}
484485
else
485486
{
486-
await _repository.WriteAsync(secretsType, keyScope, secretsContent);
487+
// We want to store encryption keys hashes to investigate sudden regenerations
488+
string hashes = GetEncryptionKeysHashes();
489+
secrets.DecryptionKeyId = hashes;
490+
string message = $"Encription keys hashes: {hashes}";
491+
_traceWriter.Info(message);
492+
_logger?.LogInformation(message);
493+
494+
await _repository.WriteAsync(secretsType, keyScope, ScriptSecretSerializer.SerializeSecrets<T>(secrets));
487495
}
488496
}
489497

@@ -580,5 +588,31 @@ public async Task PurgeOldSecretsAsync(string rootScriptPath, TraceWriter traceW
580588

581589
await _repository.PurgeOldSecretsAsync(currentFunctions, traceWriter, logger);
582590
}
591+
592+
private static string GetEncryptionKeysHashes()
593+
{
594+
string result = string.Empty;
595+
string azureWebsiteLocalEncryptionKey = Environment.GetEnvironmentVariable(DataProtectionCostants.AzureWebsiteLocalEncryptionKey) ?? string.Empty;
596+
using (SHA256Managed hash = new SHA256Managed())
597+
{
598+
599+
if (!string.IsNullOrEmpty(azureWebsiteLocalEncryptionKey))
600+
{
601+
byte[] hashBytes = hash.ComputeHash(Encoding.UTF8.GetBytes(azureWebsiteLocalEncryptionKey));
602+
string azureWebsiteLocalEncryptionKeyHash = Convert.ToBase64String(hashBytes);
603+
result += $"{DataProtectionCostants.AzureWebsiteLocalEncryptionKey}={azureWebsiteLocalEncryptionKeyHash};";
604+
}
605+
606+
string azureWebsiteEnvironmentMachineKey = Environment.GetEnvironmentVariable(DataProtectionCostants.AzureWebsiteEnvironmentMachineKey) ?? string.Empty;
607+
if (!string.IsNullOrEmpty(azureWebsiteEnvironmentMachineKey))
608+
{
609+
byte[] hashBytes = hash.ComputeHash(Encoding.UTF8.GetBytes(azureWebsiteEnvironmentMachineKey));
610+
string azureWebsiteEnvironmentMachineKeyHash = Convert.ToBase64String(hashBytes);
611+
result += $"{DataProtectionCostants.AzureWebsiteEnvironmentMachineKey}={azureWebsiteEnvironmentMachineKeyHash};";
612+
}
613+
614+
return result;
615+
}
616+
}
583617
}
584618
}

0 commit comments

Comments
 (0)