From a08100c8174d0795b96fb7bebe7c25bb12dd70ba Mon Sep 17 00:00:00 2001 From: Felix Cornelissen Date: Wed, 8 May 2024 18:55:38 +0200 Subject: [PATCH 1/2] add jwt token support, consolidate sources so we don't neet to have a objecttypen client --- .env.local.example | 12 ++-- .gitignore | 2 +- README.md | 18 ++--- src/Kiss.Elastic.Sync/ElasticBulkClient.cs | 6 +- .../ElasticEnterpriseSearchClient.cs | 6 +- src/Kiss.Elastic.Sync/Helpers.cs | 3 +- .../Kiss.Elastic.Sync.csproj | 1 + .../Objecten/ObjectTypesClient.cs | 65 ------------------- .../Objecten/ObjectenClient.cs | 51 ++++++++++++++- .../Sources/ObjectenMedewerkerClient.cs | 31 ++++----- .../Sources/ObjectenVacClient.cs | 25 +++---- .../Sources/SourceFactory.cs | 55 ++++++---------- 12 files changed, 119 insertions(+), 156 deletions(-) delete mode 100644 src/Kiss.Elastic.Sync/Objecten/ObjectTypesClient.cs diff --git a/.env.local.example b/.env.local.example index 1d9a504..7ce07c2 100644 --- a/.env.local.example +++ b/.env.local.example @@ -3,15 +3,19 @@ ENTERPRISE_SEARCH_BASE_URL= ENTERPRISE_SEARCH_PRIVATE_API_KEY= SDG_OBJECTEN_BASE_URL= SDG_OBJECTEN_TOKEN= +SDG_OBJECTEN_CLIENT_ID= +SDG_OBJECTEN_CLIENT_SECRET= SDG_OBJECT_TYPE_URL= MEDEWERKER_OBJECTEN_BASE_URL= MEDEWERKER_OBJECTEN_TOKEN= -MEDEWERKER_OBJECTTYPES_BASE_URL= -MEDEWERKER_OBJECTTYPES_TOKEN= +MEDEWERKER_OBJECTEN_CLIENT_ID= +MEDEWERKER_OBJECTEN_CLIENT_SECRET= +MEDEWERKER_OBJECT_TYPE_URL= VAC_OBJECTEN_BASE_URL= VAC_OBJECTEN_TOKEN= -VAC_OBJECTTYPES_BASE_URL= -VAC_OBJECTTYPES_TOKEN= +VAC_OBJECTEN_CLIENT_ID= +VAC_OBJECTEN_CLIENT_SECRET= +VAC_OBJECT_TYPE_URL= ELASTIC_USERNAME= ELASTIC_PASSWORD= ELASTIC_BASE_URL= \ No newline at end of file diff --git a/.gitignore b/.gitignore index 807c8ed..5fbab97 100644 --- a/.gitignore +++ b/.gitignore @@ -396,5 +396,5 @@ FodyWeavers.xsd # JetBrains Rider *.sln.iml -/.env.local +/.env*.local /src/Kiss.Elastic.Sync/Properties/launchSettings.json diff --git a/README.md b/README.md index 4754b49..3d9d13d 100644 --- a/README.md +++ b/README.md @@ -51,14 +51,14 @@ Examples of how to schedule a cron job in Kubernetes with these arguments [can b ### Variables for the different sources | Variable | Description | | --- | --- | -| SDG_BASE_URL | The base url for the SDG API to retrieve products | -| SDG_API_KEY | The API key to connect to the SDG API | -| MEDEWERKER_OBJECTEN_BASE_URL | The base url for the Objects API to retrieve mededewerkers (smoelenboek) | -| MEDEWERKER_OBJECTEN_TOKEN | The token to connect to the Objects API to retrieve medewerkers (smoelenboek) | -| MEDEWERKER_OBJECTTYPES_BASE_URL | The base url for the Object Types API to retrieve the medewerker object type (smoelenboek) | -| MEDEWERKER_OBJECTTYPES_TOKEN | The token to connect to the Object Types API to retrieve the medewerker object type (smoelenboek) | +| SDG_OBJECTEN_BASE_URL | The base url for the Objects API to retrieve Producten | +| SDG_OBJECT_TYPE_URL | The full url of the object type for Producten | +| SDG_OBJECTEN_TOKEN | The token to connect to the Objects API to retrieve Producten | +| MEDEWERKER_OBJECTEN_BASE_URL | The base url for the Objects API to retrieve mededewerkers (smoelenboek), or the PodiumD Adapter if applicable | +| MEDEWERKER_OBJECT_TYPE_URL | The full url of the object type for medewerkers (smoelenboek) | +| MEDEWERKER_OBJECTEN_TOKEN | The token to connect to the Objects API to retrieve medewerkers (smoelenboek). Use this if you are NOT using the PodiumD Adapter | +| MEDEWERKER_OBJECTEN_CLIENT_ID | The client id to generate a JWT to connect to the PodiumD Adapter to retrieve medewerkers (smoelenboek). This has to match a setting in the PodiumD Adapter | +| MEDEWERKER_OBJECTEN_CLIENT_SECRET | The client secret to generate a JWT to connect to the PodiumD Adapter to retrieve medewerkers (smoelenboek). This has to match a setting in the PodiumD Adapter | | VAC_OBJECTEN_BASE_URL | The base url for the Objects API to retrieve VACs | +| VAC_OBJECT_TYPE_URL | The full url of the object type for VACs | | VAC_OBJECTEN_TOKEN | The token to connect to the Objects API to retrieve VACs | -| VAC_OBJECTTYPES_BASE_URL | The base url for the Object Types API to retrieve the VAC object type | -| VAC_OBJECTTYPES_TOKEN | The token to connect to the Object Types API to retrieve the VAC object type | - diff --git a/src/Kiss.Elastic.Sync/ElasticBulkClient.cs b/src/Kiss.Elastic.Sync/ElasticBulkClient.cs index 5cbb629..3c90f00 100644 --- a/src/Kiss.Elastic.Sync/ElasticBulkClient.cs +++ b/src/Kiss.Elastic.Sync/ElasticBulkClient.cs @@ -37,9 +37,9 @@ public ElasticBulkClient(Uri baseUri, string username, string password) public static ElasticBulkClient Create() { - var elasticBaseUrl = Helpers.GetEnvironmentVariable("ELASTIC_BASE_URL"); - var username = Helpers.GetEnvironmentVariable("ELASTIC_USERNAME"); - var password = Helpers.GetEnvironmentVariable("ELASTIC_PASSWORD"); + var elasticBaseUrl = Helpers.GetRequiredEnvironmentVariable("ELASTIC_BASE_URL"); + var username = Helpers.GetRequiredEnvironmentVariable("ELASTIC_USERNAME"); + var password = Helpers.GetRequiredEnvironmentVariable("ELASTIC_PASSWORD"); if (!Uri.TryCreate(elasticBaseUrl, UriKind.Absolute, out var elasticBaseUri)) { diff --git a/src/Kiss.Elastic.Sync/ElasticEnterpriseSearchClient.cs b/src/Kiss.Elastic.Sync/ElasticEnterpriseSearchClient.cs index e79d258..5e49a93 100644 --- a/src/Kiss.Elastic.Sync/ElasticEnterpriseSearchClient.cs +++ b/src/Kiss.Elastic.Sync/ElasticEnterpriseSearchClient.cs @@ -33,9 +33,9 @@ public ElasticEnterpriseSearchClient(Uri baseUri, string apiKey, string metaEngi public static ElasticEnterpriseSearchClient Create() { - var elasticBaseUrl = Helpers.GetEnvironmentVariable("ENTERPRISE_SEARCH_BASE_URL"); - var elasticApiKey = Helpers.GetEnvironmentVariable("ENTERPRISE_SEARCH_PRIVATE_API_KEY"); - var elasticEngine = Helpers.GetEnvironmentVariable("ENTERPRISE_SEARCH_ENGINE"); + var elasticBaseUrl = Helpers.GetRequiredEnvironmentVariable("ENTERPRISE_SEARCH_BASE_URL"); + var elasticApiKey = Helpers.GetRequiredEnvironmentVariable("ENTERPRISE_SEARCH_PRIVATE_API_KEY"); + var elasticEngine = Helpers.GetRequiredEnvironmentVariable("ENTERPRISE_SEARCH_ENGINE"); if (!Uri.TryCreate(elasticBaseUrl, UriKind.Absolute, out var elasticBaseUri)) { diff --git a/src/Kiss.Elastic.Sync/Helpers.cs b/src/Kiss.Elastic.Sync/Helpers.cs index 88bfd55..fb18dcf 100644 --- a/src/Kiss.Elastic.Sync/Helpers.cs +++ b/src/Kiss.Elastic.Sync/Helpers.cs @@ -22,7 +22,8 @@ public static void CancelSafely(this CancellationTokenSource source) } } - public static string GetEnvironmentVariable(string name) => Environment.GetEnvironmentVariable(name, EnvironmentVariableTarget.Process) ?? throw new Exception("missing environment variable: " + name); + public static string GetRequiredEnvironmentVariable(string name) => GetOptionalEnvironmentVariable(name) ?? throw new Exception("missing environment variable: " + name); + public static string? GetOptionalEnvironmentVariable(string name) => Environment.GetEnvironmentVariable(name, EnvironmentVariableTarget.Process); public static string EncodeCredential(string userName, string password) { diff --git a/src/Kiss.Elastic.Sync/Kiss.Elastic.Sync.csproj b/src/Kiss.Elastic.Sync/Kiss.Elastic.Sync.csproj index b65a2f7..fb925bf 100644 --- a/src/Kiss.Elastic.Sync/Kiss.Elastic.Sync.csproj +++ b/src/Kiss.Elastic.Sync/Kiss.Elastic.Sync.csproj @@ -23,6 +23,7 @@ + diff --git a/src/Kiss.Elastic.Sync/Objecten/ObjectTypesClient.cs b/src/Kiss.Elastic.Sync/Objecten/ObjectTypesClient.cs deleted file mode 100644 index be3af9b..0000000 --- a/src/Kiss.Elastic.Sync/Objecten/ObjectTypesClient.cs +++ /dev/null @@ -1,65 +0,0 @@ -using System.Runtime.CompilerServices; -using System.Text.Json; -using Kiss.Elastic.Sync.Sources; - -namespace Kiss.Elastic.Sync.Objecten -{ - internal class ObjectTypesClient : IDisposable - { - private readonly HttpClient _httpClient; - - public ObjectTypesClient(Uri objectTypesBaseUri, string token) - { - _httpClient = new HttpClient - { - BaseAddress = objectTypesBaseUri - }; - _httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Token", token); - } - - public void Dispose() => _httpClient.Dispose(); - - public IAsyncEnumerable GetObjectTypeUrls(string name, CancellationToken token) => GetObjectTypeUrls(name, "api/v2/objecttypes", token); - - private async IAsyncEnumerable GetObjectTypeUrls(string name, string url, [EnumeratorCancellation] CancellationToken token) - { - string? next = null; - - using (var message = new HttpRequestMessage(HttpMethod.Get, url)) - { - using var response = await _httpClient.SendAsync(message, HttpCompletionOption.ResponseHeadersRead, token); - response.EnsureSuccessStatusCode(); - await using var stream = await response.Content.ReadAsStreamAsync(token); - using var jsonDoc = await JsonDocument.ParseAsync(stream, cancellationToken: token); - - if (!jsonDoc.TryParseZgwPagination(out var pagination)) - { - yield break; - } - - next = pagination.Next; - - foreach (var objectType in pagination.Records) - { - if (objectType.TryGetProperty("name", out var nameProp) && nameProp.ValueKind == JsonValueKind.String && nameProp.ValueEquals(name) && - objectType.TryGetProperty("url", out var objectUrl) && objectUrl.ValueKind == JsonValueKind.String) - { - var result = objectUrl.GetString(); - if (!string.IsNullOrWhiteSpace(result)) - { - yield return result; - } - } - } - }; - - if (!string.IsNullOrWhiteSpace(next)) - { - await foreach (var item in GetObjectTypeUrls(name, next, token)) - { - yield return item; - } - } - } - } -} diff --git a/src/Kiss.Elastic.Sync/Objecten/ObjectenClient.cs b/src/Kiss.Elastic.Sync/Objecten/ObjectenClient.cs index cadd1be..c746472 100644 --- a/src/Kiss.Elastic.Sync/Objecten/ObjectenClient.cs +++ b/src/Kiss.Elastic.Sync/Objecten/ObjectenClient.cs @@ -1,6 +1,11 @@ -using System.Runtime.CompilerServices; +using System.IdentityModel.Tokens.Jwt; +using System.Net.Http.Headers; +using System.Runtime.CompilerServices; +using System.Security.Claims; +using System.Text; using System.Text.Json; using System.Web; +using Microsoft.IdentityModel.Tokens; namespace Kiss.Elastic.Sync.Sources { @@ -10,13 +15,26 @@ public sealed class ObjectenClient { private readonly HttpClient _httpClient; - public ObjectenClient(Uri objectenBaseUri, string objectenToken) + public ObjectenClient(Uri objectenBaseUri, string? objectenToken, string? objectenClientId, string? objectenClientSecret) { _httpClient = new HttpClient { BaseAddress = objectenBaseUri }; - _httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Token", objectenToken); + + if (!string.IsNullOrWhiteSpace(objectenClientId) && !string.IsNullOrWhiteSpace(objectenClientSecret)) + { + _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", GetToken(objectenClientId, objectenClientSecret)); + return; + } + + if (!string.IsNullOrWhiteSpace(objectenToken)) + { + _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Token", objectenToken); + return; + } + + throw new Exception("No token or client id/secret is configured for the ObjectenClient"); } public IAsyncEnumerable GetObjecten(string type, CancellationToken token) @@ -25,6 +43,33 @@ public IAsyncEnumerable GetObjecten(string type, CancellationToken return GetObjectenInternal(url, token); } + private static string GetToken(string id, string secret) + { + var now = DateTimeOffset.UtcNow; + // one minute leeway to account for clock differences between machines + var issuedAt = now.AddMinutes(-1); + + var tokenHandler = new JwtSecurityTokenHandler(); + var key = Encoding.UTF8.GetBytes(secret); + var tokenDescriptor = new SecurityTokenDescriptor + { + Issuer = id, + IssuedAt = issuedAt.DateTime, + NotBefore = issuedAt.DateTime, + Claims = new Dictionary + { + { "client_id", id }, + { "user_id", "KISS Elastic Sync"}, + { "user_representation", "elastic-sync" } + }, + Subject = new ClaimsIdentity(), + Expires = now.AddHours(1).DateTime, + SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature) + }; + var token = tokenHandler.CreateToken(tokenDescriptor); + return tokenHandler.WriteToken(token); + } + private async IAsyncEnumerable GetObjectenInternal(string url, [EnumeratorCancellation] CancellationToken token) { string? next = null; diff --git a/src/Kiss.Elastic.Sync/Sources/ObjectenMedewerkerClient.cs b/src/Kiss.Elastic.Sync/Sources/ObjectenMedewerkerClient.cs index fdc901c..e6a0de0 100644 --- a/src/Kiss.Elastic.Sync/Sources/ObjectenMedewerkerClient.cs +++ b/src/Kiss.Elastic.Sync/Sources/ObjectenMedewerkerClient.cs @@ -1,6 +1,5 @@ using System.Runtime.CompilerServices; using System.Text.Json; -using Kiss.Elastic.Sync.Objecten; namespace Kiss.Elastic.Sync.Sources { @@ -9,7 +8,7 @@ internal sealed class ObjectenMedewerkerClient : IKissSourceClient private static readonly string[] s_nameProps = new[] { "voornaam", "voorvoegselAchternaam", "achternaam" }; private static readonly string[] s_metaProps = new[] { "function", "department", "skills" }; private readonly ObjectenClient _objectenClient; - private readonly ObjectTypesClient _objectTypesClient; + private readonly string _objecttypeUrl; public string Source => "Smoelenboek"; @@ -27,30 +26,27 @@ internal sealed class ObjectenMedewerkerClient : IKissSourceClient "telefoonnummers.telefoonnummer" }; - public ObjectenMedewerkerClient(ObjectenClient objectenClient, ObjectTypesClient objectTypesClient) + public ObjectenMedewerkerClient(ObjectenClient objectenClient, string objecttypeUrl) { _objectenClient = objectenClient; - _objectTypesClient = objectTypesClient; + _objecttypeUrl = objecttypeUrl; } public async IAsyncEnumerable Get([EnumeratorCancellation] CancellationToken token) { - await foreach (var type in _objectTypesClient.GetObjectTypeUrls("Medewerker", token)) + await foreach (var item in _objectenClient.GetObjecten(_objecttypeUrl, token)) { - await foreach (var item in _objectenClient.GetObjecten(type, token)) - { - var data = item.Data; - if (!data.TryGetProperty("identificatie", out var idProp) || idProp.ValueKind != JsonValueKind.String) - continue; + var data = item.Data; + if (!data.TryGetProperty("identificatie", out var idProp) || idProp.ValueKind != JsonValueKind.String) + continue; - var title = string.Join(' ', GetStringValues(data, s_nameProps)); - - var objectMeta = data.TryGetProperty("functie", out var functieProp) && functieProp.ValueKind == JsonValueKind.String - ? functieProp.GetString() - : null; + var title = string.Join(' ', GetStringValues(data, s_nameProps)); - yield return new KissEnvelope(data, title, objectMeta, $"smoelenboek_{idProp.GetString()}"); - } + var objectMeta = data.TryGetProperty("functie", out var functieProp) && functieProp.ValueKind == JsonValueKind.String + ? functieProp.GetString() + : null; + + yield return new KissEnvelope(data, title, objectMeta, $"smoelenboek_{idProp.GetString()}"); } } @@ -71,7 +67,6 @@ private static IEnumerable GetStringValues(JsonElement element, string[] public void Dispose() { _objectenClient.Dispose(); - _objectTypesClient.Dispose(); } } } diff --git a/src/Kiss.Elastic.Sync/Sources/ObjectenVacClient.cs b/src/Kiss.Elastic.Sync/Sources/ObjectenVacClient.cs index abdb1b8..361e27c 100644 --- a/src/Kiss.Elastic.Sync/Sources/ObjectenVacClient.cs +++ b/src/Kiss.Elastic.Sync/Sources/ObjectenVacClient.cs @@ -1,23 +1,22 @@ using System.Runtime.CompilerServices; using System.Text.Json; -using Kiss.Elastic.Sync.Objecten; namespace Kiss.Elastic.Sync.Sources { internal sealed class ObjectenVacClient : IKissSourceClient { private readonly ObjectenClient _objectenClient; - private readonly ObjectTypesClient _objectTypesClient; + private readonly string _objecttypeUrl; - public ObjectenVacClient(ObjectenClient objectenClient, ObjectTypesClient objectTypesClient) + public ObjectenVacClient(ObjectenClient objectenClient, string objecttypeUrl) { _objectenClient = objectenClient; - _objectTypesClient = objectTypesClient; + _objecttypeUrl = objecttypeUrl; } public string Source => "VAC"; - public IReadOnlyList CompletionFields { get; } = new [] + public IReadOnlyList CompletionFields { get; } = new[] { "vraag", "trefwoorden.trefwoord" @@ -26,21 +25,17 @@ public ObjectenVacClient(ObjectenClient objectenClient, ObjectTypesClient object public void Dispose() { _objectenClient.Dispose(); - _objectTypesClient.Dispose(); } public async IAsyncEnumerable Get([EnumeratorCancellation] CancellationToken token) { - await foreach (var typeUrl in _objectTypesClient.GetObjectTypeUrls("VAC", token)) + await foreach (var item in _objectenClient.GetObjecten(_objecttypeUrl, token)) { - await foreach (var item in _objectenClient.GetObjecten(typeUrl, token)) - { - var id = $"vac_{item.Id.GetString()}"; - var title = item.Data.TryGetProperty("vraag", out var titleProp) && titleProp.ValueKind == JsonValueKind.String - ? titleProp.GetString() - : ""; - yield return new KissEnvelope(item.Data, title, null, id); - } + var id = $"vac_{item.Id.GetString()}"; + var title = item.Data.TryGetProperty("vraag", out var titleProp) && titleProp.ValueKind == JsonValueKind.String + ? titleProp.GetString() + : ""; + yield return new KissEnvelope(item.Data, title, null, id); } } } diff --git a/src/Kiss.Elastic.Sync/Sources/SourceFactory.cs b/src/Kiss.Elastic.Sync/Sources/SourceFactory.cs index a5afd07..61df7ab 100644 --- a/src/Kiss.Elastic.Sync/Sources/SourceFactory.cs +++ b/src/Kiss.Elastic.Sync/Sources/SourceFactory.cs @@ -1,6 +1,4 @@ -using Kiss.Elastic.Sync.Objecten; - -namespace Kiss.Elastic.Sync.Sources +namespace Kiss.Elastic.Sync.Sources { public static class SourceFactory { @@ -11,69 +9,58 @@ public static class SourceFactory _ => GetProductClient(), }; - private static SdgProductClient GetProductClient() { - var sdgBaseUrl = Helpers.GetEnvironmentVariable("SDG_OBJECTEN_BASE_URL"); - var sdgApiKey = Helpers.GetEnvironmentVariable("SDG_OBJECTEN_TOKEN"); - var typeurl = Helpers.GetEnvironmentVariable("SDG_OBJECT_TYPE_URL"); + var sdgBaseUrl = Helpers.GetRequiredEnvironmentVariable("SDG_OBJECTEN_BASE_URL"); + var sdgApiKey = Helpers.GetOptionalEnvironmentVariable("SDG_OBJECTEN_TOKEN"); + var objectenClientId = Helpers.GetOptionalEnvironmentVariable("SDG_OBJECTEN_CLIENT_ID"); + var objectenClientSecret = Helpers.GetOptionalEnvironmentVariable("SDG_OBJECTEN_CLIENT_SECRET"); + var typeurl = Helpers.GetRequiredEnvironmentVariable("SDG_OBJECT_TYPE_URL"); if (!Uri.TryCreate(sdgBaseUrl, UriKind.Absolute, out var sdgBaseUri)) { throw new Exception("sdg base url is niet valide: " + sdgBaseUrl); } - var objecten = new ObjectenClient(sdgBaseUri, sdgApiKey); + var objecten = new ObjectenClient(sdgBaseUri, sdgApiKey, objectenClientId, objectenClientSecret); return new SdgProductClient(objecten, typeurl); } private static ObjectenMedewerkerClient GetMedewerkerClient() { - var objectenBaseUrl = Helpers.GetEnvironmentVariable("MEDEWERKER_OBJECTEN_BASE_URL"); - var objectenToken = Helpers.GetEnvironmentVariable("MEDEWERKER_OBJECTEN_TOKEN"); + var objectenBaseUrl = Helpers.GetOptionalEnvironmentVariable("MEDEWERKER_OBJECTEN_BASE_URL"); + var objectenToken = Helpers.GetOptionalEnvironmentVariable("MEDEWERKER_OBJECTEN_TOKEN"); + var objectenClientId = Helpers.GetOptionalEnvironmentVariable("MEDEWERKER_OBJECTEN_CLIENT_ID"); + var objectenClientSecret = Helpers.GetOptionalEnvironmentVariable("MEDEWERKER_OBJECTEN_CLIENT_SECRET"); + var typeurl = Helpers.GetRequiredEnvironmentVariable("MEDEWERKER_OBJECT_TYPE_URL"); if (!Uri.TryCreate(objectenBaseUrl, UriKind.Absolute, out var objectenBaseUri)) { throw new Exception("objecten base url is niet valide: " + objectenBaseUrl); } - var objectTypesBaseUrl = Helpers.GetEnvironmentVariable("MEDEWERKER_OBJECTTYPES_BASE_URL"); - var objectTypesToken = Helpers.GetEnvironmentVariable("MEDEWERKER_OBJECTTYPES_TOKEN"); - - if (!Uri.TryCreate(objectTypesBaseUrl, UriKind.Absolute, out var objectTypesBaseUri)) - { - throw new Exception("objecttypes base url is niet valide: " + objectTypesBaseUrl); - } - - var objecten = new ObjectenClient(objectenBaseUri, objectenToken); - var types = new ObjectTypesClient(objectTypesBaseUri, objectTypesToken); + var objecten = new ObjectenClient(objectenBaseUri, objectenToken, objectenClientId, objectenClientSecret); - return new ObjectenMedewerkerClient(objecten, types); + return new ObjectenMedewerkerClient(objecten, typeurl); } private static ObjectenVacClient GetVacClient() { - var objectenBaseUrl = Helpers.GetEnvironmentVariable("VAC_OBJECTEN_BASE_URL"); - var objectenToken = Helpers.GetEnvironmentVariable("VAC_OBJECTEN_TOKEN"); + var objectenBaseUrl = Helpers.GetRequiredEnvironmentVariable("VAC_OBJECTEN_BASE_URL"); + var objectenToken = Helpers.GetOptionalEnvironmentVariable("VAC_OBJECTEN_TOKEN"); + var objectenClientId = Helpers.GetOptionalEnvironmentVariable("VAC_OBJECTEN_CLIENT_ID"); + var objectenClientSecret = Helpers.GetOptionalEnvironmentVariable("VAC_OBJECTEN_CLIENT_SECRET"); + var typeurl = Helpers.GetRequiredEnvironmentVariable("VAC_OBJECT_TYPE_URL"); if (!Uri.TryCreate(objectenBaseUrl, UriKind.Absolute, out var objectenBaseUri)) { throw new Exception("objecten base url is niet valide: " + objectenBaseUrl); } - var objectTypesBaseUrl = Helpers.GetEnvironmentVariable("VAC_OBJECTTYPES_BASE_URL"); - var objectTypesToken = Helpers.GetEnvironmentVariable("VAC_OBJECTTYPES_TOKEN"); - - if (!Uri.TryCreate(objectTypesBaseUrl, UriKind.Absolute, out var objectTypesBaseUri)) - { - throw new Exception("objecttypes base url is niet valide: " + objectTypesBaseUrl); - } - - var objecten = new ObjectenClient(objectenBaseUri, objectenToken); - var types = new ObjectTypesClient(objectTypesBaseUri, objectTypesToken); + var objecten = new ObjectenClient(objectenBaseUri, objectenToken, objectenClientId, objectenClientSecret); - return new ObjectenVacClient(objecten, types); + return new ObjectenVacClient(objecten, typeurl); } } } From ed208e10672a7de7c114d975168e43abfc868e70 Mon Sep 17 00:00:00 2001 From: Felix Cornelissen Date: Wed, 8 May 2024 19:04:37 +0200 Subject: [PATCH 2/2] chore: update to dotnet 8 --- src/Kiss.Elastic.Sync/Dockerfile | 18 +++++------------- src/Kiss.Elastic.Sync/Kiss.Elastic.Sync.csproj | 4 ++-- 2 files changed, 7 insertions(+), 15 deletions(-) diff --git a/src/Kiss.Elastic.Sync/Dockerfile b/src/Kiss.Elastic.Sync/Dockerfile index 2954dfa..0dee229 100644 --- a/src/Kiss.Elastic.Sync/Dockerfile +++ b/src/Kiss.Elastic.Sync/Dockerfile @@ -1,25 +1,17 @@ -FROM mcr.microsoft.com/dotnet/runtime:6.0 AS base +FROM mcr.microsoft.com/dotnet/runtime:8.0 AS base +USER app WORKDIR /app -RUN addgroup --group kiss --gid 2000 \ - && adduser \ - --uid 1000 \ - --gid 2000 \ - "kiss" -RUN chown kiss:kiss /app -FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build +FROM mcr.microsoft.com/dotnet/sdk:8.0 AS publish +ARG BUILD_CONFIGURATION=Release WORKDIR /src COPY ["src/Kiss.Elastic.Sync/Kiss.Elastic.Sync.csproj", "src/Kiss.Elastic.Sync/"] RUN dotnet restore "src/Kiss.Elastic.Sync/Kiss.Elastic.Sync.csproj" COPY . . WORKDIR "/src/src/Kiss.Elastic.Sync" -RUN dotnet build "Kiss.Elastic.Sync.csproj" -c Release -o /app/build - -FROM build AS publish -RUN dotnet publish "Kiss.Elastic.Sync.csproj" -c Release -o /app/publish /p:UseAppHost=false +RUN dotnet publish "Kiss.Elastic.Sync.csproj" --no-restore -c Release -o /app/publish /p:UseAppHost=false FROM base AS final WORKDIR /app COPY --from=publish /app/publish . -USER kiss:kiss ENTRYPOINT ["dotnet", "Kiss.Elastic.Sync.dll"] diff --git a/src/Kiss.Elastic.Sync/Kiss.Elastic.Sync.csproj b/src/Kiss.Elastic.Sync/Kiss.Elastic.Sync.csproj index fb925bf..aa9012d 100644 --- a/src/Kiss.Elastic.Sync/Kiss.Elastic.Sync.csproj +++ b/src/Kiss.Elastic.Sync/Kiss.Elastic.Sync.csproj @@ -2,7 +2,7 @@ Exe - net6.0 + net8.0 enable enable Linux @@ -22,7 +22,7 @@ - +