|
| 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