Skip to content
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
2 changes: 2 additions & 0 deletions src/Agent.Sdk/Util/ILoggedSecretMasker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ namespace Agent.Sdk.Util
/// </summary>
public interface ILoggedSecretMasker : ISecretMasker
{
int MinSecretLengthLimit { get; }

void AddRegex(String pattern, string origin);
void AddValue(String value, string origin);
void AddValueEncoder(ValueEncoder encoder, string origin);
Expand Down
18 changes: 9 additions & 9 deletions src/Agent.Sdk/Util/LoggedSecretMasker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ public class LoggedSecretMasker : ILoggedSecretMasker
private ISecretMasker _secretMasker;
private ITraceWriter _trace;

// We don't allow to skip secrets longer than 4 characters.
private readonly short _maxMinSecretLength = 4;

private void Trace(string msg)
{
this._trace?.Info(msg);
Expand Down Expand Up @@ -73,6 +70,9 @@ public void AddRegex(string pattern, string origin)
AddRegex(pattern);
}

// We don't allow to skip secrets longer than 4 characters.
public int MinSecretLengthLimit => 4;

public int MinSecretLength
{
get
Expand All @@ -81,14 +81,14 @@ public int MinSecretLength
}
set
{
if (value > _maxMinSecretLength)
if (value > MinSecretLengthLimit)
{
_secretMasker.MinSecretLength = _maxMinSecretLength;

throw new ArgumentException($"Not allowed minimum secret length. Set max value: {_maxMinSecretLength}");
_secretMasker.MinSecretLength = MinSecretLengthLimit;
}
else
{
_secretMasker.MinSecretLength = value;
}

_secretMasker.MinSecretLength = value;
}
}

Expand Down
23 changes: 10 additions & 13 deletions src/Agent.Worker/ExecutionContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -518,26 +518,23 @@ public void InitializeJob(Pipelines.AgentJobRequestMessage message, Cancellation
// Prepend Path
PrependPath = new List<string>();

// Docker (JobContainer)
string imageName = Variables.Get("_PREVIEW_VSTS_DOCKER_IMAGE");
if (string.IsNullOrEmpty(imageName))
{
imageName = Environment.GetEnvironmentVariable("_PREVIEW_VSTS_DOCKER_IMAGE");
}

var minSecretLen = AgentKnobs.MaskedSecretMinLength.GetValue(this).AsInt();
HostContext.SecretMasker.MinSecretLength = minSecretLen;

try
if (HostContext.SecretMasker.MinSecretLength < minSecretLen)
{
this.HostContext.SecretMasker.MinSecretLength = minSecretLen;
warnings.Add(StringUtil.Loc("MinSecretsLengtLimitWarning", HostContext.SecretMasker.MinSecretLength));
}
catch (ArgumentException ex)

HostContext.SecretMasker.RemoveShortSecretsFromDictionary();

// Docker (JobContainer)
string imageName = Variables.Get("_PREVIEW_VSTS_DOCKER_IMAGE");
if (string.IsNullOrEmpty(imageName))
{
warnings.Add(ex.Message);
imageName = Environment.GetEnvironmentVariable("_PREVIEW_VSTS_DOCKER_IMAGE");
}

this.HostContext.SecretMasker.RemoveShortSecretsFromDictionary();

Containers = new List<ContainerInfo>();
_defaultStepTarget = null;
_currentStepTarget = null;
Expand Down
1 change: 1 addition & 0 deletions src/Misc/layoutbin/en-US/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,7 @@
"MinRequiredDockerServerVersion": "Min required docker engine API server version is '{0}', your docker ('{1}') server version is '{2}'",
"MinRequiredGitLfsVersion": "Min required git-lfs version is '{0}', your git-lfs ('{1}') version is '{2}'",
"MinRequiredGitVersion": "Min required git version is '{0}', your git ('{1}') version is '{2}'",
"MinSecretsLengtLimitWarning": "The value of the minimum length of the secrets is too high. Maximum value is set: {0}",
"MissingAgent": "The agent no longer exists on the server. Please reconfigure the agent.",
"MissingAttachmentFile": "Cannot upload task attachment file, attachment file location is not specified or attachment file does not exist on disk.",
"MissingAttachmentName": "Can't add task attachment, attachment name is not provided.",
Expand Down
16 changes: 3 additions & 13 deletions src/Test/L0/SecretMaskerTests/LoggedSecretMaskerL0.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,27 +85,17 @@ public void LoggedSecretMasker_Skipping_ShortSecrets()
Assert.Equal("123", resultMessage);
}

[Fact]
[Trait("Level", "L0")]
[Trait("Category", "SecretMasker")]
public void LoggedSecretMasker_Throws_Exception_If_Large_MinSecretLength_Specified()
{
var lsm = new LoggedSecretMasker(_secretMasker);

Assert.Throws<ArgumentException>(() => lsm.MinSecretLength = 5);
}

[Fact]
[Trait("Level", "L0")]
[Trait("Category", "SecretMasker")]
public void LoggedSecretMasker_Sets_MinSecretLength_To_MaxValue()
{
var lsm = new LoggedSecretMasker(_secretMasker);
var expectedMinSecretsLengthValue = lsm.MinSecretLengthLimit;

try { lsm.MinSecretLength = 5; }
catch (ArgumentException) { }
lsm.MinSecretLength = 5;

Assert.Equal(4, lsm.MinSecretLength);
Assert.Equal(expectedMinSecretsLengthValue, lsm.MinSecretLength);
}

[Fact]
Expand Down