Use global bootstrap flag

This commit is contained in:
Dmitry Telegin 2019-10-31 00:22:30 +03:00 committed by Stian Thorgersen
parent 11f761ec1f
commit 203646627f
2 changed files with 25 additions and 26 deletions

View file

@ -97,6 +97,8 @@ public class KeycloakApplication extends Application {
public static final String SERVER_CONTEXT_CONFIG_PROPERTY_OVERRIDES = "keycloak.server.context.config.property-overrides";
public static final AtomicBoolean BOOTSTRAP_ADMIN_USER = new AtomicBoolean(false);
private static final Logger logger = Logger.getLogger(KeycloakApplication.class);
protected boolean embedded = false;
@ -107,8 +109,6 @@ public class KeycloakApplication extends Application {
protected KeycloakSessionFactory sessionFactory;
protected String contextPath;
private AtomicBoolean bootstrapAdminUser = new AtomicBoolean(false);
public KeycloakApplication() {
try {
@ -197,7 +197,7 @@ public class KeycloakApplication extends Application {
@Override
public void run(KeycloakSession session) {
boolean shouldBootstrapAdmin = new ApplianceBootstrap(session).isNoMasterUser();
bootstrapAdminUser.set(shouldBootstrapAdmin);
BOOTSTRAP_ADMIN_USER.set(shouldBootstrapAdmin);
}
});
@ -385,10 +385,6 @@ public class KeycloakApplication extends Application {
return singletons;
}
boolean isBootstrap() {
return bootstrapAdminUser.get();
}
public void importRealms() {
String files = System.getProperty("keycloak.import");
if (files != null) {

View file

@ -68,17 +68,12 @@ public class WelcomeResource {
private static final String KEYCLOAK_STATE_CHECKER = "WELCOME_STATE_CHECKER";
private boolean bootstrap = true;
@Context
protected HttpHeaders headers;
@Context
private KeycloakSession session;
@Context
private KeycloakApplication application;
public WelcomeResource() {
}
@ -107,7 +102,7 @@ public class WelcomeResource {
public Response createUser(final MultivaluedMap<String, String> formData) {
checkBootstrap();
if (!bootstrap) {
if (!shouldBootstrap()) {
return createWelcomePage(null, null);
} else {
if (!isLocal()) {
@ -141,7 +136,7 @@ public class WelcomeResource {
ApplianceBootstrap applianceBootstrap = new ApplianceBootstrap(session);
if (applianceBootstrap.isNoMasterUser()) {
bootstrap = false;
setBootstrap(false);
applianceBootstrap.createMasterRealmUser(username, password);
ServicesLogger.LOGGER.createdInitialAdminUser(username);
@ -179,20 +174,21 @@ public class WelcomeResource {
private Response createWelcomePage(String successMessage, String errorMessage) {
try {
Theme theme = getTheme();
Theme theme = getTheme();
Map<String, Object> map = new HashMap<>();
Map<String, Object> map = new HashMap<>();
map.put("productName", Version.NAME);
map.put("productNameFull", Version.NAME_FULL);
map.put("productName", Version.NAME);
map.put("productNameFull", Version.NAME_FULL);
map.put("properties", theme.getProperties());
map.put("properties", theme.getProperties());
URI uri = Urls.themeRoot(session.getContext().getUri().getBaseUri());
String resourcesPath = uri.getPath() + "/" + theme.getType().toString().toLowerCase() +"/" + theme.getName();
map.put("resourcesPath", resourcesPath);
URI uri = Urls.themeRoot(session.getContext().getUri().getBaseUri());
String resourcesPath = uri.getPath() + "/" + theme.getType().toString().toLowerCase() + "/" + theme.getName();
map.put("resourcesPath", resourcesPath);
map.put("bootstrap", bootstrap);
boolean bootstrap = shouldBootstrap();
map.put("bootstrap", bootstrap);
if (bootstrap) {
boolean isLocal = isLocal();
map.put("localUser", isLocal);
@ -230,9 +226,16 @@ public class WelcomeResource {
}
private void checkBootstrap() {
if (bootstrap && application.isBootstrap()) {
bootstrap = new ApplianceBootstrap(session).isNoMasterUser();
}
if (shouldBootstrap())
KeycloakApplication.BOOTSTRAP_ADMIN_USER.compareAndSet(true, new ApplianceBootstrap(session).isNoMasterUser());
}
private boolean shouldBootstrap() {
return KeycloakApplication.BOOTSTRAP_ADMIN_USER.get();
}
private void setBootstrap(boolean value) {
KeycloakApplication.BOOTSTRAP_ADMIN_USER.set(value);
}
private boolean isLocal() {