Skip to content

Commit

Permalink
Fix that plugins ignores proxy configuration (#4260)
Browse files Browse the repository at this point in the history
* Fix plugins that plugins ignores proxyconfig

* Update src/Agent.Sdk/AgentWebProxy.cs

Co-authored-by: Konstantin Tyukalov <52399739+KonstantinTyukalov@users.noreply.github.com>

* Update AgentWebProxy.cs

* Update RepositoryPluginL0.cs

---------

Co-authored-by: Konstantin Tyukalov <52399739+KonstantinTyukalov@users.noreply.github.com>
  • Loading branch information
kirill-ivlev and KonstantinTyukalov authored May 3, 2023
1 parent 0811269 commit e0dd7e0
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 20 deletions.
4 changes: 4 additions & 0 deletions src/Agent.Sdk/AgentWebProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ namespace Agent.Sdk
{
public class AgentWebProxySettings
{
public static string AgentProxyUrlKey = "Agent.ProxyUrl".ToLower();
public static string AgentProxyUsernameKey = "Agent.ProxyUsername".ToLower();
public static string AgentProxyPasswordKey = "Agent.ProxyPassword".ToLower();
public static string AgentProxyBypassListKey = "Agent.ProxyBypassList".ToLower();
public string ProxyAddress { get; set; }
public string ProxyUsername { get; set; }
public string ProxyPassword { get; set; }
Expand Down
8 changes: 4 additions & 4 deletions src/Agent.Sdk/CommandPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,12 +187,12 @@ private AgentCertificateSettings GetCertConfiguration()

private AgentWebProxySettings GetProxyConfiguration()
{
string proxyUrl = this.Variables.GetValueOrDefault("Agent.ProxyUrl")?.Value;
string proxyUrl = this.Variables.GetValueOrDefault(AgentWebProxySettings.AgentProxyUrlKey)?.Value;
if (!string.IsNullOrEmpty(proxyUrl))
{
string proxyUsername = this.Variables.GetValueOrDefault("Agent.ProxyUsername")?.Value;
string proxyPassword = this.Variables.GetValueOrDefault("Agent.ProxyPassword")?.Value;
List<string> proxyBypassHosts = StringUtil.ConvertFromJson<List<string>>(this.Variables.GetValueOrDefault("Agent.ProxyBypassList")?.Value ?? "[]");
string proxyUsername = this.Variables.GetValueOrDefault(AgentWebProxySettings.AgentProxyUsernameKey)?.Value;
string proxyPassword = this.Variables.GetValueOrDefault(AgentWebProxySettings.AgentProxyPasswordKey)?.Value;
List<string> proxyBypassHosts = StringUtil.ConvertFromJson<List<string>>(this.Variables.GetValueOrDefault(AgentWebProxySettings.AgentProxyBypassListKey)?.Value ?? "[]");
return new AgentWebProxySettings()
{
ProxyAddress = proxyUrl,
Expand Down
22 changes: 14 additions & 8 deletions src/Agent.Sdk/LogPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ public class AgentLogPluginHostContext
public List<Pipelines.RepositoryResource> Repositories { get; set; }
public Dictionary<string, VariableValue> Variables { get; set; }
public Dictionary<string, Pipelines.TaskStepDefinitionReference> Steps { get; set; }
public AgentWebProxySettings WebProxySettings { get; private set; }

[JsonIgnore]
public VssConnection VssConnection
Expand Down Expand Up @@ -187,12 +188,12 @@ private VssConnection InitializeVssConnection()
}
}

var proxySetting = GetProxyConfiguration();
if (proxySetting != null)
WebProxySettings = GetProxyConfiguration();
if (WebProxySettings != null)
{
if (!string.IsNullOrEmpty(proxySetting.ProxyAddress))
if (!string.IsNullOrEmpty(WebProxySettings.ProxyAddress))
{
VssHttpMessageHandler.DefaultWebProxy = new AgentWebProxy(proxySetting.ProxyAddress, proxySetting.ProxyUsername, proxySetting.ProxyPassword, proxySetting.ProxyBypassList);
VssHttpMessageHandler.DefaultWebProxy = new AgentWebProxy(WebProxySettings.ProxyAddress, WebProxySettings.ProxyUsername, WebProxySettings.ProxyPassword, WebProxySettings.ProxyBypassList);
}
}

Expand Down Expand Up @@ -241,12 +242,12 @@ private AgentCertificateSettings GetCertConfiguration()

private AgentWebProxySettings GetProxyConfiguration()
{
string proxyUrl = this.Variables.GetValueOrDefault("Agent.ProxyUrl")?.Value;
string proxyUrl = this.Variables.GetValueOrDefault(AgentWebProxySettings.AgentProxyUrlKey)?.Value;
if (!string.IsNullOrEmpty(proxyUrl))
{
string proxyUsername = this.Variables.GetValueOrDefault("Agent.ProxyUsername")?.Value;
string proxyPassword = this.Variables.GetValueOrDefault("Agent.ProxyPassword")?.Value;
List<string> proxyBypassHosts = StringUtil.ConvertFromJson<List<string>>(this.Variables.GetValueOrDefault("Agent.ProxyBypassList")?.Value ?? "[]");
string proxyUsername = this.Variables.GetValueOrDefault(AgentWebProxySettings.AgentProxyUsernameKey)?.Value;
string proxyPassword = this.Variables.GetValueOrDefault(AgentWebProxySettings.AgentProxyPasswordKey)?.Value;
List<string> proxyBypassHosts = StringUtil.ConvertFromJson<List<string>>(this.Variables.GetValueOrDefault(AgentWebProxySettings.AgentProxyBypassListKey)?.Value ?? "[]");
return new AgentWebProxySettings()
{
ProxyAddress = proxyUrl,
Expand Down Expand Up @@ -275,6 +276,11 @@ public class AgentLogPluginHost
private int _shortCircuitThreshold;
private int _shortCircuitMonitorFrequency;

public Dictionary<string, IAgentLogPluginContext> PluginContexts
{
get => _pluginContexts;
}

public AgentLogPluginHost(
AgentLogPluginHostContext hostContext,
List<IAgentLogPlugin> plugins,
Expand Down
17 changes: 9 additions & 8 deletions src/Agent.Sdk/TaskPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public AgentTaskPluginExecutionContext(ITraceWriter trace)
public Dictionary<string, string> Inputs { get; set; }
public ContainerInfo Container { get; set; }
public Dictionary<string, string> JobSettings { get; set; }
public AgentWebProxySettings WebProxySettings { get; private set; }

[JsonIgnore]
public VssConnection VssConnection
Expand Down Expand Up @@ -111,12 +112,12 @@ public VssConnection InitializeVssConnection()
}
}

var proxySetting = GetProxyConfiguration();
if (proxySetting != null)
WebProxySettings = GetProxyConfiguration();
if (WebProxySettings != null)
{
if (!string.IsNullOrEmpty(proxySetting.ProxyAddress))
if (!string.IsNullOrEmpty(WebProxySettings.ProxyAddress))
{
VssHttpMessageHandler.DefaultWebProxy = new AgentWebProxy(proxySetting.ProxyAddress, proxySetting.ProxyUsername, proxySetting.ProxyPassword, proxySetting.ProxyBypassList);
VssHttpMessageHandler.DefaultWebProxy = new AgentWebProxy(WebProxySettings.ProxyAddress, WebProxySettings.ProxyUsername, WebProxySettings.ProxyPassword, WebProxySettings.ProxyBypassList);
}
}

Expand Down Expand Up @@ -319,12 +320,12 @@ public AgentCertificateSettings GetCertConfiguration()

public AgentWebProxySettings GetProxyConfiguration()
{
string proxyUrl = this.Variables.GetValueOrDefault("Agent.ProxyUrl")?.Value;
string proxyUrl = this.Variables.GetValueOrDefault(AgentWebProxySettings.AgentProxyUrlKey)?.Value;
if (!string.IsNullOrEmpty(proxyUrl))
{
string proxyUsername = this.Variables.GetValueOrDefault("Agent.ProxyUsername")?.Value;
string proxyPassword = this.Variables.GetValueOrDefault("Agent.ProxyPassword")?.Value;
List<string> proxyBypassHosts = StringUtil.ConvertFromJson<List<string>>(this.Variables.GetValueOrDefault("Agent.ProxyBypassList")?.Value ?? "[]");
string proxyUsername = this.Variables.GetValueOrDefault(AgentWebProxySettings.AgentProxyUsernameKey)?.Value;
string proxyPassword = this.Variables.GetValueOrDefault(AgentWebProxySettings.AgentProxyPasswordKey)?.Value;
List<string> proxyBypassHosts = StringUtil.ConvertFromJson<List<string>>(this.Variables.GetValueOrDefault(AgentWebProxySettings.AgentProxyBypassListKey)?.Value ?? "[]");
return new AgentWebProxySettings()
{
ProxyAddress = proxyUrl,
Expand Down
45 changes: 45 additions & 0 deletions src/Test/L0/Plugin/LogPluginHostL0.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using System.Collections.ObjectModel;
using Pipelines = Microsoft.TeamFoundation.DistributedTask.Pipelines;
using Agent.Sdk;
using Microsoft.TeamFoundation.TestManagement.WebApi;

namespace Microsoft.VisualStudio.Services.Agent.Tests.LogPluginHost
{
Expand Down Expand Up @@ -403,6 +404,49 @@ public async Task LogPluginHost_NotInitialized()
}
}

[Fact]
[Trait("Level", "L0")]
[Trait("Category", "Plugin")]
public void LogPluginHost_HandleProxyConfig()
{
using TestHostContext tc = new TestHostContext(this);
TestTrace trace = new TestTrace(tc);
var proxyUrl = "http://example.com:80";
var proxyUser = "proxy_user";
var proxyPassword = "proxy_password";

AgentLogPluginHostContext hostContext = new AgentLogPluginHostContext()
{
Endpoints = new List<ServiceEndpoint>(),
PluginAssemblies = new List<string>(),
Repositories = new List<Pipelines.RepositoryResource>(),
Variables = new Dictionary<string, VariableValue>()
{
{ AgentWebProxySettings.AgentProxyUrlKey, proxyUrl },
{ AgentWebProxySettings.AgentProxyUsernameKey, proxyUser },
{ AgentWebProxySettings.AgentProxyPasswordKey, proxyPassword },
},
Steps = new Dictionary<string, Pipelines.TaskStepDefinitionReference>()
};
var systemConnection = new ServiceEndpoint()
{
Name = WellKnownServiceEndpointNames.SystemVssConnection,
Id = Guid.NewGuid(),
Url = new Uri("https://dev.azure.com/test"),
Authorization = new EndpointAuthorization()
{
Scheme = EndpointAuthorizationSchemes.OAuth,
Parameters = { { EndpointAuthorizationParameters.AccessToken, "Test" } }
}
};

hostContext.Endpoints.Add(systemConnection);
Assert.NotNull(hostContext.VssConnection);
Assert.Equal(hostContext.WebProxySettings.ProxyAddress, proxyUrl);
Assert.Equal(hostContext.WebProxySettings.ProxyUsername, proxyUser);
Assert.Equal(hostContext.WebProxySettings.ProxyPassword, proxyPassword);
}

[Fact]
[Trait("Level", "L0")]
[Trait("Category", "Plugin")]
Expand All @@ -411,6 +455,7 @@ public async Task LogPluginHost_HandleInitialExceptions()
using (TestHostContext tc = new TestHostContext(this))
{
AgentLogPluginHostContext hostContext = CreateTestLogPluginHostContext();

hostContext.Variables["throw_initialize"] = "1";

List<IAgentLogPlugin> plugins = new List<IAgentLogPlugin>() { new TestPlugin1(), new PluginExceptionTest() };
Expand Down
45 changes: 45 additions & 0 deletions src/Test/L0/Plugin/RepositoryPluginL0.cs
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,51 @@ public async Task RepositoryPlugin_PathInputMoveFolder()
}
}

[Fact]
[Trait("Level", "L0")]
[Trait("Category", "Plugin")]
public void RepositoryPlugin_HandleProxyConfig()
{
using TestHostContext tc = new TestHostContext(this);
var proxyUrl = "http://example.com:80";
var proxyUser = "proxy_user";
var proxyPassword = "proxy_password";

AgentTaskPluginExecutionContext hostContext = new AgentTaskPluginExecutionContext()
{
Endpoints = new List<ServiceEndpoint>(),
Inputs = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
{

},
Repositories = new List<Pipelines.RepositoryResource>(),
Variables = new Dictionary<string, VariableValue>(StringComparer.OrdinalIgnoreCase)
{
{ AgentWebProxySettings.AgentProxyUrlKey, proxyUrl },
{ AgentWebProxySettings.AgentProxyUsernameKey, proxyUser },
{ AgentWebProxySettings.AgentProxyPasswordKey, proxyPassword },

}
};
var systemConnection = new ServiceEndpoint()
{
Name = WellKnownServiceEndpointNames.SystemVssConnection,
Id = Guid.NewGuid(),
Url = new Uri("https://dev.azure.com/test"),
Authorization = new EndpointAuthorization()
{
Scheme = EndpointAuthorizationSchemes.OAuth,
Parameters = { { EndpointAuthorizationParameters.AccessToken, "Test" } }
}
};

hostContext.Endpoints.Add(systemConnection);
Assert.NotNull(hostContext.VssConnection);
Assert.Equal(hostContext.WebProxySettings.ProxyAddress, proxyUrl);
Assert.Equal(hostContext.WebProxySettings.ProxyUsername, proxyUser);
Assert.Equal(hostContext.WebProxySettings.ProxyPassword, proxyPassword);
}

[Fact]
[Trait("Level", "L0")]
[Trait("Category", "Plugin")]
Expand Down

0 comments on commit e0dd7e0

Please sign in to comment.