Skip to content

Commit 4451672

Browse files
mathewcankitkumarr
authored andcommitted
Fixing cache issue (#6561)
1 parent a797bc2 commit 4451672

File tree

2 files changed

+50
-8
lines changed

2 files changed

+50
-8
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,8 @@ private async Task<ScriptSecrets> LoadSecretsAsync(ScriptSecretsType type, strin
385385
{
386386
if (keyValue != null)
387387
{
388-
if (_authorizationCache.TryGetValue(keyValue, out (string, AuthorizationLevel) value))
388+
string cacheKey = $"{keyValue}{functionName}";
389+
if (_authorizationCache.TryGetValue(cacheKey, out (string, AuthorizationLevel) value))
389390
{
390391
// we've already authorized this key value so return the cached result
391392
return value;
@@ -399,7 +400,7 @@ private async Task<ScriptSecrets> LoadSecretsAsync(ScriptSecretsType type, strin
399400
if (result.Item2 != AuthorizationLevel.Anonymous)
400401
{
401402
// key match
402-
_authorizationCache[keyValue] = result;
403+
_authorizationCache[cacheKey] = result;
403404
return result;
404405
}
405406
else

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

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Linq;
88
using System.Security.Cryptography;
99
using System.Threading.Tasks;
10+
using Microsoft.Azure.WebJobs.Extensions.Http;
1011
using Microsoft.Azure.WebJobs.Logging;
1112
using Microsoft.Azure.WebJobs.Script.Diagnostics;
1213
using Microsoft.Azure.WebJobs.Script.WebHost;
@@ -63,8 +64,8 @@ public async Task CachedSecrets_UsedWhenPresent()
6364
var functionSecrets = await secretManager.GetFunctionSecretsAsync("function1", true);
6465

6566
Assert.Equal(4, functionSecrets.Count);
66-
Assert.Equal("function1value", functionSecrets["test-function-1"]);
67-
Assert.Equal("function2value", functionSecrets["test-function-2"]);
67+
Assert.Equal("function1value1", functionSecrets["test-function1-1"]);
68+
Assert.Equal("function1value2", functionSecrets["test-function1-2"]);
6869
Assert.Equal("hostfunction1value", functionSecrets["test-host-function-1"]);
6970
Assert.Equal("hostfunction2value", functionSecrets["test-host-function-2"]);
7071

@@ -87,6 +88,46 @@ public async Task CachedSecrets_UsedWhenPresent()
8788
}
8889
}
8990

91+
[Theory]
92+
[InlineData("function1value1", "test-function1-1", "function1", AuthorizationLevel.Function)]
93+
[InlineData("function1value2", "test-function1-2", "function1", AuthorizationLevel.Function)]
94+
[InlineData("function2value1", "test-function2-1", "function2", AuthorizationLevel.Function)]
95+
[InlineData("function2value2", "test-function2-2", "function2", AuthorizationLevel.Function)]
96+
[InlineData("function2value1", null, "function1", AuthorizationLevel.Anonymous)]
97+
[InlineData("function1value1", null, "function2", AuthorizationLevel.Anonymous)]
98+
[InlineData("invalid", null, "function1", AuthorizationLevel.Anonymous)]
99+
[InlineData("invalid", null, "function2", AuthorizationLevel.Anonymous)]
100+
[InlineData("hostfunction1value", "test-host-function-1", "function1", AuthorizationLevel.Function)]
101+
[InlineData("hostfunction2value", "test-host-function-2", "function1", AuthorizationLevel.Function)]
102+
[InlineData("hostfunction1value", "test-host-function-1", "function2", AuthorizationLevel.Function)]
103+
[InlineData("hostfunction2value", "test-host-function-2", "function2", AuthorizationLevel.Function)]
104+
[InlineData("test-master-key", "master", "function1", AuthorizationLevel.Admin)]
105+
[InlineData("test-master-key", "master", "function2", AuthorizationLevel.Admin)]
106+
[InlineData("test-master-key", "master", null, AuthorizationLevel.Admin)]
107+
[InlineData("system1value", "test-system-1", null, AuthorizationLevel.System)]
108+
[InlineData("system2value", "test-system-2", null, AuthorizationLevel.System)]
109+
public async Task GetAuthorizationLevelOrNullAsync_ReturnsExpectedResult(string keyValue, string expectedKeyName, string functionName, AuthorizationLevel expectedLevel)
110+
{
111+
using (var directory = new TempDirectory())
112+
{
113+
string startupContextPath = Path.Combine(directory.Path, Guid.NewGuid().ToString());
114+
_testEnvironment.SetEnvironmentVariable(EnvironmentSettingNames.AzureWebsiteStartupContextCache, startupContextPath);
115+
_testEnvironment.SetEnvironmentVariable(EnvironmentSettingNames.WebSiteAuthEncryptionKey, TestEncryptionKey);
116+
117+
WriteStartContextCache(startupContextPath);
118+
119+
using (var secretManager = CreateSecretManager(directory.Path))
120+
{
121+
for (int i = 0; i < 3; i++)
122+
{
123+
(string, AuthorizationLevel) result = await secretManager.GetAuthorizationLevelOrNullAsync(keyValue, functionName);
124+
Assert.Equal(result.Item2, expectedLevel);
125+
Assert.Equal(result.Item1, expectedKeyName);
126+
}
127+
}
128+
}
129+
}
130+
90131
private FunctionAppSecrets WriteStartContextCache(string path)
91132
{
92133
var secrets = new FunctionAppSecrets();
@@ -111,17 +152,17 @@ private FunctionAppSecrets WriteStartContextCache(string path)
111152
Name = "function1",
112153
Secrets = new Dictionary<string, string>
113154
{
114-
{ "test-function-1", "function1value" },
115-
{ "test-function-2", "function2value" }
155+
{ "test-function1-1", "function1value1" },
156+
{ "test-function1-2", "function1value2" }
116157
}
117158
},
118159
new FunctionAppSecrets.FunctionSecrets
119160
{
120161
Name = "function2",
121162
Secrets = new Dictionary<string, string>
122163
{
123-
{ "test-function-1", "function1value" },
124-
{ "test-function-2", "function2value" }
164+
{ "test-function2-1", "function2value1" },
165+
{ "test-function2-2", "function2value2" }
125166
}
126167
}
127168
};

0 commit comments

Comments
 (0)