Skip to content

Commit

Permalink
set username on multiple uris to the actual used one + better redaction
Browse files Browse the repository at this point in the history
  • Loading branch information
Mrxx99 committed Nov 2, 2024
1 parent 13d43f2 commit 894faf3
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/NATS.Client.Core/NatsConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,9 @@ private static NatsOpts ReadUserInfoFromConnectionString(NatsOpts opts)
var natsUris = opts.GetSeedUris(suppressRandomization: true);
var maskedUris = new List<string>(natsUris.Length);

var usesPasswordInUrl = false;
var usesTokenInUrl = false;

foreach (var natsUri in natsUris)
{
var uriBuilder = new UriBuilder(natsUri.Uri);
Expand All @@ -306,6 +309,7 @@ private static NatsOpts ReadUserInfoFromConnectionString(NatsOpts opts)
if (first)
{
first = false;
usesPasswordInUrl = true;

opts = opts with
{
Expand All @@ -324,6 +328,7 @@ private static NatsOpts ReadUserInfoFromConnectionString(NatsOpts opts)
if (first)
{
first = false;
usesTokenInUrl = true;

opts = opts with
{
Expand All @@ -338,6 +343,16 @@ private static NatsOpts ReadUserInfoFromConnectionString(NatsOpts opts)
}
}

if (usesPasswordInUrl)
{
uriBuilder.UserName = opts.AuthOpts.Username; // show actual used user name in logs
}
else if (usesTokenInUrl)
{
uriBuilder.UserName = "***"; // to redact the token from logs
uriBuilder.Password = null; // when token is used remove password
}

maskedUris.Add(uriBuilder.ToString().TrimEnd('/'));
}

Expand Down

0 comments on commit 894faf3

Please sign in to comment.