Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v3.x] Address cred scan and test secrets #10264

Merged
merged 1 commit into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ namespace Microsoft.Azure.WebJobs.Script.Tests.Managment
[Trait(TestTraits.Group, TestTraits.ContainerInstanceTests)]
public class InstanceControllerTests
{
[SuppressMessage("Microsoft.Security", "CS002:SecretInNextLine", Justification = "Fake key for testing purposes.")]
private const string ContainerEncryptionKey = "/a/vXvWJ3Hzgx4PFxlDUJJhQm5QVyGiu0NNLFm/ZMMg=";

private readonly TestOptionsFactory<ScriptApplicationHostOptions> _optionsFactory = new TestOptionsFactory<ScriptApplicationHostOptions>(new ScriptApplicationHostOptions());
private readonly Mock<IRunFromPackageHandler> _runFromPackageHandler;

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

var encryptedHostAssignmentValue = SimpleWebTokenHelper.Encrypt(JsonConvert.SerializeObject(hostAssignmentContext), ContainerEncryptionKey.ToKeyBytes());
var encryptedHostAssignmentValue = SimpleWebTokenHelper.Encrypt(JsonConvert.SerializeObject(hostAssignmentContext), TestHelpers.EncryptionKey.ToKeyBytes());

var encryptedHostAssignmentContext = new EncryptedHostAssignmentContext()
{
EncryptedContext = encryptedHostAssignmentValue
};

environment.SetEnvironmentVariable(EnvironmentSettingNames.ContainerEncryptionKey, ContainerEncryptionKey);
environment.SetEnvironmentVariable(EnvironmentSettingNames.ContainerEncryptionKey, TestHelpers.EncryptionKey);

IActionResult result = await instanceController.Assign(encryptedHostAssignmentContext);

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

var encryptedHostAssignmentValue = SimpleWebTokenHelper.Encrypt(JsonConvert.SerializeObject(hostAssignmentContext), ContainerEncryptionKey.ToKeyBytes());
var encryptedHostAssignmentValue = SimpleWebTokenHelper.Encrypt(JsonConvert.SerializeObject(hostAssignmentContext), TestHelpers.EncryptionKey.ToKeyBytes());

var encryptedHostAssignmentContext = new EncryptedHostAssignmentContext()
{
EncryptedContext = encryptedHostAssignmentValue
};

environment.SetEnvironmentVariable(EnvironmentSettingNames.ContainerEncryptionKey, ContainerEncryptionKey);
environment.SetEnvironmentVariable(EnvironmentSettingNames.ContainerEncryptionKey, TestHelpers.EncryptionKey);

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

var encryptedHostAssignmentValue = SimpleWebTokenHelper.Encrypt(JsonConvert.SerializeObject(hostAssignmentContext), ContainerEncryptionKey.ToKeyBytes());
var encryptedHostAssignmentValue = SimpleWebTokenHelper.Encrypt(JsonConvert.SerializeObject(hostAssignmentContext), TestHelpers.EncryptionKey.ToKeyBytes());

var encryptedHostAssignmentContext = new EncryptedHostAssignmentContext()
{
EncryptedContext = encryptedHostAssignmentValue
};

environment.SetEnvironmentVariable(EnvironmentSettingNames.ContainerEncryptionKey, ContainerEncryptionKey);
environment.SetEnvironmentVariable(EnvironmentSettingNames.ContainerEncryptionKey, TestHelpers.EncryptionKey);

await instanceController.Assign(encryptedHostAssignmentContext);
Assert.Null(startupContextProvider.Context);
Expand Down Expand Up @@ -252,14 +249,14 @@ public async Task Assignment_Invokes_InstanceManager_Methods_For_Warmup_Requests

var encryptedHostAssignmentValue =
SimpleWebTokenHelper.Encrypt(JsonConvert.SerializeObject(hostAssignmentContext),
ContainerEncryptionKey.ToKeyBytes());
TestHelpers.EncryptionKey.ToKeyBytes());

var encryptedHostAssignmentContext = new EncryptedHostAssignmentContext()
{
EncryptedContext = encryptedHostAssignmentValue
};

environment.SetEnvironmentVariable(EnvironmentSettingNames.ContainerEncryptionKey, ContainerEncryptionKey);
environment.SetEnvironmentVariable(EnvironmentSettingNames.ContainerEncryptionKey, TestHelpers.EncryptionKey);

