Skip to content

Commit e674625

Browse files
danielmursa-devSonnyBA
authored andcommitted
[#485] tokenauth setup configuration (#494)
* [#485] Merge closed branch * [#485] Add SitesConfigurationStep and TokenAuthConfigurationStep in settings * [#485] Black and isort * [#485] Create Step and Model * [#485] Fix requirements * [#485] Fix yaml file * [#485] Update namespace * [#485] Fix tests * [#485] Update generate tokens * [#485] Fix docs * [#485] Add missing field in docs * [#485] Add validate_no_empty for identifier * [#485] Open-api-framework new realease * [#485] Run bin/generate_envvar_docs.sh * [#485] Update fixtures demodata * [#485] Add sites_config_enable in data.yaml
1 parent 8d3d5b6 commit e674625

23 files changed

+826
-10
lines changed

docker/setup_configuration/data.yaml

+23
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
sites_config_enable: true
2+
sites_config:
3+
items:
4+
- domain: example.com
5+
name: Example site
6+
7+
18
zgw_consumers_config_enable: true
29
zgw_consumers:
310
services:
@@ -19,20 +26,36 @@ zgw_consumers:
1926
header_key: Authorization
2027
header_value: Token ba9d233e95e04c4a8a661a27daffe7c9bd019067
2128

29+
2230
notifications_config_enable: true
2331
notifications_config:
2432
notifications_api_service_identifier: notifications-api
2533
notification_delivery_max_retries: 1
2634
notification_delivery_retry_backoff: 2
2735
notification_delivery_retry_backoff_max: 3
2836

37+
2938
objecttypes_config_enable: true
3039
objecttypes:
3140
items:
3241
- uuid: b427ef84-189d-43aa-9efd-7bb2c459e281
3342
name: Object Type 1
3443
service_identifier: objecttypes-api
3544

45+
46+
tokenauth_config_enable: true
47+
tokenauth:
48+
items:
49+
- identifier: token-1
50+
token: 18b2b74ef994314b84021d47b9422e82b685d82f
51+
contact_person: Person 1
52+
email: person-1@example.com
53+
organization: Organization 1
54+
application: Application 1
55+
administration: Administration 1
56+
is_superuser: true
57+
58+
3659
oidc_db_config_enable: true
3760
oidc_db_config_admin_auth:
3861
items:

docs/installation/config_cli.rst

+22
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,28 @@ created. An example of a configuration could be seen below:
106106
107107
Tokens configuration
108108
--------------------
109+
Create or update the (single) YAML configuration file with your settings:
110+
111+
.. code-block:: yaml
112+
113+
...
114+
tokenauth_config_enable: true
115+
tokenauth:
116+
items:
117+
- identifier: token-1
118+
token: ba9d233e95e04c4a8a661a27daffe7c9bd019067
119+
contact_person: Person 1
120+
email: person-1@example.com
121+
organization: Organization XYZ # optional
122+
application: Application XYZ # optional
123+
administration: Administration XYZ # optional
124+
is_superuser: true # optional
125+
126+
- identifier: token-2
127+
token: 7b2b212d9f16d171a70a1d927cdcfbd5ca7a4799
128+
contact_person: Person 2
129+
email: person-2@example.com
130+
...
109131
110132
Mozilla-django-oidc-db
111133
----------------------

requirements/base.in

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ jsonschema
66
furl
77

88
# Common ground libraries
9+
django-setup-configuration>=0.5.0
910
notifications-api-common[setup-configuration]
1011
zgw-consumers[setup-configuration]
1112
mozilla-django-oidc-db[setup-configuration]

requirements/base.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,9 @@ django-sendfile2==0.7.0
153153
# via django-privates
154154
django-sessionprofile==3.0.0
155155
# via open-api-framework
156-
django-setup-configuration==0.4.0
156+
django-setup-configuration==0.5.0
157157
# via
158+
# -r requirements/base.in
158159
# mozilla-django-oidc-db
159160
# notifications-api-common
160161
# open-api-framework

requirements/ci.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ django-sessionprofile==3.0.0
239239
# via
240240
# -r requirements/base.txt
241241
# open-api-framework
242-
django-setup-configuration==0.4.0
242+
django-setup-configuration==0.5.0
243243
# via
244244
# -r requirements/base.txt
245245
# mozilla-django-oidc-db

requirements/dev.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ django-sessionprofile==3.0.0
254254
# via
255255
# -r requirements/base.txt
256256
# open-api-framework
257-
django-setup-configuration==0.4.0
257+
django-setup-configuration==0.5.0
258258
# via
259259
# -r requirements/base.txt
260260
# mozilla-django-oidc-db

src/objects/conf/base.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,9 @@
8484
# Django setup configuration
8585
#
8686
SETUP_CONFIGURATION_STEPS = (
87+
"django_setup_configuration.contrib.sites.steps.SitesConfigurationStep",
8788
"zgw_consumers.contrib.setup_configuration.steps.ServiceConfigurationStep",
8889
"notifications_api_common.contrib.setup_configuration.steps.NotificationConfigurationStep",
89-
"objects.setup_configuration.steps.objecttypes.ObjectTypesConfigurationStep",
9090
"mozilla_django_oidc_db.setup_configuration.steps.AdminOIDCConfigurationStep",
91+
"objects.setup_configuration.steps.token_auth.TokenAuthConfigurationStep",
9192
)

src/objects/fixtures/demodata.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -779,7 +779,8 @@
779779
"organization": "",
780780
"last_modified": "2020-12-23T11:43:16.820Z",
781781
"created": "2020-12-22T16:27:00.751Z",
782-
"token": "cd63e158f3aca276ef284e3033d020a22899c728"
782+
"token": "cd63e158f3aca276ef284e3033d020a22899c728",
783+
"identifier": "token-1"
783784
}
784785
},
785786
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from django_setup_configuration.models import ConfigurationModel
2+
3+
from objects.token.models import TokenAuth
4+
5+
6+
class TokenAuthConfigurationModel(ConfigurationModel):
7+
class Meta:
8+
django_model_refs = {
9+
TokenAuth: (
10+
"identifier",
11+
"token",
12+
"contact_person",
13+
"email",
14+
"organization",
15+
"application",
16+
"administration",
17+
"is_superuser",
18+
)
19+
}
20+
21+
22+
class TokenAuthGroupConfigurationModel(ConfigurationModel):
23+
items: list[TokenAuthConfigurationModel]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import logging
2+
3+
from django.core.exceptions import ValidationError
4+
from django.db import IntegrityError
5+
6+
from django_setup_configuration.configuration import BaseConfigurationStep
7+
from django_setup_configuration.exceptions import ConfigurationRunFailed
8+
9+
from objects.setup_configuration.models.token_auth import (
10+
TokenAuthGroupConfigurationModel,
11+
)
12+
from objects.token.models import TokenAuth
13+
14+
logger = logging.getLogger(__name__)
15+
16+
17+
class TokenAuthConfigurationStep(
18+
BaseConfigurationStep[TokenAuthGroupConfigurationModel]
19+
):
20+
"""
21+
Configure tokens for other applications to access Objects API
22+
"""
23+
24+
namespace = "tokenauth"
25+
enable_setting = "tokenauth_config_enable"
26+
27+
verbose_name = "Configuration to set up authentication tokens for objects"
28+
config_model = TokenAuthGroupConfigurationModel
29+
30+
def execute(self, model: TokenAuthGroupConfigurationModel) -> None:
31+
if len(model.items) == 0:
32+
logger.warning("No tokens provided for configuration")
33+
34+
for item in model.items:
35+
logger.info(f"Configuring {item.identifier}")
36+
37+
model_kwargs = {
38+
"identifier": item.identifier,
39+
"token": item.token,
40+
"contact_person": item.contact_person,
41+
"email": item.email,
42+
"organization": item.organization,
43+
"application": item.application,
44+
"administration": item.administration,
45+
"is_superuser": item.is_superuser,
46+
}
47+
48+
token_instance = TokenAuth(**model_kwargs)
49+
50+
try:
51+
token_instance.full_clean(exclude=("id",), validate_unique=False)
52+
except ValidationError as exception:
53+
exception_message = (
54+
f"Validation error(s) occured for {item.identifier}."
55+
)
56+
raise ConfigurationRunFailed(exception_message) from exception
57+
58+
logger.debug(f"No validation errors found for {item.identifier}")
59+
60+
try:
61+
logger.debug(f"Saving {item.identifier}")
62+
63+
TokenAuth.objects.update_or_create(
64+
identifier=item.identifier,
65+
defaults={
66+
key: value
67+
for key, value in model_kwargs.items()
68+
if key != "identifier"
69+
},
70+
)
71+
except IntegrityError as exception:
72+
exception_message = f"Failed configuring token {item.identifier}."
73+
raise ConfigurationRunFailed(exception_message) from exception
74+
75+
logger.info(f"Configured {item.identifier}")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
tokenauth_config_enable: true
2+
tokenauth:
3+
items:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
tokenauth_config_enable: true
2+
tokenauth:
3+
items:
4+
- identifier: token-1
5+
token: 18b2b74ef994314b84021d47b9422e82b685d82f
6+
contact_person: Person 1
7+
email: person-1@example.com
8+
organization: Organization 1
9+
application: Application 1
10+
administration: Administration 1
11+
is_superuser: True
12+
13+
- identifier: token-2
14+
token: e882642bd0ec2482adcdc97258c2e6f98cb06d85
15+
contact_person: Person 2
16+
email: person-2@example.com
17+
organization: Organization 2
18+
application: Application 2
19+
administration: Administration 2
20+
is_superuser: True
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
tokenauth_config_enable: true
2+
tokenauth:
3+
items:
4+
- identifier: token-1
5+
token: 18b2b74ef994314b84021d47b9422e82b685d82f
6+
contact_person: Person 1
7+
email: person-1@example.com
8+
9+
- identifier: token-2
10+
token: e882642bd0ec2482adcdc97258c2e6f98cb06d85
11+
contact_person: Person 2
12+
email: person-2@example.com

0 commit comments

Comments
 (0)