|
| 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 typing as tp |
| 18 | +import uuid as sys_uuid |
| 19 | + |
| 20 | +from gcl_iam.tests.functional import clients as iam_clients |
| 21 | + |
| 22 | + |
| 23 | +class TestAuditApi: |
| 24 | + def test_node_audit_lifecycle( |
| 25 | + self, |
| 26 | + node_factory: tp.Callable, |
| 27 | + user_api_client: iam_clients.GenesisCoreTestRESTClient, |
| 28 | + auth_user_admin: iam_clients.GenesisCoreAuth, |
| 29 | + ): |
| 30 | + client = user_api_client(auth_user_admin) |
| 31 | + node = node_factory() |
| 32 | + node_url = client.build_collection_uri(["compute", "nodes"]) |
| 33 | + |
| 34 | + response = client.post(node_url, json=node) |
| 35 | + assert response.status_code == 201 |
| 36 | + |
| 37 | + resource_url = client.build_resource_uri( |
| 38 | + ["compute", "nodes", node["uuid"]] |
| 39 | + ) |
| 40 | + response = client.put(resource_url, json={"cores": 2, "ram": 2048}) |
| 41 | + assert response.status_code == 200 |
| 42 | + |
| 43 | + response = client.delete(resource_url) |
| 44 | + assert response.status_code == 204 |
| 45 | + |
| 46 | + response = client.get( |
| 47 | + client.build_collection_uri(["security", "audit"]), |
| 48 | + params={"resource_uuid": node["uuid"]}, |
| 49 | + ) |
| 50 | + assert response.status_code == 200 |
| 51 | + |
| 52 | + events = response.json() |
| 53 | + assert len(events) == 3 |
| 54 | + assert {event["action"] for event in events} == { |
| 55 | + "create", |
| 56 | + "update", |
| 57 | + "delete", |
| 58 | + } |
| 59 | + |
| 60 | + events_by_action = {event["action"]: event for event in events} |
| 61 | + for event in events: |
| 62 | + assert event["service_name"] == "compute" |
| 63 | + assert event["resource_type"] == "node" |
| 64 | + assert event["resource_uuid"] == node["uuid"] |
| 65 | + assert event["project_id"] == node["project_id"] |
| 66 | + assert event["actor_user_uuid"] == auth_user_admin.uuid |
| 67 | + |
| 68 | + assert events_by_action["create"]["snapshot"]["cores"] == 1 |
| 69 | + assert events_by_action["update"]["snapshot"]["cores"] == 2 |
| 70 | + assert events_by_action["update"]["snapshot"]["ram"] == 2048 |
| 71 | + assert events_by_action["delete"]["snapshot"] is None |
| 72 | + |
| 73 | + def test_audit_events_are_project_scoped( |
| 74 | + self, |
| 75 | + node_factory: tp.Callable, |
| 76 | + user_api_client: iam_clients.GenesisCoreTestRESTClient, |
| 77 | + auth_user_admin: iam_clients.GenesisCoreAuth, |
| 78 | + auth_test1_p1_user: iam_clients.GenesisCoreAuth, |
| 79 | + auth_test2_p1_user: iam_clients.GenesisCoreAuth, |
| 80 | + ): |
| 81 | + admin_client = user_api_client(auth_user_admin) |
| 82 | + nodes_url = admin_client.build_collection_uri(["compute", "nodes"]) |
| 83 | + node_a = node_factory(project_id=sys_uuid.UUID(auth_test1_p1_user.project_id)) |
| 84 | + node_b = node_factory(project_id=sys_uuid.UUID(auth_test2_p1_user.project_id)) |
| 85 | + |
| 86 | + assert admin_client.post(nodes_url, json=node_a).status_code == 201 |
| 87 | + assert admin_client.post(nodes_url, json=node_b).status_code == 201 |
| 88 | + |
| 89 | + project_client = user_api_client( |
| 90 | + auth_test1_p1_user, |
| 91 | + permissions=["audit.events.read"], |
| 92 | + project_id=auth_test1_p1_user.project_id, |
| 93 | + ) |
| 94 | + response = project_client.get( |
| 95 | + project_client.build_collection_uri(["security", "audit"]), |
| 96 | + ) |
| 97 | + |
| 98 | + assert response.status_code == 200 |
| 99 | + assert {event["resource_uuid"] for event in response.json()} == { |
| 100 | + node_a["uuid"] |
| 101 | + } |
| 102 | + |
| 103 | + for node in (node_a, node_b): |
| 104 | + admin_client.delete( |
| 105 | + admin_client.build_resource_uri( |
| 106 | + ["compute", "nodes", node["uuid"]] |
| 107 | + ) |
| 108 | + ) |
0 commit comments