From b4a8cbb820914c82f7da81d7d459c7c1f8241d6a Mon Sep 17 00:00:00 2001 From: Ivo Studensky Date: Fri, 15 Nov 2024 15:08:19 +0100 Subject: [PATCH] [JBEAP-27684] migrate security configuration to new application and mgmt property files --- .../MigrateLegacySecurityRealmsToElytron.java | 39 ++++++++++++++++--- 1 file changed, 34 insertions(+), 5 deletions(-) diff --git a/servers/wildfly26.0/src/main/java/org/jboss/migration/wfly/task/security/MigrateLegacySecurityRealmsToElytron.java b/servers/wildfly26.0/src/main/java/org/jboss/migration/wfly/task/security/MigrateLegacySecurityRealmsToElytron.java index 6d32e744..bba6dea4 100644 --- a/servers/wildfly26.0/src/main/java/org/jboss/migration/wfly/task/security/MigrateLegacySecurityRealmsToElytron.java +++ b/servers/wildfly26.0/src/main/java/org/jboss/migration/wfly/task/security/MigrateLegacySecurityRealmsToElytron.java @@ -45,6 +45,10 @@ import org.jboss.migration.wfly11.task.subsystem.elytron.SecurityDomainAddOperation; import org.jboss.migration.wfly11.task.subsystem.elytron.ServerSSLContextAddOperation; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; + import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.DOMAIN_CONTROLLER; import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.HOST; import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.HTTP_AUTHENTICATION_FACTORY; @@ -69,14 +73,39 @@ public class MigrateLegacySecurityRealmsToElytron extends ManageableServerCon private static final String TASK_NAME = "security.migrate-legacy-security-realms-to-elytron"; + private static final String MIGRATION_APPLICATION_USERS_PROPERTIES_FILENAME = "migration-application-users.properties"; + private static final String MIGRATION_APPLICATION_ROLES_PROPERTIES_FILENAME = "migration-application-roles.properties"; + private static final String MIGRATION_MGMT_USERS_PROPERTIES_FILENAME = "migration-mgmt-users.properties"; + private static final String MIGRATION_MGMT_GROUPS_PROPERTIES_FILENAME = "migration-mgmt-groups.properties"; + public MigrateLegacySecurityRealmsToElytron(LegacySecurityConfigurations legacySecurityConfigurations) { name(TASK_NAME); skipPolicy(TaskSkipPolicy.skipIfDefaultTaskSkipPropertyIsSet()); - beforeRun(context -> context.getLogger().debugf("Migrating legacy security realms to Elytron...")); + beforeRun(context -> { + context.getLogger().debugf("Migrating legacy security realms to Elytron..."); + legacySecurityConfigurations.getSecurityConfigurations().forEach( + (serverConfigurationPath, legacySecurityConfiguration) -> { + final Path configurationDir = legacySecurityConfiguration.getTargetConfiguration().getConfigurationDir(); + createEmptyFile(configurationDir.resolve(MIGRATION_APPLICATION_USERS_PROPERTIES_FILENAME)); + createEmptyFile(configurationDir.resolve(MIGRATION_APPLICATION_ROLES_PROPERTIES_FILENAME)); + createEmptyFile(configurationDir.resolve(MIGRATION_MGMT_USERS_PROPERTIES_FILENAME)); + createEmptyFile(configurationDir.resolve(MIGRATION_MGMT_GROUPS_PROPERTIES_FILENAME)); + }); + }); subtasks(ManageableServerConfigurationCompositeSubtasks.of(new MigrateToElytron<>(legacySecurityConfigurations), new UpdateManagementInterfaces<>(legacySecurityConfigurations))); afterRun(context -> context.getLogger().debugf("Legacy security realms migrated to Elytron.")); } + private void createEmptyFile(final Path propertyFile) { + if (!Files.isRegularFile(propertyFile)) { + try { + Files.createFile(propertyFile); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + } + public static class MigrateToElytron extends ManageableServerConfigurationLeafTask.Builder { private static final String SUBTASK_NAME = TASK_NAME + ".update-subsystems"; @@ -132,11 +161,11 @@ protected void addOperationSteps(LegacySecurityConfiguration legacySecurityConfi protected void addDefaultApplicationRealm(LegacySecurityConfiguration legacySecurityConfiguration, SubsystemResource subsystemResource, Operations.CompositeOperationBuilder compositeOperationBuilder, TaskContext taskContext) { final String securityRealmName = legacySecurityConfiguration.getDefaultElytronApplicationRealmName(); final PropertiesRealmAddOperation propertiesRealmAddOperation = new PropertiesRealmAddOperation(subsystemResource.getResourcePathAddress(), securityRealmName); - propertiesRealmAddOperation.usersProperties(new PropertiesRealmAddOperation.Properties("application-users.properties") + propertiesRealmAddOperation.usersProperties(new PropertiesRealmAddOperation.Properties(MIGRATION_APPLICATION_USERS_PROPERTIES_FILENAME) .relativeTo(subsystemResource.getServerConfiguration().getConfigurationType() == StandaloneServerConfiguration.RESOURCE_TYPE ? "jboss.server.config.dir" : "jboss.domain.config.dir") .digestRealmName(securityRealmName) ); - propertiesRealmAddOperation.groupsProperties(new PropertiesRealmAddOperation.Properties("application-roles.properties") + propertiesRealmAddOperation.groupsProperties(new PropertiesRealmAddOperation.Properties(MIGRATION_APPLICATION_ROLES_PROPERTIES_FILENAME) .relativeTo(subsystemResource.getServerConfiguration().getConfigurationType() == StandaloneServerConfiguration.RESOURCE_TYPE ? "jboss.server.config.dir" : "jboss.domain.config.dir") ); compositeOperationBuilder.addStep(propertiesRealmAddOperation.toModelNode()); @@ -145,11 +174,11 @@ protected void addDefaultApplicationRealm(LegacySecurityConfiguration legacySecu protected void addDefaultManagementRealm(LegacySecurityConfiguration legacySecurityConfiguration, SubsystemResource subsystemResource, Operations.CompositeOperationBuilder compositeOperationBuilder, TaskContext taskContext) { final String securityRealmName = legacySecurityConfiguration.getDefaultElytronManagementRealmName(); final PropertiesRealmAddOperation propertiesRealmAddOperation = new PropertiesRealmAddOperation(subsystemResource.getResourcePathAddress(), securityRealmName); - propertiesRealmAddOperation.usersProperties(new PropertiesRealmAddOperation.Properties("mgmt-users.properties") + propertiesRealmAddOperation.usersProperties(new PropertiesRealmAddOperation.Properties(MIGRATION_MGMT_USERS_PROPERTIES_FILENAME) .relativeTo(subsystemResource.getServerConfiguration().getConfigurationType() == StandaloneServerConfiguration.RESOURCE_TYPE ? "jboss.server.config.dir" : "jboss.domain.config.dir") .digestRealmName(securityRealmName) ); - propertiesRealmAddOperation.groupsProperties(new PropertiesRealmAddOperation.Properties("mgmt-groups.properties") + propertiesRealmAddOperation.groupsProperties(new PropertiesRealmAddOperation.Properties(MIGRATION_MGMT_GROUPS_PROPERTIES_FILENAME) .relativeTo(subsystemResource.getServerConfiguration().getConfigurationType() == StandaloneServerConfiguration.RESOURCE_TYPE ? "jboss.server.config.dir" : "jboss.domain.config.dir") ); compositeOperationBuilder.addStep(propertiesRealmAddOperation.toModelNode());