Skip to content

Commit

Permalink
Fix Eureka URI masking
Browse files Browse the repository at this point in the history
  • Loading branch information
bart-vmware authored and TimHess committed Jul 16, 2024
1 parent 8ae9892 commit c5d4a94
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 60 deletions.
49 changes: 24 additions & 25 deletions src/Common/src/Common/Extensions/UriExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,48 @@

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;

namespace Steeltoe.Common.Extensions;

public static class UriExtensions
{
public static string ToMaskedString(this Uri source)
private static readonly char[] _uriSeparatorChar = { ',' };

/// <summary>
/// Parse a querystring into a dictionary of key value pairs
/// </summary>
/// <param name="querystring">The querystring to parse</param>
/// <returns>Pairs of keys and values</returns>
public static Dictionary<string, string> ParseQuerystring(string querystring)
{
if (source == null)
var result = new Dictionary<string, string>();
foreach (var pair in querystring.Split('&'))
{
throw new ArgumentNullException(nameof(source));
if (!string.IsNullOrEmpty(pair))
{
var kvp = pair.Split('=');
result.Add(WebUtility.UrlDecode(kvp[0]), WebUtility.UrlDecode(kvp[1]));
}
}

return source.ToMaskedUri().ToString();
return result;
}

public static Uri ToMaskedUri(this Uri source)
public static string ToMaskedString(this Uri source)
{
if (source == null)
{
throw new ArgumentNullException(nameof(source));
}

var uris = source.ToString();
return string.Join(",", uris.Split(_uriSeparatorChar, StringSplitOptions.RemoveEmptyEntries).Select(uri => new Uri(uri).ToMaskedUri().ToString()));
}

private static Uri ToMaskedUri(this Uri source)
{
if (string.IsNullOrEmpty(source.UserInfo))
{
return source;
Expand All @@ -42,24 +61,4 @@ public static Uri ToMaskedUri(this Uri source)

return builder.Uri;
}

/// <summary>
/// Parse a querystring into a dictionary of key value pairs
/// </summary>
/// <param name="querystring">The querystring to parse</param>
/// <returns>Pairs of keys and values</returns>
public static Dictionary<string, string> ParseQuerystring(string querystring)
{
var result = new Dictionary<string, string>();
foreach (var pair in querystring.Split('&'))
{
if (!string.IsNullOrEmpty(pair))
{
var kvp = pair.Split('=');
result.Add(WebUtility.UrlDecode(kvp[0]), WebUtility.UrlDecode(kvp[1]));
}
}

return result;
}
}
30 changes: 4 additions & 26 deletions src/Common/test/Common.Test/Extensions/UriExtensionsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,19 @@ namespace Steeltoe.Common.Test.Extensions;

public class UriExtensionsTest
{
[Fact]
public void MaskExistingBasicAuthenticationToString()
{
var uri = new Uri("http://username:password@www.example.com/");
var expected = "http://****:****@www.example.com/";

var masked = uri.ToMaskedString();

Assert.Equal(expected, masked);
}

[Fact]
public void MaskExistingBasicAuthentication()
{
var uri = new Uri("http://username:password@www.example.com/");
var expected = new Uri("http://****:****@www.example.com/");
var uri = new Uri("http://username:password@www.example.com/,http://user2:pass2@www.other.com/");
var expected = "http://****:****@www.example.com/,http://****:****@www.other.com/";

var masked = uri.ToMaskedUri();
var masked = uri.ToMaskedString();

Assert.Equal(expected, masked);
}

[Fact]
public void DontMaskStringIfNotBasicAuthenticationExists()
public void DontMaskIfNotBasicAuthenticationExists()
{
var uri = new Uri("http://www.example.com/");
var expected = uri.ToString();
Expand All @@ -42,15 +31,4 @@ public void DontMaskStringIfNotBasicAuthenticationExists()

Assert.Equal(expected, masked);
}

[Fact]
public void DontMaskUriIfNotBasicAuthenticationExists()
{
var uri = new Uri("http://www.example.com/");
var expected = new Uri(uri.ToString());

var masked = uri.ToMaskedUri();

Assert.Equal(expected, masked);
}
}
3 changes: 2 additions & 1 deletion src/Discovery/src/Eureka/DiscoveryClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,8 @@ protected internal async T.Task<bool> FetchRegistryAsync(bool fullUpdate)
catch (Exception e)
{
// Log
_logger.LogError(e, "FetchRegistry Failed for Eureka service urls: {EurekaServerServiceUrls}", new Uri(ClientConfig.EurekaServerServiceUrls).ToMaskedString());
var masked = new Uri(ClientConfig.EurekaServerServiceUrls).ToMaskedString();
_logger.LogError(e, "FetchRegistry Failed for Eureka service urls: {EurekaServerServiceUrls}", masked);
return false;
}

Expand Down
16 changes: 8 additions & 8 deletions src/Discovery/src/Eureka/Transport/EurekaHttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ private async Task<EurekaHttpResponse> RegisterAsyncInternal(InstanceInfo info)
}
catch (Exception e)
{
_logger?.LogError(e, "RegisterAsync Failed, request was made to {requestUri}, retry: {retry}", requestUri.ToMaskedUri(), retry);
_logger?.LogError(e, "RegisterAsync Failed, request was made to {requestUri}, retry: {retry}", requestUri.ToMaskedString(), retry);
}
finally
{
Expand Down Expand Up @@ -250,7 +250,7 @@ private async Task<EurekaHttpResponse<InstanceInfo>> SendHeartBeatAsyncInternal(
}
catch (Exception e)
{
_logger?.LogError(e, "SendHeartBeatAsync Failed, request was made to {requestUri}", requestUri.ToMaskedUri());
_logger?.LogError(e, "SendHeartBeatAsync Failed, request was made to {requestUri}", requestUri.ToMaskedString());
}
finally
{
Expand Down Expand Up @@ -363,7 +363,7 @@ private async Task<EurekaHttpResponse<Application>> GetApplicationAsyncInternal(
}
catch (Exception e)
{
_logger?.LogError(e, "GetApplicationAsync Failed, request was made to {requestUri}", requestUri.ToMaskedUri());
_logger?.LogError(e, "GetApplicationAsync Failed, request was made to {requestUri}", requestUri.ToMaskedString());
}
finally
{
Expand Down Expand Up @@ -460,7 +460,7 @@ private async Task<EurekaHttpResponse> CancelAsyncInternal(string appName, strin
}
catch (Exception e)
{
_logger?.LogError(e, "CancelAsync Failed, request was made to {requestUri}", requestUri.ToMaskedUri());
_logger?.LogError(e, "CancelAsync Failed, request was made to {requestUri}", requestUri.ToMaskedString());
}
finally
{
Expand Down Expand Up @@ -536,7 +536,7 @@ private async Task<EurekaHttpResponse> DeleteStatusOverrideAsyncInternal(string
}
catch (Exception e)
{
_logger?.LogError(e, "DeleteStatusOverrideAsync Failed, request was made to {requestUri}", requestUri.ToMaskedUri());
_logger?.LogError(e, "DeleteStatusOverrideAsync Failed, request was made to {requestUri}", requestUri.ToMaskedString());
}
finally
{
Expand Down Expand Up @@ -613,7 +613,7 @@ private async Task<EurekaHttpResponse> StatusUpdateAsyncInternal(string appName,
}
catch (Exception e)
{
_logger?.LogError(e, "StatusUpdateAsync Failed, request was made to {requestUri}", requestUri.ToMaskedUri());
_logger?.LogError(e, "StatusUpdateAsync Failed, request was made to {requestUri}", requestUri.ToMaskedString());
}
finally
{
Expand Down Expand Up @@ -865,7 +865,7 @@ protected virtual async Task<EurekaHttpResponse<InstanceInfo>> DoGetInstanceAsyn
}
catch (Exception e)
{
_logger?.LogError(e, "DoGetInstanceAsync Failed, request was made to {requestUri}", requestUri.ToMaskedUri());
_logger?.LogError(e, "DoGetInstanceAsync Failed, request was made to {requestUri}", requestUri.ToMaskedString());
}
finally
{
Expand Down Expand Up @@ -945,7 +945,7 @@ protected virtual async Task<EurekaHttpResponse<Applications>> DoGetApplications
}
catch (Exception e)
{
_logger?.LogError(e, "DoGetApplicationsAsync Failed, request was made to {requestUri}", requestUri.ToMaskedUri());
_logger?.LogError(e, "DoGetApplicationsAsync Failed, request was made to {requestUri}", requestUri.ToMaskedString());
}
finally
{
Expand Down

0 comments on commit c5d4a94

Please sign in to comment.