Skip to content

Commit

Permalink
script to change the database and add partitions (#144)
Browse files Browse the repository at this point in the history
* script to change the database and add partitions

* added one more partition for 2024

* background service to create partitions and relevant integration test

* refactor program.cs, partitioncreationhostedservice

* log for checking the dispose error

* fixed dispose and updated list to readonlylist

* Fix failing tests, remove unnecessary files

* Update build-analyze.yml to use linux runner (#146)

* Update build-analyze.yml to use linux runner

* removed unecessary commands

* updated comment and removed empty lines

* add webjobs package

* logging for testing

* remove noincremental from build

* update the test report path

* update sonar reportspaths

* removed admin from app datasource

* fixed pr comments

* fixed PR comments

* Fixed PR comments and removed unecessary methods

* Removed postgressqlsettings, added new setting format in development json

---------

Co-authored-by: acn-dgopa <acn-dgopa@dev-acn-tje-14>
  • Loading branch information
acn-dgopa and acn-dgopa authored Nov 8, 2024
1 parent 4be7cf9 commit d5f31c2
Show file tree
Hide file tree
Showing 86 changed files with 1,313 additions and 15,981 deletions.
33 changes: 19 additions & 14 deletions .github/workflows/build-analyze.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
analyze:
name: Analyze
if: ((github.event_name == 'pull_request' && github.event.pull_request.head.repo.fork == false) || github.event_name == 'push') && github.repository_owner == 'Altinn' && github.actor != 'dependabot[bot]'
runs-on: windows-latest
runs-on: ubuntu-latest
steps:
- name: Setup .NET
uses: actions/setup-dotnet@v4
Expand All @@ -39,7 +39,7 @@ jobs:
- name: Set up JDK 11
uses: actions/setup-java@v4
with:
distribution: 'microsoft'
distribution: "microsoft"
java-version: 17
- uses: actions/checkout@v4
with:
Expand All @@ -59,23 +59,28 @@ jobs:
restore-keys: ${{ runner.os }}-sonar-scanner
- name: Install SonarCloud scanner
if: steps.cache-sonar-scanner.outputs.cache-hit != 'true'
shell: powershell
shell: bash
run: |
New-Item -Path .\.sonar\scanner -ItemType Directory
dotnet tool update dotnet-sonarscanner --tool-path .\.sonar\scanner
mkdir -p ./.sonar/scanner
dotnet tool update dotnet-sonarscanner --tool-path ./.sonar/scanner
- name: Analyze
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
shell: powershell
shell: bash
run: |
.\.sonar\scanner\dotnet-sonarscanner begin /k:"Altinn_altinn-auth-audit-log" /o:"altinn" /d:sonar.login="${{ secrets.SONAR_TOKEN }}" /d:sonar.host.url="https://sonarcloud.io" /d:sonar.cs.vstest.reportsPaths="**/*.trx" /d:sonar.cs.opencover.reportsPaths="**/coverage.opencover.xml" /d:sonar.cpd.exclusions="**/Swagger/*Filter.cs"
set -ex
dotnet tool install --global dotnet-coverage
./.sonar/scanner/dotnet-sonarscanner begin \
/k:"Altinn_altinn-auth-audit-log" /o:"altinn" \
/d:sonar.token="${{ secrets.SONAR_TOKEN }}" \
/d:sonar.host.url="https://sonarcloud.io" \
/d:sonar.cs.vstest.reportsPaths="TestResults/**/*.trx" \
/d:sonar.cs.vscoveragexml.reportsPaths="TestResults/coverage.xml" \
/d:sonar.cpd.exclusions="**/Swagger/*Filter.cs"
dotnet build Altinn.Auth.AuditLog.sln
dotnet test Altinn.Auth.AuditLog.sln `
--no-build `
--results-directory TestResults/ `
--collect:"XPlat Code Coverage" `
-- DataCollectionRunSettings.DataCollectors.DataCollector.Configuration.Format=opencover
dotnet build
dotnet coverage collect 'dotnet test --no-build --results-directory TestResults/' -f xml -o 'TestResults/coverage.xml'
.\.sonar\scanner\dotnet-sonarscanner end /d:sonar.login="${{ secrets.SONAR_TOKEN }}"
./.sonar/scanner/dotnet-sonarscanner end /d:sonar.token="${{ secrets.SONAR_TOKEN }}"
2 changes: 2 additions & 0 deletions Altinn.Auth.AuditLog.sln
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Altinn.Auth.AuditLog.Tests"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Altinn.Auth.AuditLog.Functions.Tests", "test\Altinn.Auth.AuditLog.Functions.Tests\Altinn.Auth.AuditLog.Functions.Tests.csproj", "{90D12F35-F9DE-4CB1-9D98-F907E3BBAAF6}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{C4B0F39E-5901-4534-AC24-B800959D6B90}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down
19 changes: 19 additions & 0 deletions src/Altinn.Auth.AuditLog.Core/Models/Partition.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Altinn.Auth.AuditLog.Core.Models
{
/// <summary>
/// Used for partition creation
/// </summary>
public sealed record Partition
{
public required string Name { get; set; }
public required DateOnly StartDate { get; set; }
public required DateOnly EndDate { get; set;}
public required string SchemaName { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Altinn.Auth.AuditLog.Core.Models;
using Altinn.Auth.AuditLog.Core.Models;
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -12,6 +12,11 @@ namespace Altinn.Auth.AuditLog.Core.Repositories.Interfaces
/// </summary>
public interface IAuthenticationEventRepository
{
/// <summary>
/// inserts an authentication event to the database
/// </summary>
/// <param name="authenticationEvent"></param>
/// <returns></returns>
Task InsertAuthenticationEvent(AuthenticationEvent authenticationEvent);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Altinn.Auth.AuditLog.Core.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Altinn.Auth.AuditLog.Core.Repositories.Interfaces
{
/// <summary>
/// Interface for PostgresSQL operations on partition management
/// </summary>
public interface IPartitionManagerRepository
{
/// <summary>
/// Checks and creates necessary partition for authentication event table
/// </summary>
/// <param name="partitions">the list of partitions to be created</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/>.</param>
/// <returns>true if the partition is created</returns>
Task CreatePartitions(IReadOnlyList<Partition> partitions, CancellationToken cancellationToken = default);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Altinn.Auth.AuditLog.Core.Models;
using Altinn.Auth.AuditLog.Core.Models;
using Altinn.Auth.AuditLog.Core.Repositories.Interfaces;
using Altinn.Auth.AuditLog.Core.Services.Interfaces;
using Microsoft.Extensions.Logging;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Altinn.Auth.AuditLog.Core.Models;
using Altinn.Auth.AuditLog.Core.Models;
using System;
using System.Collections.Generic;
using System.Linq;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,30 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<GenerateEmbeddedFilesManifest>true</GenerateEmbeddedFilesManifest>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Altinn.Authorization.ServiceDefaults" Version="2.6.1" />
<PackageReference Include="Altinn.Authorization.ServiceDefaults.Npgsql" Version="2.6.1" />
<PackageReference Include="Altinn.Authorization.ServiceDefaults.Npgsql.Yuniql" Version="2.6.1" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.1" />
<PackageReference Include="Npgsql" Version="8.0.5" />
<PackageReference Include="Microsoft.Extensions.FileProviders.Embedded" Version="8.0.10" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Altinn.Auth.AuditLog.Core\Altinn.Auth.AuditLog.Core.csproj" />
</ItemGroup>

<ItemGroup>
<InternalsVisibleTo Include="$(AssemblyName).Tests" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="Migration/**/*.sql" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Altinn.Auth.AuditLog.Core.Models;
using Altinn.Auth.AuditLog.Core.Repositories.Interfaces;
using Altinn.Auth.AuditLog.Persistence.Configuration;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Npgsql;

namespace Altinn.Auth.AuditLog.Persistence
Expand All @@ -18,55 +10,57 @@ 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
/// </summary>
/// <param name="dataSource">The postgreSQL datasource for AuditLogDB</param>
/// <param name="logger">handler for logger service</param>
public AuthenticationEventRepository(
NpgsqlDataSource dataSource,
public AuthenticationEventRepository(NpgsqlDataSource dataSource,
ILogger<AuthenticationEventRepository> logger)
{
_dataSource = dataSource;
_logger = logger;
}

/// <inheritdoc/>
public async Task InsertAuthenticationEvent(AuthenticationEvent authenticationEvent)
{
const string INSERTAUTHNEVENT = /*strpsql*/@"
INSERT INTO authentication.eventlog(
sessionid,
externalsessionid,
subscriptionkey,
externaltokenissuer,
created,
userid,
supplierid,
orgnumber,
eventtypeid,
authenticationmethodid,
authenticationlevelid,
ipaddress,
isauthenticated
const string INSERTAUTHNEVENT = /*strpsql*/
"""
INSERT INTO authentication.eventlogv1 (
sessionid,
externalsessionid,
subscriptionkey,
externaltokenissuer,
created,
userid,
supplierid,
orgnumber,
eventtypeid,
authenticationmethodid,
authenticationlevelid,
ipaddress,
isauthenticated
)
VALUES (
@sessionid,
@externalsessionid,
@subscriptionkey,
@externaltokenissuer,
@created,
@userid,
@supplierid,
@orgnumber,
@eventtypeid,
@authenticationmethodid,
@authenticationlevelid,
@ipaddress,
@isauthenticated
@sessionid,
@externalsessionid,
@subscriptionkey,
@externaltokenissuer,
@created,
@userid,
@supplierid,
@orgnumber,
@eventtypeid,
@authenticationmethodid,
@authenticationlevelid,
@ipaddress,
@isauthenticated
)
RETURNING *;";
RETURNING *;
""";

if (authenticationEvent == null)
{
Expand Down Expand Up @@ -104,6 +98,6 @@ INSERT INTO authentication.eventlog(
_logger.LogError(e, "AuditLog // AuditLogMetadataRepository // InsertAuthenticationEvent // Exception");
throw;
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,7 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Altinn.Auth.AuditLog.Core.Models;
using Altinn.Auth.AuditLog.Core.Repositories;
using Altinn.Auth.AuditLog.Core.Repositories.Interfaces;
using Altinn.Auth.AuditLog.Persistence.Configuration;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Npgsql;

namespace Altinn.Auth.AuditLog.Persistence
Expand All @@ -37,38 +27,40 @@ public AuthorizationEventRepository(

public async Task InsertAuthorizationEvent(AuthorizationEvent authorizationEvent)
{
const string INSERTAUTHZEVENT = /*strpsql*/@"
INSERT INTO authz.eventlog(
sessionid,
created,
subjectuserid,
subjectorgcode,
subjectorgnumber,
subjectparty,
resourcepartyid,
resource,
instanceid,
operation,
ipaddress,
contextrequestjson,
decision
const string INSERTAUTHZEVENT = /*strpsql*/
"""
INSERT INTO authz.eventlogv1(
sessionid,
created,
subjectuserid,
subjectorgcode,
subjectorgnumber,
subjectparty,
resourcepartyid,
resource,
instanceid,
operation,
ipaddress,
contextrequestjson,
decision
)
VALUES (
@sessionid,
@created,
@subjectuserid,
@subjectorgcode,
@subjectorgnumber,
@subjectparty,
@resourcepartyid,
@resource,
@instanceid,
@operation,
@ipaddress,
@contextrequestjson,
@decision
@sessionid,
@created,
@subjectuserid,
@subjectorgcode,
@subjectorgnumber,
@subjectparty,
@resourcepartyid,
@resource,
@instanceid,
@operation,
@ipaddress,
@contextrequestjson,
@decision
)
RETURNING *;";
RETURNING *;
""";

if (authorizationEvent == null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ public class PostgreSQLSettings
/// </summary>
public string AuthAuditLogDbPwd { get; set; }

/// <summary>
/// Connection string for the postgres db
/// </summary>
public string AdminConnectionString { get; set; }

/// <summary>
/// Password for app user for the postgres db
/// </summary>
public string AuthAuditLogDbAdminPwd { get; set; }

/// <summary>
/// Gets or sets a value indicating whether to include parameter values in logging/tracing.
/// </summary>
Expand Down
Loading

0 comments on commit d5f31c2

Please sign in to comment.