|
5 | 5 | using System.Collections.Concurrent;
|
6 | 6 | using System.Globalization;
|
7 | 7 | using System.IO;
|
| 8 | +using System.Security.Cryptography; |
8 | 9 | using Newtonsoft.Json;
|
9 | 10 |
|
10 | 11 | namespace WebJobs.Script.WebHost
|
@@ -52,8 +53,14 @@ public virtual HostSecrets GetHostSecrets()
|
52 | 53 | }
|
53 | 54 | else
|
54 | 55 | {
|
55 |
| - // initialize with empty instance |
56 |
| - _hostSecrets = new HostSecrets(); |
| 56 | + // initialize with new secrets and save it |
| 57 | + _hostSecrets = new HostSecrets |
| 58 | + { |
| 59 | + MasterKey = GenerateSecretString(), |
| 60 | + FunctionKey = GenerateSecretString() |
| 61 | + }; |
| 62 | + |
| 63 | + File.WriteAllText(secretFilePath, JsonConvert.SerializeObject(_hostSecrets, Formatting.Indented)); |
57 | 64 | }
|
58 | 65 | }
|
59 | 66 | return _hostSecrets;
|
@@ -81,14 +88,32 @@ public virtual FunctionSecrets GetFunctionSecrets(string functionName)
|
81 | 88 | }
|
82 | 89 | else
|
83 | 90 | {
|
84 |
| - // initialize with empty instance |
85 |
| - secrets = new FunctionSecrets(); |
| 91 | + // initialize with new secrets and save it |
| 92 | + secrets = new FunctionSecrets |
| 93 | + { |
| 94 | + Key = GenerateSecretString() |
| 95 | + }; |
| 96 | + |
| 97 | + File.WriteAllText(secretFilePath, JsonConvert.SerializeObject(secrets, Formatting.Indented)); |
86 | 98 | }
|
87 | 99 |
|
88 | 100 | return secrets;
|
89 | 101 | });
|
90 | 102 | }
|
91 | 103 |
|
| 104 | + static string GenerateSecretString() |
| 105 | + { |
| 106 | + using (var rng = RandomNumberGenerator.Create()) |
| 107 | + { |
| 108 | + byte[] data = new byte[40]; |
| 109 | + rng.GetBytes(data); |
| 110 | + string secret = Convert.ToBase64String(data); |
| 111 | + |
| 112 | + // Replace pluses as they are problematic as URL values |
| 113 | + return secret.Replace('+', 'a'); |
| 114 | + } |
| 115 | + } |
| 116 | + |
92 | 117 | private void OnChanged(object sender, FileSystemEventArgs e)
|
93 | 118 | {
|
94 | 119 | // clear the cached secrets if they exist
|
|
0 commit comments