16
16
using Microsoft . Azure . WebJobs . Script . Diagnostics ;
17
17
using Microsoft . Azure . WebJobs . Script . WebHost . Properties ;
18
18
using Microsoft . Extensions . Logging ;
19
+ using DataProtectionCostants = Microsoft . Azure . Web . DataProtection . Constants ;
19
20
20
21
namespace Microsoft . Azure . WebJobs . Script . WebHost
21
22
{
@@ -459,14 +460,6 @@ private async Task PersistSecretsAsync<T>(T secrets, string keyScope = null, boo
459
460
ScriptSecretsType secretsType = secrets . SecretsType ;
460
461
if ( isNonDecryptable )
461
462
{
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
-
470
463
string [ ] secretBackups = await _repository . GetSecretSnapshots ( secrets . SecretsType , keyScope ) ;
471
464
472
465
if ( secretBackups . Length >= ScriptConstants . MaximumSecretBackupCount )
@@ -479,6 +472,11 @@ private async Task PersistSecretsAsync<T>(T secrets, string keyScope = null, boo
479
472
}
480
473
else
481
474
{
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
+
482
480
await _repository . WriteAsync ( secretsType , keyScope , secrets ) ;
483
481
}
484
482
}
@@ -594,5 +592,29 @@ private string GetFunctionName(string keyScope, ScriptSecretsType secretsType)
594
592
{
595
593
return ( secretsType == ScriptSecretsType . Function ) ? keyScope : null ;
596
594
}
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
+ }
597
619
}
598
620
}
0 commit comments