hs.hsadmin.ng/src/main/resources/db/changelog/113-test-customer-rbac.sql

241 lines
8.1 KiB
MySQL
Raw Normal View History

2022-07-29 11:39:32 +02:00
--liquibase formatted sql
2022-07-29 08:46:04 +02:00
2022-07-29 11:39:32 +02:00
-- ============================================================================
--changeset test-customer-rbac-CREATE-OBJECT:1 endDelimiter:--//
2022-07-29 11:39:32 +02:00
-- ----------------------------------------------------------------------------
2022-07-29 08:46:04 +02:00
2022-07-29 11:39:32 +02:00
/*
Creates the related RbacObject through a BEFORE INSERT TRIGGER.
*/
drop trigger if exists createRbacObjectForCustomer_Trigger on test_customer;
2022-07-29 08:46:04 +02:00
create trigger createRbacObjectForCustomer_Trigger
before insert
on test_customer
2022-07-29 08:46:04 +02:00
for each row
execute procedure createRbacObject();
2022-07-29 11:39:32 +02:00
--//
-- ============================================================================
--changeset test-customer-rbac-ROLE-DESCRIPTORS:1 endDelimiter:--//
2022-07-29 11:39:32 +02:00
-- ----------------------------------------------------------------------------
2022-07-29 08:46:04 +02:00
create or replace function testCustomerOwner(customer test_customer)
2022-07-29 08:46:04 +02:00
returns RbacRoleDescriptor
language plpgsql
strict as $$
begin
return roleDescriptor('test_customer', customer.uuid, 'owner');
2022-07-29 08:46:04 +02:00
end; $$;
create or replace function testCustomerAdmin(customer test_customer)
2022-07-29 08:46:04 +02:00
returns RbacRoleDescriptor
language plpgsql
strict as $$
begin
return roleDescriptor('test_customer', customer.uuid, 'admin');
2022-07-29 08:46:04 +02:00
end; $$;
create or replace function testCustomerTenant(customer test_customer)
2022-07-29 08:46:04 +02:00
returns RbacRoleDescriptor
language plpgsql
strict as $$
begin
return roleDescriptor('test_customer', customer.uuid, 'tenant');
2022-07-29 08:46:04 +02:00
end; $$;
2022-07-29 11:39:32 +02:00
--//
-- ============================================================================
--changeset test-customer-rbac-ROLES-CREATION:1 endDelimiter:--//
2022-07-29 11:39:32 +02:00
-- ----------------------------------------------------------------------------
2022-07-29 08:46:04 +02:00
2022-07-29 11:39:32 +02:00
/*
Creates the roles and their assignments for a new customer for the AFTER INSERT TRIGGER.
*/
2022-07-29 08:46:04 +02:00
create or replace function createRbacRolesForTestCustomer()
2022-07-29 08:46:04 +02:00
returns trigger
language plpgsql
strict as $$
declare
testCustomerOwnerUuid uuid;
2022-07-29 08:46:04 +02:00
customerAdminUuid uuid;
begin
if TG_OP <> 'INSERT' then
raise exception 'invalid usage of TRIGGER AFTER INSERT';
end if;
-- the owner role with full access for Hostsharing administrators
testCustomerOwnerUuid = createRole(
testCustomerOwner(NEW),
2022-07-29 08:46:04 +02:00
grantingPermissions(forObjectUuid => NEW.uuid, permitOps => array ['*']),
beneathRole(testGlobalAdmin())
2022-07-29 08:46:04 +02:00
);
-- the admin role for the customer's admins, who can view and add products
customerAdminUuid = createRole(
testCustomerAdmin(NEW),
2022-07-29 08:46:04 +02:00
grantingPermissions(forObjectUuid => NEW.uuid, permitOps => array ['view', 'add-package']),
2022-08-13 16:47:36 +02:00
-- NO auto assume for customer owner to avoid exploding permissions for administrators
withUser(NEW.adminUserName, 'create'), -- implicitly ignored if null
grantedByRole(testGlobalAdmin())
2022-07-29 08:46:04 +02:00
);
-- allow the customer owner role (thus administrators) to assume the customer admin role
call grantRoleToRole(customerAdminUuid, testCustomerOwnerUuid, false);
2022-07-29 08:46:04 +02:00
-- the tenant role which later can be used by owners+admins of sub-objects
perform createRole(
testCustomerTenant(NEW),
2022-07-29 08:46:04 +02:00
grantingPermissions(forObjectUuid => NEW.uuid, permitOps => array ['view'])
);
return NEW;
end; $$;
2022-07-29 11:39:32 +02:00
/*
An AFTER INSERT TRIGGER which creates the role structure for a new customer.
*/
drop trigger if exists createRbacRolesForTestCustomer_Trigger on test_customer;
create trigger createRbacRolesForTestCustomer_Trigger
2022-07-29 08:46:04 +02:00
after insert
on test_customer
2022-07-29 08:46:04 +02:00
for each row
execute procedure createRbacRolesForTestCustomer();
2022-07-29 11:39:32 +02:00
--//
2022-07-29 12:14:22 +02:00
2022-07-29 11:39:32 +02:00
-- ============================================================================
--changeset test-customer-rbac-ROLES-REMOVAL:1 endDelimiter:--//
2022-07-29 11:39:32 +02:00
-- ----------------------------------------------------------------------------
/*
Deletes the roles and their assignments of a deleted customer for the BEFORE DELETE TRIGGER.
*/
2022-07-29 08:46:04 +02:00
create or replace function deleteRbacRulesForTestCustomer()
2022-07-29 08:46:04 +02:00
returns trigger
language plpgsql
strict as $$
begin
if TG_OP = 'DELETE' then
call deleteRole(findRoleId(testCustomerOwner(OLD)));
call deleteRole(findRoleId(testCustomerAdmin(OLD)));
call deleteRole(findRoleId(testCustomerTenant(OLD)));
2022-07-29 08:46:04 +02:00
else
raise exception 'invalid usage of TRIGGER BEFORE DELETE';
end if;
end; $$;
2022-07-29 11:39:32 +02:00
/*
An BEFORE DELETE TRIGGER which deletes the role structure of a customer.
*/
drop trigger if exists deleteRbacRulesForTestCustomer_Trigger on test_customer;
create trigger deleteRbacRulesForTestCustomer_Trigger
2022-07-29 08:46:04 +02:00
before delete
on test_customer
2022-07-29 08:46:04 +02:00
for each row
execute procedure deleteRbacRulesForTestCustomer();
2022-07-29 11:39:32 +02:00
--//
2022-07-29 08:46:04 +02:00
2022-07-29 11:39:32 +02:00
-- ============================================================================
--changeset test-customer-rbac-IDENTITY-VIEW:1 endDelimiter:--//
2022-07-29 11:39:32 +02:00
-- ----------------------------------------------------------------------------
/*
Creates a view to the customer main table which maps the identifying name
(in this case, the prefix) to the objectUuid.
*/
drop view if exists test_customer_iv;
create or replace view test_customer_iv as
select target.uuid, target.prefix as idName
from test_customer as target;
2022-07-29 08:46:04 +02:00
-- TODO: Is it ok that everybody has access to this information?
grant all privileges on test_customer_iv to restricted;
2022-07-29 08:46:04 +02:00
2022-07-29 11:39:32 +02:00
/*
Returns the objectUuid for a given identifying name (in this case the prefix).
*/
create or replace function test_customerUuidByIdName(idName varchar)
2022-07-29 08:46:04 +02:00
returns uuid
language sql
strict as $$
select uuid from test_customer_iv iv where iv.idName = test_customerUuidByIdName.idName;
2022-07-29 08:46:04 +02:00
$$;
2022-08-03 06:12:16 +02:00
/*
Returns the identifying name for a given objectUuid (in this case the prefix).
*/
create or replace function test_customerIdNameByUuid(uuid uuid)
2022-08-03 06:12:16 +02:00
returns varchar
language sql
strict as $$
select idName from test_customer_iv iv where iv.uuid = test_customerIdNameByUuid.uuid;
2022-08-03 06:12:16 +02:00
$$;
2022-07-29 11:39:32 +02:00
--//
2022-07-29 08:46:04 +02:00
2022-07-29 11:39:32 +02:00
-- ============================================================================
--changeset test-customer-rbac-RESTRICTED-VIEW:1 endDelimiter:--//
2022-07-29 11:39:32 +02:00
-- ----------------------------------------------------------------------------
/*
2022-08-03 06:12:16 +02:00
Creates a view to the customer main table with row-level limitation
2022-07-29 11:39:32 +02:00
based on the 'view' permission of the current user or assumed roles.
*/
2022-07-29 08:46:04 +02:00
set session session authorization default;
drop view if exists test_customer_rv;
create or replace view test_customer_rv as
select target.*
from test_customer as target
where target.uuid in (select queryAccessibleObjectUuidsOfSubjectIds('view', 'test_customer', currentSubjectsUuids()));
grant all privileges on test_customer_rv to restricted;
2022-07-29 11:39:32 +02:00
--//
-- ============================================================================
--changeset test-customer-rbac-ADD-CUSTOMER:1 endDelimiter:--//
-- ----------------------------------------------------------------------------
/*
Creates a global permission for add-customer and assigns it to the hostsharing admins role.
*/
do language plpgsql $$
declare
addCustomerPermissions uuid[];
globalObjectUuid uuid;
globalAdminRoleUuid uuid ;
begin
call defineContext('granting global add-customer permission to global admin role', null, null, null);
globalAdminRoleUuid := findRoleId(testGlobalAdmin());
globalObjectUuid := (select uuid from global);
addCustomerPermissions := createPermissions(globalObjectUuid, array ['add-customer']);
call grantPermissionsToRole(globalAdminRoleUuid, addCustomerPermissions);
end;
$$;
/**
Used by the trigger to prevent the add-customer to current user respectively assumed roles.
*/
create or replace function addTestCustomerNotAllowedForCurrentSubjects()
returns trigger
language PLPGSQL
as $$
begin
raise exception '[403] add-customer not permitted for %',
array_to_string(currentSubjects(), ';', 'null');
end; $$;
/**
Checks if the user or assumed roles are allowed to add a new customer.
*/
create trigger test_customer_insert_trigger
before insert
on test_customer
for each row
when ( currentUser() <> 'mike@example.org' or not hasGlobalPermission('add-customer') )
execute procedure addTestCustomerNotAllowedForCurrentSubjects();
--//