Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update npgsqldbtype for created date time #90

Merged
merged 9 commits into from
Mar 18, 2024
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
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using Altinn.Auth.AuditLog.Functions.Clients;
using Altinn.Auth.AuditLog.Core.Enum;
using Altinn.Auth.AuditLog.Core.Models;
using Altinn.Auth.AuditLog.Functions.Clients;
using Altinn.Auth.AuditLog.Functions.Clients.Interfaces;
using Altinn.Auth.AuditLog.Functions.Configuration;
using Altinn.Auth.AuditLog.Functions.Enum;
using Altinn.Auth.AuditLog.Functions.Models;
using Altinn.Auth.AuditLog.Functions.Tests.Helpers;
using Azure.Messaging;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -30,7 +30,7 @@ public class AuditLogClientTest
{
UserId = 20000003,
Created = DateTime.UtcNow,
AuthenticationMethod = Enum.AuthenticationMethod.BankID,
AuthenticationMethod = AuthenticationMethod.BankID,
EventType = AuthenticationEventType.Authenticate,
AuthenticationLevel = SecurityLevel.VerySensitive,
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Altinn.Auth.AuditLog.Functions.Clients.Interfaces;
using Altinn.Auth.AuditLog.Functions.Models;
using Altinn.Auth.AuditLog.Core.Models;
using Altinn.Auth.AuditLog.Functions.Clients.Interfaces;
using Altinn.Auth.AuditLog.Functions.Tests.Helpers;
using Altinn.Auth.AuditLog.Functions.Tests.Utils;
using Azure.Messaging;
Expand All @@ -22,7 +22,7 @@ public class AuthorizationEventsProcessorTest
public async Task Run_ConfirmDeserializationOfAuthorizationEvent()
{
string serializedAuthorizationEvent = "{" +
"\"Created\":\"0001-01-01T00:00:00\",\"SubjectOrgCode\":\"skd\"," +
"\"Created\":\"2018-05-15T02:05:00+00:00\",\"SubjectOrgCode\":\"skd\"," +
"\"ResourcePartyId\":1000,\"Resource\":\"taxreport\",\"InstanceId\":\"1000/26133fb5-a9f2-45d4-90b1-f6d93ad40713\"," +
"\"Operation\":\"read\",\"IpAdress\":\"192.0.2.1\"," +
"\"ContextRequestJson\":\"{\\u0022ReturnPolicyIdList\\u0022:false,\\u0022CombinedDecision\\u0022:false,\\u0022XPathVersion\\u0022:null,\\u0022Attributes\\u0022:[{\\u0022Id\\u0022:null,\\u0022Content\\u0022:null,\\u0022Attributes\\u0022:[{\\u0022Issuer\\u0022:null,\\u0022AttributeId\\u0022:\\u0022urn:altinn:org\\u0022,\\u0022IncludeInResult\\u0022:false,\\u0022AttributeValues\\u0022:[{\\u0022Value\\u0022:\\u0022skd\\u0022,\\u0022DataType\\u0022:\\u0022http://www.w3.org/2001/XMLSchema#string\\u0022,\\u0022Attributes\\u0022:[{\\u0022IsNamespaceDeclaration\\u0022:false,\\u0022Name\\u0022:{\\u0022LocalName\\u0022:\\u0022DataType\\u0022,\\u0022Namespace\\u0022:{\\u0022NamespaceName\\u0022:\\u0022\\u0022},\\u0022NamespaceName\\u0022:\\u0022\\u0022},\\u0022NextAttribute\\u0022:null,\\u0022NodeType\\u0022:2,\\u0022PreviousAttribute\\u0022:null,\\u0022Value\\u0022:\\u0022http://www.w3.org/2001/XMLSchema#string\\u0022,\\u0022BaseUri\\u0022:\\u0022\\u0022,\\u0022Document\\u0022:null,\\u0022Parent\\u0022:null}],\\u0022Elements\\u0022:[]}]}],\\u0022Category\\u0022:\\u0022urn:oasis:names:tc:xacml:1.0:subject-category:access-subject\\u0022},{\\u0022Id\\u0022:null,\\u0022Content\\u0022:null,\\u0022Attributes\\u0022:[{\\u0022Issuer\\u0022:null,\\u0022AttributeId\\u0022:\\u0022urn:altinn:instance-id\\u0022,\\u0022IncludeInResult\\u0022:false,\\u0022AttributeValues\\u0022:[{\\u0022Value\\u0022:\\u00221000/26133fb5-a9f2-45d4-90b1-f6d93ad40713\\u0022,\\u0022DataType\\u0022:\\u0022http://www.w3.org/2001/XMLSchema#string\\u0022,\\u0022Attributes\\u0022:[{\\u0022IsNamespaceDeclaration\\u0022:false,\\u0022Name\\u0022:{\\u0022LocalName\\u0022:\\u0022DataType\\u0022,\\u0022Namespace\\u0022:{\\u0022NamespaceName\\u0022:\\u0022\\u0022},\\u0022NamespaceName\\u0022:\\u0022\\u0022},\\u0022NextAttribute\\u0022:null,\\u0022NodeType\\u0022:2,\\u0022PreviousAttribute\\u0022:null,\\u0022Value\\u0022:\\u0022http://www.w3.org/2001/XMLSchema#string\\u0022,\\u0022BaseUri\\u0022:\\u0022\\u0022,\\u0022Document\\u0022:null,\\u0022Parent\\u0022:null}],\\u0022Elements\\u0022:[]}]},{\\u0022Issuer\\u0022:null,\\u0022AttributeId\\u0022:\\u0022urn:altinn:org\\u0022,\\u0022IncludeInResult\\u0022:false,\\u0022AttributeValues\\u0022:[{\\u0022Value\\u0022:\\u0022skd\\u0022,\\u0022DataType\\u0022:\\u0022http://www.w3.org/2001/XMLSchema#string\\u0022,\\u0022Attributes\\u0022:[],\\u0022Elements\\u0022:[]}]},{\\u0022Issuer\\u0022:null,\\u0022AttributeId\\u0022:\\u0022urn:altinn:app\\u0022,\\u0022IncludeInResult\\u0022:false,\\u0022AttributeValues\\u0022:[{\\u0022Value\\u0022:\\u0022taxreport\\u0022,\\u0022DataType\\u0022:\\u0022http://www.w3.org/2001/XMLSchema#string\\u0022,\\u0022Attributes\\u0022:[],\\u0022Elements\\u0022:[]}]},{\\u0022Issuer\\u0022:null,\\u0022AttributeId\\u0022:\\u0022urn:altinn:task\\u0022,\\u0022IncludeInResult\\u0022:false,\\u0022AttributeValues\\u0022:[{\\u0022Value\\u0022:\\u0022Task_1\\u0022,\\u0022DataType\\u0022:\\u0022http://www.w3.org/2001/XMLSchema#string\\u0022,\\u0022Attributes\\u0022:[],\\u0022Elements\\u0022:[]}]},{\\u0022Issuer\\u0022:null,\\u0022AttributeId\\u0022:\\u0022urn:altinn:partyid\\u0022,\\u0022IncludeInResult\\u0022:true,\\u0022AttributeValues\\u0022:[{\\u0022Value\\u0022:\\u00221000\\u0022,\\u0022DataType\\u0022:\\u0022http://www.w3.org/2001/XMLSchema#string\\u0022,\\u0022Attributes\\u0022:[],\\u0022Elements\\u0022:[]}]}],\\u0022Category\\u0022:\\u0022urn:oasis:names:tc:xacml:3.0:attribute-category:resource\\u0022},{\\u0022Id\\u0022:null,\\u0022Content\\u0022:null,\\u0022Attributes\\u0022:[{\\u0022Issuer\\u0022:null,\\u0022AttributeId\\u0022:\\u0022urn:oasis:names:tc:xacml:1.0:action:action-id\\u0022,\\u0022IncludeInResult\\u0022:false,\\u0022AttributeValues\\u0022:[{\\u0022Value\\u0022:\\u0022read\\u0022,\\u0022DataType\\u0022:\\u0022http://www.w3.org/2001/XMLSchema#string\\u0022,\\u0022Attributes\\u0022:[{\\u0022IsNamespaceDeclaration\\u0022:false,\\u0022Name\\u0022:{\\u0022LocalName\\u0022:\\u0022DataType\\u0022,\\u0022Namespace\\u0022:{\\u0022NamespaceName\\u0022:\\u0022\\u0022},\\u0022NamespaceName\\u0022:\\u0022\\u0022},\\u0022NextAttribute\\u0022:null,\\u0022NodeType\\u0022:2,\\u0022PreviousAttribute\\u0022:null,\\u0022Value\\u0022:\\u0022http://www.w3.org/2001/XMLSchema#string\\u0022,\\u0022BaseUri\\u0022:\\u0022\\u0022,\\u0022Document\\u0022:null,\\u0022Parent\\u0022:null}],\\u0022Elements\\u0022:[]}]}],\\u0022Category\\u0022:\\u0022urn:oasis:names:tc:xacml:3.0:attribute-category:action\\u0022},{\\u0022Id\\u0022:null,\\u0022Content\\u0022:null,\\u0022Attributes\\u0022:[],\\u0022Category\\u0022:\\u0022urn:oasis:names:tc:xacml:3.0:attribute-category:environment\\u0022}],\\u0022RequestReferences\\u0022:[]}\"}";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Altinn.Auth.AuditLog.Functions.Clients.Interfaces;
using Altinn.Auth.AuditLog.Functions.Enum;
using Altinn.Auth.AuditLog.Functions.Models;
using Altinn.Auth.AuditLog.Core.Enum;
using Altinn.Auth.AuditLog.Core.Models;
using Altinn.Auth.AuditLog.Functions.Clients.Interfaces;
using Azure.Messaging;
using Microsoft.Extensions.Logging;
using Moq;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
using Altinn.Auth.AuditLog.Functions.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using Altinn.Auth.AuditLog.Core.Models;

namespace Altinn.Auth.AuditLog.Functions.Tests.Helpers
{
Expand All @@ -15,6 +9,7 @@ public static AuthorizationEvent GetAuthorizationEvent()
AuthorizationEvent authorizationEvent = new AuthorizationEvent()
{
SubjectUserId = 2000000,
Created = new DateTimeOffset(2018, 05, 15, 02, 05, 00, TimeSpan.Zero),
ResourcePartyId = 1000,
Resource = "taxreport",
InstanceId = "1000/26133fb5-a9f2-45d4-90b1-f6d93ad40713",
Expand Down
10 changes: 5 additions & 5 deletions Altinn.Auth.AuditLog.Functions.Tests/Utils/AssertionUtil.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
using System;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Altinn.Auth.AuditLog.Functions.Models;
using Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.Resources;
using Action = Altinn.Auth.AuditLog.Functions.Models.Action;
using Resources = Altinn.Auth.AuditLog.Functions.Models.Resource;
using Attribute = Altinn.Auth.AuditLog.Functions.Models.Attribute;
using Action = Altinn.Auth.AuditLog.Core.Models.Action;
using Resources = Altinn.Auth.AuditLog.Core.Models.Resource;
using Attribute = Altinn.Auth.AuditLog.Core.Models.Attribute;
using Altinn.Auth.AuditLog.Core.Models;

namespace Altinn.Auth.AuditLog.Functions.Tests.Utils
{
Expand Down
5 changes: 3 additions & 2 deletions src/Altinn.Auth.AuditLog.Core/Models/AuthenticationEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ public class AuthenticationEvent
public string? ExternalTokenIssuer { get; set; }

/// <summary>
/// Date, time of the authentication event. Set by producer of logevents
/// Date and time of the authentication event. Set by producer of logevents
/// </summary>
public DateTime Created { get; set; }
[Required]
public DateTimeOffset? Created { get; set; }

/// <summary>
/// Id of the user that triggered that authentication event
Expand Down
9 changes: 5 additions & 4 deletions src/Altinn.Auth.AuditLog.Core/Models/AuthorizationEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ public class AuthorizationEvent
public string? SessionId { get; set; }

/// <summary>
/// Date, time of the authorization event. Set by producer of logevents
/// Date and time of the authorization event. Set by producer of logevents
/// </summary>
public DateTime Created { get; set; }
[Required]
public DateTimeOffset? Created { get; set; }

/// <summary>
/// The userid for the user that requested authorization
Expand Down Expand Up @@ -58,7 +59,7 @@ public class AuthorizationEvent
/// <summary>
/// Type of operation
/// </summary>
public string Operation { get; set; }
public required string Operation { get; set; }

/// <summary>
/// The Ip adress of the calling party
Expand All @@ -68,7 +69,7 @@ public class AuthorizationEvent
/// <summary>
/// The enriched context request
/// </summary>
public string ContextRequestJson { get; set; }
public required string ContextRequestJson { get; set; }

/// <summary>
/// Decision for the authorization request
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace Altinn.Auth.AuditLog.Persistence
public class AuthenticationEventRepository : IAuthenticationEventRepository
{
private readonly ILogger _logger;
private readonly NpgsqlDataSource _dataSource;
private readonly NpgsqlDataSource _dataSource;

/// <summary>
/// Initializes a new instance of the <see cref="AuthenticationEventRepository"/> class
Expand All @@ -30,7 +30,7 @@ public AuthenticationEventRepository(
ILogger<AuthenticationEventRepository> logger)
{
_dataSource = dataSource;
_logger = logger;
_logger = logger;
}

public async Task InsertAuthenticationEvent(AuthenticationEvent authenticationEvent)
Expand Down Expand Up @@ -72,7 +72,12 @@ INSERT INTO authentication.eventlog(
{
throw new ArgumentNullException(nameof(authenticationEvent));
}


if (!authenticationEvent.Created.HasValue)
{
throw new ArgumentNullException(nameof(AuthenticationEvent.Created));
}

try
{
await using NpgsqlCommand pgcom = _dataSource.CreateCommand(INSERTAUTHNEVENT);
Expand All @@ -81,7 +86,7 @@ INSERT INTO authentication.eventlog(
pgcom.Parameters.AddWithValue("externalsessionid", NpgsqlTypes.NpgsqlDbType.Text, string.IsNullOrEmpty(authenticationEvent.ExternalSessionId) ? DBNull.Value : authenticationEvent.ExternalSessionId);
pgcom.Parameters.AddWithValue("subscriptionkey", NpgsqlTypes.NpgsqlDbType.Text, string.IsNullOrEmpty(authenticationEvent.SubscriptionKey) ? DBNull.Value : authenticationEvent.SubscriptionKey);
pgcom.Parameters.AddWithValue("externaltokenissuer", NpgsqlTypes.NpgsqlDbType.Text, string.IsNullOrEmpty(authenticationEvent.ExternalTokenIssuer) ? DBNull.Value : authenticationEvent.ExternalTokenIssuer);
pgcom.Parameters.AddWithValue("created", NpgsqlTypes.NpgsqlDbType.Timestamp, authenticationEvent.Created == DateTime.MinValue ? DBNull.Value : authenticationEvent.Created);
pgcom.Parameters.AddWithValue("created", NpgsqlTypes.NpgsqlDbType.TimestampTz, authenticationEvent.Created.Value.ToOffset(TimeSpan.Zero));
pgcom.Parameters.AddWithValue("userid", NpgsqlTypes.NpgsqlDbType.Integer, (authenticationEvent.UserId == null) ? DBNull.Value : authenticationEvent.UserId);
pgcom.Parameters.AddWithValue("supplierid", NpgsqlTypes.NpgsqlDbType.Text, string.IsNullOrEmpty(authenticationEvent.SupplierId) ? DBNull.Value : authenticationEvent.SupplierId);
pgcom.Parameters.AddWithValue("orgnumber", NpgsqlTypes.NpgsqlDbType.Integer, (authenticationEvent.OrgNumber == null) ? DBNull.Value : authenticationEvent.OrgNumber);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class AuthorizationEventRepository : IAuthorizationEventRepository
{
private readonly ILogger _logger;
private readonly NpgsqlDataSource _dataSource;

/// <summary>
/// Initializes a new instance of the <see cref="AuthorizationEventRepository"/> class
/// </summary>
Expand All @@ -33,7 +33,6 @@ public AuthorizationEventRepository(
{
_dataSource = dataSource;
_logger = logger;

}

public async Task InsertAuthorizationEvent(AuthorizationEvent authorizationEvent)
Expand Down Expand Up @@ -75,12 +74,27 @@ INSERT INTO authz.eventlog(
{
throw new ArgumentNullException(nameof(authorizationEvent));
}


if (!authorizationEvent.Created.HasValue)
{
throw new ArgumentNullException(nameof(authorizationEvent), "Created must not be null");
}

if (string.IsNullOrEmpty(authorizationEvent.Operation))
{
throw new ArgumentNullException(nameof(authorizationEvent), "Operation must not be null or empty");
}

if (string.IsNullOrEmpty(authorizationEvent.ContextRequestJson))
{
throw new ArgumentNullException(nameof(authorizationEvent), "Context request must not be null or empty");
}

try
{
await using NpgsqlCommand pgcom = _dataSource.CreateCommand(INSERTAUTHZEVENT);
pgcom.Parameters.AddWithValue("sessionid", NpgsqlTypes.NpgsqlDbType.Text, string.IsNullOrEmpty(authorizationEvent.SessionId) ? DBNull.Value : authorizationEvent.SessionId);
pgcom.Parameters.AddWithValue("created", NpgsqlTypes.NpgsqlDbType.Timestamp, authorizationEvent.Created == DateTime.MinValue ? DBNull.Value : authorizationEvent.Created);
pgcom.Parameters.AddWithValue("created", NpgsqlTypes.NpgsqlDbType.TimestampTz, authorizationEvent.Created.Value.ToOffset(TimeSpan.Zero));
pgcom.Parameters.AddWithValue("subjectuserid", NpgsqlTypes.NpgsqlDbType.Integer, (authorizationEvent.SubjectUserId == null) ? DBNull.Value : authorizationEvent.SubjectUserId);
pgcom.Parameters.AddWithValue("subjectorgcode", NpgsqlTypes.NpgsqlDbType.Text, string.IsNullOrEmpty(authorizationEvent.SubjectOrgCode) ? DBNull.Value : authorizationEvent.SubjectOrgCode);
pgcom.Parameters.AddWithValue("subjectorgnumber", NpgsqlTypes.NpgsqlDbType.Integer, (authorizationEvent.SubjectOrgNumber == null) ? DBNull.Value : authorizationEvent.SubjectOrgNumber);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.17.2" />
<PackageReference Include="Microsoft.Extensions.Azure" Version="1.7.2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Altinn.Auth.AuditLog.Core\Altinn.Auth.AuditLog.Core.csproj" />
</ItemGroup>
<ItemGroup>
<None Update="host.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
using System;
using System.Text.Json;
using System.Text.Json.Serialization;
using Altinn.Auth.AuditLog.Core.Models;
using Altinn.Auth.AuditLog.Functions.Clients.Interfaces;
using Altinn.Auth.AuditLog.Functions.Models;
using Azure.Storage.Queues.Models;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Altinn.Auth.AuditLog.Functions
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Altinn.Auth.AuditLog.Functions.Clients.Interfaces;
using Altinn.Auth.AuditLog.Core.Models;
using Altinn.Auth.AuditLog.Functions.Clients.Interfaces;
using Altinn.Auth.AuditLog.Functions.Configuration;
using Altinn.Auth.AuditLog.Functions.Models;
using Azure.Messaging;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
using Altinn.Auth.AuditLog.Functions.Models;
using Azure.Messaging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Altinn.Auth.AuditLog.Core.Models;

namespace Altinn.Auth.AuditLog.Functions.Clients.Interfaces
{
Expand Down

This file was deleted.

Loading
Loading