Skip to content

Commit

Permalink
Merge branch '26-password-environment'
Browse files Browse the repository at this point in the history
  • Loading branch information
christianspecht committed Apr 6, 2020
2 parents 563b341 + d6fe3ab commit ae7edc3
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using ScmBackup.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xunit;

namespace ScmBackup.Tests.ConfigTests
{
public class EnvironmentVariableConfigReaderTests
{
private IConfigReader sut;
private FakeConfigReader reader;

public EnvironmentVariableConfigReaderTests()
{
reader = new FakeConfigReader();
reader.SetDefaultFakeConfig();
sut = new EnvironmentVariableConfigReader(reader);

Environment.SetEnvironmentVariable("scmbackup_test", "foo");
}

[Theory]
[InlineData("%scmbackup_test%bar", "foobar")] // part of the password
[InlineData("%scmbackup_test%", "foo")] // whole password
public void ReplacesInPassword(string originalPw, string changedPw)
{
reader.FakeConfig.Sources.First().Password = originalPw;

var result = sut.ReadConfig();

Assert.Equal(changedPw, result.Sources.First().Password);
}

[Fact]
public void DoesNothingWhenPasswordContainsNoVariable()
{
reader.FakeConfig.Sources.First().Password = "bar";

var result = sut.ReadConfig();

Assert.Equal("bar", result.Sources.First().Password);
}
}
}
4 changes: 3 additions & 1 deletion src/ScmBackup/CompositionRoot/Bootstrapper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using ScmBackup.Hosters;
using ScmBackup.Configuration;
using ScmBackup.Hosters;
using ScmBackup.Http;
using ScmBackup.Loggers;
using ScmBackup.Scm;
Expand Down Expand Up @@ -34,6 +35,7 @@ public static Container BuildContainer()
container.Register<IConfigBackupMaker, ConfigBackupMaker>();

container.Register<IConfigReader, ConfigReader>(Lifestyle.Singleton);
container.RegisterDecorator<IConfigReader, EnvironmentVariableConfigReader>(Lifestyle.Singleton);
container.RegisterDecorator<IConfigReader, ValidatingConfigReader>(Lifestyle.Singleton);

container.Register<IHttpRequest, HttpRequest>();
Expand Down
41 changes: 41 additions & 0 deletions src/ScmBackup/Configuration/EnvironmentVariableConfigReader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace ScmBackup.Configuration
{
/// <summary>
/// decorator for ConfigReader, replaces %foo% values with the respective environment variables
/// </summary>
internal class EnvironmentVariableConfigReader : IConfigReader
{
private readonly IConfigReader configReader;
private Config config = null;

public EnvironmentVariableConfigReader(IConfigReader configReader)
{
this.configReader = configReader;
}

public Config ReadConfig()
{
if (this.config != null)
{
return this.config;
}

var config = this.configReader.ReadConfig();

foreach (var source in config.Sources)
{
if (!string.IsNullOrWhiteSpace(source.Password))
{
source.Password = Environment.ExpandEnvironmentVariables(source.Password);
}
}

this.config = config;
return config;
}
}
}

0 comments on commit ae7edc3

Please sign in to comment.