Skip to content

Commit 00ee7a8

Browse files
authored
Implement logging via NpgsqlDataSource (#4504)
Closes #4493
1 parent 12958dd commit 00ee7a8

38 files changed

+427
-359
lines changed

src/Npgsql/BackendMessages/AuthenticationMessages.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -130,13 +130,11 @@ internal AuthenticationSASLContinueMessage(NpgsqlReadBuffer buf, int len)
130130

131131
class AuthenticationSCRAMServerFirstMessage
132132
{
133-
static readonly ILogger Logger = NpgsqlLoggingConfiguration.ConnectionLogger;
134-
135133
internal string Nonce { get; }
136134
internal string Salt { get; }
137135
internal int Iteration { get; }
138136

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

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

187185
class AuthenticationSCRAMServerFinalMessage
188186
{
189-
static readonly ILogger Logger = NpgsqlLoggingConfiguration.ConnectionLogger;
190-
191187
internal string ServerSignature { get; }
192188

193-
internal static AuthenticationSCRAMServerFinalMessage Load(byte[] bytes)
189+
internal static AuthenticationSCRAMServerFinalMessage Load(byte[] bytes, ILogger connectionLogger)
194190
{
195191
var data = PGUtil.UTF8Encoding.GetString(bytes);
196192
string? serverSignature = null;
@@ -200,7 +196,7 @@ internal static AuthenticationSCRAMServerFinalMessage Load(byte[] bytes)
200196
if (part.StartsWith("v=", StringComparison.Ordinal))
201197
serverSignature = part.Substring(2);
202198
else
203-
Logger.LogDebug("Unknown part in SCRAM server-first message:" + part);
199+
connectionLogger.LogDebug("Unknown part in SCRAM server-first message:" + part);
204200
}
205201

206202
if (serverSignature == null)

src/Npgsql/BackendMessages/ErrorOrNoticeMessage.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,8 @@ class ErrorOrNoticeMessage
2626
internal string? Line { get; }
2727
internal string? Routine { get; }
2828

29-
static readonly ILogger Logger = NpgsqlLoggingConfiguration.ExceptionLogger;
30-
3129
// ReSharper disable once FunctionComplexityOverflow
32-
internal static ErrorOrNoticeMessage Load(NpgsqlReadBuffer buf, bool includeDetail)
30+
internal static ErrorOrNoticeMessage Load(NpgsqlReadBuffer buf, bool includeDetail, ILogger exceptionLogger)
3331
{
3432
(string? severity, string? invariantSeverity, string? code, string? message, string? detail, string? hint) = (null, null, null, null, null, null);
3533
var (position, internalPosition) = (0, 0);
@@ -69,7 +67,7 @@ internal static ErrorOrNoticeMessage Load(NpgsqlReadBuffer buf, bool includeDeta
6967
var positionStr = buf.ReadNullTerminatedStringRelaxed();
7068
if (!int.TryParse(positionStr, out var tmpPosition))
7169
{
72-
Logger.LogWarning("Non-numeric position in ErrorResponse: " + positionStr);
70+
exceptionLogger.LogWarning("Non-numeric position in ErrorResponse: " + positionStr);
7371
continue;
7472
}
7573
position = tmpPosition;
@@ -78,7 +76,7 @@ internal static ErrorOrNoticeMessage Load(NpgsqlReadBuffer buf, bool includeDeta
7876
var internalPositionStr = buf.ReadNullTerminatedStringRelaxed();
7977
if (!int.TryParse(internalPositionStr, out var internalPositionTmp))
8078
{
81-
Logger.LogWarning("Non-numeric position in ErrorResponse: " + internalPositionStr);
79+
exceptionLogger.LogWarning("Non-numeric position in ErrorResponse: " + internalPositionStr);
8280
continue;
8381
}
8482
internalPosition = internalPositionTmp;

src/Npgsql/Internal/NpgsqlConnector.Auth.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ async Task AuthenticateSASL(List<string> mechanisms, string username, bool async
8585
var sslStream = (SslStream)_stream;
8686
if (sslStream.RemoteCertificate is null)
8787
{
88-
Logger.LogWarning("Remote certificate null, falling back to SCRAM-SHA-256");
88+
ConnectionLogger.LogWarning("Remote certificate null, falling back to SCRAM-SHA-256");
8989
}
9090
else
9191
{
@@ -95,7 +95,7 @@ async Task AuthenticateSASL(List<string> mechanisms, string username, bool async
9595
var algorithmName = remoteCertificate.SignatureAlgorithm.FriendlyName;
9696
if (algorithmName is null)
9797
{
98-
Logger.LogWarning("Signature algorithm was null, falling back to SCRAM-SHA-256");
98+
ConnectionLogger.LogWarning("Signature algorithm was null, falling back to SCRAM-SHA-256");
9999
}
100100
else if (algorithmName.StartsWith("sha1", StringComparison.OrdinalIgnoreCase) ||
101101
algorithmName.StartsWith("md5", StringComparison.OrdinalIgnoreCase) ||
@@ -113,7 +113,7 @@ async Task AuthenticateSASL(List<string> mechanisms, string username, bool async
113113
}
114114
else
115115
{
116-
Logger.LogWarning(
116+
ConnectionLogger.LogWarning(
117117
$"Support for signature algorithm {algorithmName} is not yet implemented, falling back to SCRAM-SHA-256");
118118
}
119119

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

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

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

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

212+
212213
static string GetNonce()
213214
{
214215
using var rncProvider = RandomNumberGenerator.Create();
@@ -446,7 +447,7 @@ class AuthenticationCompleteException : Exception { }
446447
if (ProvidePasswordCallback is { } passwordCallback)
447448
try
448449
{
449-
Logger.LogTrace($"Taking password from {nameof(ProvidePasswordCallback)} delegate");
450+
ConnectionLogger.LogTrace($"Taking password from {nameof(ProvidePasswordCallback)} delegate");
450451
password = passwordCallback(Host, Port, Settings.Database!, username);
451452
}
452453
catch (Exception e)
@@ -467,7 +468,7 @@ class AuthenticationCompleteException : Exception { }
467468
.GetFirstMatchingEntry(Host, Port, Settings.Database!, username);
468469
if (matchingEntry != null)
469470
{
470-
Logger.LogTrace("Taking password from pgpass file");
471+
ConnectionLogger.LogTrace("Taking password from pgpass file");
471472
password = matchingEntry.Password;
472473
}
473474
}

0 commit comments

Comments
 (0)