Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Refactor] Update MinSecretLength property. #4137

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -11,6 +11,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 @@ -14,9 +14,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 @@ -76,6 +73,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 @@ -84,14 +84,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 @@ -401,6 +401,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 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 @@ -77,27 +77,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(new 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(new SecretMasker());
var expectedMinSecretsLengthValue = 4;

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

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

[Fact]
Expand Down