From 150ea7622d49f7cad72ef0ca3c16e0631d4d2d52 Mon Sep 17 00:00:00 2001 From: Norbert Truchsess Date: Fri, 9 Aug 2024 00:10:55 +0200 Subject: [PATCH] fix sonar findings --- .../Framework.Linq/NullableExtensions.cs | 8 ++--- .../AuthenticationFlowsUpdater.cs | 4 +-- .../BusinessLogic/ClientsUpdater.cs | 8 ++--- .../BusinessLogic/IdentityProvidersUpdater.cs | 2 +- .../BusinessLogic/RealmUpdater.cs | 2 +- .../BusinessLogic/RolesUpdater.cs | 2 +- .../BusinessLogic/UsersUpdater.cs | 8 ++--- .../Models/KeycloakRealmSettingsExtentions.cs | 34 +++++++++---------- 8 files changed, 34 insertions(+), 34 deletions(-) diff --git a/src/framework/Framework.Linq/NullableExtensions.cs b/src/framework/Framework.Linq/NullableExtensions.cs index 6b25b20468..578d367ba8 100644 --- a/src/framework/Framework.Linq/NullableExtensions.cs +++ b/src/framework/Framework.Linq/NullableExtensions.cs @@ -24,9 +24,9 @@ public static class NullableExtensions public static bool IsNullOrEmpty(this IEnumerable? collection) => collection == null || !collection.Any(); - public static IEnumerable> FilterNotNullValues(this IEnumerable> source) => - source.Where(x => x.Value != null).Cast>(); + public static IEnumerable> FilterNotNullValues(this IEnumerable> source) where TValue : notnull => + source.Where(x => x.Value is not null).Cast>(); - public static IEnumerable FilterNotNull(this IEnumerable source) => - source.Where(x => x != null).Cast(); + public static IEnumerable FilterNotNull(this IEnumerable source) where T : notnull => + source.Where(x => x is not null).Cast(); } diff --git a/src/keycloak/Keycloak.Seeding/BusinessLogic/AuthenticationFlowsUpdater.cs b/src/keycloak/Keycloak.Seeding/BusinessLogic/AuthenticationFlowsUpdater.cs index c76aff95fe..af6f2d65f4 100644 --- a/src/keycloak/Keycloak.Seeding/BusinessLogic/AuthenticationFlowsUpdater.cs +++ b/src/keycloak/Keycloak.Seeding/BusinessLogic/AuthenticationFlowsUpdater.cs @@ -275,7 +275,7 @@ await _keycloak.CreateAuthenticationExecutionConfigurationAsync( new AuthenticatorConfig { Alias = update.AuthenticatorConfig, - Config = _seedData.GetAuthenticatorConfig(update.AuthenticatorConfig).Config?.FilterNotNullValues()?.ToDictionary() + Config = _seedData.GetAuthenticatorConfig(update.AuthenticatorConfig).Config?.FilterNotNullValues().ToDictionary() }, cancellationToken).ConfigureAwait(ConfigureAwaitOptions.None); break; @@ -292,7 +292,7 @@ await _keycloak.DeleteAuthenticatorConfigurationAsync( if (config == null) throw new UnexpectedConditionException("authenticatorConfig is null"); config.Alias = update.AuthenticatorConfig; - config.Config = updateConfig.Config?.FilterNotNullValues()?.ToDictionary(); + config.Config = updateConfig.Config?.FilterNotNullValues().ToDictionary(); await _keycloak.UpdateAuthenticatorConfigurationAsync(_realm, execution.AuthenticationConfig, config, cancellationToken).ConfigureAwait(ConfigureAwaitOptions.None); break; } diff --git a/src/keycloak/Keycloak.Seeding/BusinessLogic/ClientsUpdater.cs b/src/keycloak/Keycloak.Seeding/BusinessLogic/ClientsUpdater.cs index 717f77bcc8..92582fd248 100644 --- a/src/keycloak/Keycloak.Seeding/BusinessLogic/ClientsUpdater.cs +++ b/src/keycloak/Keycloak.Seeding/BusinessLogic/ClientsUpdater.cs @@ -215,8 +215,8 @@ private static async Task UpdateDefaultClientScopes(KeycloakClient keycloak, str PublicClient = update.PublicClient, FrontChannelLogout = update.FrontchannelLogout, Protocol = update.Protocol, - Attributes = update.Attributes?.FilterNotNullValues()?.ToDictionary(), - AuthenticationFlowBindingOverrides = update.AuthenticationFlowBindingOverrides?.FilterNotNullValues()?.ToDictionary(), + Attributes = update.Attributes?.FilterNotNullValues().ToDictionary(), + AuthenticationFlowBindingOverrides = update.AuthenticationFlowBindingOverrides?.FilterNotNullValues().ToDictionary(), FullScopeAllowed = update.FullScopeAllowed, NodeReregistrationTimeout = update.NodeReRegistrationTimeout, Access = update.Access == null @@ -299,8 +299,8 @@ private static PartialImport CreatePartialImportClient(ClientModel update) => PublicClient = update.PublicClient, FrontChannelLogout = update.FrontchannelLogout, Protocol = update.Protocol, - Attributes = update.Attributes?.FilterNotNullValues()?.ToDictionary(), - AuthenticationFlowBindingOverrides = update.AuthenticationFlowBindingOverrides?.FilterNotNullValues()?.ToDictionary(), + Attributes = update.Attributes?.FilterNotNullValues().ToDictionary(), + AuthenticationFlowBindingOverrides = update.AuthenticationFlowBindingOverrides?.FilterNotNullValues().ToDictionary(), FullScopeAllowed = update.FullScopeAllowed, NodeReregistrationTimeout = update.NodeReRegistrationTimeout, ProtocolMappers = update.ProtocolMappers?.Select(x => ProtocolMappersUpdater.CreateProtocolMapper(x.Id, x)), diff --git a/src/keycloak/Keycloak.Seeding/BusinessLogic/IdentityProvidersUpdater.cs b/src/keycloak/Keycloak.Seeding/BusinessLogic/IdentityProvidersUpdater.cs index 849f38bf59..a043c8c81e 100644 --- a/src/keycloak/Keycloak.Seeding/BusinessLogic/IdentityProvidersUpdater.cs +++ b/src/keycloak/Keycloak.Seeding/BusinessLogic/IdentityProvidersUpdater.cs @@ -244,7 +244,7 @@ private static bool CompareIdentityProviderConfig(Config? config, IdentityProvid private static IdentityProviderMapper UpdateIdentityProviderMapper(IdentityProviderMapper mapper, IdentityProviderMapperModel updateMapper) { mapper._IdentityProviderMapper = updateMapper.IdentityProviderMapper; - mapper.Config = updateMapper.Config?.FilterNotNullValues()?.ToDictionary(); + mapper.Config = updateMapper.Config?.FilterNotNullValues().ToDictionary(); return mapper; } diff --git a/src/keycloak/Keycloak.Seeding/BusinessLogic/RealmUpdater.cs b/src/keycloak/Keycloak.Seeding/BusinessLogic/RealmUpdater.cs index cb11307b41..1d11cc06a6 100644 --- a/src/keycloak/Keycloak.Seeding/BusinessLogic/RealmUpdater.cs +++ b/src/keycloak/Keycloak.Seeding/BusinessLogic/RealmUpdater.cs @@ -125,7 +125,7 @@ public async Task UpdateRealm(string keycloakInstanceName, CancellationToken can keycloakRealm.ResetCredentialsFlow = seedRealm.ResetCredentialsFlow; keycloakRealm.ClientAuthenticationFlow = seedRealm.ClientAuthenticationFlow; keycloakRealm.DockerAuthenticationFlow = seedRealm.DockerAuthenticationFlow; - keycloakRealm.Attributes = seedRealm.Attributes?.FilterNotNullValues()?.ToDictionary(); + keycloakRealm.Attributes = seedRealm.Attributes?.FilterNotNullValues().ToDictionary(); keycloakRealm.UserManagedAccessAllowed = seedRealm.UserManagedAccessAllowed; keycloakRealm.PasswordPolicy = seedRealm.PasswordPolicy; diff --git a/src/keycloak/Keycloak.Seeding/BusinessLogic/RolesUpdater.cs b/src/keycloak/Keycloak.Seeding/BusinessLogic/RolesUpdater.cs index 09cab6e61d..928aff5d80 100644 --- a/src/keycloak/Keycloak.Seeding/BusinessLogic/RolesUpdater.cs +++ b/src/keycloak/Keycloak.Seeding/BusinessLogic/RolesUpdater.cs @@ -217,7 +217,7 @@ private static Role CreateRole(RoleModel update) => Description = update.Description, Composite = update.Composite, ClientRole = update.ClientRole, - Attributes = update.Attributes?.FilterNotNullValues()?.ToDictionary() + Attributes = update.Attributes?.FilterNotNullValues().ToDictionary() }; private static Role CreateUpdateRole(string id, string containerId, RoleModel update) diff --git a/src/keycloak/Keycloak.Seeding/BusinessLogic/UsersUpdater.cs b/src/keycloak/Keycloak.Seeding/BusinessLogic/UsersUpdater.cs index 6b0053cda2..f0d9a64ec3 100644 --- a/src/keycloak/Keycloak.Seeding/BusinessLogic/UsersUpdater.cs +++ b/src/keycloak/Keycloak.Seeding/BusinessLogic/UsersUpdater.cs @@ -156,7 +156,7 @@ await seed.Except(userRoles.Select(x => x.Name)).IfAnyAwait( DisableableCredentialTypes = update.DisableableCredentialTypes, RequiredActions = update.RequiredActions, NotBefore = update.NotBefore, - Attributes = update.Attributes?.FilterNotNullValues()?.ToDictionary(), + Attributes = update.Attributes?.FilterNotNullValues().ToDictionary(), Groups = update.Groups, ServiceAccountClientId = update.ServiceAccountClientId, Access = CreateUpdateUserAccess(update.Access), @@ -282,7 +282,7 @@ private static Credentials CreateUpdateCredentials(CredentialsModel update) => new() { Algorithm = update.Algorithm, - Config = update.Config?.FilterNotNullValues()?.ToDictionary(), + Config = update.Config?.FilterNotNullValues().ToDictionary(), Counter = update.Counter, CreatedDate = update.CreatedDate, Device = update.Device, @@ -324,8 +324,8 @@ private static PartialImport CreatePartialImportUser(UserModel update) => RequiredActions = update.RequiredActions, NotBefore = update.NotBefore, Access = CreateUpdateUserAccess(update.Access), - Attributes = update.Attributes?.FilterNotNullValues()?.ToDictionary(), - ClientRoles = update.ClientRoles?.FilterNotNullValues()?.ToDictionary(), + Attributes = update.Attributes?.FilterNotNullValues().ToDictionary(), + ClientRoles = update.ClientRoles?.FilterNotNullValues().ToDictionary(), Credentials = update.Credentials?.Select(CreateUpdateCredentials), FederatedIdentities = update.FederatedIdentities?.Select(CreateUpdateFederatedIdentity), FederationLink = update.FederationLink, diff --git a/src/keycloak/Keycloak.Seeding/Models/KeycloakRealmSettingsExtentions.cs b/src/keycloak/Keycloak.Seeding/Models/KeycloakRealmSettingsExtentions.cs index d587cdbc21..a29dc5f4bc 100644 --- a/src/keycloak/Keycloak.Seeding/Models/KeycloakRealmSettingsExtentions.cs +++ b/src/keycloak/Keycloak.Seeding/Models/KeycloakRealmSettingsExtentions.cs @@ -108,7 +108,7 @@ public static KeycloakRealm ToModel(this KeycloakRealmSettings keycloakRealmSett WebAuthnPolicyPasswordlessAcceptableAaguids = keycloakRealmSettings.WebAuthnPolicyPasswordlessAcceptableAaguids, Users = keycloakRealmSettings.Users?.Select(ToModel), ScopeMappings = keycloakRealmSettings.ScopeMappings?.Select(ToModel), - ClientScopeMappings = keycloakRealmSettings.ClientScopeMappings?.Select(ToModel)?.ToImmutableDictionary(), + ClientScopeMappings = keycloakRealmSettings.ClientScopeMappings?.Select(ToModel).ToImmutableDictionary(), Clients = keycloakRealmSettings.Clients?.Select(ToModel), ClientScopes = keycloakRealmSettings.ClientScopes?.Select(ToModel), DefaultDefaultClientScopes = keycloakRealmSettings.DefaultDefaultClientScopes, @@ -126,7 +126,7 @@ public static KeycloakRealm ToModel(this KeycloakRealmSettings keycloakRealmSett AdminEventsDetailsEnabled = keycloakRealmSettings.AdminEventsDetailsEnabled, IdentityProviders = keycloakRealmSettings.IdentityProviders?.Select(ToModel), IdentityProviderMappers = keycloakRealmSettings.IdentityProviderMappers?.Select(ToModel), - Components = keycloakRealmSettings.Components?.Select(ToModel)?.ToImmutableDictionary(), + Components = keycloakRealmSettings.Components?.Select(ToModel).ToImmutableDictionary(), InternationalizationEnabled = keycloakRealmSettings.InternationalizationEnabled, SupportedLocales = keycloakRealmSettings.SupportedLocales, DefaultLocale = keycloakRealmSettings.DefaultLocale, @@ -139,7 +139,7 @@ public static KeycloakRealm ToModel(this KeycloakRealmSettings keycloakRealmSett ResetCredentialsFlow = keycloakRealmSettings.ResetCredentialsFlow, ClientAuthenticationFlow = keycloakRealmSettings.ClientAuthenticationFlow, DockerAuthenticationFlow = keycloakRealmSettings.DockerAuthenticationFlow, - Attributes = keycloakRealmSettings.Attributes?.Select(ToModel)?.ToImmutableDictionary(), + Attributes = keycloakRealmSettings.Attributes?.Select(ToModel).ToImmutableDictionary(), KeycloakVersion = keycloakRealmSettings.KeycloakVersion, UserManagedAccessAllowed = keycloakRealmSettings.UserManagedAccessAllowed, ClientProfiles = keycloakRealmSettings.ClientProfiles?.ToModel(), @@ -177,12 +177,12 @@ private static RoleModel ToModel(this RoleSettings roleSettings) => roleSettings.Composite, roleSettings.ClientRole, roleSettings.ContainerId, - roleSettings.Attributes?.Select(ToModel)?.ToImmutableDictionary(), + roleSettings.Attributes?.Select(ToModel).ToImmutableDictionary(), roleSettings.Composites?.ToModel()); private static RolesModel ToModel(this RolesSettings rolesSettings) => new(rolesSettings.Realm?.Select(x => x.ToModel()), - rolesSettings.Client?.Select(ToModel)?.ToImmutableDictionary()); + rolesSettings.Client?.Select(ToModel).ToImmutableDictionary()); private static KeyValuePair?> ToModel(UserClientRolesSettings userClientRolesSettings) => KeyValuePair.Create( @@ -193,9 +193,9 @@ private static GroupModel ToModel(GroupSettings groupSettings) => new(groupSettings.Id, groupSettings.Name, groupSettings.Path, - groupSettings.Attributes?.Select(ToModel)?.ToImmutableDictionary(), + groupSettings.Attributes?.Select(ToModel).ToImmutableDictionary(), groupSettings.RealmRoles, - groupSettings.ClientRoles?.Select(ToModel)?.ToImmutableDictionary(), + groupSettings.ClientRoles?.Select(ToModel).ToImmutableDictionary(), groupSettings.SubGroups); public static KeyValuePair ToModel(CredentialsConfigSettings credentialsConfigSettings) => @@ -205,7 +205,7 @@ private static GroupModel ToModel(GroupSettings groupSettings) => private static CredentialsModel ToModel(CredentialsSettings credentialsSettings) => new(credentialsSettings.Algorithm, - credentialsSettings.Config?.Select(ToModel)?.ToImmutableDictionary(), + credentialsSettings.Config?.Select(ToModel).ToImmutableDictionary(), credentialsSettings.Counter, credentialsSettings.CreatedDate, credentialsSettings.Device, @@ -246,13 +246,13 @@ private static UserModel ToModel(UserSettings userSettings) => userSettings.FirstName, userSettings.LastName, userSettings.Email, - userSettings.Attributes?.Select(ToModel)?.ToImmutableDictionary(), + userSettings.Attributes?.Select(ToModel).ToImmutableDictionary(), userSettings.Credentials?.Select(ToModel), userSettings.DisableableCredentialTypes, userSettings.RequiredActions, userSettings.FederatedIdentities?.Select(ToModel), userSettings.RealmRoles, - userSettings.ClientRoles?.Select(ToModel)?.ToImmutableDictionary(), + userSettings.ClientRoles?.Select(ToModel).ToImmutableDictionary(), userSettings.NotBefore, userSettings.Groups, userSettings.ServiceAccountClientId, @@ -295,7 +295,7 @@ private static ProtocolMapperModel ToModel(ProtocolMapperSettings protocolMapper protocolMapperSettings.Protocol, protocolMapperSettings.ProtocolMapper, protocolMapperSettings.ConsentRequired, - protocolMapperSettings.Config?.Select(ToModel)?.ToImmutableDictionary()); + protocolMapperSettings.Config?.Select(ToModel).ToImmutableDictionary()); private static ClientAccessModel ToModel(this ClientAccessSettings clientAccessSettings) => new(clientAccessSettings.Configure, @@ -324,8 +324,8 @@ private static ClientModel ToModel(ClientSettings clientSettings) => clientSettings.PublicClient, clientSettings.FrontchannelLogout, clientSettings.Protocol, - clientSettings.Attributes?.Select(ToModel)?.ToImmutableDictionary(), - clientSettings.AuthenticationFlowBindingOverrides?.Select(ToModel)?.ToImmutableDictionary(), + clientSettings.Attributes?.Select(ToModel).ToImmutableDictionary(), + clientSettings.AuthenticationFlowBindingOverrides?.Select(ToModel).ToImmutableDictionary(), clientSettings.FullScopeAllowed, clientSettings.NodeReRegistrationTimeout, clientSettings.DefaultClientScopes, @@ -341,7 +341,7 @@ private static ClientScopeModel ToModel(this ClientScopeSettings clientScopeSett new(clientScopeSettings.Id, clientScopeSettings.Name, clientScopeSettings.Protocol, - clientScopeSettings.Attributes?.Select(ToModel)?.ToImmutableDictionary(), + clientScopeSettings.Attributes?.Select(ToModel).ToImmutableDictionary(), clientScopeSettings.ProtocolMappers?.Select(ToModel), clientScopeSettings.Description); @@ -431,7 +431,7 @@ private static IdentityProviderMapperModel ToModel(IdentityProviderMapperSetting identityProviderMapperSettings.Name, identityProviderMapperSettings.IdentityProviderAlias, identityProviderMapperSettings.IdentityProviderMapper, - identityProviderMapperSettings.Config?.Select(ToModel)?.ToImmutableDictionary()); + identityProviderMapperSettings.Config?.Select(ToModel).ToImmutableDictionary()); private static KeyValuePair?> ToModel(ComponentConfigSettings componentConfigSettings) => KeyValuePair.Create( @@ -444,7 +444,7 @@ private static ComponentModel ToModel(ComponentSettings componentSettings) => componentSettings.ProviderId, componentSettings.SubType, componentSettings.SubComponents, - componentSettings.Config?.Select(ToModel)?.ToImmutableDictionary()); + componentSettings.Config?.Select(ToModel).ToImmutableDictionary()); private static KeyValuePair?> ToModel(ComponentSettingsEntry componentSettingsEntry) => KeyValuePair.Create( @@ -478,7 +478,7 @@ private static AuthenticationFlowModel ToModel(AuthenticationFlowSettings authen private static AuthenticatorConfigModel ToModel(AuthenticatorConfigSettings authenticatorConfigSettings) => new(authenticatorConfigSettings.Id, authenticatorConfigSettings.Alias, - authenticatorConfigSettings.Config?.Select(ToModel)?.ToImmutableDictionary()); + authenticatorConfigSettings.Config?.Select(ToModel).ToImmutableDictionary()); private static RequiredActionModel ToModel(this RequiredActionSettings requiredActionSettings) => new(requiredActionSettings.Alias,