Skip to content

Commit e9d5500

Browse files
author
Espen Gulbrandsen
committed
Merge branch 'feature/657-create-layers' into feature/658-log-entries
2 parents 857a521 + 87a2935 commit e9d5500

File tree

11 files changed

+335
-54
lines changed

11 files changed

+335
-54
lines changed

src/Events/Altinn.Platform.Events.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<PropertyGroup>
33
<TargetFramework>net9.0</TargetFramework>
44
<GenerateDocumentationFile>true</GenerateDocumentationFile>
5+
<UserSecretsId>a4f2b926-afa9-4af7-a7a5-5ddbaccb203e</UserSecretsId>
56
<!-- SonarCloud needs this -->
67
<ProjectGuid>{6599d6d0-228d-4057-87c3-31cd33068bbd}</ProjectGuid>
78
</PropertyGroup>

src/Events/Controllers/StorageController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public async Task<ActionResult<string>> Post([FromBody] CloudEvent cloudEvent)
6161
}
6262
catch (Exception e)
6363
{
64-
_logger.LogError(e, "Temporarily unable to save cloudEventId {cloudEventId} to storage, please try again.", cloudEvent?.Id);
64+
_logger.LogError(e, "Temporarily unable to save cloudEventId {CloudEventId} to storage, please try again.", cloudEvent?.Id);
6565
return StatusCode(503, e.Message);
6666
}
6767
}

src/Events/Models/LogEntryDto.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using System;
1+
#nullable enable
2+
3+
using System;
24
using System.Net;
35
using CloudNative.CloudEvents;
46

@@ -12,36 +14,36 @@ public record LogEntryDto
1214
/// <summary>
1315
/// The cloud event id associated with the logged event <see cref="CloudEvent"/>"/>
1416
/// </summary>
15-
public string CloudEventId { get; set; }
17+
public string? CloudEventId { get; set; }
1618

1719
/// <summary>
1820
/// The resource associated with the cloud event <see cref="CloudEvent"/>
1921
/// </summary>
20-
public string CloudEventResource { get; set; }
22+
public string? CloudEventResource { get; set; }
2123

2224
/// <summary>
2325
/// The type associated with the logged event <see cref="CloudEvent"/>
2426
/// </summary>
25-
public string CloudEventType { get; set; }
27+
public string? CloudEventType { get; set; }
2628

2729
/// <summary>
2830
/// The subscription id associated with the post action. <see cref="Subscription"/>"/>
2931
/// </summary>
30-
public int SubscriptionId { get; set; }
32+
public int? SubscriptionId { get; set; }
3133

3234
/// <summary>
3335
/// The consumer of the event <see cref="Subscription"/>
3436
/// </summary>
35-
public string Consumer { get; set; }
37+
public string? Consumer { get; set; }
3638

3739
/// <summary>
3840
/// The consumers webhook endpoint <see cref="Subscription"/>
3941
/// </summary>
40-
public Uri Endpoint { get; set; }
42+
public Uri? Endpoint { get; set; }
4143

4244
/// <summary>
4345
/// The staus code returned from the subscriber endpoint
4446
/// </summary>
45-
public HttpStatusCode StatusCode { get; set; }
47+
public HttpStatusCode? StatusCode { get; set; }
4648
}
4749
}

src/Events/Models/Subscription.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public class Subscription
6565
public bool Validated { get; set; }
6666

6767
/// <summary>
68-
/// Serializes the subscriont to a JSON string.
68+
/// Serializes the subscription to a JSON string.
6969
/// </summary>
7070
/// <returns>Serialized cloud event</returns>
7171
public string Serialize()
@@ -74,7 +74,7 @@ public string Serialize()
7474
}
7575

7676
/// <summary>
77-
/// Deserializes the subscriont to from JSON string.
77+
/// Deserializes the subscription from a JSON string.
7878
/// </summary>
7979
/// <returns>Cloud event</returns>
8080
public static Subscription Deserialize(string jsonString)

src/Events/Models/TraceLog.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,17 @@ public class TraceLog
1313
/// <summary>
1414
/// Gets or sets the unique identifier for the cloud event.
1515
/// </summary>
16-
public Guid CloudEventId { get; set; }
16+
public Guid? CloudEventId { get; set; }
1717

1818
/// <summary>
1919
/// Gets or sets the resource associated with the trace log entry.
2020
/// </summary>
21-
public string Resource { get; set; } = default!;
21+
public string? Resource { get; set; } = default!;
2222

