Skip to content

Commit

Permalink
Converted SecretDeserializer
Browse files Browse the repository at this point in the history
  • Loading branch information
adamconnelly committed Feb 10, 2020
1 parent 3756fa0 commit 84edbc8
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,24 @@ namespace Promitor.Core.Scraping.Configuration.Serialization.v1.Core
{
public class SecretDeserializer : Deserializer<SecretV1>
{
private const string RawValueTag = "rawValue";
private const string EnvironmentVariableTag = "environmentVariable";

public SecretDeserializer(ILogger<SecretDeserializer> logger) : base(logger)
{
MapOptional(secret => secret.RawValue);
MapOptional(secret => secret.EnvironmentVariable);
}

public override SecretV1 Deserialize(YamlMappingNode node, IErrorReporter errorReporter)
{
var rawValue = node.GetString(RawValueTag);
var environmentVariable = node.GetString(EnvironmentVariableTag);
var secret = base.Deserialize(node, errorReporter);

var secret = new SecretV1
if (string.IsNullOrEmpty(secret.EnvironmentVariable) && string.IsNullOrEmpty(secret.RawValue))
{
RawValue = rawValue,
EnvironmentVariable = environmentVariable
};
errorReporter.ReportError(node, "Either 'environmentVariable' or 'rawValue' must be supplied for a secret.");
}

if (!string.IsNullOrEmpty(secret.RawValue) && !string.IsNullOrEmpty(secret.EnvironmentVariable))
{
Logger.LogWarning("Secret with environment variable '{EnvironmentVariable}' also has a rawValue provided.", secret.EnvironmentVariable);
errorReporter.ReportWarning(node, $"Secret with environment variable '{secret.EnvironmentVariable}' also has a rawValue provided.");
}

return secret;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.ComponentModel;
using Microsoft.Extensions.Logging.Abstractions;
using Moq;
using Promitor.Core.Scraping.Configuration.Serialization;
using Promitor.Core.Scraping.Configuration.Serialization.v1.Core;
using Xunit;

Expand Down Expand Up @@ -52,5 +54,35 @@ public void Deserialize_EnvironmentVariableNotSupplied_Null()
"rawValue: abc123",
s => s.EnvironmentVariable);
}

[Fact]
public void Deserialize_EnvironmentVariableAndRawValueNotSupplied_ReportsError()
{
// Arrange
var node = YamlUtils.CreateYamlNode("name: 123");
var errorReporter = new Mock<ErrorReporter>();

// Act
_deserializer.Deserialize(node, errorReporter.Object);

// Assert
errorReporter.Verify(r => r.ReportError(node, "Either 'environmentVariable' or 'rawValue' must be supplied for a secret."));
}

[Fact]
public void Deserialize_EnvironmentVariableAndRawValueBothSupplied_ReportsWarning()
{
// Arrange
var node = YamlUtils.CreateYamlNode(
@"rawValue: 123
environmentVariable: PROMITOR_SECRET");
var errorReporter = new Mock<ErrorReporter>();

// Act
_deserializer.Deserialize(node, errorReporter.Object);

// Assert
errorReporter.Verify(r => r.ReportWarning(node, "Secret with environment variable 'PROMITOR_SECRET' also has a rawValue provided."));
}
}
}

0 comments on commit 84edbc8

Please sign in to comment.