Skip to content

Commit bd78424

Browse files
committed
Test coverage improved to address security issues
1 parent ae3bbda commit bd78424

File tree

12 files changed

+2677
-29
lines changed

12 files changed

+2677
-29
lines changed

src/Deveel.Messaging.Connector.Abstractions/Messaging/ChannelConnectorBase.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -187,20 +187,20 @@ protected async Task<ConnectorResult<bool>> AuthenticateAsync(ConnectionSettings
187187
"No suitable authentication configuration found for the provided connection settings");
188188
}
189189

190-
Logger.LogUsingAuthenticationConfiguration(authConfig.AuthenticationType.ToString());
190+
Logger.LogUsingAuthenticationConfiguration(authConfig.AuthenticationType);
191191

192192
// Perform authentication
193193
var authResult = await _authenticationManager.AuthenticateAsync(connectionSettings, authConfig, cancellationToken);
194194

195195
if (authResult.IsSuccessful && authResult.Credential != null)
196196
{
197197
_authenticationCredential = authResult.Credential;
198-
Logger.LogAuthenticationSuccessful(authConfig.AuthenticationType.ToString());
198+
Logger.LogAuthenticationSuccessful(authConfig.AuthenticationType);
199199
return ConnectorResult<bool>.Success(true);
200200
}
201201
else
202202
{
203-
Logger.LogAuthenticationFailed(authResult.ErrorMessage ?? "Unknown error");
203+
Logger.LogAuthenticationFailed(authConfig.AuthenticationType);
204204
return ConnectorResult<bool>.Fail(ConnectorErrorCodes.AuthenticationFailed,
205205
authResult.ErrorMessage ?? "Authentication failed");
206206
}
@@ -239,7 +239,7 @@ protected async Task<ConnectorResult<bool>> RefreshAuthenticationAsync(Connectio
239239

240240
if (authConfig == null)
241241
{
242-
Logger.LogAuthenticationConfigurationNotFound(_authenticationCredential.AuthenticationType.ToString());
242+
Logger.LogAuthenticationConfigurationNotFound(_authenticationCredential.AuthenticationType);
243243
return await AuthenticateAsync(connectionSettings, cancellationToken);
244244
}
245245

@@ -254,7 +254,7 @@ protected async Task<ConnectorResult<bool>> RefreshAuthenticationAsync(Connectio
254254
}
255255
else
256256
{
257-
Logger.LogAuthenticationRefreshFailed(authResult.ErrorMessage ?? "Unknown error");
257+
Logger.LogAuthenticationRefreshFailed();
258258
return ConnectorResult<bool>.Fail(ConnectorErrorCodes.AuthenticationFailed,
259259
authResult.ErrorMessage ?? "Authentication refresh failed");
260260
}

src/Deveel.Messaging.Connector.Abstractions/Messaging/ChannelConnectorBaseLoggerExtensions.cs renamed to src/Deveel.Messaging.Connector.Abstractions/Messaging/LoggerExtensions.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace Deveel.Messaging
1010
/// <summary>
1111
/// Provides high-performance logging extensions for Channel Connector Base operations using source-generated logging methods.
1212
/// </summary>
13-
internal static partial class ChannelConnectorBaseLoggerExtensions
13+
internal static partial class LoggerExtensions
1414
{
1515
#region Authentication Logging
1616

@@ -30,19 +30,19 @@ internal static partial class ChannelConnectorBaseLoggerExtensions
3030
EventId = 2003,
3131
Level = LogLevel.Debug,
3232
Message = "Using authentication configuration: {AuthenticationType}")]
33-
internal static partial void LogUsingAuthenticationConfiguration(this ILogger logger, string authenticationType);
33+
internal static partial void LogUsingAuthenticationConfiguration(this ILogger logger, AuthenticationType authenticationType);
3434

3535
[LoggerMessage(
3636
EventId = 2004,
3737
Level = LogLevel.Information,
3838
Message = "Authentication successful using {AuthenticationType}")]
39-
internal static partial void LogAuthenticationSuccessful(this ILogger logger, string authenticationType);
39+
internal static partial void LogAuthenticationSuccessful(this ILogger logger, AuthenticationType authenticationType);
4040

4141
[LoggerMessage(
4242
EventId = 2005,
4343
Level = LogLevel.Error,
44-
Message = "Authentication failed: {ErrorMessage}")]
45-
internal static partial void LogAuthenticationFailed(this ILogger logger, string errorMessage);
44+
Message = "Authentication failed using {AuthenticationType}")]
45+
internal static partial void LogAuthenticationFailed(this ILogger logger, AuthenticationType authenticationType);
4646

