Combinar várias políticas por usuário - Amazon Redshift

Combinar várias políticas por usuário

O RLS no Amazon Redshift é compatível com a anexação de várias políticas por usuário e objeto. Quando há várias políticas definidas para um usuário, o Amazon Redshift aplica todas as políticas com a sintaxe AND ou OR dependendo da definição RLS CONJUNCTION TYPE da tabela. Para obter mais informações sobre o tipo de conjunção, consulte ALTER TABLE.

Várias políticas em uma tabela podem ser associadas a você. Várias políticas estão diretamente anexadas a você ou você pertence a várias funções, e as funções têm políticas diferentes anexadas a elas.

Quando as várias políticas precisam restringir o acesso a linhas em uma determinada relação, você pode definir RLS CONJUNCTION TYPE da relação como AND. Considere o seguinte exemplo. Alice só consegue ver um evento Sports que tenham um "catname" NBA de acordo com a política especificada.

-- Create an analyst role and grant it to a user named Alice. CREATE ROLE analyst; CREATE USER alice WITH PASSWORD 'Name_is_alice_1'; GRANT ROLE analyst TO alice; -- Create an RLS policy that only lets the user see sports. CREATE RLS POLICY policy_sports WITH (catgroup VARCHAR(10)) USING (catgroup = 'Sports'); -- Create an RLS policy that only lets the user see NBA. CREATE RLS POLICY policy_nba WITH (catname VARCHAR(10)) USING (catname = 'NBA'); -- Attach both to the analyst role. ATTACH RLS POLICY policy_sports ON category TO ROLE analyst; ATTACH RLS POLICY policy_nba ON category TO ROLE analyst; -- Activate RLS on the category table with AND CONJUNCTION TYPE. ALTER TABLE category ROW LEVEL SECURITY ON CONJUNCTION TYPE AND; -- Change session to Alice. SET SESSION AUTHORIZATION alice; -- Select all from the category table. SELECT catgroup, catname FROM category; catgroup | catname ---------+--------- Sports | NBA (1 row)

Quando as várias políticas precisam permitir a usuários ver mais linhas em uma determinada relação, o usuário pode definir RLS CONJUNCTION TYPE da relação como OR. Considere o seguinte exemplo. Alice só consegue ver "Shows" e "Esportes" de acordo com a política especificada.

-- Create an analyst role and grant it to a user named Alice. CREATE ROLE analyst; CREATE USER alice WITH PASSWORD 'Name_is_alice_1'; GRANT ROLE analyst TO alice; -- Create an RLS policy that only lets the user see concerts. CREATE RLS POLICY policy_concerts WITH (catgroup VARCHAR(10)) USING (catgroup = 'Concerts'); -- Create an RLS policy that only lets the user see sports. CREATE RLS POLICY policy_sports WITH (catgroup VARCHAR(10)) USING (catgroup = 'Sports'); -- Attach both to the analyst role. ATTACH RLS POLICY policy_concerts ON category TO ROLE analyst; ATTACH RLS POLICY policy_sports ON category TO ROLE analyst; -- Activate RLS on the category table with OR CONJUNCTION TYPE. ALTER TABLE category ROW LEVEL SECURITY ON CONJUNCTION TYPE OR; -- Change session to Alice. SET SESSION AUTHORIZATION alice; -- Select all from the category table. SELECT catgroup, count(*) FROM category GROUP BY catgroup ORDER BY catgroup; catgroup | count ---------+------- Concerts | 3 Sports | 5 (2 rows)