Skip to content

armgit / core

Public Imported from GitHub
0

100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps

Removed broken DISABLE_API_SECURITY option

Christian Bauer2d1ec6835f95
diff --git a/container/src/main/java/org/openremote/container/security/IdentityService.java b/container/src/main/java/org/openremote/container/security/IdentityService.java
index be0298d1c..29b72edea 100644
--- a/container/src/main/java/org/openremote/container/security/IdentityService.java
+++ b/container/src/main/java/org/openremote/container/security/IdentityService.java
@@ -67,9 +67,6 @@ public class IdentityService implements ContainerService {
private static final Logger LOG = Logger.getLogger(IdentityService.class.getName());
- public static final String DISABLE_API_SECURITY = "DISABLE_API_SECURITY";
- public static final boolean DISABLE_API_SECURITY_DEFAULT = false;
-
public static final String IDENTITY_NETWORK_SECURE = "IDENTITY_NETWORK_SECURE";
public static final boolean IDENTITY_NETWORK_SECURE_DEFAULT = false;
public static final String IDENTITY_NETWORK_HOST = "IDENTITY_NETWORK_HOST";
@@ -92,19 +89,38 @@ public class IdentityService implements ContainerService {
public static final String AUTH_PATH = "/auth";
+ // Each realm in Keycloak has a client application with this identifier
+ final protected String clientId;
+
+ // The token issuer URL, the externally visible address of this service
protected URI externalAuthServerUri;
+
+ // The internal host where Keycloak can be found
protected UriBuilder keycloakHostUri;
protected UriBuilder keycloakServiceUri;
+
+ // The client we use to access Keycloak
protected Client httpClient;
+
+ // Cache realms so we don't have to access Keycloak for every token validation
protected LoadingCache<ClientRealm.Key, ClientRealm> clientApplicationCache;
+
+ // The configuration for the Keycloak servlet extension, looks up the client application per realm
protected KeycloakConfigResolver keycloakConfigResolver;
+
+ // Optional reverse proxy that listens to AUTH_PATH and forwards requests to Keycloak
protected ProxyHandler authProxyHandler;
- protected String clientId;
+
+ // Configuration options for new realms
protected int sessionTimeoutSeconds;
// This will pass authentication ("NOT ATTEMPTED" state), but later fail any role authorization
final protected KeycloakDeployment notAuthenticatedKeycloakDeployment = new KeycloakDeployment();
+ public IdentityService(String clientId) {
+ this.clientId = clientId;
+ }
+
@Override
public void init(Container container) throws Exception {
boolean identityNetworkSecure = getBoolean(container.getConfig(), IDENTITY_NETWORK_SECURE, IDENTITY_NETWORK_SECURE_DEFAULT);
@@ -158,26 +174,20 @@ public class IdentityService implements ContainerService {
clientApplicationCache = createClientApplicationCache();
- if (getBoolean(container.getConfig(), DISABLE_API_SECURITY, DISABLE_API_SECURITY_DEFAULT)) {
- LOG.warning("###################### API SECURITY DISABLED! ######################");
- } else {
- if (getClientId() == null)
- throw new IllegalStateException("Client ID must be set to enable API security");
- keycloakConfigResolver = request -> {
- // The realm we authenticate against must be available as a request header
- String realm = request.getHeader(WebService.REQUEST_HEADER_REALM);
- if (realm == null || realm.length() == 0) {
- LOG.fine("No realm in request, no authentication will be attempted: " + request.getURI());
- return notAuthenticatedKeycloakDeployment;
- }
- ClientRealm clientRealm = getClientRealm(realm, getClientId());
- if (clientRealm == null) {
- LOG.fine("No client application configured for realm, no authentication will be attempted: " + request.getURI());
- return notAuthenticatedKeycloakDeployment;
- }
- return clientRealm.keycloakDeployment;
- };
- }
+ keycloakConfigResolver = request -> {
+ // The realm we authenticate against must be available as a request header
+ String realm = request.getHeader(WebService.REQUEST_HEADER_REALM);
+ if (realm == null || realm.length() == 0) {
+ LOG.fine("No realm in request, no authentication will be attempted: " + request.getURI());
+ return notAuthenticatedKeycloakDeployment;
+ }
+ ClientRealm clientRealm = getClientRealm(realm, getClientId());
+ if (clientRealm == null) {
+ LOG.fine("No client application configured for realm, no authentication will be attempted: " + request.getURI());
+ return notAuthenticatedKeycloakDeployment;
+ }
+ return clientRealm.keycloakDeployment;
+ };
authProxyHandler = new ProxyHandler(
new SimpleProxyClientProvider(keycloakHostUri.build()),
@@ -190,6 +200,10 @@ public class IdentityService implements ContainerService {
);
}
+ public String getClientId() {
+ return clientId;
+ }
+
@Override
public void start(Container container) throws Exception {
// TODO Not a great way to block startup while we wait for other services (Hystrix?)
@@ -343,14 +357,6 @@ public class IdentityService implements ContainerService {
.build(loader);
}
- public String getClientId() {
- return clientId;
- }
-
- public void setClientId(String clientId) {
- this.clientId = clientId;
- }
-
public void configureRealm(RealmRepresentation realmRepresentation, int accessTokenLifespanSeconds) {
realmRepresentation.setDisplayNameHtml(
"<div class=\"kc-logo-text\"><span>OpenRemote: "
@@ -366,14 +372,14 @@ public class IdentityService implements ContainerService {
realmRepresentation.setSslRequired(SslRequired.NONE.toString());
}
- public ClientRepresentation createClientApplication(String realm, String clientId, String appName, boolean isDevMode) {
+ public ClientRepresentation createClientApplication(String realm, String clientId, String appName, boolean enableDirectAccess) {
ClientRepresentation client = new ClientRepresentation();
client.setClientId(clientId);
client.setName(appName);
client.setPublicClient(true);
- if (isDevMode) {
+ if (enableDirectAccess) {
// We need direct access for integration tests
LOG.warning("### Allowing direct access grants for client app '" + appName + "', this must NOT be used in production! ###");
client.setDirectAccessGrantsEnabled(true);
diff --git a/container/src/main/java/org/openremote/container/web/WebService.java b/container/src/main/java/org/openremote/container/web/WebService.java
index 2b6190100..a32c59870 100644
--- a/container/src/main/java/org/openremote/container/web/WebService.java
+++ b/container/src/main/java/org/openremote/container/web/WebService.java
@@ -32,6 +32,7 @@ import io.undertow.util.HttpString;
import org.jboss.resteasy.plugins.server.servlet.HttpServlet30Dispatcher;
import org.jboss.resteasy.plugins.server.servlet.ResteasyContextParameters;
import org.jboss.resteasy.spi.ResteasyDeployment;
+import org.keycloak.adapters.KeycloakConfigResolver;
import org.openremote.container.Container;
import org.openremote.container.ContainerService;
import org.openremote.container.json.ElementalMessageBodyConverter;
@@ -270,14 +271,19 @@ public abstract class WebService implements ContainerService {
throw new IllegalStateException(
"No identity service found, make sure " + IdentityService.class.getName() + " is added before this service"
);
+
+ KeycloakConfigResolver keycloakConfigResolver = identityService.getKeycloakConfigResolver();
+ if (keycloakConfigResolver == null)
+ throw new IllegalStateException(
+ "No Keycloak config resolver found, make sure " + IdentityService.class.getName() + " is initialized"
+ );
+
LOG.info("Deploying secure web context: " + deploymentInfo.getContextPath());
deploymentInfo.addOuterHandlerChainWrapper(AuthOverloadHandler::new);
deploymentInfo.setSecurityDisabled(false);
LoginConfig loginConfig = new LoginConfig(SimpleKeycloakServletExtension.AUTH_MECHANISM, "OpenRemote");
deploymentInfo.setLoginConfig(loginConfig);
- deploymentInfo.addServletExtension(new SimpleKeycloakServletExtension(
- identityService.getKeycloakConfigResolver()
- ));
+ deploymentInfo.addServletExtension(new SimpleKeycloakServletExtension(keycloakConfigResolver));
} else {
LOG.info("Deploying insecure web context: " + deploymentInfo.getContextPath());
}
diff --git a/manager/server/src/main/java/org/openremote/manager/server/security/ManagerIdentityService.java b/manager/server/src/main/java/org/openremote/manager/server/security/ManagerIdentityService.java
index 3c45b6e02..99a593105 100644
--- a/manager/server/src/main/java/org/openremote/manager/server/security/ManagerIdentityService.java
+++ b/manager/server/src/main/java/org/openremote/manager/server/security/ManagerIdentityService.java
@@ -46,13 +46,18 @@ public class ManagerIdentityService extends IdentityService {
private static final Logger LOG = Logger.getLogger(ManagerIdentityService.class.getName());
- protected Container container;
+ protected boolean devMode;
+
+ public ManagerIdentityService() {
+ super(Constants.KEYCLOAK_CLIENT_ID);
+ }
@Override
public void init(Container container) throws Exception {
- this.container = container;
- setClientId(Constants.KEYCLOAK_CLIENT_ID);
super.init(container);
+
+ this.devMode = container.isDevMode();
+
enableAuthProxy(container.getService(WebService.class));
container.getService(WebService.class).getApiSingletons().add(
@@ -91,14 +96,14 @@ public class ManagerIdentityService extends IdentityService {
List<Tenant> tenants = new ArrayList<>();
for (RealmRepresentation realm : realms) {
- tenants.add(convert(container.JSON, Tenant.class, realm));
+ tenants.add(convert(Container.JSON, Tenant.class, realm));
}
return tenants.toArray(new Tenant[tenants.size()]);
}
public Tenant getTenant(String bearerAuth, String realm) {
return convert(
- container.JSON,
+ Container.JSON,
Tenant.class,
getRealms(bearerAuth).realm(realm).toRepresentation()
);
@@ -110,7 +115,7 @@ public class ManagerIdentityService extends IdentityService {
public void createTenant(String bearerAuth, Tenant tenant) throws Exception {
LOG.fine("Create tenant: " + tenant);
- RealmRepresentation realmRepresentation = convert(container.JSON, RealmRepresentation.class, tenant);
+ RealmRepresentation realmRepresentation = convert(Container.JSON, RealmRepresentation.class, tenant);
configureRealm(realmRepresentation);
getRealms(bearerAuth).create(realmRepresentation);
// TODO This is not atomic, write compensation actions
@@ -120,7 +125,7 @@ public class ManagerIdentityService extends IdentityService {
public void createClientApplication(String bearerAuth, String realm) {
ClientsResource clientsResource = getRealms(bearerAuth).realm(realm).clients();
ClientRepresentation client = createClientApplication(
- realm, KEYCLOAK_CLIENT_ID, "OpenRemote", container.isDevMode()
+ realm, KEYCLOAK_CLIENT_ID, "OpenRemote", devMode
);
clientsResource.create(client);
client = clientsResource.findByClientId(client.getClientId()).get(0);
@@ -131,7 +136,7 @@ public class ManagerIdentityService extends IdentityService {
public void updateTenant(String bearerAuth, String realm, Tenant tenant) throws Exception {
LOG.fine("Update tenant: " + tenant);
getRealms(bearerAuth).realm(realm).update(
- convert(container.JSON, RealmRepresentation.class, tenant)
+ convert(Container.JSON, RealmRepresentation.class, tenant)
);
}