4747
[LoggerMessage(
4848
EventId = 2006,
@@ -66,7 +66,7 @@ internal static partial class ChannelConnectorBaseLoggerExtensions
6666
EventId = 2009,
6767
Level = LogLevel.Warning,
6868
Message = "Authentication configuration not found for credential type: {AuthenticationType}")]
69-
internal static partial void LogAuthenticationConfigurationNotFound(this ILogger logger, string authenticationType);
69+
internal static partial void LogAuthenticationConfigurationNotFound(this ILogger logger, AuthenticationType authenticationType);
7070

7171
[LoggerMessage(
7272
EventId = 2010,
@@ -77,8 +77,8 @@ internal static partial class ChannelConnectorBaseLoggerExtensions
7777
[LoggerMessage(
7878
EventId = 2011,
7979
Level = LogLevel.Error,
80-
Message = "Authentication refresh failed: {ErrorMessage}")]
81-
internal static partial void LogAuthenticationRefreshFailed(this ILogger logger, string errorMessage);
80+
Message = "Authentication refresh failed")]
81+
internal static partial void LogAuthenticationRefreshFailed(this ILogger logger);
8282

8383
[LoggerMessage(
8484
EventId = 2012,

src/Deveel.Messaging.Connector.Firebase/Messaging/FirebaseServiceAccountAuthenticationProvider.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public override Task<AuthenticationResult> ObtainCredentialAsync(ConnectionSetti
4646
{
4747
try
4848
{
49-
_logger.LogDebug("Obtaining Firebase service account credential");
49+
_logger.LogObtainingServiceAccountCredential();
5050

5151
// Get service account key - can be JSON string or file path
5252
var serviceAccountKey = GetStringParameter(connectionSettings, "ServiceAccountKey") ??
@@ -67,15 +67,15 @@ public override Task<AuthenticationResult> ObtainCredentialAsync(ConnectionSetti
6767
return Task.FromResult(CreateFailureResult($"Service account key file not found: {serviceAccountKey}", "SERVICE_ACCOUNT_FILE_NOT_FOUND"));
6868
}
6969

70-
_logger.LogDebug("Using service account key file: {FilePath}", serviceAccountKey);
70+
_logger.LogUsingServiceAccountKeyFile();
7171
}
7272
else
7373
{
7474
// Validate JSON format
7575
try
7676
{
7777
System.Text.Json.JsonDocument.Parse(serviceAccountKey);
78-
_logger.LogDebug("Using service account key JSON string");
78+
_logger.LogUsingServiceAccountKeyJson();
7979
}
8080
catch (System.Text.Json.JsonException)
8181
{
@@ -96,12 +96,12 @@ public override Task<AuthenticationResult> ObtainCredentialAsync(ConnectionSetti
9696
credential.Properties["ProjectId"] = projectId;
9797
}
9898

99-
_logger.LogInformation("Successfully prepared Firebase service account credential");
99+
_logger.LogFirebaseServiceAccountCredentialPrepared();
100100
return Task.FromResult(CreateSuccessResult(credential));
101101
}
102102
catch (Exception ex)
103103
{
104-
_logger.LogError(ex, "Error preparing Firebase service account credential");
104+
_logger.LogFirebaseServiceAccountCredentialError(ex);
105105
return Task.FromResult(CreateFailureResult($"Error preparing credential: {ex.Message}", "CREDENTIAL_ERROR"));
106106
}
107107
}
@@ -114,12 +114,12 @@ public override Task<AuthenticationResult> RefreshCredentialAsync(Authentication
114114
if (existingCredential.AuthenticationType == AuthenticationType.Certificate &&
115115
!string.IsNullOrWhiteSpace(existingCredential.CredentialValue))
116116
{
117-
_logger.LogDebug("Service account credential is still valid, no refresh needed");
117+
_logger.LogServiceAccountCredentialValid();
118118
return Task.FromResult(CreateSuccessResult(existingCredential));
119119
}
120120

121121
// If credential is invalid, obtain a new one
122-
_logger.LogDebug("Service account credential is invalid, obtaining new credential");
122+
_logger.LogServiceAccountCredentialInvalid();
123123
return ObtainCredentialAsync(connectionSettings, cancellationToken);
124124
}
125125
}

src/Deveel.Messaging.Connector.Firebase/Messaging/FirebasePushConnectorLoggerExtensions.cs renamed to src/Deveel.Messaging.Connector.Firebase/Messaging/LoggerExtensions.cs

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace Deveel.Messaging
1010
/// <summary>
1111
/// Provides high-performance logging extensions for Firebase Push Connector operations using source-generated logging methods.
1212
/// </summary>
13-
internal static partial class FirebasePushConnectorLoggerExtensions
13+
internal static partial class LoggerExtensions
1414
{
1515
#region Initialization Logging
1616

@@ -105,5 +105,51 @@ internal static partial class FirebasePushConnectorLoggerExtensions
105105
internal static partial void LogBatchSendFailed(this ILogger logger, Exception exception);
106106

107107
#endregion
108+
109+
#region Firebase Authentication Logging
110+
111+
[LoggerMessage(
112+
EventId = 1401,
113+
Level = LogLevel.Debug,
114+
Message = "Obtaining Firebase service account credential")]
115+
internal static partial void LogObtainingServiceAccountCredential(this ILogger logger);
116+
117+
[LoggerMessage(
118+
EventId = 1402,
119+
Level = LogLevel.Debug,
120+
Message = "Using service account key file")]
121+
internal static partial void LogUsingServiceAccountKeyFile(this ILogger logger);
122+
123+
[LoggerMessage(
124+
EventId = 1403,
125+
Level = LogLevel.Debug,
126+
Message = "Using service account key JSON string")]
127+
internal static partial void LogUsingServiceAccountKeyJson(this ILogger logger);
128+
129+
[LoggerMessage(
130+
EventId = 1404,
131+
Level = LogLevel.Information,
132+
Message = "Successfully prepared Firebase service account credential")]
133+
internal static partial void LogFirebaseServiceAccountCredentialPrepared(this ILogger logger);
134+
135+
[LoggerMessage(
136+
EventId = 1405,
137+
Level = LogLevel.Error,
138+
Message = "Error preparing Firebase service account credential")]
139+
internal static partial void LogFirebaseServiceAccountCredentialError(this ILogger logger, Exception exception);
140+
141+
[LoggerMessage(
142+
EventId = 1406,
143+
Level = LogLevel.Debug,
144+
Message = "Service account credential is still valid, no refresh needed")]
145+
internal static partial void LogServiceAccountCredentialValid(this ILogger logger);
146+
147+
[LoggerMessage(
148+
EventId = 1407,
149+
Level = LogLevel.Debug,
150+
Message = "Service account credential is invalid, obtaining new credential")]
151+
internal static partial void LogServiceAccountCredentialInvalid(this ILogger logger);
152+
153+
#endregion
108154
}
109155
}

src/Deveel.Messaging.Connectors/Messaging/ChannelRegistryBuilder.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,7 @@ internal ChannelRegistryBuilder(IServiceCollection services)
5252
public ChannelRegistryBuilder RegisterConnector<TConnector>(Func<IServiceProvider, IChannelSchema, TConnector>? connectorFactory = null)
5353
where TConnector : class, IChannelConnector
5454
{
55-
_registrationDescriptors.Add(new ConnectorRegistrationDescriptor(typeof(TConnector),
56-
connectorFactory != null
57-
? (serviceProvider, schema) => connectorFactory(serviceProvider, schema)
58-
: null));
59-
60-
return this;
55+
return RegisterConnector(typeof(TConnector), connectorFactory);
6156
}
6257

6358
/// <summary>
@@ -77,7 +72,12 @@ public ChannelRegistryBuilder RegisterConnector(Type connectorType, Func<IServic
7772
throw new ArgumentException($"Type '{connectorType.Name}' must implement {nameof(IChannelConnector)}.", nameof(connectorType));
7873
}
7974

80-
_registrationDescriptors.Add(new ConnectorRegistrationDescriptor(connectorType, connectorFactory));
75+
if (!Attribute.IsDefined(connectorType, typeof(ChannelSchemaAttribute)))
76+
throw new ArgumentException($"Type '{connectorType.Name}' must be decorated with {nameof(ChannelSchemaAttribute)}.", nameof(connectorType));
77+
78+
_registrationDescriptors.Add(new ConnectorRegistrationDescriptor(connectorType,
79+
connectorFactory != null ? (serviceProvider, schema) => connectorFactory(serviceProvider, schema) : null));
80+
8181

8282
return this;
8383
}

src/Deveel.Messaging.Connectors/Messaging/ConnectorDescriptor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public ConnectorDescriptor(Type connectorType, IChannelSchema schema)
5252
/// <summary>
5353
/// Gets the display name from the master schema, or the connector type name if not available.
5454
/// </summary>
55-
public string DisplayName => Schema.DisplayName ?? ConnectorType.Name;
55+
public string DisplayName => String.IsNullOrWhiteSpace(Schema.DisplayName) ? ConnectorType.Name : Schema.DisplayName;
5656

5757
/// <summary>
5858
/// Gets the capabilities supported by the connector from the master schema.

0 commit comments

Comments
 (0)