-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch '26-password-environment'
- Loading branch information
Showing
3 changed files
with
90 additions
and
1 deletion.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
src/ScmBackup.Tests/ConfigTests/EnvironmentVariableConfigReaderTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/ScmBackup/Configuration/EnvironmentVariableConfigReader.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |