-
Notifications
You must be signed in to change notification settings - Fork 360
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add "attach storage namespace [to repository]" IAM action type (#2430)
* Document proposed solution: add AttachStorageNamespace action * Test for AttachStorageNamespace permission in CreateRepository * Correctly AND fs:CreateRepository and fs:AttachStorageNamespace Multiple permissions passed to `Authorize` are disjunctive ("OR"). To get a conjection ("AND") need to call `Authorize` twice. * Add migrate {up,down} scripts for AttachStorageNamespace Allow `fs:AttachStorageNamespace` for _all_ storage namespaces on all rules that allow a user to perform some `fs:CreateRepository` action (no matter how restricted). This preserves current semantics. Skip doing this if the rule already allows some `fs:AttachStorageNamespace` action (no matter how restricted) -- so UP -> DOWN -> UP changes nothing. Do nothing. Keep existing `fs:AttachStorageNamespace` rules. These will do nothing until re-upgrading. On re-upgrade, these rules are unchanged. * Add changelog entry * Make upgrade script work with PostgreSQL v11 This is our minimal supported version of PostgreSQL, so don't use (useful!) `jsonb_*` functions that were added in v12. (Downgrade the SQL in the SQL upgrade script, essentially...).
- Loading branch information
1 parent
c00d972
commit 78078b7
Showing
7 changed files
with
81 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
-- Keep all "fs:AttachStorageNamespace" operations, they will merely have no effect. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
BEGIN; | ||
|
||
-- wild_stars returns T if pattern matches hay using IAM-style wildcards: * | ||
-- is any string, ? is a single char. It fails if pattern contains % or _ | ||
-- chars. | ||
CREATE OR REPLACE FUNCTION pg_temp.wild_stars(pattern VARCHAR, hay VARCHAR) | ||
RETURNS BOOLEAN LANGUAGE plpgsql IMMUTABLE AS $$ | ||
DECLARE | ||
unsafe BOOLEAN; | ||
match BOOLEAN; | ||
|
||
BEGIN | ||
SELECT pattern LIKE '%\%%' OR pattern LIKE '%\_%' INTO STRICT unsafe; | ||
IF unsafe THEN | ||
RAISE EXCEPTION 'unsafe pattern % contains "%%" or "_"', pattern; | ||
END IF; | ||
SELECT hay LIKE replace(replace(pattern, '*', '%'), '?', '_') INTO STRICT match; | ||
RETURN match; | ||
END; | ||
$$; | ||
|
||
-- jsonb_string translates a JSONB string object to SQL TEXT. See | ||
-- https://stackoverflow.com/a/58755595. | ||
CREATE OR REPLACE FUNCTION pg_temp.jsonb_string(j JSONB) RETURNS TEXT LANGUAGE sql IMMUTABLE AS $$ | ||
SELECT j ->> 0 | ||
$$; | ||
|
||
-- statement_has_action returns T if statement (of the JSON type stored in | ||
-- auth_policies) mentions action. | ||
CREATE OR REPLACE FUNCTION pg_temp.statement_has_action(statement JSONB, action VARCHAR) | ||
RETURNS BOOLEAN LANGUAGE sql IMMUTABLE AS $$ | ||
SELECT action IN ( | ||
SELECT jsonb_array_elements_text(value->'Action') | ||
FROM jsonb_array_elements(statement) | ||
); | ||
$$; | ||
|
||
UPDATE auth_policies | ||
SET statement = statement || '[{"Action": ["fs:AttachStorageNamespace"], "Effect": "allow", "Resource": "*"}]'::jsonb | ||
WHERE id IN ( | ||
SELECT DISTINCT id FROM ( | ||
SELECT id, pg_temp.jsonb_string(s->'Effect') AS effect, jsonb_array_elements(s->'Action') AS action | ||
FROM ( | ||
SELECT id, jsonb_array_elements(statement) s FROM auth_policies | ||
-- Update only statements that never mention | ||
-- AttachStorageNamespace. So downgrade can do nothing, and | ||
-- re-upgrading will not re-add an existing statement or harm | ||
-- pre-existing policies. | ||
WHERE NOT pg_temp.statement_has_action(statement, 'fs:AttachStorageNamespace') | ||
) y | ||
) x | ||
WHERE effect = 'allow' AND pg_temp.wild_stars(pg_temp.jsonb_string(action), 'fs:CreateRepository') | ||
); | ||
|
||
END; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters