Skip to content

Commit 5f2342d

Browse files
ahmelsayedpragnagopa
authored andcommitted
Fix functionName case in KubernetesSecretsRepository (#7362)
1 parent a0b4f7d commit 5f2342d

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ public KubernetesSecretsRepository(IEnvironment environment, IKubernetesClient k
5959

6060
public async Task<ScriptSecrets> ReadAsync(ScriptSecretsType type, string functionName)
6161
{
62+
if (type == ScriptSecretsType.Function && string.IsNullOrEmpty(functionName))
63+
{
64+
throw new ArgumentNullException($"{nameof(functionName)} cannot be null or empty with {nameof(type)} = {nameof(ScriptSecretsType.Function)}");
65+
}
66+
67+
functionName = functionName?.ToLowerInvariant();
6268
return type == ScriptSecretsType.Host ? await ReadHostSecrets() : await ReadFunctionSecrets(functionName);
6369
}
6470

@@ -69,6 +75,12 @@ public async Task WriteAsync(ScriptSecretsType type, string functionName, Script
6975
throw new InvalidOperationException($"{nameof(KubernetesSecretsRepository)} is readonly when no {EnvironmentSettingNames.AzureWebJobsKubernetesSecretName} is specified.");
7076
}
7177

78+
if (type == ScriptSecretsType.Function && string.IsNullOrEmpty(functionName))
79+
{
80+
throw new ArgumentNullException($"{nameof(functionName)} cannot be null or empty with {nameof(type)} = {nameof(ScriptSecretsType.Function)}");
81+
}
82+
83+
functionName = functionName?.ToLowerInvariant();
7284
var newKeys = await Mergekeys(type, functionName, secrets);
7385
await _kubernetesClient.UpdateSecrets(newKeys);
7486
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the MIT License. See License.txt in the project root for license information.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Threading.Tasks;
7+
using Microsoft.Azure.WebJobs.Script.WebHost;
8+
using Moq;
9+
using Xunit;
10+
11+
namespace Microsoft.Azure.WebJobs.Script.Tests
12+
{
13+
public class KubernetesSecretsRepositoryTests
14+
{
15+
[Fact]
16+
public async Task Read_Write_Functions_Keys()
17+
{
18+
var environment = new TestEnvironment();
19+
environment.SetEnvironmentVariable(EnvironmentSettingNames.AzureWebJobsKubernetesSecretName, "test");
20+
environment.SetEnvironmentVariable(EnvironmentSettingNames.KubernetesServiceHost, "127.0.0.1");
21+
environment.SetEnvironmentVariable(EnvironmentSettingNames.KubernetesServiceHttpsPort, "443");
22+
IDictionary<string, string> configMapData = new Dictionary<string, string>();
23+
var clientMock = new Mock<IKubernetesClient>(MockBehavior.Strict);
24+
clientMock.Setup(c => c.GetSecrets()).ReturnsAsync(configMapData);
25+
clientMock.SetupGet(c => c.IsWritable).Returns(true);
26+
clientMock.Setup(c => c.OnSecretChange(It.IsAny<Action>()));
27+
clientMock.Setup(c => c.UpdateSecrets(It.IsAny<IDictionary<string, string>>())).Returns<IDictionary<string, string>>(a =>
28+
{
29+
foreach (var k in a)
30+
{
31+
configMapData[k.Key] = k.Value;
32+
}
33+
return Task.CompletedTask;
34+
});
35+
36+
var repo = new KubernetesSecretsRepository(environment, clientMock.Object);
37+
38+
await repo.WriteAsync(ScriptSecretsType.Function, "FUNCTION1", new FunctionSecrets
39+
{
40+
Keys = new List<Key>
41+
{
42+
new Key { Name = "Key1", Value = "value" },
43+
new Key { Name = "key2", Value = "value" }
44+
}
45+
});
46+
await repo.WriteAsync(ScriptSecretsType.Function, "function2", new FunctionSecrets
47+
{
48+
Keys = new List<Key>
49+
{
50+
new Key { Name = "Key1", Value = "value" },
51+
new Key { Name = "key2", Value = "value" }
52+
}
53+
});
54+
55+
var result = await repo.ReadAsync(ScriptSecretsType.Function, "function1");
56+
Assert.NotNull(result);
57+
Assert.Equal("value", result.GetFunctionKey("Key1", "function1").Value);
58+
Assert.Equal("value", result.GetFunctionKey("key2", "function1").Value);
59+
60+
result = await repo.ReadAsync(ScriptSecretsType.Function, "function2");
61+
Assert.NotNull(result);
62+
Assert.Equal("value", result.GetFunctionKey("Key1", "function2").Value);
63+
Assert.Equal("value", result.GetFunctionKey("key2", "function2").Value);
64+
}
65+
}
66+
}

0 commit comments

Comments
 (0)