89286c2a by Adam Heath

Use a ToolFactory to build a working Keycloak at startup.

1 parent af3ad96c
1 <?xml version="1.0" encoding="UTF-8" ?>
2 <!-- No copyright or license for configuration file, details here are not considered a creative work. -->
3 <moqui-conf xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://moqui.org/xsd/moqui-conf-3.xsd">
4 <default-property name="moqui_keycloak_realm" value="master"/>
5 <default-property name="moqui_keycloak_server_url" value="http://keycloak"/>
6 <default-property name="moqui_keycloak_client_id" value="moqui"/>
7 <default-property name="moqui_keycloak_client_secret" is-secret="true"/>
8
9 <tools>
10 <tool-factory class="org.moqui.keycloak.KeycloakToolFactory" init-priority="20" disabled="false"/>
11 </tools>
12 </moqui-conf>
...@@ -23,6 +23,9 @@ import org.moqui.entity.EntityFind ...@@ -23,6 +23,9 @@ import org.moqui.entity.EntityFind
23 import org.moqui.entity.EntityList 23 import org.moqui.entity.EntityList
24 import org.moqui.entity.EntityValue 24 import org.moqui.entity.EntityValue
25 25
26 import org.moqui.keycloak.KeycloakToolFactory
27
28
26 import org.keycloak.OAuth2Constants 29 import org.keycloak.OAuth2Constants
27 import org.keycloak.admin.client.Keycloak 30 import org.keycloak.admin.client.Keycloak
28 import org.keycloak.admin.client.KeycloakBuilder 31 import org.keycloak.admin.client.KeycloakBuilder
...@@ -50,16 +53,6 @@ String keycloakToJson(Object o) { ...@@ -50,16 +53,6 @@ String keycloakToJson(Object o) {
50 return JsonSerialization.writeValueAsString(o) 53 return JsonSerialization.writeValueAsString(o)
51 } 54 }
52 55
53 Keycloak buildKeycloak() {
54 return KeycloakBuilder.builder()
55 .serverUrl('http://keycloak')
56 .realm('master')
57 .grantType(OAuth2Constants.CLIENT_CREDENTIALS)
58 .clientId('moqui')
59 .clientSecret('iXsnjGEbIVT8DQky2yCU9NQhnqDYyi7g')
60 .build()
61 }
62
63 Map<String, Object> buildClientConsent() { 56 Map<String, Object> buildClientConsent() {
64 return [ 57 return [
65 clientId: null, 58 clientId: null,
...@@ -137,7 +130,7 @@ Map<String, Object> joinUserAccountToKeycloak() { ...@@ -137,7 +130,7 @@ Map<String, Object> joinUserAccountToKeycloak() {
137 return [:] 130 return [:]
138 } 131 }
139 132
140 Keycloak keycloak = buildKeycloak() 133 Keycloak keycloak = KeycloakToolFactory.getInstance()
141 134
142 try { 135 try {
143 RealmResource realm = keycloak.realm('master') 136 RealmResource realm = keycloak.realm('master')
...@@ -279,7 +272,7 @@ Map<String, Object> pushKeycloakUser() { ...@@ -279,7 +272,7 @@ Map<String, Object> pushKeycloakUser() {
279 Map<String, Object> getKeycloakUsers() { 272 Map<String, Object> getKeycloakUsers() {
280 String keycloakClientId = 'moqui' 273 String keycloakClientId = 'moqui'
281 274
282 Keycloak keycloak = buildKeycloak() 275 Keycloak keycloak = ec.getTool('Keycloak', Keycloak.class)
283 276
284 try { 277 try {
285 RealmResource realm = keycloak.realm('master') 278 RealmResource realm = keycloak.realm('master')
......
1 /*
2 * This software is in the public domain under CC0 1.0 Universal plus a
3 * Grant of Patent License.
4 *
5 * To the extent possible under law, the author(s) have dedicated all
6 * copyright and related and neighboring rights to this software to the
7 * public domain worldwide. This software is distributed without any
8 * warranty.
9 *
10 * You should have received a copy of the CC0 Public Domain Dedication
11 * along with this software (see the LICENSE.md file). If not, see
12 * <http://creativecommons.org/publicdomain/zero/1.0/>.
13 */
14 package org.moqui.keycloak
15
16 import groovy.transform.CompileStatic
17 import org.moqui.context.ExecutionContextFactory
18 import org.moqui.context.ToolFactory
19 import org.slf4j.Logger
20 import org.slf4j.LoggerFactory
21
22 import org.keycloak.OAuth2Constants
23 import org.keycloak.admin.client.Keycloak
24 import org.keycloak.admin.client.KeycloakBuilder
25 import org.keycloak.representations.info.ServerInfoRepresentation
26
27 /**
28 *
29 */
30 @CompileStatic
31 class KeycloakToolFactory implements ToolFactory<Keycloak> {
32 protected final static Logger logger = LoggerFactory.getLogger(KeycloakToolFactory.class)
33 final static String TOOL_NAME = "Keycloak"
34
35 protected Keycloak keycloak = null
36
37 KeycloakToolFactory() { }
38
39 @Override
40 String getName() {
41 return TOOL_NAME
42 }
43
44 @Override
45 void init(ExecutionContextFactory ecf) {
46 logger.info("Starting Keycloak")
47 try {
48 ServerInfoRepresentation serverInfo = keycloak.serverInfo().getInfo()
49 } catch (Exception e) {
50 logger.error("Could not connect to keycloak server")
51 throw e
52 }
53 }
54
55 @Override
56 void preFacadeInit(ExecutionContextFactory ecf) {
57 this.keycloak = KeycloakBuilder.builder()
58 .serverUrl((String) System.getProperty("moqui_keycloak_server_url"))
59 .realm((String) System.getProperty("moqui_keycloak_realm"))
60 .grantType((String) OAuth2Constants.CLIENT_CREDENTIALS)
61 .clientId((String) System.getProperty("moqui_keycloak_client_id"))
62 .clientSecret((String) System.getProperty("moqui_keycloak_client_secret"))
63 .build()
64 }
65
66 @Override
67 Keycloak getInstance(Object... parameters) {
68 return keycloak
69 }
70
71 @Override
72 void destroy() {
73 if (keycloak != null) {
74 keycloak.close()
75 }
76 }
77 }