2323
/// <summary>
2424
/// Gets or sets the type of event being logged. <see cref="CloudEvent"/>
2525
/// </summary>
26-
public string EventType { get; set; } = default!;
26+
public string? EventType { get; set; } = default!;
2727

2828
/// <summary>
2929
/// Gets or sets the consumer associated with the trace log entry.

src/Events/Program.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Diagnostics.CodeAnalysis;
23
using System.IO;
34
using System.Reflection;
45
using System.Text.Json;
@@ -107,6 +108,8 @@ async Task SetConfigurationProviders(ConfigurationManager config)
107108

108109
config.AddJsonFile(configJsonFile2, optional: false, reloadOnChange: true);
109110

111+
config.AddUserSecrets<Program>();
112+
110113
config.AddEnvironmentVariables();
111114

112115
await ConnectToKeyVaultAndSetApplicationInsights(config);
@@ -287,6 +290,7 @@ void ConfigureServices(IServiceCollection services, IConfiguration config)
287290
services.AddSingleton<ITraceLogService, TraceLogService>();
288291
services.AddSingleton<IOutboundService, OutboundService>();
289292
services.AddSingleton<ISubscriptionService, SubscriptionService>();
293+
services.AddSingleton<ITraceLogService, TraceLogService>();
290294
services.AddSingleton<IAppSubscriptionService, AppSubscriptionService>();
291295
services.AddSingleton<IGenericSubscriptionService, GenericSubscriptionService>();
292296
services.AddSingleton<ICloudEventRepository, CloudEventRepository>();
@@ -400,3 +404,14 @@ void Configure(IConfiguration config)
400404

401405
app.MapHealthChecks("/health");
402406
}
407+
408+
/// <summary>
409+
/// Startup class.
410+
/// </summary>
411+
[ExcludeFromCodeCoverage]
412+
public sealed partial class Program
413+
{
414+
private Program()
415+
{
416+
}
417+
}
Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,22 @@
11
{
2+
"$schema": "http://json.schemastore.org/launchsettings.json",
3+
"iisSettings": {
4+
"windowsAuthentication": false,
5+
"anonymousAuthentication": true,
6+
"iisExpress": {
7+
"applicationUrl": "http://localhost:53961/",
8+
"sslPort": 44359
9+
}
10+
},
211
"profiles": {
3-
"IIS Express": {
4-
"commandName": "IISExpress",
5-
"launchBrowser": true,
6-
"launchUrl": "events/swagger",
7-
"environmentVariables": {
8-
"ASPNETCORE_ENVIRONMENT": "Development"
9-
}
10-
},
1112
"Altinn.Platform.Events": {
1213
"commandName": "Project",
1314
"launchBrowser": true,
14-
"launchUrl": "events/swagger",
15+
"launchUrl": "swagger",
1516
"environmentVariables": {
1617
"ASPNETCORE_ENVIRONMENT": "Development"
1718
},
1819
"applicationUrl": "http://localhost:5080"
1920
}
20-
},
21-
"$schema": "http://json.schemastore.org/launchsettings.json",
22-
"iisSettings": {
23-
"windowsAuthentication": false,
24-
"anonymousAuthentication": true,
25-
"iisExpress": {
26-
"applicationUrl": "http://localhost:53961/",
27-
"sslPort": 44359
28-
}
2921
}
30-
}
22+
}

src/Events/Services/Interfaces/ITraceLogService.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
using System;
22
using System.Net;
33
using System.Threading.Tasks;
4+
45
using Altinn.Platform.Events.Models;
6+
57
using CloudNative.CloudEvents;
68

79
namespace Altinn.Platform.Events.Services.Interfaces
@@ -22,7 +24,7 @@ public interface ITraceLogService
2224
/// Log response from webhook post to subscriber.
2325
/// </summary>
2426
/// <param name="logEntryDto">Data transfer object associated with cloud event, status code, and subscription</param>
25-
/// <returns></returns>
27+
/// <returns>A string representation of the GUID or an empty string</returns>
2628
Task<string> CreateWebhookResponseEntry(LogEntryDto logEntryDto);
2729

