All Downloads are FREE. Search and download functionalities are using the official Maven repository.

dk.grinn.keycloak.migration.KeycloakMigrate Maven / Gradle / Ivy

package dk.grinn.keycloak.migration;

/*-
 * #%L
 * Keycloak : Migrate : Core
 * %%
 * Copyright (C) 2019 Jonas Grann & Kim Jersin
 * %%
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 * #L%
 */
import dk.grinn.keycloak.admin.GkcadmConfiguration;
import dk.grinn.keycloak.admin.ProgressStream;
import dk.grinn.keycloak.migration.boundary.CreateSession;
import dk.grinn.keycloak.migration.boundary.SessionResource;
import dk.grinn.keycloak.migration.core.rest.Create;
import dk.grinn.keycloak.migration.entities.ScriptHistory;

import javax.ws.rs.NotFoundException;
import java.util.*;

import dk.grinn.keycloak.migration.core.ClientMigrationContext;
import dk.grinn.keycloak.migration.core.migrate.MigratorService;
import static java.util.Collections.*;
import javax.annotation.PostConstruct;
import javax.enterprise.context.Dependent;
import javax.enterprise.inject.Instance;
import javax.inject.Inject;
import org.jboss.logging.Logger;
import org.jboss.weld.context.activator.ActivateRequestContext;

/**
 * Main migration class.
 * 

*/ @Dependent public class KeycloakMigrate { public static final String ENVIRONMENT_CONFIG_PREFIX = "GKCADM_"; private static final Logger LOG = Logger.getLogger(KeycloakMigrate.class); @Inject protected Instance migrator; @Inject protected GkcadmConfiguration configuration; @Inject protected ClientMigrationContext context; @Inject protected ProgressStream out; @PostConstruct public void init() { LOG.debug("Init:"); } /** * Initialize and scan the class,- and script locations for migrations. * * @param location * @param commands */ @ActivateRequestContext public void execute(String location, List commands) throws Exception { LOG.debugf("Location: %s, commands: %s", location, commands); configuration.setLocation(location); for (String cmd : commands) { switch (cmd) { case "info": info(); break; case "migrate": migrate(); break; case "clean": clean(); break; case "abort": abort(); break; } } } /** * @throws Exception */ protected void migrate() throws Exception { // New migrator service for each realm for (var realm : configuration.getRealms()) { migrator.get().migrate(realm, configuration); } } protected void clean() { SessionResource sessions = context.migrations().sessions(); out.println("--------------------------------"); List realms = new ArrayList<>(configuration.getRealms()); reverse(realms); realms.forEach((realm) -> { UUID sessionId = Create.uuid( sessions.createSession(new CreateSession(realm)) ); try { context.keycloak().realm(realm).remove(); out.printf("Cleaned realm: '%s'\n", realm); } catch (NotFoundException ex) { // Ignore } finally { sessions.removeSession(sessionId); } }); } protected void info() throws Exception { for (var realm : configuration.getRealms()) { List history = null; try { history = context.migrations().realms().scripts(realm).getScripts(); } catch (NotFoundException ex) { // Ignore } out.printf("\nRealm: '%s'\n", realm); out.println("--------------------------------"); if (history != null) { migrator.get().info(realm, configuration, history); } else { out.println("(Not under migration control)"); } } } protected void abort() { for (var realm : configuration.getRealms()) { out.printf("\nAbort: '%s'\n", realm); context.migrations().realms().abortSession(realm); } } public Set getNotDefinedConfigEnvironmentKeys(Set envKeys) { Set result = new LinkedHashSet<>(); for (String key : envKeys) { if (key.startsWith(ENVIRONMENT_CONFIG_PREFIX)) { if (!configuration.containsKey( key.substring(ENVIRONMENT_CONFIG_PREFIX.length()).toLowerCase().replace('_', '.') )) { result.add(key); } } } return result; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy