Skip to content

Commit 01785f9

Browse files
committed
Creating function default function secret if file does not exist
1 parent c68526c commit 01785f9

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,15 @@ public virtual IDictionary<string, string> GetFunctionSecrets(string functionNam
120120
string secretsFilePath = GetFunctionSecretsFilePath(functionName);
121121
if (!TryLoadFunctionSecrets(functionName, out secrets, secretsFilePath))
122122
{
123-
// initialize an empty FunctionSecrets instance
124-
secrets = new FunctionSecrets();
123+
secrets = new FunctionSecrets
124+
{
125+
Keys = new List<Key>
126+
{
127+
GenerateKey(ScriptConstants.DefaultFunctionKeyName)
128+
}
129+
};
130+
131+
PersistSecrets(secrets, secretsFilePath);
125132
}
126133

127134
// Read all secrets, which will run the keys through the appropriate readers

test/WebJobs.Script.Tests/Security/SecretManagerTests.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ public void GetHostSecrets_WhenNoHostSecretFileExists_GeneratesSecretsAndPersist
216216
}
217217

218218
[Fact]
219-
public void GetFunctionSecrets_WhenNoSecretFileExists_ReturnsEmptySecretsAndDoesNotPersistsFile()
219+
public void GetFunctionSecrets_WhenNoSecretFileExists_CreatesDefaultSecretAndPersistsFile()
220220
{
221221
var secretsPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
222222
try
@@ -234,8 +234,9 @@ public void GetFunctionSecrets_WhenNoSecretFileExists_ReturnsEmptySecretsAndDoes
234234
bool functionSecretsExists = File.Exists(Path.Combine(secretsPath, "testfunction.json"));
235235

236236
Assert.NotNull(functionSecrets);
237-
Assert.False(functionSecretsExists);
238-
Assert.Equal(0, functionSecrets.Count);
237+
Assert.True(functionSecretsExists);
238+
Assert.Equal(1, functionSecrets.Count);
239+
Assert.Equal(ScriptConstants.DefaultFunctionKeyName, functionSecrets.Keys.First());
239240
}
240241
}
241242
finally

0 commit comments

Comments
 (0)