2830
/// <summary>
@@ -31,7 +33,7 @@ public interface ITraceLogService
3133
/// <param name="cloudEvent">Cloud Event associated with log entry <see cref="CloudEvent"/></param>
3234
/// <param name="subscription">Subscription associated with log entry <see cref="Subscription"/></param>
3335
/// <param name="activity">Type of activity associated with log entry <see cref="TraceLogActivity"/></param>
34-
/// <returns></returns>
36+
/// <returns>Returns empty string if a log entry can't be created</returns>
3537
Task<string> CreateLogEntryWithSubscriptionDetails(CloudEvent cloudEvent, Subscription subscription, TraceLogActivity activity);
3638
}
3739
}

src/Events/Services/OutboundService.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,9 @@ private async Task AuthorizeAndEnqueueOutbound(CloudEvent cloudEvent, Subscripti
100100

101101
await _traceLogService.CreateLogEntryWithSubscriptionDetails(cloudEvent, subscription, TraceLogActivity.OutboundQueue); // log that entry was added to outbound queue
102102
}
103-
else // add unauthorized trace log entry
103+
else
104104
{
105+
// add unauthorized trace log entry
105106
await _traceLogService.CreateLogEntryWithSubscriptionDetails(cloudEvent, subscription, TraceLogActivity.Unauthorized);
106107
}
107108
}

src/Events/Services/TraceLogService.cs

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -75,30 +75,47 @@ public async Task<string> CreateLogEntryWithSubscriptionDetails(CloudEvent cloud
7575
}
7676
}
7777

78-
/// <inheritdoc/>
78+
/// <summary>
79+
/// Log response from webhook post to subscriber.
80+
/// Should be called by the storage controller when a webhook POST response is received. The controller action should handle exceptions
81+
/// for better handling of correct status codes
82+
/// </summary>
83+
/// <param name="logEntryDto">A data transfer object passed to the controller endpoint <see cref="LogEntryDto"/></param>
84+
/// <returns>A string representation of the cloud event id</returns>
7985
public async Task<string> CreateWebhookResponseEntry(LogEntryDto logEntryDto)
8086
{
81-
try
87+
var parseResult = Guid.TryParse(logEntryDto.CloudEventId, out Guid parsedGuid);
88+
89+
if (!parseResult)
8290
{
83-
var traceLogEntry = new TraceLog
84-
{
85-
CloudEventId = Guid.Parse(logEntryDto.CloudEventId),
86-
Resource = logEntryDto.CloudEventResource,
87-
EventType = logEntryDto.CloudEventType,
88-
Consumer = logEntryDto.Consumer,
89-
SubscriberEndpoint = logEntryDto.Endpoint.ToString(),
90-
SubscriptionId = logEntryDto.SubscriptionId,
91-
ResponseCode = (int?)logEntryDto.StatusCode,
92-
Activity = TraceLogActivity.WebhookPostResponse
93-
};
94-
await _traceLogRepository.CreateTraceLogEntry(traceLogEntry);
95-
return logEntryDto.CloudEventId;
91+
_logger.LogError("Error creating trace log entry for webhook POST response: Invalid GUID");
92+
return string.Empty;
9693
}
97-
catch (Exception exception)
94+
95+
if (string.IsNullOrEmpty(logEntryDto.CloudEventId) || logEntryDto.Endpoint == null || logEntryDto.StatusCode == null)
9896
{
99-
_logger.LogError(exception, "Error creating trace log entry for webhook POST response: {Message}", exception.Message);
100-
throw;
97+
_logger.LogError(
98+
"Error creating trace log entry for webhook POST response: Missing required input parameters {Id} {Endpoint} {StatusCode}",
99+
logEntryDto.CloudEventId,
100+
logEntryDto.Endpoint,
101+
logEntryDto.StatusCode);
102+
103+
return string.Empty;
101104
}
105+
106+
var traceLogEntry = new TraceLog
107+
{
108+
CloudEventId = parsedGuid,
109+
Resource = logEntryDto.CloudEventResource,
110+
EventType = logEntryDto.CloudEventType,
111+
Consumer = logEntryDto.Consumer,
112+
SubscriberEndpoint = logEntryDto.Endpoint.ToString(),
113+
SubscriptionId = logEntryDto.SubscriptionId,
114+
ResponseCode = (int?)logEntryDto.StatusCode,
115+
Activity = TraceLogActivity.WebhookPostResponse
116+
};
117+
await _traceLogRepository.CreateTraceLogEntry(traceLogEntry);
118+
return logEntryDto.CloudEventId;
102119
}
103120
}
104121
}

0 commit comments

Comments
 (0)