Skip to content

Commit 8c9efc8

Browse files
committed
[change] Hide sensitive fields for shared objects in the API response
1 parent c75c6b5 commit 8c9efc8

File tree

9 files changed

+72
-29
lines changed

9 files changed

+72
-29
lines changed

openwisp_controller/connection/admin.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ class CredentialsAdmin(MultitenantAdminMixin, TimeReadonlyAdminMixin, admin.Mode
5757
"created",
5858
"modified",
5959
]
60-
sensitive_fields = ["params"]
6160

6261
def get_urls(self):
6362
options = getattr(self.model, "_meta")

openwisp_controller/connection/base/models.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ class AbstractCredentials(ConnectorMixin, ShareableOrgMixinUniqueName, BaseModel
9090
# Controls the number of objects which can be stored in memory
9191
# before commiting them to database during bulk auto add operation.
9292
chunk_size = 1000
93+
sensitive_fields = ["params"]
9394

9495
connector = models.CharField(
9596
_("connection type"),

openwisp_controller/connection/tests/test_admin.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,8 @@ def test_credential_admin_sensitive_fields(self):
166166
self._test_sensitive_fields_visibility_on_shared_and_org_objects(
167167
sensitive_fields=["params"],
168168
shared_obj_path=reverse(
169-
f"admin:{self.app_label}_credentials_change", args=(shared_credentials.id,)
169+
f"admin:{self.app_label}_credentials_change",
170+
args=(shared_credentials.id,),
170171
),
171172
org_obj_path=reverse(
172173
f"admin:{self.app_label}_credentials_change", args=(org_credentials.id,)

openwisp_controller/connection/tests/test_api.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,22 @@ def test_operator_access_shared_credentials(self):
508508
expected_count=1,
509509
)
510510

511+
def test_credentials_sensitive_fields_visibility(self):
512+
"""
513+
Test that sensitive fields are hidden for shared objects for non-superusers.
514+
"""
515+
org = self._get_org()
516+
shared_credentials = self._create_credentials(organization=None)
517+
org_credentials = self._create_credentials(organization=org)
518+
self._test_sensitive_fields_visibility_on_shared_and_org_objects(
519+
sensitive_fields=["params"],
520+
shared_obj=shared_credentials,
521+
org_obj=org_credentials,
522+
listview_name="connection_api:credential_list",
523+
detailview_name="connection_api:credential_detail",
524+
organization=org,
525+
)
526+
511527
def test_get_deviceconnection_list(self):
512528
d1 = self._create_device()
513529
path = reverse("connection_api:deviceconnection_list", args=(d1.pk,))
@@ -523,7 +539,6 @@ def test_get_deviceconnection_list(self):
523539
for cred in creds:
524540
DeviceConnection.objects.create(device=d1, credentials=cred)
525541
response = self.client.get(path)
526-
print(response.data)
527542
self.assertEqual(response.status_code, 200)
528543
created_list = [conn["created"] for conn in response.data["results"]]
529544
sorted_created = sorted(created_list, reverse=True)

openwisp_controller/pki/admin.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@
1414
@admin.register(Ca)
1515
class CaAdmin(MultitenantAdminMixin, AbstractCaAdmin, VersionAdmin):
1616
history_latest_first = True
17-
sensitive_fields = [
18-
"private_key",
19-
]
2017

2118

2219
CaAdmin.fields.insert(2, "organization")
@@ -28,9 +25,6 @@ class CaAdmin(MultitenantAdminMixin, AbstractCaAdmin, VersionAdmin):
2825
@admin.register(Cert)
2926
class CertAdmin(MultitenantAdminMixin, AbstractCertAdmin, VersionAdmin):
3027
multitenant_shared_relations = ("ca",)
31-
sensitive_fields = [
32-
"private_key",
33-
]
3428
history_latest_first = True
3529

3630

openwisp_controller/pki/base/models.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111

1212
class AbstractCa(ShareableOrgMixin, UnqiueCommonNameMixin, BaseCa):
13+
sensitive_fields = ["private_key"]
14+
1315
class Meta(BaseCa.Meta):
1416
abstract = True
1517
constraints = [
@@ -21,6 +23,8 @@ class Meta(BaseCa.Meta):
2123

2224

2325
class AbstractCert(ShareableOrgMixin, UnqiueCommonNameMixin, BaseCert):
26+
sensitive_fields = ["private_key"]
27+
2428
ca = models.ForeignKey(
2529
get_model_name("django_x509", "Ca"),
2630
verbose_name=_("CA"),
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from django.db import migrations
2+
3+
from . import assign_permissions_to_groups
4+
5+
6+
class Migration(migrations.Migration):
7+
dependencies = [
8+
("pki", "0011_disallowed_blank_key_length_or_digest"),
9+
]
10+
operations = [
11+
migrations.RunPython(
12+
assign_permissions_to_groups, reverse_code=migrations.RunPython.noop
13+
)
14+
]

openwisp_controller/pki/migrations/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
def assign_permissions_to_groups(apps, schema_editor):
77
create_default_permissions(apps, schema_editor)
88
operators_read_only_admins_manage = ["ca", "cert"]
9-
manage_operations = ["add", "change", "delete"]
9+
manage_operations = ["add", "change", "view", "delete"]
1010
Group = get_swapped_model(apps, "openwisp_users", "Group")
1111

1212
try:

openwisp_controller/pki/tests/test_api.py

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -175,15 +175,22 @@ def test_org_admin_access_shared_ca(self):
175175
create_payload=create_payload,
176176
update_payload=update_payload,
177177
expected_count=1,
178-
expected_status_codes={
179-
"create": 400,
180-
"list": 200,
181-
"retrieve": 403,
182-
"update": 403,
183-
"delete": 403,
184-
"head": 403,
185-
"option": 200,
186-
},
178+
)
179+
180+
def test_ca_sensitive_fields_visibility(self):
181+
"""
182+
Test that sensitive fields are hidden for shared objects for non-superusers.
183+
"""
184+
org = self._get_org()
185+
shared_ca = self._create_ca(organization=None)
186+
org_ca = self._create_ca(organization=org)
187+
self._test_sensitive_fields_visibility_on_shared_and_org_objects(
188+
sensitive_fields=["private_key"],
189+
shared_obj=shared_ca,
190+
org_obj=org_ca,
191+
listview_name="pki_api:ca_list",
192+
detailview_name="pki_api:ca_detail",
193+
organization=org,
187194
)
188195

189196
def test_cert_post_api(self):
@@ -341,15 +348,6 @@ def test_org_admin_access_shared_cert(self):
341348
create_payload=create_payload,
342349
update_payload=update_payload,
343350
expected_count=1,
344-
expected_status_codes={
345-
"create": 400,
346-
"list": 200,
347-
"retrieve": 403,
348-
"update": 403,
349-
"delete": 403,
350-
"head": 403,
351-
"option": 200,
352-
},
353351
)
354352

355353
def test_org_admin_access_cert_with_shared_ca(self):
@@ -379,11 +377,28 @@ def test_org_admin_access_cert_with_shared_ca(self):
379377
"retrieve": 200,
380378
"update": 200,
381379
"delete": 204,
382-
"head": 403,
380+
"head": 200,
383381
"option": 200,
384382
},
385383
)
386384

385+
def test_cert_sensitive_fields_visibility(self):
386+
"""
387+
Test that sensitive fields are hidden for shared objects for non-superusers.
388+
"""
389+
org = self._get_org()
390+
shared_ca = self._create_ca(organization=None)
391+
shared_cert = self._create_cert(ca=shared_ca, organization=None)
392+
org_cert = self._create_cert(ca=shared_ca, organization=org)
393+
self._test_sensitive_fields_visibility_on_shared_and_org_objects(
394+
sensitive_fields=["private_key"],
395+
shared_obj=shared_cert,
396+
org_obj=org_cert,
397+
listview_name="pki_api:cert_list",
398+
detailview_name="pki_api:cert_detail",
399+
organization=org,
400+
)
401+
387402
@capture_any_output()
388403
def test_bearer_authentication(self):
389404
self.client.logout()

0 commit comments

Comments
 (0)