Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 4 additions & 8 deletions src/Npgsql/BackendMessages/AuthenticationMessages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,11 @@ internal AuthenticationSASLContinueMessage(NpgsqlReadBuffer buf, int len)

class AuthenticationSCRAMServerFirstMessage
{
static readonly ILogger Logger = NpgsqlLoggingConfiguration.ConnectionLogger;

internal string Nonce { get; }
internal string Salt { get; }
internal int Iteration { get; }

internal static AuthenticationSCRAMServerFirstMessage Load(byte[] bytes)
internal static AuthenticationSCRAMServerFirstMessage Load(byte[] bytes, ILogger connectionLogger)
{
var data = PGUtil.UTF8Encoding.GetString(bytes);
string? nonce = null, salt = null;
Expand All @@ -151,7 +149,7 @@ internal static AuthenticationSCRAMServerFirstMessage Load(byte[] bytes)
else if (part.StartsWith("i=", StringComparison.Ordinal))
iteration = int.Parse(part.Substring(2));
else
Logger.LogDebug("Unknown part in SCRAM server-first message:" + part);
connectionLogger.LogDebug("Unknown part in SCRAM server-first message:" + part);
}

if (nonce == null)
Expand Down Expand Up @@ -186,11 +184,9 @@ internal AuthenticationSASLFinalMessage(NpgsqlReadBuffer buf, int len)

class AuthenticationSCRAMServerFinalMessage
{
static readonly ILogger Logger = NpgsqlLoggingConfiguration.ConnectionLogger;

internal string ServerSignature { get; }

internal static AuthenticationSCRAMServerFinalMessage Load(byte[] bytes)
internal static AuthenticationSCRAMServerFinalMessage Load(byte[] bytes, ILogger connectionLogger)
{
var data = PGUtil.UTF8Encoding.GetString(bytes);
string? serverSignature = null;
Expand All @@ -200,7 +196,7 @@ internal static AuthenticationSCRAMServerFinalMessage Load(byte[] bytes)
if (part.StartsWith("v=", StringComparison.Ordinal))
serverSignature = part.Substring(2);
else
Logger.LogDebug("Unknown part in SCRAM server-first message:" + part);
connectionLogger.LogDebug("Unknown part in SCRAM server-first message:" + part);
}

if (serverSignature == null)
Expand Down
8 changes: 3 additions & 5 deletions src/Npgsql/BackendMessages/ErrorOrNoticeMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,8 @@ class ErrorOrNoticeMessage
internal string? Line { get; }
internal string? Routine { get; }

static readonly ILogger Logger = NpgsqlLoggingConfiguration.ExceptionLogger;

// ReSharper disable once FunctionComplexityOverflow
internal static ErrorOrNoticeMessage Load(NpgsqlReadBuffer buf, bool includeDetail)
internal static ErrorOrNoticeMessage Load(NpgsqlReadBuffer buf, bool includeDetail, ILogger exceptionLogger)
{
(string? severity, string? invariantSeverity, string? code, string? message, string? detail, string? hint) = (null, null, null, null, null, null);
var (position, internalPosition) = (0, 0);
Expand Down Expand Up @@ -69,7 +67,7 @@ internal static ErrorOrNoticeMessage Load(NpgsqlReadBuffer buf, bool includeDeta
var positionStr = buf.ReadNullTerminatedStringRelaxed();
if (!int.TryParse(positionStr, out var tmpPosition))
{
Logger.LogWarning("Non-numeric position in ErrorResponse: " + positionStr);
exceptionLogger.LogWarning("Non-numeric position in ErrorResponse: " + positionStr);
continue;
}
position = tmpPosition;
Expand All @@ -78,7 +76,7 @@ internal static ErrorOrNoticeMessage Load(NpgsqlReadBuffer buf, bool includeDeta
var internalPositionStr = buf.ReadNullTerminatedStringRelaxed();
if (!int.TryParse(internalPositionStr, out var internalPositionTmp))
{
Logger.LogWarning("Non-numeric position in ErrorResponse: " + internalPositionStr);
exceptionLogger.LogWarning("Non-numeric position in ErrorResponse: " + internalPositionStr);
continue;
}
internalPosition = internalPositionTmp;
Expand Down
15 changes: 8 additions & 7 deletions src/Npgsql/Internal/NpgsqlConnector.Auth.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ async Task AuthenticateSASL(List<string> mechanisms, string username, bool async
var sslStream = (SslStream)_stream;
if (sslStream.RemoteCertificate is null)
{
Logger.LogWarning("Remote certificate null, falling back to SCRAM-SHA-256");
ConnectionLogger.LogWarning("Remote certificate null, falling back to SCRAM-SHA-256");
}
else
{
Expand All @@ -95,7 +95,7 @@ async Task AuthenticateSASL(List<string> mechanisms, string username, bool async
var algorithmName = remoteCertificate.SignatureAlgorithm.FriendlyName;
if (algorithmName is null)
{
Logger.LogWarning("Signature algorithm was null, falling back to SCRAM-SHA-256");
ConnectionLogger.LogWarning("Signature algorithm was null, falling back to SCRAM-SHA-256");
}
else if (algorithmName.StartsWith("sha1", StringComparison.OrdinalIgnoreCase) ||
algorithmName.StartsWith("md5", StringComparison.OrdinalIgnoreCase) ||
Expand All @@ -113,7 +113,7 @@ async Task AuthenticateSASL(List<string> mechanisms, string username, bool async
}
else
{
Logger.LogWarning(
ConnectionLogger.LogWarning(
$"Support for signature algorithm {algorithmName} is not yet implemented, falling back to SCRAM-SHA-256");
}

Expand Down Expand Up @@ -167,7 +167,7 @@ async Task AuthenticateSASL(List<string> mechanisms, string username, bool async
var saslContinueMsg = Expect<AuthenticationSASLContinueMessage>(await ReadMessage(async), this);
if (saslContinueMsg.AuthRequestType != AuthenticationRequestType.AuthenticationSASLContinue)
throw new NpgsqlException("[SASL] AuthenticationSASLContinue message expected");
var firstServerMsg = AuthenticationSCRAMServerFirstMessage.Load(saslContinueMsg.Payload);
var firstServerMsg = AuthenticationSCRAMServerFirstMessage.Load(saslContinueMsg.Payload, ConnectionLogger);
if (!firstServerMsg.Nonce.StartsWith(clientNonce, StringComparison.Ordinal))
throw new NpgsqlException("[SCRAM] Malformed SCRAMServerFirst message: server nonce doesn't start with client nonce");

Expand Down Expand Up @@ -201,14 +201,15 @@ async Task AuthenticateSASL(List<string> mechanisms, string username, bool async
if (saslFinalServerMsg.AuthRequestType != AuthenticationRequestType.AuthenticationSASLFinal)
throw new NpgsqlException("[SASL] AuthenticationSASLFinal message expected");

var scramFinalServerMsg = AuthenticationSCRAMServerFinalMessage.Load(saslFinalServerMsg.Payload);
var scramFinalServerMsg = AuthenticationSCRAMServerFinalMessage.Load(saslFinalServerMsg.Payload, ConnectionLogger);
if (scramFinalServerMsg.ServerSignature != Convert.ToBase64String(serverSignature))
throw new NpgsqlException("[SCRAM] Unable to verify server signature");

var okMsg = Expect<AuthenticationRequestMessage>(await ReadMessage(async), this);
if (okMsg.AuthRequestType != AuthenticationRequestType.AuthenticationOk)
throw new NpgsqlException("[SASL] Expected AuthenticationOK message");


static string GetNonce()
{
using var rncProvider = RandomNumberGenerator.Create();
Expand Down Expand Up @@ -446,7 +447,7 @@ class AuthenticationCompleteException : Exception { }
if (ProvidePasswordCallback is { } passwordCallback)
try
{
Logger.LogTrace($"Taking password from {nameof(ProvidePasswordCallback)} delegate");
ConnectionLogger.LogTrace($"Taking password from {nameof(ProvidePasswordCallback)} delegate");
password = passwordCallback(Host, Port, Settings.Database!, username);
}
catch (Exception e)
Expand All @@ -467,7 +468,7 @@ class AuthenticationCompleteException : Exception { }
.GetFirstMatchingEntry(Host, Port, Settings.Database!, username);
if (matchingEntry != null)
{
Logger.LogTrace("Taking password from pgpass file");
ConnectionLogger.LogTrace("Taking password from pgpass file");
password = matchingEntry.Password;
}
}
Expand Down
Loading