Skip to content

Commit 50da576

Browse files
committed
Dns: introduce basic permissions
1 parent 6a5107d commit 50da576

3 files changed

Lines changed: 144 additions & 4 deletions

File tree

genesis_core/tests/functional/restapi/iam/test_projects.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def test_list_projects_admin_user(self, user_api_client, auth_user_admin):
9191

9292
projects = client.list_projects()
9393

94-
assert len(projects) == 3 # 3 project from migrations
94+
assert len(projects) == 4 # 4 projects from migrations
9595

9696
def test_create_project_wo_project_from_admin(
9797
self,

genesis_core/user_api/dns/api/controllers.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class DomainController(
4242
ra_controllers.BaseResourceControllerPaginated,
4343
):
4444
__policy_service_name__ = "dns"
45-
__policy_name__ = "domains"
45+
__policy_name__ = "domain"
4646

4747
__resource__ = resources.ResourceByRAModel(
4848
models.Domain,
@@ -62,7 +62,7 @@ class RecordController(
6262
):
6363
__pr_name__ = "domain"
6464
__policy_service_name__ = "dns"
65-
__policy_name__ = "records"
65+
__policy_name__ = "record"
6666

6767
__resource__ = resources.ResourceByRAModel(
6868
models.Record,
@@ -71,7 +71,7 @@ class RecordController(
7171
default=field_p.Permissions.RW,
7272
fields={
7373
"domain_id": {constants.ALL: field_p.Permissions.HIDDEN},
74-
"name": {constants.ALL: field_p.Permissions.HIDDEN},
74+
"name": {constants.ALL: field_p.Permissions.RO},
7575
"content": {constants.ALL: field_p.Permissions.HIDDEN},
7676
},
7777
),
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
# Copyright 2025 Genesis Corporation.
2+
#
3+
# All Rights Reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
6+
# not use this file except in compliance with the License. You may obtain
7+
# a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14+
# License for the specific language governing permissions and limitations
15+
# under the License.
16+
17+
import uuid as sys_uuid
18+
from restalchemy.storage.sql import migrations
19+
20+
21+
NS_UUID = sys_uuid.UUID("dfd0c604-607f-4260-981f-374f88435ea0")
22+
GENESIS_CORE_ORG_UUID = "11111111-1111-1111-1111-111111111111"
23+
OWNER_ROLE_UUID = "726f6c65-0000-0000-0000-000000000002"
24+
25+
DNS_NODE_DEF_PERMISSIONS = (
26+
("dns.domain.read", "List and read own domains"),
27+
("dns.domain.create", "Create own domains"),
28+
("dns.domain.update", "Update own domains"),
29+
("dns.domain.delete", "Delete own domains"),
30+
("dns.record.read", "List and read own records"),
31+
("dns.record.create", "Create own records"),
32+
("dns.record.update", "Update own records"),
33+
("dns.record.delete", "Delete own records"),
34+
)
35+
36+
37+
def _u(name: str) -> str:
38+
return str(sys_uuid.uuid5(NS_UUID, name))
39+
40+
41+
DNS_PROJECT_UUID = _u("GenesisCore-Dns-Project")
42+
43+
44+
class MigrationStep(migrations.AbstarctMigrationStep):
45+
46+
def __init__(self):
47+
self._depends = ["0020-init-secret-a643b1.py"]
48+
49+
@property
50+
def migration_id(self):
51+
return "7adac7d0-3d20-4c09-a6f5-f90f4442a5d4"
52+
53+
@property
54+
def is_manual(self):
55+
return False
56+
57+
def _create_permissions(self, session):
58+
for name, description in DNS_NODE_DEF_PERMISSIONS:
59+
session.execute(
60+
f"""
61+
INSERT INTO iam_permissions (
62+
uuid, name, description
63+
) VALUES (
64+
'{_u(name)}',
65+
'{name}',
66+
'{description}'
67+
)
68+
ON CONFLICT (uuid) DO NOTHING;
69+
"""
70+
)
71+
72+
def _create_project(self, session):
73+
session.execute(
74+
f"""
75+
INSERT INTO iam_projects (
76+
uuid, name, description, organization
77+
) VALUES (
78+
'{DNS_PROJECT_UUID}',
79+
'dns-core',
80+
'Dns Core Project',
81+
'{GENESIS_CORE_ORG_UUID}'
82+
)
83+
ON CONFLICT (uuid) DO NOTHING;
84+
"""
85+
)
86+
87+
def _create_bindings(self, session):
88+
for name, _ in DNS_NODE_DEF_PERMISSIONS:
89+
session.execute(
90+
f"""
91+
INSERT INTO iam_binding_permissions (
92+
uuid, role, permission, project_id
93+
) VALUES (
94+
gen_random_uuid(),
95+
'{OWNER_ROLE_UUID}',
96+
'{_u(name)}',
97+
'{DNS_PROJECT_UUID}'
98+
);
99+
"""
100+
)
101+
102+
def upgrade(self, session):
103+
self._create_permissions(session)
104+
self._create_project(session)
105+
self._create_bindings(session)
106+
107+
def _delete_bindings(self, session):
108+
for name, _ in DNS_NODE_DEF_PERMISSIONS:
109+
session.execute(
110+
f"""
111+
DELETE FROM iam_binding_permissions
112+
WHERE
113+
permission = '{_u(name)}';
114+
"""
115+
)
116+
117+
def _delete_project(self, session):
118+
session.execute(
119+
f"""
120+
DELETE FROM iam_projects
121+
WHERE uuid = '{DNS_PROJECT_UUID}';
122+
"""
123+
)
124+
125+
def _delete_permissions(self, session):
126+
for name, _ in DNS_NODE_DEF_PERMISSIONS:
127+
session.execute(
128+
f"""
129+
DELETE FROM iam_permissions
130+
WHERE uuid = '{_u(name)}';
131+
"""
132+
)
133+
134+
def downgrade(self, session):
135+
self._delete_bindings(session)
136+
self._delete_project(session)
137+
self._delete_permissions(session)
138+
139+
140+
migration_step = MigrationStep()

0 commit comments

Comments
 (0)