|
| 1 | +# Copyright 2026 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 | + |
| 19 | +from restalchemy.storage.sql import migrations |
| 20 | + |
| 21 | +# Same namespace/role/project as 0053 (network.lb permissions): border is |
| 22 | +# the second network resource driven by project owners (and, via manifest |
| 23 | +# bindings, by the ecosystem's service user for managed realms). |
| 24 | +NS_UUID = sys_uuid.UUID("dfd0c604-607f-4260-981f-374f88435ea0") |
| 25 | +OWNER_ROLE_UUID = "726f6c65-0000-0000-0000-000000000002" |
| 26 | + |
| 27 | +BORDER_PERMISSIONS = ( |
| 28 | + ("network.border.read", "List and read borders (NAT gateways)"), |
| 29 | + ("network.border.create", "Create borders (NAT gateways)"), |
| 30 | + ("network.border.update", "Update borders (NAT gateways)"), |
| 31 | + ("network.border.delete", "Delete borders (NAT gateways)"), |
| 32 | +) |
| 33 | + |
| 34 | + |
| 35 | +def _u(name: str) -> str: |
| 36 | + return str(sys_uuid.uuid5(NS_UUID, name)) |
| 37 | + |
| 38 | + |
| 39 | +COMPUTE_PROJECT_UUID = _u("GenesisCore-Compute-Project") |
| 40 | + |
| 41 | + |
| 42 | +class MigrationStep(migrations.AbstarctMigrationStep): |
| 43 | + def __init__(self): |
| 44 | + self._depends = ["0066-border-inline-rules-c9a3d1.py"] |
| 45 | + |
| 46 | + @property |
| 47 | + def migration_id(self): |
| 48 | + return "d4a7b2c8-11f3-4b6e-9c0d-8f2f5f8a1e42" |
| 49 | + |
| 50 | + @property |
| 51 | + def is_manual(self): |
| 52 | + return False |
| 53 | + |
| 54 | + def upgrade(self, session): |
| 55 | + for name, description in BORDER_PERMISSIONS: |
| 56 | + session.execute(f""" |
| 57 | + INSERT INTO iam_permissions ( |
| 58 | + uuid, name, description |
| 59 | + ) VALUES ( |
| 60 | + '{_u(name)}', |
| 61 | + '{name}', |
| 62 | + '{description}' |
| 63 | + ) |
| 64 | + ON CONFLICT (uuid) DO NOTHING; |
| 65 | + """) |
| 66 | + session.execute(f""" |
| 67 | + INSERT INTO iam_binding_permissions ( |
| 68 | + uuid, role, permission, project_id |
| 69 | + ) VALUES ( |
| 70 | + '{_u("binding." + name)}', |
| 71 | + '{OWNER_ROLE_UUID}', |
| 72 | + '{_u(name)}', |
| 73 | + '{COMPUTE_PROJECT_UUID}' |
| 74 | + ) |
| 75 | + ON CONFLICT (uuid) DO NOTHING; |
| 76 | + """) |
| 77 | + |
| 78 | + def downgrade(self, session): |
| 79 | + for name, _ in BORDER_PERMISSIONS: |
| 80 | + session.execute(f""" |
| 81 | + DELETE FROM iam_binding_permissions |
| 82 | + WHERE permission = '{_u(name)}'; |
| 83 | + """) |
| 84 | + session.execute(f""" |
| 85 | + DELETE FROM iam_permissions |
| 86 | + WHERE uuid = '{_u(name)}'; |
| 87 | + """) |
| 88 | + |
| 89 | + |
| 90 | +migration_step = MigrationStep() |
0 commit comments