100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps
Java asset-managementbuildingdashboarddataplatformdevice-managementedge-computingenergyenergy-managementenergy-monitorfleet-managementfleet-telematicsiotiot-applicationiot-platformmodbusmqttmqtt-brokerota-updaterules-enginesmartcity
Increased manager keycloak client password complexity (#1344)
Rich Turner2dfbc137a7eb
diff --git a/container/src/main/java/org/openremote/container/persistence/PersistenceService.java b/container/src/main/java/org/openremote/container/persistence/PersistenceService.javaindex 39cb14de7..53db9ea54 100644--- a/container/src/main/java/org/openremote/container/persistence/PersistenceService.java+++ b/container/src/main/java/org/openremote/container/persistence/PersistenceService.java@@ -603,6 +603,20 @@ public class PersistenceService implements ContainerService, Consumer<Persistencreturn storageDir;}+ /**+ * Will resolve relative paths relative to {@link #getStorageDir()}+ */+ public Path resolvePath(String path) {+ return resolvePath(Path.of(path));+ }++ /**+ * Will resolve relative paths relative to {@link #getStorageDir()}+ */+ public Path resolvePath(Path path) {+ return getStorageDir().resolve(path);+ }+public static Field[] getEntityPropertyFields(Class<?> clazz, List<String> includeFields, List<String> excludeFields) {return Arrays.stream(clazz.getDeclaredFields()).filter(field -> ((field.isAnnotationPresent(Column.class) || field.isAnnotationPresent(EmbeddedId.class) || field.isAnnotationPresent(JoinColumn.class) || field.isAnnotationPresent(Formula.class)) && (excludeFields == null || !excludeFields.contains(field.getName())))diff --git a/container/src/main/java/org/openremote/container/security/keycloak/KeycloakIdentityProvider.java b/container/src/main/java/org/openremote/container/security/keycloak/KeycloakIdentityProvider.javaindex a1ebe95da..0dba1efcb 100644--- a/container/src/main/java/org/openremote/container/security/keycloak/KeycloakIdentityProvider.java+++ b/container/src/main/java/org/openremote/container/security/keycloak/KeycloakIdentityProvider.java@@ -29,7 +29,6 @@ import io.undertow.server.handlers.proxy.ProxyHandler;import io.undertow.servlet.api.DeploymentInfo;import io.undertow.servlet.api.LoginConfig;import jakarta.ws.rs.NotFoundException;-import jakarta.ws.rs.core.Response;import jakarta.ws.rs.core.UriBuilder;import org.jboss.resteasy.client.jaxrs.ResteasyClient;import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget;@@ -67,8 +66,6 @@ import java.util.function.Supplier;import java.util.logging.Level;import java.util.logging.Logger;-import static jakarta.ws.rs.core.Response.Status.Family.REDIRECTION;-import static jakarta.ws.rs.core.Response.Status.Family.SUCCESSFUL;import static java.util.concurrent.TimeUnit.MINUTES;import static org.openremote.container.util.MapAccess.getInteger;import static org.openremote.container.util.MapAccess.getString;diff --git a/manager/Dockerfile b/manager/Dockerfileindex d78455056..9a316f967 100644--- a/manager/Dockerfile+++ b/manager/Dockerfile@@ -39,7 +39,7 @@ ENV OR_DB_CONNECTION_TIMEOUT_SECONDS ${OR_DB_CONNECTION_TIMEOUT_SECONDS:-300}ENV OR_DB_FLYWAY_OUT_OF_ORDER ${OR_DB_FLYWAY_OUT_OF_ORDER}ENV OR_KEYCLOAK_HOST ${OR_KEYCLOAK_HOST:-keycloak}ENV OR_KEYCLOAK_PORT ${OR_KEYCLOAK_PORT:-8080}-ENV OR_KEYCLOAK_GRANT_FILE ${OR_KEYCLOAK_GRANT_FILE:-/deployment/manager/keycloak.json}+ENV OR_KEYCLOAK_GRANT_FILE ${OR_KEYCLOAK_GRANT_FILE:-manager/keycloak-credentials.json}ENV OR_APP_DOCROOT ${OR_APP_DOCROOT:-/opt/web}ENV OR_CUSTOM_APP_DOCROOT ${OR_CUSTOM_APP_DOCROOT:-/deployment/manager/app}ENV OR_PROVISIONING_DOCROOT ${OR_PROVISIONING_DOCROOT:-/deployment/manager/provisioning}diff --git a/manager/src/main/java/org/openremote/manager/security/ManagerKeycloakIdentityProvider.java b/manager/src/main/java/org/openremote/manager/security/ManagerKeycloakIdentityProvider.javaindex 96223be48..d73264439 100644--- a/manager/src/main/java/org/openremote/manager/security/ManagerKeycloakIdentityProvider.java+++ b/manager/src/main/java/org/openremote/manager/security/ManagerKeycloakIdentityProvider.java@@ -37,7 +37,6 @@ import org.openremote.container.persistence.PersistenceService;import org.openremote.container.security.AuthContext;import org.openremote.container.security.keycloak.KeycloakIdentityProvider;import org.openremote.container.timer.TimerService;-import org.openremote.model.util.UniqueIdentifierGenerator;import org.openremote.container.web.WebService;import org.openremote.manager.apps.ConsoleAppService;import org.openremote.manager.asset.AssetStorageService;@@ -57,6 +56,7 @@ import org.openremote.model.query.filter.RealmPredicate;import org.openremote.model.rules.RealmRuleset;import org.openremote.model.security.*;import org.openremote.model.util.TextUtil;+import org.openremote.model.util.UniqueIdentifierGenerator;import org.openremote.model.util.ValueUtil;import java.io.InputStream;@@ -64,7 +64,6 @@ import java.net.URISyntaxException;import java.nio.charset.StandardCharsets;import java.nio.file.Files;import java.nio.file.Path;-import java.nio.file.Paths;import java.nio.file.StandardOpenOption;import java.util.*;import java.util.function.BiFunction;@@ -73,7 +72,8 @@ import java.util.logging.Level;import java.util.logging.Logger;import java.util.stream.Collectors;-import static org.openremote.container.util.MapAccess.*;+import static org.openremote.container.util.MapAccess.getBoolean;+import static org.openremote.container.util.MapAccess.getString;import static org.openremote.model.Constants.*;import static org.openremote.model.util.ValueUtil.convert;@@ -88,7 +88,7 @@ public class ManagerKeycloakIdentityProvider extends KeycloakIdentityProvider impublic static final String DEFAULT_REALM_KEYCLOAK_THEME = "DEFAULT_REALM_KEYCLOAK_THEME";public static final String DEFAULT_REALM_KEYCLOAK_THEME_DEFAULT = "openremote";public static final String OR_KEYCLOAK_GRANT_FILE = "OR_KEYCLOAK_GRANT_FILE";- public static final String OR_KEYCLOAK_GRANT_FILE_DEFAULT = "manager/build/keycloak.json";+ public static final String OR_KEYCLOAK_GRANT_FILE_DEFAULT = "manager/keycloak-credentials.json";public static final String OR_KEYCLOAK_PUBLIC_URI = "OR_KEYCLOAK_PUBLIC_URI";public static final String OR_KEYCLOAK_PUBLIC_URI_DEFAULT = "/auth";@@ -144,16 +144,16 @@ public class ManagerKeycloakIdentityProvider extends KeycloakIdentityProvider improtected OAuthGrant getStoredCredentials(Container container) {// Try and load keycloak proxy credentials from the file systemString grantFile = getString(container.getConfig(), OR_KEYCLOAK_GRANT_FILE, OR_KEYCLOAK_GRANT_FILE_DEFAULT);- Path grantPath = TextUtil.isNullOrEmpty(grantFile) ? null : Paths.get(grantFile);+ Path grantPath = TextUtil.isNullOrEmpty(grantFile) ? null : persistenceService.resolvePath(grantFile);OAuthGrant grant = null;if (grantPath != null && Files.isReadable(grantPath)) {- LOG.info("Loading OR_KEYCLOAK_GRANT_FILE: " + grantFile);+ LOG.info("Loading OR_KEYCLOAK_GRANT_FILE: " + grantPath);try (InputStream is = Files.newInputStream(grantPath)) {String grantJson = IOUtils.toString(is, StandardCharsets.UTF_8);grant = ValueUtil.parse(grantJson, OAuthGrant.class).orElseGet(() -> {- LOG.warning("Failed to load OR_KEYCLOAK_GRANT_FILE: " + grantFile);+ LOG.warning("Failed to load OR_KEYCLOAK_GRANT_FILE: " + grantPath);return null;});} catch (Exception ex) {@@ -171,33 +171,39 @@ public class ManagerKeycloakIdentityProvider extends KeycloakIdentityProvider imreturn null;}+ Path grantPath = persistenceService.resolvePath(grantFile);+// Create a new super user for the keycloak proxy so admin user can be modified if desiredUser keycloakProxyUser = new User().setUsername(MANAGER_CLIENT_ID).setEnabled(true).setSystemAccount(true);- String password = UniqueIdentifierGenerator.generateId();- keycloakProxyUser = createUpdateUser(MASTER_REALM, keycloakProxyUser, password, true);+ // Make password complex enough to hopefully meet any password policy that is set in keycloak+ String password = UniqueIdentifierGenerator.generateId() + "$*#@$";- // Make this proxy user a super user by giving them admin realm role- updateUserRealmRoles(MASTER_REALM, keycloakProxyUser.getId(), addRealmRoles(MASTER_REALM, keycloakProxyUser.getId(), REALM_ADMIN_ROLE));+ try {+ keycloakProxyUser = createUpdateUser(MASTER_REALM, keycloakProxyUser, password, true);- // Use same details as default keycloak grant but change the username and password to our new user- OAuthPasswordGrant grant = getDefaultKeycloakGrant(container);- grant.setUsername(keycloakProxyUser.getUsername()).setPassword(password);- Path grantPath = Paths.get(grantFile);+ // Make this proxy user a super user by giving them admin realm role+ updateUserRealmRoles(MASTER_REALM, keycloakProxyUser.getId(), addRealmRoles(MASTER_REALM, keycloakProxyUser.getId(), REALM_ADMIN_ROLE));++ // Use same details as default keycloak grant but change the username and password to our new user+ OAuthPasswordGrant grant = getDefaultKeycloakGrant(container);+ grant.setUsername(keycloakProxyUser.getUsername()).setPassword(password);++ // Ensure the directory path exists+ grantPath.getParent().toFile().mkdirs();- try {Files.write(grantPath, ValueUtil.asJSON(grant).orElse("null").getBytes(),StandardOpenOption.CREATE,StandardOpenOption.WRITE,StandardOpenOption.TRUNCATE_EXISTING);++ return grant;} catch (Exception e) {- LOG.info("Failed to write " + OR_KEYCLOAK_GRANT_FILE + ": " + grantFile);+ LOG.info("Failed to write " + OR_KEYCLOAK_GRANT_FILE + ": " + grantPath);return null;}-- return grant;}@Overridediff --git a/profile/deploy.yml b/profile/deploy.ymlindex bf7a353b5..f06a98b73 100644--- a/profile/deploy.yml+++ b/profile/deploy.yml@@ -311,6 +311,14 @@ services:# Set the number of concurrent attribute event processors (defaults to number of CPU cores)OR_ATTRIBUTE_EVENT_THREADS:+ # Set the persistence storage dir (must also be volume mapped)+ OR_STORAGE_DIR:++ # Set the path to the keycloak credentials grant file generated at setup to allow the manager to communicate with+ # the Keycloak admin API; this means OR_ADMIN_PASSWORD can be changed without breaking connection to Keycloak+ # relative paths are resolved relative to OR_STORAGE_DIR+ OR_KEYCLOAK_GRANT_FILE:+# Configure the default max age for data points stored in the database (individual attributes can use a custom# value by using the DATA_POINTS_MAX_AGE_DAYS AssetMeta item).# OR_DATA_POINTS_MAX_AGE_DAYS: 30