15
15
using Microsoft . Azure . WebJobs . Extensions . Http ;
16
16
using Microsoft . Azure . WebJobs . Script . Diagnostics ;
17
17
using Microsoft . Azure . WebJobs . Script . WebHost . Properties ;
18
+ using Microsoft . Azure . WebJobs . Script . WebHost . Security ;
18
19
using Microsoft . Extensions . Logging ;
19
- using DataProtectionCostants = Microsoft . Azure . Web . DataProtection . Constants ;
20
+
21
+ using DataProtectionConstants = Microsoft . Azure . Web . DataProtection . Constants ;
20
22
21
23
namespace Microsoft . Azure . WebJobs . Script . WebHost
22
24
{
@@ -254,7 +256,7 @@ public async Task<KeyOperationResult> SetMasterKeyAsync(string value = null)
254
256
if ( value == null )
255
257
{
256
258
// Generate a new secret (clear)
257
- masterKey = GenerateSecret ( ) ;
259
+ masterKey = SecretGenerator . GenerateMasterKeyValue ( ) ;
258
260
result = OperationResult . Created ;
259
261
}
260
262
else
@@ -303,7 +305,7 @@ private async Task<KeyOperationResult> AddOrUpdateSecretAsync(ScriptSecretsType
303
305
{
304
306
OperationResult result = OperationResult . NotFound ;
305
307
306
- secret = secret ?? GenerateSecret ( ) ;
308
+ secret = secret ?? SecretGenerator . GenerateFunctionKeyValue ( ) ;
307
309
308
310
await ModifyFunctionSecretsAsync ( secretsType , keyScope , secrets =>
309
311
{
@@ -493,10 +495,10 @@ private HostSecrets GenerateHostSecrets()
493
495
{
494
496
return new HostSecrets
495
497
{
496
- MasterKey = GenerateKey ( ScriptConstants . DefaultMasterKeyName ) ,
498
+ MasterKey = GenerateMasterKey ( ) ,
497
499
FunctionKeys = new List < Key >
498
500
{
499
- GenerateKey ( ScriptConstants . DefaultFunctionKeyName )
501
+ GenerateFunctionKey ( ) ,
500
502
} ,
501
503
SystemKeys = new List < Key > ( )
502
504
} ;
@@ -506,10 +508,10 @@ private HostSecrets GenerateHostSecrets(HostSecrets secrets)
506
508
{
507
509
if ( secrets . MasterKey . IsEncrypted )
508
510
{
509
- secrets . MasterKey . Value = GenerateSecret ( ) ;
511
+ secrets . MasterKey . Value = SecretGenerator . GenerateMasterKeyValue ( ) ;
510
512
}
511
- secrets . SystemKeys = RegenerateKeys ( secrets . SystemKeys ) ;
512
- secrets . FunctionKeys = RegenerateKeys ( secrets . FunctionKeys ) ;
513
+ secrets . SystemKeys = RegenerateKeys ( secrets . SystemKeys , SecretGenerator . SystemKeySeed ) ;
514
+ secrets . FunctionKeys = RegenerateKeys ( secrets . FunctionKeys , SecretGenerator . FunctionKeySeed ) ;
513
515
return secrets ;
514
516
}
515
517
@@ -519,24 +521,24 @@ private FunctionSecrets GenerateFunctionSecrets()
519
521
{
520
522
Keys = new List < Key >
521
523
{
522
- GenerateKey ( ScriptConstants . DefaultFunctionKeyName )
524
+ GenerateFunctionKey ( )
523
525
}
524
526
} ;
525
527
}
526
528
527
529
private FunctionSecrets GenerateFunctionSecrets ( FunctionSecrets secrets )
528
530
{
529
- secrets . Keys = RegenerateKeys ( secrets . Keys ) ;
531
+ secrets . Keys = RegenerateKeys ( secrets . Keys , SecretGenerator . FunctionKeySeed ) ;
530
532
return secrets ;
531
533
}
532
534
533
- private IList < Key > RegenerateKeys ( IList < Key > list )
535
+ private IList < Key > RegenerateKeys ( IList < Key > list , ulong seed )
534
536
{
535
537
return list . Select ( k =>
536
538
{
537
539
if ( k . IsEncrypted )
538
540
{
539
- k . Value = GenerateSecret ( ) ;
541
+ k . Value = SecretGenerator . GenerateIdentifiableSecret ( seed ) ;
540
542
}
541
543
return k ;
542
544
} ) . ToList ( ) ;
@@ -594,31 +596,32 @@ private HostSecrets ReadHostSecrets(HostSecrets hostSecrets)
594
596
} ;
595
597
}
596
598
597
- private Key GenerateKey ( string name = null )
599
+ private Key GenerateMasterKey ( )
598
600
{
599
- string secret = GenerateSecret ( ) ;
601
+ string secret = SecretGenerator . GenerateMasterKeyValue ( ) ;
600
602
601
- return CreateKey ( name , secret ) ;
603
+ return CreateKey ( ScriptConstants . DefaultMasterKeyName , secret ) ;
602
604
}
603
605
604
- private Key CreateKey ( string name , string secret )
606
+ private Key GenerateFunctionKey ( )
605
607
{
606
- var key = new Key ( name , secret ) ;
608
+ string secret = SecretGenerator . GenerateFunctionKeyValue ( ) ;
607
609
608
- return _keyValueConverterFactory . WriteKey ( key ) ;
610
+ return CreateKey ( ScriptConstants . DefaultFunctionKeyName , secret ) ;
609
611
}
610
612
611
- internal static string GenerateSecret ( )
613
+ private Key CreateKey ( string name , ulong seed )
612
614
{
613
- using ( var rng = RandomNumberGenerator . Create ( ) )
614
- {
615
- byte [ ] data = new byte [ 40 ] ;
616
- rng . GetBytes ( data ) ;
617
- string secret = Convert . ToBase64String ( data ) ;
615
+ string secret = SecretGenerator . GenerateIdentifiableSecret ( seed ) ;
618
616
619
- // Replace pluses as they are problematic as URL values
620
- return secret . Replace ( '+' , 'a' ) ;
621
- }
617
+ return CreateKey ( name , secret ) ;
618
+ }
619
+
620
+ private Key CreateKey ( string name , string secret )
621
+ {
622
+ var key = new Key ( name , secret ) ;
623
+
624
+ return _keyValueConverterFactory . WriteKey ( key ) ;
622
625
}
623
626
624
627
private void OnSecretsChanged ( object sender , SecretsChangedEventArgs e )
@@ -725,22 +728,22 @@ private void InitializeCache()
725
728
private string GetEncryptionKeysHashes ( )
726
729
{
727
730
string result = string . Empty ;
728
- string azureWebsiteLocalEncryptionKey = SystemEnvironment . Instance . GetEnvironmentVariable ( DataProtectionCostants . AzureWebsiteLocalEncryptionKey ) ?? string . Empty ;
731
+ string azureWebsiteLocalEncryptionKey = SystemEnvironment . Instance . GetEnvironmentVariable ( DataProtectionConstants . AzureWebsiteLocalEncryptionKey ) ?? string . Empty ;
729
732
SHA256Managed hash = new SHA256Managed ( ) ;
730
733
731
734
if ( ! string . IsNullOrEmpty ( azureWebsiteLocalEncryptionKey ) )
732
735
{
733
736
byte [ ] hashBytes = hash . ComputeHash ( Encoding . UTF8 . GetBytes ( azureWebsiteLocalEncryptionKey ) ) ;
734
737
string azureWebsiteLocalEncryptionKeyHash = Convert . ToBase64String ( hashBytes ) ;
735
- result += $ "{ DataProtectionCostants . AzureWebsiteLocalEncryptionKey } ={ azureWebsiteLocalEncryptionKeyHash } ;";
738
+ result += $ "{ DataProtectionConstants . AzureWebsiteLocalEncryptionKey } ={ azureWebsiteLocalEncryptionKeyHash } ;";
736
739
}
737
740
738
- string azureWebsiteEnvironmentMachineKey = SystemEnvironment . Instance . GetEnvironmentVariable ( DataProtectionCostants . AzureWebsiteEnvironmentMachineKey ) ?? string . Empty ;
741
+ string azureWebsiteEnvironmentMachineKey = SystemEnvironment . Instance . GetEnvironmentVariable ( DataProtectionConstants . AzureWebsiteEnvironmentMachineKey ) ?? string . Empty ;
739
742
if ( ! string . IsNullOrEmpty ( azureWebsiteEnvironmentMachineKey ) )
740
743
{
741
744
byte [ ] hashBytes = hash . ComputeHash ( Encoding . UTF8 . GetBytes ( azureWebsiteEnvironmentMachineKey ) ) ;
742
745
string azureWebsiteEnvironmentMachineKeyHash = Convert . ToBase64String ( hashBytes ) ;
743
- result += $ "{ DataProtectionCostants . AzureWebsiteEnvironmentMachineKey } ={ azureWebsiteEnvironmentMachineKeyHash } ;";
746
+ result += $ "{ DataProtectionConstants . AzureWebsiteEnvironmentMachineKey } ={ azureWebsiteEnvironmentMachineKeyHash } ;";
744
747
}
745
748
746
749
return result ;
0 commit comments