Skip to content

Commit 789f00e

Browse files
authored
Address test secrets being flagged (#10264)
1 parent ad6d561 commit 789f00e

File tree

8 files changed

+64
-52
lines changed

8 files changed

+64
-52
lines changed

test/WebJobs.Script.Tests.Integration/Management/InstanceControllerTests.cs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@ namespace Microsoft.Azure.WebJobs.Script.Tests.Managment
2828
[Trait(TestTraits.Group, TestTraits.ContainerInstanceTests)]
2929
public class InstanceControllerTests
3030
{
31-
[SuppressMessage("Microsoft.Security", "CS002:SecretInNextLine", Justification = "Fake key for testing purposes.")]
32-
private const string ContainerEncryptionKey = "/a/vXvWJ3Hzgx4PFxlDUJJhQm5QVyGiu0NNLFm/ZMMg=";
33-
3431
private readonly TestOptionsFactory<ScriptApplicationHostOptions> _optionsFactory = new TestOptionsFactory<ScriptApplicationHostOptions>(new ScriptApplicationHostOptions());
3532
private readonly Mock<IRunFromPackageHandler> _runFromPackageHandler;
3633

@@ -82,14 +79,14 @@ public async Task Assign_MSISpecializationFailure_ReturnsError()
8279
hostAssignmentContext.Environment[EnvironmentSettingNames.MsiEndpoint] = "http://localhost:8081";
8380
hostAssignmentContext.Environment[EnvironmentSettingNames.MsiSecret] = "secret";
8481

85-
var encryptedHostAssignmentValue = SimpleWebTokenHelper.Encrypt(JsonConvert.SerializeObject(hostAssignmentContext), ContainerEncryptionKey.ToKeyBytes());
82+
var encryptedHostAssignmentValue = SimpleWebTokenHelper.Encrypt(JsonConvert.SerializeObject(hostAssignmentContext), TestHelpers.EncryptionKey.ToKeyBytes());
8683

8784
var encryptedHostAssignmentContext = new EncryptedHostAssignmentContext()
8885
{
8986
EncryptedContext = encryptedHostAssignmentValue
9087
};
9188

92-
environment.SetEnvironmentVariable(EnvironmentSettingNames.ContainerEncryptionKey, ContainerEncryptionKey);
89+
environment.SetEnvironmentVariable(EnvironmentSettingNames.ContainerEncryptionKey, TestHelpers.EncryptionKey);
9390

9491
IActionResult result = await instanceController.Assign(encryptedHostAssignmentContext);
9592

@@ -158,14 +155,14 @@ public async Task Assignment_Sets_Secrets_Context()
158155
hostAssignmentContext.Secrets = new FunctionAppSecrets();
159156
hostAssignmentContext.IsWarmupRequest = false; // non-warmup Request
160157

161-
var encryptedHostAssignmentValue = SimpleWebTokenHelper.Encrypt(JsonConvert.SerializeObject(hostAssignmentContext), ContainerEncryptionKey.ToKeyBytes());
158+
var encryptedHostAssignmentValue = SimpleWebTokenHelper.Encrypt(JsonConvert.SerializeObject(hostAssignmentContext), TestHelpers.EncryptionKey.ToKeyBytes());
162159

163160
var encryptedHostAssignmentContext = new EncryptedHostAssignmentContext()
164161
{
165162
EncryptedContext = encryptedHostAssignmentValue
166163
};
167164

168-
environment.SetEnvironmentVariable(EnvironmentSettingNames.ContainerEncryptionKey, ContainerEncryptionKey);
165+
environment.SetEnvironmentVariable(EnvironmentSettingNames.ContainerEncryptionKey, TestHelpers.EncryptionKey);
169166

170167
await instanceController.Assign(encryptedHostAssignmentContext);
171168
Assert.NotNull(startupContextProvider.Context);
@@ -211,14 +208,14 @@ public async Task Assignment_Does_Not_Set_Secrets_Context_For_Warmup_Request()
211208
hostAssignmentContext.Secrets = new FunctionAppSecrets();
212209
hostAssignmentContext.IsWarmupRequest = true; // Warmup Request
213210

214-
var encryptedHostAssignmentValue = SimpleWebTokenHelper.Encrypt(JsonConvert.SerializeObject(hostAssignmentContext), ContainerEncryptionKey.ToKeyBytes());
211+
var encryptedHostAssignmentValue = SimpleWebTokenHelper.Encrypt(JsonConvert.SerializeObject(hostAssignmentContext), TestHelpers.EncryptionKey.ToKeyBytes());
215212

216213
var encryptedHostAssignmentContext = new EncryptedHostAssignmentContext()
217214
{
218215
EncryptedContext = encryptedHostAssignmentValue
219216
};
220217

221-
environment.SetEnvironmentVariable(EnvironmentSettingNames.ContainerEncryptionKey, ContainerEncryptionKey);
218+
environment.SetEnvironmentVariable(EnvironmentSettingNames.ContainerEncryptionKey, TestHelpers.EncryptionKey);
222219

223220
await instanceController.Assign(encryptedHostAssignmentContext);
224221
Assert.Null(startupContextProvider.Context);
@@ -252,14 +249,14 @@ public async Task Assignment_Invokes_InstanceManager_Methods_For_Warmup_Requests
252249

253250
var encryptedHostAssignmentValue =
254251
SimpleWebTokenHelper.Encrypt(JsonConvert.SerializeObject(hostAssignmentContext),
255-
ContainerEncryptionKey.ToKeyBytes());
252+
TestHelpers.EncryptionKey.ToKeyBytes());
256253

257254
var encryptedHostAssignmentContext = new EncryptedHostAssignmentContext()
258255
{
259256
EncryptedContext = encryptedHostAssignmentValue
260257
};
261258

262-
environment.SetEnvironmentVariable(EnvironmentSettingNames.ContainerEncryptionKey, ContainerEncryptionKey);
259+
environment.SetEnvironmentVariable(EnvironmentSettingNames.ContainerEncryptionKey, TestHelpers.EncryptionKey);
263260

264261
await instanceController.Assign(encryptedHostAssignmentContext);
265262

test/WebJobs.Script.Tests.Integration/Management/KubernetesPodControllerTests.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@ namespace Microsoft.Azure.WebJobs.Script.Tests.Managment
2828
[Trait(TestTraits.Group, TestTraits.ContainerInstanceTests)]
2929
public class KubernetesPodControllerTests
3030
{
31-
[SuppressMessage("Microsoft.Security", "CS002:SecretInNextLine", Justification = "Fake key for testing purposes.")]
32-
private const string PodEncryptionKey = "/a/vXvWJ3Hzgx4PFxlDUJJhQm5QVyGiu0NNLFm/ZMMg=";
33-
3431
private readonly TestOptionsFactory<ScriptApplicationHostOptions> _optionsFactory = new TestOptionsFactory<ScriptApplicationHostOptions>(new ScriptApplicationHostOptions());
3532

3633
[Fact]
@@ -74,14 +71,14 @@ public async Task Assignment_Succeeds_With_Encryption_Key()
7471
hostAssignmentContext.Secrets = new FunctionAppSecrets();
7572
hostAssignmentContext.IsWarmupRequest = false;
7673

77-
var encryptedHostAssignmentValue = SimpleWebTokenHelper.Encrypt(JsonConvert.SerializeObject(hostAssignmentContext), PodEncryptionKey.ToKeyBytes());
74+
var encryptedHostAssignmentValue = SimpleWebTokenHelper.Encrypt(JsonConvert.SerializeObject(hostAssignmentContext), TestHelpers.EncryptionKey.ToKeyBytes());
7875

7976
var encryptedHostAssignmentContext = new EncryptedHostAssignmentContext()
8077
{
8178
EncryptedContext = encryptedHostAssignmentValue
8279
};
8380

84-
environment.SetEnvironmentVariable(EnvironmentSettingNames.PodEncryptionKey, PodEncryptionKey);
81+
environment.SetEnvironmentVariable(EnvironmentSettingNames.PodEncryptionKey, TestHelpers.EncryptionKey);
8582
environment.SetEnvironmentVariable(EnvironmentSettingNames.KubernetesServiceHost, "http://localhost:80");
8683
environment.SetEnvironmentVariable(EnvironmentSettingNames.PodNamespace, "k8se-apps");
8784

@@ -131,7 +128,7 @@ public async Task Assignment_Fails_Without_Encryption_Key()
131128
hostAssignmentContext.Secrets = new FunctionAppSecrets();
132129
hostAssignmentContext.IsWarmupRequest = false;
133130

134-
var encryptedHostAssignmentValue = SimpleWebTokenHelper.Encrypt(JsonConvert.SerializeObject(hostAssignmentContext), PodEncryptionKey.ToKeyBytes());
131+
var encryptedHostAssignmentValue = SimpleWebTokenHelper.Encrypt(JsonConvert.SerializeObject(hostAssignmentContext), TestHelpers.EncryptionKey.ToKeyBytes());
135132

136133
var encryptedHostAssignmentContext = new EncryptedHostAssignmentContext()
137134
{

test/WebJobs.Script.Tests.Integration/Management/MeshServiceClientTests.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33

44
using System;
55
using System.Collections.Generic;
6-
using System.Diagnostics.CodeAnalysis;
76
using System.Linq;
87
using System.Net;
98
using System.Net.Http;
9+
using System.Text;
1010
using System.Threading;
1111
using System.Threading.Tasks;
1212
using Microsoft.Azure.WebJobs.Script.WebHost.Management;
@@ -21,9 +21,6 @@ namespace Microsoft.Azure.WebJobs.Script.Tests.Integration.Management
2121
{
2222
public class MeshServiceClientTests
2323
{
24-
[SuppressMessage("Microsoft.Security", "CS002:SecretInNextLine", Justification = "Fake key for testing purposes.")]
25-
private const string ConnectionString = "DefaultEndpointsProtocol=https;AccountName=storageaccount;AccountKey=whXtW6WP8QTh84TT5wdjgzeFTj7Vc1aOiCVjTXohpE+jALoKOQ9nlQpj5C5zpgseVJxEVbaAhptP5j5DpaLgtA==";
26-
2724
private const string MeshInitUri = "http://localhost:8954/";
2825
private const string ContainerName = "MockContainerName";
2926
private readonly IMeshServiceClient _meshServiceClient;
@@ -83,9 +80,7 @@ public async Task MountsCifsShare()
8380
StatusCode = HttpStatusCode.OK
8481
});
8582

86-
87-
88-
await _meshServiceClient.MountCifs(ConnectionString, "sharename", "/data");
83+
await _meshServiceClient.MountCifs(TestHelpers.StorageConnectionString, "sharename", "/data");
8984

9085
await Task.Delay(500);
9186

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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.IO;
6+
using System.Security.Cryptography;
7+
using System.Text;
8+
9+
namespace Microsoft.Azure.WebJobs.Script.Tests
10+
{
11+
public static partial class TestHelpers
12+
{
13+
#if DEBUG
14+
public const string BuildConfig = "debug";
15+
#else
16+
public const string BuildConfig = "release";
17+
#endif
18+
// Not a real storage account key.
19+
public static readonly string StorageAccountKey = Convert.ToBase64String(Encoding.UTF8.GetBytes("PLACEHOLDER"));
20+
21+
// Not a real connection string.
22+
public static readonly string StorageConnectionString = $"DefaultEndpointsProtocol=http;AccountName=fakeaccount;AccountKey={StorageAccountKey}";
23+
24+
private static readonly Lazy<string> _encryptionKey = new Lazy<string>(
25+
() =>
26+
{
27+
using Aes aes = Aes.Create();
28+
aes.GenerateKey();
29+
return Convert.ToBase64String(aes.Key);
30+
});
31+
32+
public static string EncryptionKey => _encryptionKey.Value;
33+
34+
/// <summary>
35+
/// Gets the common root directory that functions tests create temporary directories under.
36+
/// This enables us to clean up test files by deleting this single directory.
37+
/// </summary>
38+
public static string FunctionsTestDirectory
39+
{
40+
get
41+
{
42+
return Path.Combine(Path.GetTempPath(), "FunctionsTest");
43+
}
44+
}
45+
}
46+
}

test/WebJobs.Script.Tests.Shared/TestHelpers.cs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,27 +29,9 @@ namespace Microsoft.Azure.WebJobs.Script.Tests
2929
{
3030
public static partial class TestHelpers
3131
{
32-
#if DEBUG
33-
public const string BuildConfig = "debug";
34-
#else
35-
public const string BuildConfig = "release";
36-
#endif
37-
3832
private const string Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
3933
private static readonly Random Random = new Random();
4034

41-
/// <summary>
42-
/// Gets the common root directory that functions tests create temporary directories under.
43-
/// This enables us to clean up test files by deleting this single directory.
44-
/// </summary>
45-
public static string FunctionsTestDirectory
46-
{
47-
get
48-
{
49-
return Path.Combine(Path.GetTempPath(), "FunctionsTest");
50-
}
51-
}
52-
5335
public static Task WaitOneAsync(this WaitHandle waitHandle)
5436
{
5537
if (waitHandle == null)

test/WebJobs.Script.Tests.Shared/WebJobs.Script.Tests.Shared.projitems

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
<Compile Include="$(MSBuildThisFileDirectory)TestTraits.cs" />
3232
<Compile Include="$(MSBuildThisFileDirectory)TestHandler.cs" />
3333
<Compile Include="$(MSBuildThisFileDirectory)TestHelpers.cs" />
34+
<Compile Include="$(MSBuildThisFileDirectory)TestHelpers.Constants.cs" />
3435
<Compile Include="$(MSBuildThisFileDirectory)TestHelpers.Functions.cs" />
3536
<Compile Include="$(MSBuildThisFileDirectory)TestInvoker.cs" />
3637
<Compile Include="$(MSBuildThisFileDirectory)TestTelemetryChannel.cs" />

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
using Microsoft.Azure.WebJobs.Script.WebHost.Models;
2020
using Microsoft.Azure.WebJobs.Script.WebHost.Properties;
2121
using Microsoft.Azure.WebJobs.Script.WebHost.Security;
22-
using Microsoft.Azure.WebJobs.Script.WebHost.Storage;
2322
using Microsoft.Extensions.Logging;
2423
using Microsoft.WebJobs.Script.Tests;
2524
using Moq;
@@ -34,8 +33,6 @@ public class SecretManagerTests
3433
{
3534
private const int TestSentinelWatcherInitializationDelayMS = 50;
3635

37-
[SuppressMessage("Microsoft.Security", "CS002:SecretInNextLine", Justification="Fake key for testing purposes.")]
38-
private const string TestEncryptionKey = "/a/vXvWJ3Hzgx4PFxlDUJJhQm5QVyGiu0NNLFm/ZMMg=";
3936
private readonly HostNameProvider _hostNameProvider;
4037
private readonly TestEnvironment _testEnvironment;
4138
private readonly TestLoggerProvider _loggerProvider;
@@ -63,7 +60,7 @@ public async Task CachedSecrets_UsedWhenPresent()
6360
{
6461
string startupContextPath = Path.Combine(directory.Path, Guid.NewGuid().ToString());
6562
_testEnvironment.SetEnvironmentVariable(EnvironmentSettingNames.AzureWebsiteStartupContextCache, startupContextPath);
66-
_testEnvironment.SetEnvironmentVariable(EnvironmentSettingNames.WebSiteAuthEncryptionKey, TestEncryptionKey);
63+
_testEnvironment.SetEnvironmentVariable(EnvironmentSettingNames.WebSiteAuthEncryptionKey, TestHelpers.EncryptionKey);
6764

6865
WriteStartContextCache(startupContextPath);
6966

@@ -120,7 +117,7 @@ public async Task GetAuthorizationLevelOrNullAsync_ReturnsExpectedResult(string
120117
{
121118
string startupContextPath = Path.Combine(directory.Path, Guid.NewGuid().ToString());
122119
_testEnvironment.SetEnvironmentVariable(EnvironmentSettingNames.AzureWebsiteStartupContextCache, startupContextPath);
123-
_testEnvironment.SetEnvironmentVariable(EnvironmentSettingNames.WebSiteAuthEncryptionKey, TestEncryptionKey);
120+
_testEnvironment.SetEnvironmentVariable(EnvironmentSettingNames.WebSiteAuthEncryptionKey, TestHelpers.EncryptionKey);
124121

125122
WriteStartContextCache(startupContextPath);
126123

@@ -181,7 +178,7 @@ private FunctionAppSecrets WriteStartContextCache(string path)
181178
};
182179

183180
string json = JsonConvert.SerializeObject(context);
184-
var encryptionKey = Convert.FromBase64String(TestEncryptionKey);
181+
var encryptionKey = Convert.FromBase64String(TestHelpers.EncryptionKey);
185182
string encryptedJson = SimpleWebTokenHelper.Encrypt(json, encryptionKey);
186183

187184
File.WriteAllText(path, encryptedJson);

test/WebJobs.Script.Tests/StartupContextProviderTests.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@ namespace Microsoft.Azure.WebJobs.Script.Tests
2020
{
2121
public class StartupContextProviderTests
2222
{
23-
[SuppressMessage("Microsoft.Security", "CS002:SecretInNextLine", Justification="Fake key for testing purposes.")]
24-
private const string TestEncryptionKey = "/a/vXvWJ3Hzgx4PFxlDUJJhQm5QVyGiu0NNLFm/ZMMg=";
25-
2623
private readonly FunctionAppSecrets _secrets;
2724
private readonly StartupContextProvider _startupContextProvider;
2825
private readonly TestEnvironment _environment;
@@ -72,7 +69,7 @@ public StartupContextProviderTests()
7269
_loggerProvider = new TestLoggerProvider();
7370
loggerFactory.AddProvider(_loggerProvider);
7471

75-
_environment.SetEnvironmentVariable(EnvironmentSettingNames.WebSiteAuthEncryptionKey, TestEncryptionKey);
72+
_environment.SetEnvironmentVariable(EnvironmentSettingNames.WebSiteAuthEncryptionKey, TestHelpers.EncryptionKey);
7673

7774
_startupContextProvider = new StartupContextProvider(_environment, loggerFactory.CreateLogger<StartupContextProvider>());
7875
}

0 commit comments

Comments
 (0)