Search in sources :

Example 1 with ApplianceBootstrap

use of org.keycloak.services.managers.ApplianceBootstrap in project keycloak by keycloak.

the class QuarkusJpaConnectionProviderFactory method createMasterRealm.

private ExportImportManager createMasterRealm(ExportImportManager exportImportManager) {
    logger.debug("bootstrap");
    KeycloakSession session = factory.create();
    try {
        session.getTransactionManager().begin();
        JtaTransactionManagerLookup lookup = (JtaTransactionManagerLookup) factory.getProviderFactory(JtaTransactionManagerLookup.class);
        if (lookup != null) {
            if (lookup.getTransactionManager() != null) {
                try {
                    Transaction transaction = lookup.getTransactionManager().getTransaction();
                    logger.debugv("bootstrap current transaction? {0}", transaction != null);
                    if (transaction != null) {
                        logger.debugv("bootstrap current transaction status? {0}", transaction.getStatus());
                    }
                } catch (SystemException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        ApplianceBootstrap applianceBootstrap = new ApplianceBootstrap(session);
        boolean createMasterRealm = applianceBootstrap.isNewInstall();
        if (exportImportManager.isRunImport() && exportImportManager.isImportMasterIncluded()) {
            createMasterRealm = false;
        }
        if (createMasterRealm) {
            applianceBootstrap.createMasterRealm();
        }
        session.getTransactionManager().commit();
    } catch (RuntimeException re) {
        if (session.getTransactionManager().isActive()) {
            session.getTransactionManager().rollback();
        }
        throw re;
    } finally {
        session.close();
    }
    if (exportImportManager.isRunImport()) {
        exportImportManager.runImport();
        Quarkus.asyncExit();
    } else {
        importRealms();
    }
    importAddUser();
    return exportImportManager;
}
Also used : ApplianceBootstrap(org.keycloak.services.managers.ApplianceBootstrap) JtaTransactionManagerLookup(org.keycloak.transaction.JtaTransactionManagerLookup) Transaction(javax.transaction.Transaction) KeycloakModelUtils.runJobInTransaction(org.keycloak.models.utils.KeycloakModelUtils.runJobInTransaction) SystemException(javax.transaction.SystemException)

Example 2 with ApplianceBootstrap

use of org.keycloak.services.managers.ApplianceBootstrap in project keycloak by keycloak.

the class QuarkusWelcomeResource method createUser.

@POST
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces(MediaType.TEXT_HTML_UTF_8)
public Response createUser() {
    MultivaluedMap<String, String> formData = request.getDecodedFormParameters();
    if (!shouldBootstrap()) {
        return createWelcomePage(null, null);
    } else {
        if (!isLocal()) {
            ServicesLogger.LOGGER.rejectedNonLocalAttemptToCreateInitialUser(session.getContext().getConnection().getRemoteAddr());
            throw new WebApplicationException(Response.Status.BAD_REQUEST);
        }
        csrfCheck(formData);
        String username = formData.getFirst("username");
        String password = formData.getFirst("password");
        String passwordConfirmation = formData.getFirst("passwordConfirmation");
        if (username != null) {
            username = username.trim();
        }
        if (username == null || username.length() == 0) {
            return createWelcomePage(null, "Username is missing");
        }
        if (password == null || password.length() == 0) {
            return createWelcomePage(null, "Password is missing");
        }
        if (!password.equals(passwordConfirmation)) {
            return createWelcomePage(null, "Password and confirmation doesn't match");
        }
        expireCsrfCookie();
        ApplianceBootstrap applianceBootstrap = new ApplianceBootstrap(session);
        applianceBootstrap.createMasterRealmUser(username, password);
        shouldBootstrap.set(false);
        ServicesLogger.LOGGER.createdInitialAdminUser(username);
        return createWelcomePage("User created", null);
    }
}
Also used : ApplianceBootstrap(org.keycloak.services.managers.ApplianceBootstrap) WebApplicationException(javax.ws.rs.WebApplicationException) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces)

Example 3 with ApplianceBootstrap

use of org.keycloak.services.managers.ApplianceBootstrap in project keycloak by keycloak.

the class QuarkusKeycloakApplication method createAdminUser.

private void createAdminUser() {
    String adminUserName = System.getenv(KEYCLOAK_ADMIN_ENV_VAR);
    String adminPassword = System.getenv(KEYCLOAK_ADMIN_PASSWORD_ENV_VAR);
    if ((adminUserName == null || adminUserName.trim().length() == 0) || (adminPassword == null || adminPassword.trim().length() == 0)) {
        return;
    }
    KeycloakSessionFactory sessionFactory = KeycloakApplication.getSessionFactory();
    KeycloakSession session = sessionFactory.create();
    KeycloakTransactionManager transaction = session.getTransactionManager();
    try {
        transaction.begin();
        new ApplianceBootstrap(session).createMasterRealmUser(adminUserName, adminPassword);
        ServicesLogger.LOGGER.addUserSuccess(adminUserName, Config.getAdminRealm());
        transaction.commit();
    } catch (IllegalStateException e) {
        session.getTransactionManager().rollback();
        ServicesLogger.LOGGER.addUserFailedUserExists(adminUserName, Config.getAdminRealm());
    } catch (Throwable t) {
        session.getTransactionManager().rollback();
        ServicesLogger.LOGGER.addUserFailed(t, adminUserName, Config.getAdminRealm());
    } finally {
        session.close();
    }
}
Also used : ApplianceBootstrap(org.keycloak.services.managers.ApplianceBootstrap) KeycloakTransactionManager(org.keycloak.models.KeycloakTransactionManager) KeycloakSession(org.keycloak.models.KeycloakSession) QuarkusKeycloakSessionFactory(org.keycloak.quarkus.runtime.integration.QuarkusKeycloakSessionFactory) KeycloakSessionFactory(org.keycloak.models.KeycloakSessionFactory)

Example 4 with ApplianceBootstrap

use of org.keycloak.services.managers.ApplianceBootstrap in project keycloak by keycloak.

the class KeycloakApplication method bootstrap.

// Bootstrap master realm, import realms and create admin user.
protected ExportImportManager bootstrap() {
    ExportImportManager[] exportImportManager = new ExportImportManager[1];
    logger.debug("bootstrap");
    KeycloakModelUtils.runJobInTransaction(sessionFactory, new KeycloakSessionTask() {

        @Override
        public void run(KeycloakSession session) {
            // TODO what is the purpose of following piece of code? Leaving it as is for now.
            JtaTransactionManagerLookup lookup = (JtaTransactionManagerLookup) sessionFactory.getProviderFactory(JtaTransactionManagerLookup.class);
            if (lookup != null) {
                if (lookup.getTransactionManager() != null) {
                    try {
                        Transaction transaction = lookup.getTransactionManager().getTransaction();
                        logger.debugv("bootstrap current transaction? {0}", transaction != null);
                        if (transaction != null) {
                            logger.debugv("bootstrap current transaction status? {0}", transaction.getStatus());
                        }
                    } catch (SystemException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
            // TODO up here ^^
            ApplianceBootstrap applianceBootstrap = new ApplianceBootstrap(session);
            exportImportManager[0] = new ExportImportManager(session);
            boolean createMasterRealm = applianceBootstrap.isNewInstall();
            if (exportImportManager[0].isRunImport() && exportImportManager[0].isImportMasterIncluded()) {
                createMasterRealm = false;
            }
            if (createMasterRealm) {
                applianceBootstrap.createMasterRealm();
            }
        }
    });
    if (exportImportManager[0].isRunImport()) {
        exportImportManager[0].runImport();
    } else {
        importRealms();
    }
    importAddUser();
    return exportImportManager[0];
}
Also used : ApplianceBootstrap(org.keycloak.services.managers.ApplianceBootstrap) JtaTransactionManagerLookup(org.keycloak.transaction.JtaTransactionManagerLookup) Transaction(javax.transaction.Transaction) SystemException(javax.transaction.SystemException) KeycloakSessionTask(org.keycloak.models.KeycloakSessionTask) KeycloakSession(org.keycloak.models.KeycloakSession) ExportImportManager(org.keycloak.exportimport.ExportImportManager)

Example 5 with ApplianceBootstrap

use of org.keycloak.services.managers.ApplianceBootstrap in project keycloak by keycloak.

the class KeycloakApplication method startup.

protected void startup() {
    KeycloakApplication.sessionFactory = createSessionFactory();
    ExportImportManager[] exportImportManager = new ExportImportManager[1];
    KeycloakModelUtils.runJobInTransaction(sessionFactory, new KeycloakSessionTask() {

        @Override
        public void run(KeycloakSession session) {
            DBLockManager dbLockManager = new DBLockManager(session);
            dbLockManager.checkForcedUnlock();
            DBLockProvider dbLock = dbLockManager.getDBLock();
            dbLock.waitForLock(DBLockProvider.Namespace.KEYCLOAK_BOOT);
            try {
                exportImportManager[0] = bootstrap();
            } finally {
                dbLock.releaseLock();
            }
        }
    });
    if (exportImportManager[0].isRunExport()) {
        exportImportManager[0].runExport();
    }
    KeycloakModelUtils.runJobInTransaction(sessionFactory, new KeycloakSessionTask() {

        @Override
        public void run(KeycloakSession session) {
            boolean shouldBootstrapAdmin = new ApplianceBootstrap(session).isNoMasterUser();
            BOOTSTRAP_ADMIN_USER.set(shouldBootstrapAdmin);
        }
    });
    sessionFactory.publish(new PostMigrationEvent());
    setupScheduledTasks(sessionFactory);
}
Also used : ApplianceBootstrap(org.keycloak.services.managers.ApplianceBootstrap) KeycloakSessionTask(org.keycloak.models.KeycloakSessionTask) KeycloakSession(org.keycloak.models.KeycloakSession) ExportImportManager(org.keycloak.exportimport.ExportImportManager) DBLockManager(org.keycloak.models.dblock.DBLockManager) DBLockProvider(org.keycloak.models.dblock.DBLockProvider) PostMigrationEvent(org.keycloak.models.utils.PostMigrationEvent)

Aggregations

ApplianceBootstrap (org.keycloak.services.managers.ApplianceBootstrap)8 KeycloakSession (org.keycloak.models.KeycloakSession)5 SystemException (javax.transaction.SystemException)2 Transaction (javax.transaction.Transaction)2 Consumes (javax.ws.rs.Consumes)2 POST (javax.ws.rs.POST)2 Produces (javax.ws.rs.Produces)2 WebApplicationException (javax.ws.rs.WebApplicationException)2 ExportImportManager (org.keycloak.exportimport.ExportImportManager)2 KeycloakSessionTask (org.keycloak.models.KeycloakSessionTask)2 JtaTransactionManagerLookup (org.keycloak.transaction.JtaTransactionManagerLookup)2 KeycloakSessionFactory (org.keycloak.models.KeycloakSessionFactory)1 KeycloakTransactionManager (org.keycloak.models.KeycloakTransactionManager)1 DBLockManager (org.keycloak.models.dblock.DBLockManager)1 DBLockProvider (org.keycloak.models.dblock.DBLockProvider)1 KeycloakModelUtils.runJobInTransaction (org.keycloak.models.utils.KeycloakModelUtils.runJobInTransaction)1 PostMigrationEvent (org.keycloak.models.utils.PostMigrationEvent)1 QuarkusKeycloakSessionFactory (org.keycloak.quarkus.runtime.integration.QuarkusKeycloakSessionFactory)1