await instanceController.Assign(encryptedHostAssignmentContext);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ namespace Microsoft.Azure.WebJobs.Script.Tests.Managment
[Trait(TestTraits.Group, TestTraits.ContainerInstanceTests)]
public class KubernetesPodControllerTests
{
[SuppressMessage("Microsoft.Security", "CS002:SecretInNextLine", Justification = "Fake key for testing purposes.")]
private const string PodEncryptionKey = "/a/vXvWJ3Hzgx4PFxlDUJJhQm5QVyGiu0NNLFm/ZMMg=";

private readonly TestOptionsFactory<ScriptApplicationHostOptions> _optionsFactory = new TestOptionsFactory<ScriptApplicationHostOptions>(new ScriptApplicationHostOptions());

[Fact]
Expand Down Expand Up @@ -74,14 +71,14 @@ public async Task Assignment_Succeeds_With_Encryption_Key()
hostAssignmentContext.Secrets = new FunctionAppSecrets();
hostAssignmentContext.IsWarmupRequest = false;

var encryptedHostAssignmentValue = SimpleWebTokenHelper.Encrypt(JsonConvert.SerializeObject(hostAssignmentContext), PodEncryptionKey.ToKeyBytes());
var encryptedHostAssignmentValue = SimpleWebTokenHelper.Encrypt(JsonConvert.SerializeObject(hostAssignmentContext), TestHelpers.EncryptionKey.ToKeyBytes());

var encryptedHostAssignmentContext = new EncryptedHostAssignmentContext()
{
EncryptedContext = encryptedHostAssignmentValue
};

environment.SetEnvironmentVariable(EnvironmentSettingNames.PodEncryptionKey, PodEncryptionKey);
environment.SetEnvironmentVariable(EnvironmentSettingNames.PodEncryptionKey, TestHelpers.EncryptionKey);
environment.SetEnvironmentVariable(EnvironmentSettingNames.KubernetesServiceHost, "http://localhost:80");
environment.SetEnvironmentVariable(EnvironmentSettingNames.PodNamespace, "k8se-apps");

Expand Down Expand Up @@ -131,7 +128,7 @@ public async Task Assignment_Fails_Without_Encryption_Key()
hostAssignmentContext.Secrets = new FunctionAppSecrets();
hostAssignmentContext.IsWarmupRequest = false;

var encryptedHostAssignmentValue = SimpleWebTokenHelper.Encrypt(JsonConvert.SerializeObject(hostAssignmentContext), PodEncryptionKey.ToKeyBytes());
var encryptedHostAssignmentValue = SimpleWebTokenHelper.Encrypt(JsonConvert.SerializeObject(hostAssignmentContext), TestHelpers.EncryptionKey.ToKeyBytes());

var encryptedHostAssignmentContext = new EncryptedHostAssignmentContext()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs.Script.WebHost.Management;
Expand All @@ -21,9 +21,6 @@ namespace Microsoft.Azure.WebJobs.Script.Tests.Integration.Management
{
public class MeshServiceClientTests
{
[SuppressMessage("Microsoft.Security", "CS002:SecretInNextLine", Justification = "Fake key for testing purposes.")]
private const string ConnectionString = "DefaultEndpointsProtocol=https;AccountName=storageaccount;AccountKey=whXtW6WP8QTh84TT5wdjgzeFTj7Vc1aOiCVjTXohpE+jALoKOQ9nlQpj5C5zpgseVJxEVbaAhptP5j5DpaLgtA==";

private const string MeshInitUri = "http://localhost:8954/";
private const string ContainerName = "MockContainerName";
private readonly IMeshServiceClient _meshServiceClient;
Expand Down Expand Up @@ -83,9 +80,7 @@ public async Task MountsCifsShare()
StatusCode = HttpStatusCode.OK
});



await _meshServiceClient.MountCifs(ConnectionString, "sharename", "/data");
await _meshServiceClient.MountCifs(TestHelpers.StorageConnectionString, "sharename", "/data");

await Task.Delay(500);

Expand Down
46 changes: 46 additions & 0 deletions test/WebJobs.Script.Tests.Shared/TestHelpers.Constants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace Microsoft.Azure.WebJobs.Script.Tests
{
public static partial class TestHelpers
{
#if DEBUG
public const string BuildConfig = "debug";
#else
public const string BuildConfig = "release";
#endif
// Not a real storage account key.
public static readonly string StorageAccountKey = Convert.ToBase64String(Encoding.UTF8.GetBytes("PLACEHOLDER"));
jviau marked this conversation as resolved.
Show resolved Hide resolved

// Not a real connection string.
public static readonly string StorageConnectionString = $"DefaultEndpointsProtocol=http;AccountName=fakeaccount;AccountKey={StorageAccountKey}";

private static readonly Lazy<string> _encryptionKey = new Lazy<string>(
() =>
{
using Aes aes = Aes.Create();
aes.GenerateKey();
return Convert.ToBase64String(aes.Key);
});

public static string EncryptionKey => _encryptionKey.Value;

/// <summary>
/// Gets the common root directory that functions tests create temporary directories under.
/// This enables us to clean up test files by deleting this single directory.
/// </summary>
public static string FunctionsTestDirectory
{
get
{
return Path.Combine(Path.GetTempPath(), "FunctionsTest");
}
}
}
}
18 changes: 0 additions & 18 deletions test/WebJobs.Script.Tests.Shared/TestHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,9 @@ namespace Microsoft.Azure.WebJobs.Script.Tests
{
public static partial class TestHelpers
{
#if DEBUG
public const string BuildConfig = "debug";
#else
public const string BuildConfig = "release";
#endif

private const string Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
private static readonly Random Random = new Random();

/// <summary>
/// Gets the common root directory that functions tests create temporary directories under.
/// This enables us to clean up test files by deleting this single directory.
/// </summary>
public static string FunctionsTestDirectory
{
get
{
return Path.Combine(Path.GetTempPath(), "FunctionsTest");
}
}

public static Task WaitOneAsync(this WaitHandle waitHandle)
{
if (waitHandle == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
<Compile Include="$(MSBuildThisFileDirectory)TestTraits.cs" />
<Compile Include="$(MSBuildThisFileDirectory)TestHandler.cs" />
<Compile Include="$(MSBuildThisFileDirectory)TestHelpers.cs" />
<Compile Include="$(MSBuildThisFileDirectory)TestHelpers.Constants.cs" />
<Compile Include="$(MSBuildThisFileDirectory)TestHelpers.Functions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)TestInvoker.cs" />
<Compile Include="$(MSBuildThisFileDirectory)TestTelemetryChannel.cs" />
Expand Down
9 changes: 3 additions & 6 deletions test/WebJobs.Script.Tests/Security/SecretManagerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
using Microsoft.Azure.WebJobs.Script.WebHost.Models;
using Microsoft.Azure.WebJobs.Script.WebHost.Properties;
using Microsoft.Azure.WebJobs.Script.WebHost.Security;
using Microsoft.Azure.WebJobs.Script.WebHost.Storage;
using Microsoft.Extensions.Logging;
using Microsoft.WebJobs.Script.Tests;
using Moq;
Expand All @@ -34,8 +33,6 @@ public class SecretManagerTests
{
private const int TestSentinelWatcherInitializationDelayMS = 50;

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

WriteStartContextCache(startupContextPath);

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

WriteStartContextCache(startupContextPath);

Expand Down Expand Up @@ -181,7 +178,7 @@ private FunctionAppSecrets WriteStartContextCache(string path)
};

string json = JsonConvert.SerializeObject(context);
var encryptionKey = Convert.FromBase64String(TestEncryptionKey);
var encryptionKey = Convert.FromBase64String(TestHelpers.EncryptionKey);
string encryptedJson = SimpleWebTokenHelper.Encrypt(json, encryptionKey);

File.WriteAllText(path, encryptedJson);
Expand Down
5 changes: 1 addition & 4 deletions test/WebJobs.Script.Tests/StartupContextProviderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ namespace Microsoft.Azure.WebJobs.Script.Tests
{
public class StartupContextProviderTests
{
[SuppressMessage("Microsoft.Security", "CS002:SecretInNextLine", Justification="Fake key for testing purposes.")]
private const string TestEncryptionKey = "/a/vXvWJ3Hzgx4PFxlDUJJhQm5QVyGiu0NNLFm/ZMMg=";

private readonly FunctionAppSecrets _secrets;
private readonly StartupContextProvider _startupContextProvider;
private readonly TestEnvironment _environment;
Expand Down Expand Up @@ -72,7 +69,7 @@ public StartupContextProviderTests()
_loggerProvider = new TestLoggerProvider();
loggerFactory.AddProvider(_loggerProvider);

_environment.SetEnvironmentVariable(EnvironmentSettingNames.WebSiteAuthEncryptionKey, TestEncryptionKey);
_environment.SetEnvironmentVariable(EnvironmentSettingNames.WebSiteAuthEncryptionKey, TestHelpers.EncryptionKey);

_startupContextProvider = new StartupContextProvider(_environment, loggerFactory.CreateLogger<StartupContextProvider>());
}
Expand Down
Loading