8
8
using System . IO ;
9
9
using System . Linq ;
10
10
using System . Security . Cryptography ;
11
+ using System . Text ;
11
12
using System . Threading ;
12
13
using System . Threading . Tasks ;
13
14
using Microsoft . Azure . WebJobs . Host ;
14
15
using Microsoft . Azure . WebJobs . Script . Config ;
15
16
using Microsoft . Azure . WebJobs . Script . WebHost . Properties ;
16
17
using Microsoft . Extensions . Logging ;
18
+ using DataProtectionCostants = Microsoft . Azure . Web . DataProtection . Constants ;
17
19
18
20
namespace Microsoft . Azure . WebJobs . Script . WebHost
19
21
{
@@ -466,7 +468,6 @@ private async Task PersistSecretsAsync<T>(T secrets, string keyScope = null, boo
466
468
}
467
469
468
470
ScriptSecretsType secretsType = secrets . SecretsType ;
469
- string secretsContent = ScriptSecretSerializer . SerializeSecrets < T > ( secrets ) ;
470
471
if ( isNonDecryptable )
471
472
{
472
473
string [ ] secretBackups = await _repository . GetSecretSnapshots ( secrets . SecretsType , keyScope ) ;
@@ -479,11 +480,18 @@ private async Task PersistSecretsAsync<T>(T secrets, string keyScope = null, boo
479
480
_logger ? . LogInformation ( message ) ;
480
481
throw new InvalidOperationException ( message ) ;
481
482
}
482
- await _repository . WriteSnapshotAsync ( secretsType , keyScope , secretsContent ) ;
483
+ await _repository . WriteSnapshotAsync ( secretsType , keyScope , ScriptSecretSerializer . SerializeSecrets < T > ( secrets ) ) ;
483
484
}
484
485
else
485
486
{
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 ) ) ;
487
495
}
488
496
}
489
497
@@ -580,5 +588,31 @@ public async Task PurgeOldSecretsAsync(string rootScriptPath, TraceWriter traceW
580
588
581
589
await _repository . PurgeOldSecretsAsync ( currentFunctions , traceWriter , logger ) ;
582
590
}
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
+ }
583
617
}
584
618
}
0 commit comments