Skip to content

Commit 9a9801a

Browse files
authored
Merge pull request #41 from fdelavega/feature/keycloak-support
Add support to Keycloak plugin
2 parents 019762c + aa78440 commit 9a9801a

10 files changed

Lines changed: 141 additions & 26 deletions

File tree

1.3/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ COPY ./docker-entrypoint.sh /
2121
COPY ./manage.py /usr/local/bin/
2222

2323
RUN adduser --system --group --shell /bin/bash wirecloud && \
24-
pip install --no-cache-dir channels asgi_ipc asgi_redis asgi_rabbitmq && \
24+
pip install --no-cache-dir channels asgi_ipc asgi_redis asgi_rabbitmq wirecloud_keycloak && \
2525
mkdir -p /opt/wirecloud_instance /var/www/static && \
2626
cd /opt && \
2727
wirecloud-admin startproject wirecloud_instance wirecloud_instance && \

1.3/docker-compose.yml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,17 @@ services:
5353
- FORWARDED_ALLOW_IPS=*
5454
- ELASTICSEARCH2_URL=http://elasticsearch:9200/
5555
- MEMCACHED_LOCATION=memcached:11211
56-
# Uncomment the following environment variables to enable IDM integration
56+
# Uncomment the following environment variables to enable FIWARE IDM integration
5757
#- FIWARE_IDM_SERVER=${FIWARE_IDM_SERVER}
5858
#- SOCIAL_AUTH_FIWARE_KEY=${SOCIAL_AUTH_FIWARE_KEY}
5959
#- SOCIAL_AUTH_FIWARE_SECRET=${SOCIAL_AUTH_FIWARE_SECRET}
60+
# Uncomment the following environment variables to enable Keycloak IDM Integration
61+
#- KEYCLOAK_IDM_SERVER=${KEYCLOAK_IDM_SERVER}
62+
#- KEYCLOAK_REALM=${KEYCLOAK_REALM}
63+
#- KEYCLOAK_KEY=${KEYCLOAK_KEY}
64+
#- KEYCLOAK_GLOBAL_ROLE=${KEYCLOAK_GLOBAL_ROLE}
65+
#- SOCIAL_AUTH_KEYCLOAK_KEY=${SOCIAL_AUTH_KEYCLOAK_KEY}
66+
#- SOCIAL_AUTH_KEYCLOAK_SECRET=${SOCIAL_AUTH_KEYCLOAK_SECRET}
6067
volumes:
6168
- ./wirecloud-data:/opt/wirecloud_instance/data
6269
- ./wirecloud-static:/var/www/static

1.3/settings.py

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -126,19 +126,53 @@
126126
# Python dotted path to the WSGI application used by Django's runserver.
127127
WSGI_APPLICATION = 'wirecloud_instance.wsgi.application'
128128

