From b45b1b212cdfd52f0d6223be2799f13137f927f5 Mon Sep 17 00:00:00 2001 From: Phil Schneider Date: Tue, 26 Nov 2024 10:51:50 +0100 Subject: [PATCH] feat: adjust configuration handling Refs: #1172 --- .../BusinessLogic/ISeedDataHandler.cs | 2 +- .../BusinessLogic/SeedDataHandler.cs | 17 +++++++++++------ .../SeederConfigurationExtensions.cs | 18 +++++------------- .../Models/KeycloakRealmSettings.cs | 2 +- ...s.cs => KeycloakRealmSettingsExtensions.cs} | 2 +- .../Models/KeycloakSeederConfigModel.cs | 2 +- src/keycloak/Keycloak.Seeding/README.md | 8 ++++---- .../Keycloak.Seeding/appsettings.example.json | 2 +- 8 files changed, 25 insertions(+), 28 deletions(-) rename src/keycloak/Keycloak.Seeding/Models/{KeycloakRealmSettingsExtentions.cs => KeycloakRealmSettingsExtensions.cs} (99%) diff --git a/src/keycloak/Keycloak.Seeding/BusinessLogic/ISeedDataHandler.cs b/src/keycloak/Keycloak.Seeding/BusinessLogic/ISeedDataHandler.cs index 6efe1ed6dd..8ef19ea64e 100644 --- a/src/keycloak/Keycloak.Seeding/BusinessLogic/ISeedDataHandler.cs +++ b/src/keycloak/Keycloak.Seeding/BusinessLogic/ISeedDataHandler.cs @@ -51,7 +51,7 @@ public interface ISeedDataHandler IEnumerable<(string ProviderType, ComponentModel ComponentModel)> RealmComponents { get; } IEnumerable<(string Locale, IEnumerable> Translations)> RealmLocalizations { get; } - KeycloakRealmSettings Configuration { get; } + SeederConfiguration Configuration { get; } Task SetClientInternalIds(IAsyncEnumerable<(string ClientId, string Id)> clientInternalIds); diff --git a/src/keycloak/Keycloak.Seeding/BusinessLogic/SeedDataHandler.cs b/src/keycloak/Keycloak.Seeding/BusinessLogic/SeedDataHandler.cs index 4e8197800e..45a2dfab20 100644 --- a/src/keycloak/Keycloak.Seeding/BusinessLogic/SeedDataHandler.cs +++ b/src/keycloak/Keycloak.Seeding/BusinessLogic/SeedDataHandler.cs @@ -20,7 +20,6 @@ using Org.Eclipse.TractusX.Portal.Backend.Framework.Async; using Org.Eclipse.TractusX.Portal.Backend.Framework.ErrorHandling; using Org.Eclipse.TractusX.Portal.Backend.Framework.Linq; -using Org.Eclipse.TractusX.Portal.Backend.Framework.Models; using Org.Eclipse.TractusX.Portal.Backend.Keycloak.Seeding.Models; using System.Collections.Immutable; using System.Text.Json; @@ -40,7 +39,7 @@ public class SeedDataHandler : ISeedDataHandler private KeycloakRealm? _keycloakRealm; private IReadOnlyDictionary? _idOfClients; - private KeycloakRealmSettings? _realmConfiguration; + private SeederConfiguration? _defaultConfiguration; public async Task Import(KeycloakRealmSettings realmSettings, CancellationToken cancellationToken) { @@ -50,7 +49,13 @@ public async Task Import(KeycloakRealmSettings realmSettings, CancellationToken async (importRealm, path) => importRealm.Merge(await ReadJsonRealm(path, realmSettings.Realm, cancellationToken).ConfigureAwait(ConfigureAwaitOptions.None)), cancellationToken).ConfigureAwait(ConfigureAwaitOptions.None)) .Merge(realmSettings.ToModel()); - _realmConfiguration = realmSettings; + _defaultConfiguration = new SeederConfiguration + { + Create = realmSettings.Create, + Update = realmSettings.Update, + Delete = realmSettings.Delete, + SeederConfigurations = realmSettings.SeederConfigurations + }; _idOfClients = null; } @@ -74,9 +79,9 @@ public string Realm get => _keycloakRealm?.Realm ?? throw new ConflictException("realm must not be null"); } - public KeycloakRealmSettings Configuration + public SeederConfiguration Configuration { - get => _realmConfiguration ?? throw new ConflictException("configuration must not be null"); + get => _defaultConfiguration ?? throw new ConflictException("configuration must not be null"); } public KeycloakRealm KeycloakRealm @@ -178,7 +183,7 @@ public KeycloakSeederConfigModel GetSpecificConfiguration(ConfigurationKey confi var configKeyString = configKey.ToString(); - specificConfiguration = _realmConfiguration?.SeederConfigurations?.SingleOrDefault(x => x.Key == configKeyString); + specificConfiguration = Configuration.SeederConfigurations?.SingleOrDefault(x => x.Key == configKeyString); _specificConfigurations.Add(configKey, specificConfiguration); return new KeycloakSeederConfigModel(Configuration, specificConfiguration); } diff --git a/src/keycloak/Keycloak.Seeding/Extensions/SeederConfigurationExtensions.cs b/src/keycloak/Keycloak.Seeding/Extensions/SeederConfigurationExtensions.cs index b37d8f60e1..63c71e7dc2 100644 --- a/src/keycloak/Keycloak.Seeding/Extensions/SeederConfigurationExtensions.cs +++ b/src/keycloak/Keycloak.Seeding/Extensions/SeederConfigurationExtensions.cs @@ -23,26 +23,27 @@ namespace Org.Eclipse.TractusX.Portal.Backend.Keycloak.Seeding.Extensions; public static class SeederConfigurationExtensions { - public static bool IsModificationAllowed(this KeycloakRealmSettings config, ConfigurationKey configKey) => + public static bool IsModificationAllowed(this SeederConfiguration config, ConfigurationKey configKey) => config.Create || config.Update || config.Delete || (config.SeederConfigurations != null && - IsModificationAllowed(config.SeederConfigurations, configKey.ToString(), out var _)); + IsModificationAllowed(config.SeederConfigurations, configKey.ToString(), false, out var _)); private static bool IsModificationAllowed( IEnumerable configurations, string targetKey, + bool isKeyParentConfig, out SeederConfiguration? matchingConfig) { matchingConfig = null; foreach (var config in configurations) { - if (config.SeederConfigurations != null && IsModificationAllowed(config.SeederConfigurations, targetKey, out matchingConfig)) + if (config.SeederConfigurations != null && IsModificationAllowed(config.SeederConfigurations, targetKey, isKeyParentConfig || config.Key == targetKey, out matchingConfig)) { return true; } - if (config.Key != targetKey || config is { Create: false, Update: false, Delete: false }) + if ((config.Key != targetKey && !isKeyParentConfig) || config is { Create: false, Update: false, Delete: false }) { continue; } @@ -93,15 +94,6 @@ public static bool ModificationAllowed(this KeycloakSeederConfigModel config, st return entity?.ModifyAllowed(modificationType) ?? config.ModificationAllowed(modificationType, entityKey); } - private static bool ModifyAllowed(this KeycloakRealmSettings configuration, ModificationType modificationType) => - modificationType switch - { - ModificationType.Create => configuration.Create, - ModificationType.Update => configuration.Update, - ModificationType.Delete => configuration.Delete, - _ => throw new ArgumentOutOfRangeException(nameof(modificationType), modificationType, null) - }; - private static bool ModifyAllowed(this SeederConfiguration configuration, ModificationType modificationType) => modificationType switch { diff --git a/src/keycloak/Keycloak.Seeding/Models/KeycloakRealmSettings.cs b/src/keycloak/Keycloak.Seeding/Models/KeycloakRealmSettings.cs index d7ea980e7e..bb4acfdf09 100644 --- a/src/keycloak/Keycloak.Seeding/Models/KeycloakRealmSettings.cs +++ b/src/keycloak/Keycloak.Seeding/Models/KeycloakRealmSettings.cs @@ -35,7 +35,7 @@ public class KeycloakRealmSettings public bool Create { get; set; } public bool Update { get; set; } public bool Delete { get; set; } - [DistinctValues] + [DistinctValues("x => x.Key")] public IEnumerable? SeederConfigurations { get; set; } public string? Id { get; set; } public string? DisplayName { get; set; } diff --git a/src/keycloak/Keycloak.Seeding/Models/KeycloakRealmSettingsExtentions.cs b/src/keycloak/Keycloak.Seeding/Models/KeycloakRealmSettingsExtensions.cs similarity index 99% rename from src/keycloak/Keycloak.Seeding/Models/KeycloakRealmSettingsExtentions.cs rename to src/keycloak/Keycloak.Seeding/Models/KeycloakRealmSettingsExtensions.cs index 932badf53e..c5850d6857 100644 --- a/src/keycloak/Keycloak.Seeding/Models/KeycloakRealmSettingsExtentions.cs +++ b/src/keycloak/Keycloak.Seeding/Models/KeycloakRealmSettingsExtensions.cs @@ -22,7 +22,7 @@ namespace Org.Eclipse.TractusX.Portal.Backend.Keycloak.Seeding.Models; -public static class KeycloakRealmSettingsExtentions +public static class KeycloakRealmSettingsExtensions { public static KeycloakRealm ToModel(this KeycloakRealmSettings keycloakRealmSettings) => new() diff --git a/src/keycloak/Keycloak.Seeding/Models/KeycloakSeederConfigModel.cs b/src/keycloak/Keycloak.Seeding/Models/KeycloakSeederConfigModel.cs index e37395c25a..ffdd624b7f 100644 --- a/src/keycloak/Keycloak.Seeding/Models/KeycloakSeederConfigModel.cs +++ b/src/keycloak/Keycloak.Seeding/Models/KeycloakSeederConfigModel.cs @@ -20,6 +20,6 @@ namespace Org.Eclipse.TractusX.Portal.Backend.Keycloak.Seeding.Models; public record KeycloakSeederConfigModel( - KeycloakRealmSettings DefaultSettings, + SeederConfiguration DefaultSettings, SeederConfiguration? SpecificConfiguration ); diff --git a/src/keycloak/Keycloak.Seeding/README.md b/src/keycloak/Keycloak.Seeding/README.md index bfca8b1db8..723d42adc2 100644 --- a/src/keycloak/Keycloak.Seeding/README.md +++ b/src/keycloak/Keycloak.Seeding/README.md @@ -14,7 +14,7 @@ In the Seeder configuration you must have one Default entry where the following "Create": true, "Update": true, "Delete": true, - "SeederConfiguration": [] + "SeederConfigurations": [] ] ``` @@ -27,7 +27,7 @@ To be able to enable or disable the functionality for specific types the SeederC **Example**: ```json - "SeederConfiguration": [ + "SeederConfigurations": [ { "Key": "Localizations", "Create": false, @@ -67,7 +67,7 @@ To be able to enable or disable the seeding for specific values the configuratio **Example** ```json - "SeederConfiguration": [ + "SeederConfigurations": [ { "Key": "Localizations", "Create": true, @@ -97,7 +97,7 @@ For some entities there is a specific entry type configuration in place. E.g. Fe **Example** ```json - "SeederConfiguration": [ + "SeederConfigurations": [ { "Key": "Users", "Create": true, diff --git a/src/keycloak/Keycloak.Seeding/appsettings.example.json b/src/keycloak/Keycloak.Seeding/appsettings.example.json index ab09b5bcd5..b6193cba7f 100644 --- a/src/keycloak/Keycloak.Seeding/appsettings.example.json +++ b/src/keycloak/Keycloak.Seeding/appsettings.example.json @@ -42,7 +42,7 @@ "Create": true, "Update": false, "Delete": true, - "SeederConfiguration": [ + "SeederConfigurations": [ { "Key": "Roles", "Create": false,