public async Task SetMasterKey_WithoutProvidedKey_GeneratesKeyAndPersistsFile() { using (var directory = new TempDirectory()) { Mock <IKeyValueConverterFactory> mockValueConverterFactory = GetConverterFactoryMock(false); KeyOperationResult result; var traceWriter = new TestTraceWriter(System.Diagnostics.TraceLevel.Verbose); ISecretsRepository repository = new FileSystemSecretsRepository(directory.Path); using (var secretManager = new SecretManager(repository, mockValueConverterFactory.Object, null)) { result = await secretManager.SetMasterKeyAsync(); } bool functionSecretsExists = File.Exists(Path.Combine(directory.Path, "testfunction.json")); string secretsJson = File.ReadAllText(Path.Combine(directory.Path, ScriptConstants.HostMetadataFileName)); HostSecrets persistedSecrets = ScriptSecretSerializer.DeserializeSecrets <HostSecrets>(secretsJson); Assert.NotNull(persistedSecrets); Assert.NotNull(persistedSecrets.MasterKey); Assert.Equal(OperationResult.Created, result.Result); Assert.Equal(result.Secret, persistedSecrets.MasterKey.Value); } }
public async Task SetMasterKey_WithProvidedKey_UsesProvidedKeyAndPersistsFile() { string testSecret = "abcde0123456789abcde0123456789abcde0123456789"; using (var directory = new TempDirectory()) { Mock <IKeyValueConverterFactory> mockValueConverterFactory = GetConverterFactoryMock(false); KeyOperationResult result; ISecretsRepository repository = new FileSystemSecretsRepository(directory.Path); using (var secretManager = new SecretManager(repository, mockValueConverterFactory.Object, NullLogger.Instance)) { result = await secretManager.SetMasterKeyAsync(testSecret); } bool functionSecretsExists = File.Exists(Path.Combine(directory.Path, "testfunction.json")); string secretsJson = File.ReadAllText(Path.Combine(directory.Path, ScriptConstants.HostMetadataFileName)); HostSecrets persistedSecrets = ScriptSecretSerializer.DeserializeSecrets <HostSecrets>(secretsJson); Assert.NotNull(persistedSecrets); Assert.NotNull(persistedSecrets.MasterKey); Assert.Equal(OperationResult.Updated, result.Result); Assert.Equal(testSecret, result.Secret); } }
public async Task SetMasterKey_WithoutProvidedKey_GeneratesKeyAndPersistsFile() { using (var directory = new TempDirectory()) { string expectedTraceMessage = "Master key Created"; Mock <IKeyValueConverterFactory> mockValueConverterFactory = GetConverterFactoryMock(false); KeyOperationResult result; var traceWriter = new TestTraceWriter(TraceLevel.Verbose); ISecretsRepository repository = new FileSystemSecretsRepository(directory.Path); using (var secretManager = new SecretManager(repository, mockValueConverterFactory.Object, traceWriter)) { result = await secretManager.SetMasterKeyAsync(); } bool functionSecretsExists = File.Exists(Path.Combine(directory.Path, "testfunction.json")); string secretsJson = File.ReadAllText(Path.Combine(directory.Path, ScriptConstants.HostMetadataFileName)); HostSecrets persistedSecrets = ScriptSecretSerializer.DeserializeSecrets <HostSecrets>(secretsJson); Assert.NotNull(persistedSecrets); Assert.NotNull(persistedSecrets.MasterKey); Assert.Equal(OperationResult.Created, result.Result); Assert.Equal(result.Secret, persistedSecrets.MasterKey.Value); Assert.True(traceWriter.Traces.Any(t => t.Level == TraceLevel.Info && t.Message.IndexOf(expectedTraceMessage) > -1), "Expected Trace message not found"); } }