129-
# FIWARE IdM configuration
130-
FIWARE_IDM_SERVER = os.environ.get('FIWARE_IDM_SERVER', '').strip()
131-
FIWARE_IDM_PUBLIC_URL = os.environ.get('FIWARE_IDM_PUBLIC_URL', FIWARE_IDM_SERVER).strip()
132-
SOCIAL_AUTH_FIWARE_KEY = os.environ.get('SOCIAL_AUTH_FIWARE_KEY', '').strip()
133-
SOCIAL_AUTH_FIWARE_SECRET = os.environ.get('SOCIAL_AUTH_FIWARE_SECRET', '').strip()
134-
IDM_AUTH_ENABLED = FIWARE_IDM_SERVER and SOCIAL_AUTH_FIWARE_KEY and SOCIAL_AUTH_FIWARE_SECRET
135-
136-
if IDM_AUTH_ENABLED:
129+
130+
# Handle some basic settings
131+
132+
## String settings
133+
STRING_SETTINGS = (
134+
"FIWARE_IDM_SERVER",
135+
"FIWARE_IDM_PUBLIC_URL",
136+
"SOCIAL_AUTH_FIWARE_KEY",
137+
"SOCIAL_AUTH_FIWARE_SECRET",
138+
"KEYCLOAK_SERVER",
139+
"KEYCLOAK_REALM",
140+
"KEYCLOAK_KEY",
141+
"SOCIAL_AUTH_KEYCLOAK_KEY",
142+
"SOCIAL_AUTH_KEYCLOAK_SECRET",
143+
)
144+
for setting in STRING_SETTINGS:
145+
value = os.environ.get(setting, "").strip()
146+
if value != "":
147+
locals()[setting] = value
148+
149+
## Boolean settings
150+
BOOLEAN_SETTINGS = (
151+
"KEYCLOAK_GLOBAL_ROLE",
152+
)
153+
for setting in BOOLEAN_SETTINGS:
154+
value = os.environ.get(setting, "").strip()
155+
if value != "":
156+
locals()[setting] = value.lower() == "true"
157+
158+
159+
# FIWARE & Keycloak configuration
160+
IDM_AUTH = 'fiware' if "FIWARE_IDM_SERVER" in locals() and "SOCIAL_AUTH_FIWARE_KEY" in locals() and "SOCIAL_AUTH_FIWARE_SECRET" in locals() else None
161+
IDM_AUTH = 'keycloak' if "KEYCLOAK_IDM_SERVER" in locals() and "KEYCLOAK_REALM" in locals() and "KEYCLOAK_KEY" in locals() and "SOCIAL_AUTH_KEYCLOAK_KEY" in locals() and "SOCIAL_AUTH_KEYCLOAK_SECRET" in locals() else IDM_AUTH
162+
163+
if IDM_AUTH == 'fiware':
137164
INSTALLED_APPS += (
138165
'wirecloud.fiware',
139166
'social_django',
140167
'haystack',
141168
)
169+
elif IDM_AUTH == 'keycloak':
170+
INSTALLED_APPS += (
171+
'wirecloud.fiware',
172+
'wirecloud.keycloak',
173+
'social_django',
174+
'haystack',
175+
)
142176
else:
143177
INSTALLED_APPS += (
144178
'wirecloud.oauth2provider',
@@ -192,10 +226,14 @@
192226
USE_X_FORWARDED_PORT = True
193227

194228
# Auth configuration
195-
if IDM_AUTH_ENABLED:
229+
if IDM_AUTH == 'fiware':
196230
AUTHENTICATION_BACKENDS = (
197231
'wirecloud.fiware.social_auth_backend.FIWAREOAuth2',
198232
)
233+
elif IDM_AUTH == 'keycloak':
234+
AUTHENTICATION_BACKENDS = (
235+
'wirecloud.keycloak.social_auth_backend.KeycloakOAuth2',
236+
)
199237
else:
200238
AUTHENTICATION_BACKENDS = (
201239
'django.contrib.auth.backends.ModelBackend',

1.3/urls.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,18 @@
88

99
from wirecloud.commons import authentication as wc_auth
1010
from wirecloud.fiware import views as wc_fiware
11+
from wirecloud.keycloak import views as wc_keycloak
1112
import wirecloud.platform.urls
1213

1314
admin.autodiscover()
1415

16+
login_method = django_auth.login
17+
if settings.IDM_AUTH == 'fiware':
18+
login_method = wc_fiware.login
19+
20+
if settings.IDM_AUTH == 'keycloak':
21+
login_method = wc_keycloak.login
22+
1523
urlpatterns = (
1624

1725
# Catalogue
@@ -21,15 +29,15 @@
2129
url(r'^cdp/', include('wirecloud.proxy.urls')),
2230

2331
# Login/logout
24-
url(r'^login/?$', wc_fiware.login if settings.IDM_AUTH_ENABLED else django_auth.login, name="login"),
32+
url(r'^login/?$', login_method, name="login"),
2533
url(r'^logout/?$', wc_auth.logout, name="logout"),
2634
url(r'^admin/logout/?$', wc_auth.logout),
2735

2836
# Admin interface
2937
url(r'^admin/', include(admin.site.urls)),
3038
)
3139

32-
if settings.IDM_AUTH_ENABLED:
40+
if settings.IDM_AUTH is not None:
3341
urlpatterns += (url('', include('social_django.urls', namespace='social')),)
3442

3543
urlpatterns += wirecloud.platform.urls.urlpatterns

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,15 @@ The following environment variables are also honored for configuring your WireCl
5858
the browser to the IdM portal)
5959
- `-e SOCIAL_AUTH_FIWARE_KEY=...` (defaults to nothing)
6060
- `-e SOCIAL_AUTH_FIWARE_SECRET=...` (defaults to nothing)
61+
- `-e KEYCLOAK_IDM_SERVER=...` (defaults to nothing, leave it empty for
62+
authenticating users using the credentials stored on the WireCloud
63+
database.)
64+
- `-e KEYCLOAK_REALM=...` (default to nothing, realm to use for connecting with
65+
keycloak)
66+
- `-e KEYCLOAK_KEY=...` (default to nothing)
67+
- `-e KEYCLOAK_GLOBAL_ROLE=...` (default to "False")
68+
- `-e SOCIAL_AUTH_KEYCLOAK_KEY=...` (defaults to nothing)
69+
- `-e SOCIAL_AUTH_KEYCLOAK_SECRET=...` (defaults to nothing)
6170

6271
When running WireCloud with TLS behind a reverse proxy such as Apache/NGINX
6372
which is responsible for doing TLS termination, be sure to set

dev/.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
/elasticsearch-data/
22
/postgres-data/
3-
/wirecloud-instance/
3+
/wirecloud-data/
44
/wirecloud-static/

dev/Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ COPY ./manage.py /usr/local/bin/
4242

4343
RUN adduser --system --group --shell /bin/bash wirecloud && \
4444
pip install --no-cache-dir channels asgi_ipc asgi_redis asgi_rabbitmq && \
45+
pip install wirecloud-keycloak && \
4546
mkdir -p /opt/wirecloud_instance /var/www/static && \
4647
cd /opt && \
4748
wirecloud-admin startproject wirecloud_instance wirecloud_instance && \

dev/docker-compose.yml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,17 @@ services:
5353
- FORWARDED_ALLOW_IPS=*
5454
- ELASTICSEARCH2_URL=http://elasticsearch:9200/
5555
- MEMCACHED_LOCATION=memcached:11211
56-
# Uncomment the following environment variables to enable IDM integration
56+
# Uncomment the following environment variables to enable FIWARE IDM integration
5757
#- FIWARE_IDM_SERVER=${FIWARE_IDM_SERVER}
5858
#- SOCIAL_AUTH_FIWARE_KEY=${SOCIAL_AUTH_FIWARE_KEY}
5959
#- SOCIAL_AUTH_FIWARE_SECRET=${SOCIAL_AUTH_FIWARE_SECRET}
60+
# Uncomment the following environment variables to enable Keycloak IDM Integration
61+
#- KEYCLOAK_IDM_SERVER=${KEYCLOAK_IDM_SERVER}
62+
#- KEYCLOAK_REALM=${KEYCLOAK_REALM}
63+
#- KEYCLOAK_KEY=${KEYCLOAK_KEY}
64+
#- KEYCLOAK_GLOBAL_ROLE=${KEYCLOAK_GLOBAL_ROLE}
65+
#- SOCIAL_AUTH_KEYCLOAK_KEY=${SOCIAL_AUTH_KEYCLOAK_KEY}
66+
#- SOCIAL_AUTH_KEYCLOAK_SECRET=${SOCIAL_AUTH_KEYCLOAK_SECRET}
6067
volumes:
6168
- ./wirecloud-data:/opt/wirecloud_instance/data
6269
- ./wirecloud-static:/var/www/static

dev/settings.py

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -126,16 +126,49 @@
126126
# Python dotted path to the WSGI application used by Django's runserver.
127127
WSGI_APPLICATION = 'wirecloud_instance.wsgi.application'
128128

129-
# FIWARE IdM configuration
130-
FIWARE_IDM_SERVER = os.environ.get('FIWARE_IDM_SERVER', '').strip()
131-
FIWARE_IDM_PUBLIC_URL = os.environ.get('FIWARE_IDM_PUBLIC_URL', FIWARE_IDM_SERVER).strip()
132-
SOCIAL_AUTH_FIWARE_KEY = os.environ.get('SOCIAL_AUTH_FIWARE_KEY', '').strip()
133-
SOCIAL_AUTH_FIWARE_SECRET = os.environ.get('SOCIAL_AUTH_FIWARE_SECRET', '').strip()
134-
IDM_AUTH_ENABLED = FIWARE_IDM_SERVER and SOCIAL_AUTH_FIWARE_KEY and SOCIAL_AUTH_FIWARE_SECRET
135-
136-
if IDM_AUTH_ENABLED:
129+
# Handle some basic settings
130+
131+
## String settings
132+
STRING_SETTINGS = (
133+
"FIWARE_IDM_SERVER",
134+
"FIWARE_IDM_PUBLIC_URL",
135+
"SOCIAL_AUTH_FIWARE_KEY",
136+
"SOCIAL_AUTH_FIWARE_SECRET",
137+
"KEYCLOAK_SERVER",
138+
"KEYCLOAK_REALM",
139+
"KEYCLOAK_KEY",
140+
"SOCIAL_AUTH_KEYCLOAK_KEY",
141+
"SOCIAL_AUTH_KEYCLOAK_SECRET",
142+
)
143+
for setting in STRING_SETTINGS:
144+
value = os.environ.get(setting, "").strip()
145+
if value != "":
146+
locals()[setting] = value
147+
148+
## Boolean settings
149+
BOOLEAN_SETTINGS = (
150+
"KEYCLOAK_GLOBAL_ROLE",
151+
)
152+
for setting in BOOLEAN_SETTINGS:
153+
value = os.environ.get(setting, "").strip()
154+
if value != "":
155+
locals()[setting] = value.lower() == "true"
156+
157+
158+
# FIWARE & Keycloak configuration
159+
IDM_AUTH = 'fiware' if "FIWARE_IDM_SERVER" in locals() and "SOCIAL_AUTH_FIWARE_KEY" in locals() and "SOCIAL_AUTH_FIWARE_SECRET" in locals() else None
160+
IDM_AUTH = 'keycloak' if "KEYCLOAK_IDM_SERVER" in locals() and "KEYCLOAK_REALM" in locals() and "KEYCLOAK_KEY" in locals() and "SOCIAL_AUTH_KEYCLOAK_KEY" in locals() and "SOCIAL_AUTH_KEYCLOAK_SECRET" in locals() else IDM_AUTH
161+
162+
if IDM_AUTH == 'fiware':
163+
INSTALLED_APPS += (
164+
'wirecloud.fiware',
165+
'social_django',
166+
'haystack',
167+
)
168+
elif IDM_AUTH == 'keycloak':
137169
INSTALLED_APPS += (
138170
'wirecloud.fiware',
171+
'wirecloud.keycloak',
139172
'social_django',
140173
'haystack',
141174
)
@@ -192,10 +225,14 @@
192225
USE_X_FORWARDED_PORT = True
193226

194227
# Auth configuration
195-
if IDM_AUTH_ENABLED:
228+
if IDM_AUTH == 'fiware':
196229
AUTHENTICATION_BACKENDS = (
197230
'wirecloud.fiware.social_auth_backend.FIWAREOAuth2',
198231
)
232+
elif IDM_AUTH == 'keycloak':
233+
AUTHENTICATION_BACKENDS = (
234+
'wirecloud.keycloak.social_auth_backend.KeycloakOAuth2',
235+
)
199236
else:
200237
AUTHENTICATION_BACKENDS = (
201238
'django.contrib.auth.backends.ModelBackend',

dev/urls.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,18 @@
88

99
from wirecloud.commons import authentication as wc_auth
1010
from wirecloud.fiware import views as wc_fiware
11+
from wirecloud.keycloak import views as wc_keycloak
1112
import wirecloud.platform.urls
1213

1314
admin.autodiscover()
1415

16+
login_method = django_auth.login
17+
if settings.IDM_AUTH == 'fiware':
18+
login_method = wc_fiware.login
19+
20+
if settings.IDM_AUTH == 'keycloak':
21+
login_method = wc_keycloak.login
22+
1523
urlpatterns = (
1624

1725
# Catalogue
@@ -21,15 +29,15 @@
2129
url(r'^cdp/', include('wirecloud.proxy.urls')),
2230

2331
# Login/logout
24-
url(r'^login/?$', wc_fiware.login if settings.IDM_AUTH_ENABLED else django_auth.login, name="login"),
32+
url(r'^login/?$', login_method, name="login"),
2533
url(r'^logout/?$', wc_auth.logout, name="logout"),
2634
url(r'^admin/logout/?$', wc_auth.logout),
2735

2836
# Admin interface
2937
url(r'^admin/', include(admin.site.urls)),
3038
)
3139

32-
if settings.IDM_AUTH_ENABLED:
40+
if settings.IDM_AUTH:
3341
urlpatterns += (url('', include('social_django.urls', namespace='social')),)
3442

3543
urlpatterns += wirecloud.platform.urls.urlpatterns

0 commit comments

Comments
 (0)