From c61d28a0351ad1ea2483acf6e4efc9485512e2ee Mon Sep 17 00:00:00 2001 From: Lee Gough Date: Mon, 25 Mar 2019 23:02:34 +0000 Subject: [PATCH 1/3] Add by to forwarded headers in reverse proxy --- .../FluentApiExtensions.cs | 12 ++-- .../NetworkInterfaceContributor.cs | 22 +++++++ .../ReverseProxy.cs | 36 +++++++++-- .../Implementation/ProxyApiFrom.cs | 63 ++++++++++++++++++- .../Implementation/ProxyServer.cs | 19 ++++-- .../forwarded_header_generation.cs | 61 ++++++++++++++++-- 6 files changed, 190 insertions(+), 23 deletions(-) create mode 100644 src/OpenRasta.Plugins.ReverseProxy/NetworkInterfaceContributor.cs diff --git a/src/OpenRasta.Plugins.ReverseProxy/FluentApiExtensions.cs b/src/OpenRasta.Plugins.ReverseProxy/FluentApiExtensions.cs index e1090443..e462d8ef 100644 --- a/src/OpenRasta.Plugins.ReverseProxy/FluentApiExtensions.cs +++ b/src/OpenRasta.Plugins.ReverseProxy/FluentApiExtensions.cs @@ -1,12 +1,6 @@ -using System; -using System.Net; -using System.Net.Http; -using System.Threading; -using OpenRasta.Configuration; +using OpenRasta.Configuration; using OpenRasta.Configuration.Fluent; using OpenRasta.Configuration.Fluent.Extensions; -using OpenRasta.Configuration.MetaModel; -using OpenRasta.Configuration.MetaModel.Handlers; using OpenRasta.Plugins.ReverseProxy.HttpClientFactory; using OpenRasta.Plugins.ReverseProxy.HttpMessageHandlers; @@ -27,6 +21,8 @@ public static T ReverseProxy(this T uses, ReverseProxyOptions options = null) { options = options ?? new ReverseProxyOptions(); + uses.PipelineContributor(); + if (options.HttpClient.RoundRobin.Enabled) { var handler = options.HttpClient.Handler; @@ -37,7 +33,7 @@ public static T ReverseProxy(this T uses, ReverseProxyOptions options = null) options.HttpClient.RoundRobin.ClientCount, handler, options.HttpClient.RoundRobin.LeaseTime); - + uses.Dependency(d => d.Singleton(() => new ReverseProxy( options.Timeout, options.ForwardedHeaders.ConvertLegacyHeaders, diff --git a/src/OpenRasta.Plugins.ReverseProxy/NetworkInterfaceContributor.cs b/src/OpenRasta.Plugins.ReverseProxy/NetworkInterfaceContributor.cs new file mode 100644 index 00000000..ab5a3ac5 --- /dev/null +++ b/src/OpenRasta.Plugins.ReverseProxy/NetworkInterfaceContributor.cs @@ -0,0 +1,22 @@ +using System.Linq; +using System.Net.NetworkInformation; +using OpenRasta.Pipeline; + +namespace OpenRasta.Plugins.ReverseProxy +{ + public class NetworkInterfaceContributor : KnownStages.IBegin + { + public void Initialize(IPipeline pipelineRunner) + { + pipelineRunner.Notify(context => + { + context.PipelineData["network.ipAddresses"] = NetworkInterface + .GetAllNetworkInterfaces() + .SelectMany(a => a.GetIPProperties().UnicastAddresses) + .Select(a => a.Address) + .ToList(); + return PipelineContinuation.Continue; + }); + } + } +} \ No newline at end of file diff --git a/src/OpenRasta.Plugins.ReverseProxy/ReverseProxy.cs b/src/OpenRasta.Plugins.ReverseProxy/ReverseProxy.cs index f71e2812..d5bee24b 100644 --- a/src/OpenRasta.Plugins.ReverseProxy/ReverseProxy.cs +++ b/src/OpenRasta.Plugins.ReverseProxy/ReverseProxy.cs @@ -2,10 +2,12 @@ using System.Collections.Generic; using System.Collections.Specialized; using System.Linq; +using System.Net; using System.Net.Http; using System.Text; using System.Threading; using System.Threading.Tasks; +using OpenRasta.Pipeline; using OpenRasta.Web; using HttpMethod = System.Net.Http.HttpMethod; @@ -28,7 +30,6 @@ public ReverseProxy(TimeSpan requestTimeout, bool convertForwardedHeaders, strin _onSend = onSend; _convertForwardedHeaders = convertForwardedHeaders; _viaIdentifier = viaIdentifier; - } public async Task Send(ICommunicationContext context, string target) @@ -142,12 +143,18 @@ void appendParameter(string key, string value) request.Headers.Add(header.Key, header.Value); } + var byIdentifier = GetByIdentifier(context.PipelineData); + if (convertLegacyHeaders && legacyForward?.Length > 0) { + if (byIdentifier != null) + { + legacyForward.Append($";by={byIdentifier}"); + } request.Headers.Add("forwarded", legacyForward.ToString()); } - request.Headers.Add("forwarded", CurrentForwarded(context)); + request.Headers.Add("forwarded", CurrentForwarded(context, byIdentifier)); } static Uri GetProxyTargetUri(TemplatedUriMatch requestUriMatch, string target, @@ -192,9 +199,30 @@ static Uri GetProxyTargetUri(TemplatedUriMatch requestUriMatch, string target, return proxyTargetUri; } - static string CurrentForwarded(ICommunicationContext context) + static string CurrentForwarded(ICommunicationContext context, string byIdentifier) + { + var currentForwarded = $"proto={context.Request.Uri.Scheme};host={context.Request.Uri.Host}"; + + if (byIdentifier != null) + { + currentForwarded = $"{currentForwarded};by={byIdentifier}"; + } + + return currentForwarded; + } + + static string GetByIdentifier(PipelineData data) { - return $"proto={context.Request.Uri.Scheme};host={context.Request.Uri.Host}"; + if (data.ContainsKey("server.localIpAddress")) + { + return data["server.localIpAddress"].ToString(); + } + + var externalIpAddresses = ((List) data["network.ipAddresses"]) + .Where(a => !IPAddress.IsLoopback(a)) + .ToList(); + + return externalIpAddresses.Count == 1 ? externalIpAddresses.Single().ToString() : $"_{Environment.MachineName}"; } } } \ No newline at end of file diff --git a/src/Tests/Plugins.ReverseProxy/Implementation/ProxyApiFrom.cs b/src/Tests/Plugins.ReverseProxy/Implementation/ProxyApiFrom.cs index 473d2dae..8c2bd51f 100644 --- a/src/Tests/Plugins.ReverseProxy/Implementation/ProxyApiFrom.cs +++ b/src/Tests/Plugins.ReverseProxy/Implementation/ProxyApiFrom.cs @@ -1,4 +1,6 @@ using System; +using System.Collections.Generic; +using System.Net; using OpenRasta.Configuration; using OpenRasta.Pipeline; using OpenRasta.Plugins.ReverseProxy; @@ -10,15 +12,21 @@ public class ProxyApiFrom : IConfigurationSource readonly string from; readonly string to; readonly ReverseProxyOptions options; + readonly string localIpAddress; + readonly List networkIpAddresses; public ProxyApiFrom( string from, string to, - ReverseProxyOptions options) + ReverseProxyOptions options, + string localIpAddress = null, + List networkIpAddresses = null) { this.from = from; this.to = to; this.options = options; + this.localIpAddress = localIpAddress; + this.networkIpAddresses = networkIpAddresses; } public void Configure() @@ -30,6 +38,9 @@ public void Configure() ResourceSpace.Uses.ReverseProxy(options); ResourceSpace.Uses.PipelineContributor(); + + ResourceSpace.Uses.PipelineContributor(() => new SetLocalIpAddress(localIpAddress)); + ResourceSpace.Uses.PipelineContributor(() => new SetNetworkIpAddresses(networkIpAddresses)); } } @@ -45,4 +56,54 @@ public void Initialize(IPipeline pipelineRunner) .Before(); } } + + public class SetLocalIpAddress : IPipelineContributor + { + readonly string localIpAddress; + + public SetLocalIpAddress(string localIpAddress) + { + this.localIpAddress = localIpAddress; + } + + public void Initialize(IPipeline pipelineRunner) + { + pipelineRunner.Notify(context => + { + if (localIpAddress != null) + { + context.PipelineData["server.localIpAddress"] = localIpAddress; + } + else + { + context.PipelineData.Remove("server.localIpAddress"); + } + return PipelineContinuation.Continue; + }) + .Before(); + } + } + + public class SetNetworkIpAddresses : IPipelineContributor + { + readonly List networkIpAddresses; + + public SetNetworkIpAddresses(List networkIpAddresses) + { + this.networkIpAddresses = networkIpAddresses; + } + + public void Initialize(IPipeline pipelineRunner) + { + pipelineRunner.Notify(context => + { + if (networkIpAddresses != null) + { + context.PipelineData["network.ipAddresses"] = networkIpAddresses; + } + return PipelineContinuation.Continue; + }) + .Before(); + } + } } \ No newline at end of file diff --git a/src/Tests/Plugins.ReverseProxy/Implementation/ProxyServer.cs b/src/Tests/Plugins.ReverseProxy/Implementation/ProxyServer.cs index f165edfd..f3068424 100644 --- a/src/Tests/Plugins.ReverseProxy/Implementation/ProxyServer.cs +++ b/src/Tests/Plugins.ReverseProxy/Implementation/ProxyServer.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.IO; +using System.Net; using System.Net.Http; using System.Threading.Tasks; using Microsoft.AspNetCore.Hosting; @@ -9,7 +10,6 @@ using OpenRasta.Hosting.AspNetCore; using OpenRasta.Plugins.ReverseProxy; using OpenRasta.Web; -using Tests.Hosting.Owin; using HttpMethod = System.Net.Http.HttpMethod; namespace Tests.Plugins.ReverseProxy.Implementation @@ -18,6 +18,8 @@ public class ProxyServer { Func _fromUri; Func _toUri; + string _fromLocalIpAddress; + List _fromNetworkIpAddresses; Action _fromOptions; Action _toOptions; @@ -32,10 +34,15 @@ public ProxyServer() _serverFactory = CreateTestServers; } - public ProxyServer FromServer(string fromUri, Action options = null) + public ProxyServer FromServer(string fromUri, + Action options = null, + string localIpAddress = null, + List networkIpAddresses = null) { _fromUri = port => fromUri; _fromOptions = options; + _fromLocalIpAddress = localIpAddress; + _fromNetworkIpAddresses = networkIpAddresses; return this; } @@ -47,10 +54,10 @@ public ProxyServer FromServer(Func fromUri, Action> handler = null, Action options = null - ,string resourceRegistrationUri = null) + , string resourceRegistrationUri = null) { _toUri = port => toUri; _toOptions = options; @@ -158,7 +165,7 @@ async Task SendAsync(string method, string uri) (IDisposable host, int port) CreateKestrelFromServer(int toPort) { var options = new ReverseProxyOptions(); - + _fromOptions?.Invoke(options); var host = new WebHostBuilder() @@ -240,7 +247,7 @@ TestServer CreateFromServer(Func httpMessageHandler) .Configure(app => { app.UseOpenRasta( - new ProxyApiFrom(_fromUri(80), _toUri(80), options)); + new ProxyApiFrom(_fromUri(80), _toUri(80), options, _fromLocalIpAddress, _fromNetworkIpAddresses)); })); } } diff --git a/src/Tests/Plugins.ReverseProxy/forwarded_headers/forwarded_header_generation.cs b/src/Tests/Plugins.ReverseProxy/forwarded_headers/forwarded_header_generation.cs index 7a527397..e78ec0f4 100644 --- a/src/Tests/Plugins.ReverseProxy/forwarded_headers/forwarded_header_generation.cs +++ b/src/Tests/Plugins.ReverseProxy/forwarded_headers/forwarded_header_generation.cs @@ -1,4 +1,9 @@ -using System.Threading.Tasks; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Threading.Tasks; +using OpenRasta.Collections; using Shouldly; using Tests.Plugins.ReverseProxy.Implementation; using Xunit; @@ -11,7 +16,7 @@ public class forwarded_header_generation public async Task legacy_is_rewritten() { using (var response = await new ProxyServer() - .FromServer("/proxy", options => options.ForwardedHeaders.ConvertLegacyHeaders = true) + .FromServer("/proxy", options => options.ForwardedHeaders.ConvertLegacyHeaders = true, "10.0.0.1") .ToServer("/proxied", async ctx => ctx.Request.Headers["X-Forwarded-Host"] + "|" + ctx.Request.Headers["Forwarded"]) .AddHeader("X-Forwarded-Host", "openrasta.example") @@ -20,7 +25,7 @@ public async Task legacy_is_rewritten() .GetAsync("proxy")) { - response.Content.ShouldBe("|host=openrasta.example;proto=https;base=\"/app\",proto=http;host=localhost"); + response.Content.ShouldBe("|host=openrasta.example;proto=https;base=\"/app\";by=10.0.0.1,proto=http;host=localhost;by=10.0.0.1"); } } @@ -31,9 +36,57 @@ public async Task forwarded_chain_is_preserved() .FromServer("/proxy") .ToServer("/proxied", async ctx => ctx.Request.Headers["Forwarded"]) .AddHeader("Forwarded", "host=openrasta.example") + .AddHeader("Forwarded", "host=openrasta.example2") .GetAsync("proxy")) { - response.Content.ShouldBe("host=openrasta.example,proto=http;host=localhost"); + response.Content.ShouldStartWith("host=openrasta.example,host=openrasta.example2,proto=http;host=localhost;by="); + } + } + + [Fact] + public async Task by_is_set_to_owin_local_ip_when_it_is_present() + { + using (var response = await new ProxyServer() + .FromServer("/proxy", localIpAddress: "10.0.10.1") + .ToServer("/proxied", async ctx => ctx.Request.Headers["Forwarded"]) + .AddHeader("Forwarded", "host=openrasta.example") + .GetAsync("proxy")) + { + response.Content.ShouldBe("host=openrasta.example,proto=http;host=localhost;by=10.0.10.1"); + } + } + + [Fact] + public async Task by_is_set_to_network_interface_ip_when_no_owin_local_ip_is_present_and_only_one_external_interface_exists() + { + using (var response = await new ProxyServer() + .FromServer("/proxy", networkIpAddresses: new List + { + IPAddress.Parse("127.0.0.1"), + IPAddress.Parse("10.0.0.2") + }) + .ToServer("/proxied", async ctx => ctx.Request.Headers["Forwarded"]) + .AddHeader("Forwarded", "host=openrasta.example") + .GetAsync("proxy")) + { + response.Content.ShouldBe("host=openrasta.example,proto=http;host=localhost;by=10.0.0.2"); + } + } + + [Fact] + public async Task by_is_set_to_obfuscated_machine_name_when_no_owin_local_ip_is_present_and_more_than_one_external_interface_exists() + { + using (var response = await new ProxyServer() + .FromServer("/proxy", networkIpAddresses: new List + { + IPAddress.Parse("10.0.0.2"), + IPAddress.Parse("10.0.0.3") + }) + .ToServer("/proxied", async ctx => ctx.Request.Headers["Forwarded"]) + .AddHeader("Forwarded", "host=openrasta.example") + .GetAsync("proxy")) + { + response.Content.ShouldBe($"host=openrasta.example,proto=http;host=localhost;by=_{Environment.MachineName}"); } } } From 756d4c333b65b4c95411c8109c6843310af50d17 Mon Sep 17 00:00:00 2001 From: Lee Gough Date: Wed, 27 Mar 2019 13:00:06 +0000 Subject: [PATCH 2/3] Remove redundent null checks --- src/OpenRasta.Plugins.ReverseProxy/ReverseProxy.cs | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/src/OpenRasta.Plugins.ReverseProxy/ReverseProxy.cs b/src/OpenRasta.Plugins.ReverseProxy/ReverseProxy.cs index d5bee24b..38cd2cf6 100644 --- a/src/OpenRasta.Plugins.ReverseProxy/ReverseProxy.cs +++ b/src/OpenRasta.Plugins.ReverseProxy/ReverseProxy.cs @@ -147,10 +147,7 @@ void appendParameter(string key, string value) if (convertLegacyHeaders && legacyForward?.Length > 0) { - if (byIdentifier != null) - { - legacyForward.Append($";by={byIdentifier}"); - } + legacyForward.Append($";by={byIdentifier}"); request.Headers.Add("forwarded", legacyForward.ToString()); } @@ -201,14 +198,7 @@ static Uri GetProxyTargetUri(TemplatedUriMatch requestUriMatch, string target, static string CurrentForwarded(ICommunicationContext context, string byIdentifier) { - var currentForwarded = $"proto={context.Request.Uri.Scheme};host={context.Request.Uri.Host}"; - - if (byIdentifier != null) - { - currentForwarded = $"{currentForwarded};by={byIdentifier}"; - } - - return currentForwarded; + return $"proto={context.Request.Uri.Scheme};host={context.Request.Uri.Host};by={byIdentifier}"; } static string GetByIdentifier(PipelineData data) From 0ea2f7217752780402aa8e0ae7a4baf9d343a0e1 Mon Sep 17 00:00:00 2001 From: Lee Gough Date: Fri, 29 Mar 2019 11:02:59 +0000 Subject: [PATCH 3/3] Add ByIdentifierOverride option to ForwardedHeaders config --- .../FluentApiExtensions.cs | 8 ++--- .../NetworkInterfaceContributor.cs | 22 ------------- .../ReverseProxy.cs | 32 +++++++------------ .../ReverseProxyOptions.cs | 1 + .../Implementation/ProxyApiFrom.cs | 31 +----------------- .../Implementation/ProxyServer.cs | 7 ++-- .../forwarded_header_generation.cs | 24 +++++--------- 7 files changed, 27 insertions(+), 98 deletions(-) delete mode 100644 src/OpenRasta.Plugins.ReverseProxy/NetworkInterfaceContributor.cs diff --git a/src/OpenRasta.Plugins.ReverseProxy/FluentApiExtensions.cs b/src/OpenRasta.Plugins.ReverseProxy/FluentApiExtensions.cs index e462d8ef..ede7daea 100644 --- a/src/OpenRasta.Plugins.ReverseProxy/FluentApiExtensions.cs +++ b/src/OpenRasta.Plugins.ReverseProxy/FluentApiExtensions.cs @@ -20,8 +20,6 @@ public static void ReverseProxyFor(this IUriDefinition uriConfiguration, string public static T ReverseProxy(this T uses, ReverseProxyOptions options = null) where T : IUses { options = options ?? new ReverseProxyOptions(); - - uses.PipelineContributor(); if (options.HttpClient.RoundRobin.Enabled) { @@ -39,7 +37,8 @@ public static T ReverseProxy(this T uses, ReverseProxyOptions options = null) options.ForwardedHeaders.ConvertLegacyHeaders, options.Via.Pseudonym, factory.GetClient, - options.OnSend + options.OnSend, + options.ForwardedHeaders.ByIdentifierOverride ))); } else @@ -49,7 +48,8 @@ public static T ReverseProxy(this T uses, ReverseProxyOptions options = null) options.ForwardedHeaders.ConvertLegacyHeaders, options.Via.Pseudonym, options.HttpClient.Factory, - options.OnSend + options.OnSend, + options.ForwardedHeaders.ByIdentifierOverride ))); } diff --git a/src/OpenRasta.Plugins.ReverseProxy/NetworkInterfaceContributor.cs b/src/OpenRasta.Plugins.ReverseProxy/NetworkInterfaceContributor.cs deleted file mode 100644 index ab5a3ac5..00000000 --- a/src/OpenRasta.Plugins.ReverseProxy/NetworkInterfaceContributor.cs +++ /dev/null @@ -1,22 +0,0 @@ -using System.Linq; -using System.Net.NetworkInformation; -using OpenRasta.Pipeline; - -namespace OpenRasta.Plugins.ReverseProxy -{ - public class NetworkInterfaceContributor : KnownStages.IBegin - { - public void Initialize(IPipeline pipelineRunner) - { - pipelineRunner.Notify(context => - { - context.PipelineData["network.ipAddresses"] = NetworkInterface - .GetAllNetworkInterfaces() - .SelectMany(a => a.GetIPProperties().UnicastAddresses) - .Select(a => a.Address) - .ToList(); - return PipelineContinuation.Continue; - }); - } - } -} \ No newline at end of file diff --git a/src/OpenRasta.Plugins.ReverseProxy/ReverseProxy.cs b/src/OpenRasta.Plugins.ReverseProxy/ReverseProxy.cs index 38cd2cf6..27459fb2 100644 --- a/src/OpenRasta.Plugins.ReverseProxy/ReverseProxy.cs +++ b/src/OpenRasta.Plugins.ReverseProxy/ReverseProxy.cs @@ -1,13 +1,10 @@ using System; -using System.Collections.Generic; using System.Collections.Specialized; using System.Linq; -using System.Net; using System.Net.Http; using System.Text; using System.Threading; using System.Threading.Tasks; -using OpenRasta.Pipeline; using OpenRasta.Web; using HttpMethod = System.Net.Http.HttpMethod; @@ -20,16 +17,19 @@ public class ReverseProxy readonly TimeSpan _timeout; readonly bool _convertForwardedHeaders; readonly string _viaIdentifier; + readonly string _byIdentifierOverride; public ReverseProxy(TimeSpan requestTimeout, bool convertForwardedHeaders, string viaIdentifier, Func clientFactory, - Action onSend) + Action onSend, + string byIdentifierOverride = null) { _timeout = requestTimeout; _httpClient = clientFactory; _onSend = onSend; _convertForwardedHeaders = convertForwardedHeaders; _viaIdentifier = viaIdentifier; + _byIdentifierOverride = byIdentifierOverride; } public async Task Send(ICommunicationContext context, string target) @@ -43,7 +43,7 @@ public async Task Send(ICommunicationContext context, stri PrepareRequestBody(context, requestMessage); - PrepareRequestHeaders(context, requestMessage, _convertForwardedHeaders); + PrepareRequestHeaders(context, requestMessage, _convertForwardedHeaders, _byIdentifierOverride); var viaIdentifier = PrepareViaHeader(context, requestMessage); @@ -91,7 +91,7 @@ static void PrepareRequestBody(ICommunicationContext context, HttpRequestMessage } static void PrepareRequestHeaders(ICommunicationContext context, HttpRequestMessage request, - bool convertLegacyHeaders) + bool convertLegacyHeaders, string byIdentifierOverride = null) { StringBuilder legacyForward = null; @@ -143,7 +143,11 @@ void appendParameter(string key, string value) request.Headers.Add(header.Key, header.Value); } - var byIdentifier = GetByIdentifier(context.PipelineData); + var byIdentifier = + byIdentifierOverride ?? + (context.PipelineData.ContainsKey("server.localIpAddress") + ? context.PipelineData["server.localIpAddress"].ToString() + : $"_{Environment.MachineName}"); if (convertLegacyHeaders && legacyForward?.Length > 0) { @@ -200,19 +204,5 @@ static string CurrentForwarded(ICommunicationContext context, string byIdentifie { return $"proto={context.Request.Uri.Scheme};host={context.Request.Uri.Host};by={byIdentifier}"; } - - static string GetByIdentifier(PipelineData data) - { - if (data.ContainsKey("server.localIpAddress")) - { - return data["server.localIpAddress"].ToString(); - } - - var externalIpAddresses = ((List) data["network.ipAddresses"]) - .Where(a => !IPAddress.IsLoopback(a)) - .ToList(); - - return externalIpAddresses.Count == 1 ? externalIpAddresses.Single().ToString() : $"_{Environment.MachineName}"; - } } } \ No newline at end of file diff --git a/src/OpenRasta.Plugins.ReverseProxy/ReverseProxyOptions.cs b/src/OpenRasta.Plugins.ReverseProxy/ReverseProxyOptions.cs index 3c31a089..382402bd 100644 --- a/src/OpenRasta.Plugins.ReverseProxy/ReverseProxyOptions.cs +++ b/src/OpenRasta.Plugins.ReverseProxy/ReverseProxyOptions.cs @@ -25,6 +25,7 @@ public class ForwardedHeadersOptions { public bool ConvertLegacyHeaders { get; set; } public bool RunAsForwardedHost { get; set; } + public string ByIdentifierOverride { get; set; } } public class HttpClientOptions diff --git a/src/Tests/Plugins.ReverseProxy/Implementation/ProxyApiFrom.cs b/src/Tests/Plugins.ReverseProxy/Implementation/ProxyApiFrom.cs index 8c2bd51f..4c4607c3 100644 --- a/src/Tests/Plugins.ReverseProxy/Implementation/ProxyApiFrom.cs +++ b/src/Tests/Plugins.ReverseProxy/Implementation/ProxyApiFrom.cs @@ -1,6 +1,4 @@ using System; -using System.Collections.Generic; -using System.Net; using OpenRasta.Configuration; using OpenRasta.Pipeline; using OpenRasta.Plugins.ReverseProxy; @@ -13,20 +11,17 @@ public class ProxyApiFrom : IConfigurationSource readonly string to; readonly ReverseProxyOptions options; readonly string localIpAddress; - readonly List networkIpAddresses; public ProxyApiFrom( string from, string to, ReverseProxyOptions options, - string localIpAddress = null, - List networkIpAddresses = null) + string localIpAddress = null) { this.from = from; this.to = to; this.options = options; this.localIpAddress = localIpAddress; - this.networkIpAddresses = networkIpAddresses; } public void Configure() @@ -40,7 +35,6 @@ public void Configure() ResourceSpace.Uses.PipelineContributor(); ResourceSpace.Uses.PipelineContributor(() => new SetLocalIpAddress(localIpAddress)); - ResourceSpace.Uses.PipelineContributor(() => new SetNetworkIpAddresses(networkIpAddresses)); } } @@ -83,27 +77,4 @@ public void Initialize(IPipeline pipelineRunner) .Before(); } } - - public class SetNetworkIpAddresses : IPipelineContributor - { - readonly List networkIpAddresses; - - public SetNetworkIpAddresses(List networkIpAddresses) - { - this.networkIpAddresses = networkIpAddresses; - } - - public void Initialize(IPipeline pipelineRunner) - { - pipelineRunner.Notify(context => - { - if (networkIpAddresses != null) - { - context.PipelineData["network.ipAddresses"] = networkIpAddresses; - } - return PipelineContinuation.Continue; - }) - .Before(); - } - } } \ No newline at end of file diff --git a/src/Tests/Plugins.ReverseProxy/Implementation/ProxyServer.cs b/src/Tests/Plugins.ReverseProxy/Implementation/ProxyServer.cs index f3068424..1d56c81e 100644 --- a/src/Tests/Plugins.ReverseProxy/Implementation/ProxyServer.cs +++ b/src/Tests/Plugins.ReverseProxy/Implementation/ProxyServer.cs @@ -19,7 +19,6 @@ public class ProxyServer Func _fromUri; Func _toUri; string _fromLocalIpAddress; - List _fromNetworkIpAddresses; Action _fromOptions; Action _toOptions; @@ -36,13 +35,11 @@ public ProxyServer() public ProxyServer FromServer(string fromUri, Action options = null, - string localIpAddress = null, - List networkIpAddresses = null) + string localIpAddress = null) { _fromUri = port => fromUri; _fromOptions = options; _fromLocalIpAddress = localIpAddress; - _fromNetworkIpAddresses = networkIpAddresses; return this; } @@ -247,7 +244,7 @@ TestServer CreateFromServer(Func httpMessageHandler) .Configure(app => { app.UseOpenRasta( - new ProxyApiFrom(_fromUri(80), _toUri(80), options, _fromLocalIpAddress, _fromNetworkIpAddresses)); + new ProxyApiFrom(_fromUri(80), _toUri(80), options, _fromLocalIpAddress)); })); } } diff --git a/src/Tests/Plugins.ReverseProxy/forwarded_headers/forwarded_header_generation.cs b/src/Tests/Plugins.ReverseProxy/forwarded_headers/forwarded_header_generation.cs index e78ec0f4..0e277d73 100644 --- a/src/Tests/Plugins.ReverseProxy/forwarded_headers/forwarded_header_generation.cs +++ b/src/Tests/Plugins.ReverseProxy/forwarded_headers/forwarded_header_generation.cs @@ -16,7 +16,7 @@ public class forwarded_header_generation public async Task legacy_is_rewritten() { using (var response = await new ProxyServer() - .FromServer("/proxy", options => options.ForwardedHeaders.ConvertLegacyHeaders = true, "10.0.0.1") + .FromServer("/proxy", options => options.ForwardedHeaders.ConvertLegacyHeaders = true) .ToServer("/proxied", async ctx => ctx.Request.Headers["X-Forwarded-Host"] + "|" + ctx.Request.Headers["Forwarded"]) .AddHeader("X-Forwarded-Host", "openrasta.example") @@ -25,7 +25,7 @@ public async Task legacy_is_rewritten() .GetAsync("proxy")) { - response.Content.ShouldBe("|host=openrasta.example;proto=https;base=\"/app\";by=10.0.0.1,proto=http;host=localhost;by=10.0.0.1"); + response.Content.ShouldMatch("^\\|host=openrasta.example;proto=https;base=\\\"/app\\\";by=.*,proto=http;host=localhost;by=.*$"); } } @@ -39,7 +39,7 @@ public async Task forwarded_chain_is_preserved() .AddHeader("Forwarded", "host=openrasta.example2") .GetAsync("proxy")) { - response.Content.ShouldStartWith("host=openrasta.example,host=openrasta.example2,proto=http;host=localhost;by="); + response.Content.ShouldMatch("^host=openrasta.example,host=openrasta.example2,proto=http;host=localhost;by=.*$"); } } @@ -57,31 +57,23 @@ public async Task by_is_set_to_owin_local_ip_when_it_is_present() } [Fact] - public async Task by_is_set_to_network_interface_ip_when_no_owin_local_ip_is_present_and_only_one_external_interface_exists() + public async Task by_is_set_to_configured_override_when_it_is_present() { using (var response = await new ProxyServer() - .FromServer("/proxy", networkIpAddresses: new List - { - IPAddress.Parse("127.0.0.1"), - IPAddress.Parse("10.0.0.2") - }) + .FromServer("/proxy", options => options.ForwardedHeaders.ByIdentifierOverride = "foo", "10.0.10.1") .ToServer("/proxied", async ctx => ctx.Request.Headers["Forwarded"]) .AddHeader("Forwarded", "host=openrasta.example") .GetAsync("proxy")) { - response.Content.ShouldBe("host=openrasta.example,proto=http;host=localhost;by=10.0.0.2"); + response.Content.ShouldBe("host=openrasta.example,proto=http;host=localhost;by=foo"); } } [Fact] - public async Task by_is_set_to_obfuscated_machine_name_when_no_owin_local_ip_is_present_and_more_than_one_external_interface_exists() + public async Task by_is_set_to_obfuscated_machine_name_when_no_owin_local_ip_is_present_and_no_override_configured() { using (var response = await new ProxyServer() - .FromServer("/proxy", networkIpAddresses: new List - { - IPAddress.Parse("10.0.0.2"), - IPAddress.Parse("10.0.0.3") - }) + .FromServer("/proxy") .ToServer("/proxied", async ctx => ctx.Request.Headers["Forwarded"]) .AddHeader("Forwarded", "host=openrasta.example") .GetAsync("proxy"))