Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[part of 832]/add db id to statements #855

Merged
merged 3 commits into from
Jul 15, 2019

Conversation

blakestier
Copy link

@blakestier blakestier commented Jul 10, 2019

🔩 Description

Adds db_id and policy_id (db_id) to iam_statements, allowing the removal of the superfluous iam_policy_statements table. Given that policy: statement is one: many, we didn't need the associations table.

I've left adding the new statement db_id as the new foreign key to iam_statement_projects as the PR's tests (and a gnarly, unfortunate rebase) made this PR unwieldy.

👍 Definition of Done

Tests pass.

👟 Demo Script / Repro Steps

Bring up hab studio, rebuild components/authz-service, CURL to create, delete, update, get policies with statements:

In ui, create new user coolbean, pw chefautomate

curl -XPOST -d '{"name":"test", "id":"newpolicy", "members":["user:local:coolbean"], "statements":[{"effect":"ALLOW", "actions":["*"] }]}' -H "api-token: $TOK" https://a2-dev.test/apis/iam/v2beta/policies --insecure

(Logged in as coolbean, with legacy polices deleted, should see Users (and be able to create/update/delete them)

curl -H "api-token: $TOK" https://a2-dev.test/apis/iam/v2beta/policies/newpolicy --insecure

curl -XPUT -d '{"name":"test", "members":["user:local:coolbean"], "statements":[{"effect":"ALLOW", "actions":["*"] }, {"effect":"DENY", "actions":["iam:users:delete"]}]}' -H "api-token: $TOK" https://a2-dev.test/apis/iam/v2beta/policies --insecure

curl -H "api-token: $TOK" https://a2-dev.test/apis/iam/v2beta/policies/newpolicy --insecure

(Coolbean should now not be able to delete users)

curl -XDELETE -H "api-token: $TOK" https://a2-dev.test/apis/iam/v2beta/policies/newpolicy --insecure

curl -H "api-token: $TOK" https://a2-dev.test/apis/iam/v2beta/policies/newpolicy --insecure (not found)

(Coolbean can't do a darn thing)

⛓️ Related Resources

✅ Checklist

  • Necessary tests added/updated?
  • Necessary docs added/updated?
  • Code actually executed?
  • Vetting performed (unit tests, lint, etc.)?

@susanev susanev added automate-auth WIP auth-team anything that needs to be on the auth team board labels Jul 11, 2019
@blakestier blakestier force-pushed the blake/a2-865/add_db_id_statements branch 4 times, most recently from aecd43b to c5e5c9e Compare July 12, 2019 21:42
@blakestier blakestier changed the title Blake/a2 865/add db id statements [A2 865]/add db id to statements Jul 12, 2019
@blakestier blakestier removed the WIP label Jul 12, 2019
@blakestier blakestier force-pushed the blake/a2-865/add_db_id_statements branch from c5e5c9e to 0fe7b88 Compare July 12, 2019 21:48
@blakestier blakestier marked this pull request as ready for review July 13, 2019 02:27
@@ -1,7 +1,6 @@
BEGIN;

SET CONSTRAINTS iam_policy_members_policy_id_fkey DEFERRED;
SET CONSTRAINTS iam_policy_statements_policy_id_fkey DEFERRED;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are from the upgrade migrations-- not to be confused with those with which we cannot tamper

DROP TABLE iam_policy_statements CASCADE;
ALTER TABLE iam_statement_projects
ADD UNIQUE (statement_id, project_id),
ADD CONSTRAINT iam_statement_projects_statement_id_fkey FOREIGN KEY (statement_id) REFERENCES iam_statements(id) ON DELETE CASCADE DEFERRABLE;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replace constraint until db_id switch is complete to preserve cascade deletes

@blakestier blakestier force-pushed the blake/a2-865/add_db_id_statements branch from f20073e to 5e3a041 Compare July 13, 2019 03:02
LANGUAGE SQL;

CREATE OR REPLACE FUNCTION query_policy (_policy_id TEXT, _projects_filter TEXT[])
RETURNS json
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to go through the exercise of seeing what is involved in converting this style of query we had been using as a team into the newer style that @srenatus started in his recent PR (#878), and also figured I could save you some grunt work, @blakestier. So I tackled query_policy and I believe I've got it working. Can chat about it when you have time.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd propose another round for this. Let's merge this, if we find it OK, instead of letting scope creep...

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@msorens That's awesome. Once we merge this I'll give you a heads up and we can get your change in before doing any more db work! 😅

Copy link
Contributor

@srenatus srenatus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

iam_policy_statements
WHERE
statement_id = iams.id);
DROP TABLE iam_policy_statements CASCADE;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💥 👍

COALESCE(json_agg(proj.id) FILTER (WHERE proj.id IS NOT NULL),
'[]')
FROM iam_statement_projects AS stmt_projs
LEFT OUTER JOIN iam_projects AS proj ON stmt_projs.statement_id = stmt.id WHERE stmt_projs.project_id = proj.id) AS projects
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nit] I believe LEFT implies OUTER:

The words INNER and OUTER are optional in all forms. INNER is the default; LEFT, RIGHT, and FULL imply an outer join.

From https://www.postgresql.org/docs/9.2/queries-table-expressions.html

Suggested change
LEFT OUTER JOIN iam_projects AS proj ON stmt_projs.statement_id = stmt.id WHERE stmt_projs.project_id = proj.id) AS projects
LEFT JOIN iam_projects AS proj ON stmt_projs.statement_id = stmt.id WHERE stmt_projs.project_id = proj.id) AS projects

But I'm not sure it matters, or if this query in its current form is what we'd want anyways. 🤔 Now that this isn't n:m, I think some of this could perhaps be further simplified...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

....but OTOH this works 😄Not sure there's a simpler way at all...

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We've just been explicit about joins so far . . . 🤷‍♂ I'll leave this for now in light of Michael's potential clean-up, though. I'd like to believe there's a simpler way 🦄

LANGUAGE SQL;

CREATE OR REPLACE FUNCTION query_policy (_policy_id TEXT, _projects_filter TEXT[])
RETURNS json
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd propose another round for this. Let's merge this, if we find it OK, instead of letting scope creep...

@srenatus
Copy link
Contributor

I've left adding the new statement db_id as the new foreign key to iam_statement_projects as the PR's tests (and a gnarly, unfortunate rebase) made this PR unwieldy.

I'm on it.

@srenatus srenatus force-pushed the blake/a2-865/add_db_id_statements branch from 5e3a041 to 47e9a01 Compare July 15, 2019 12:20
@srenatus
Copy link
Contributor

⚠️ Rebased this

Blake Johnson added 3 commits July 15, 2019 08:48
Signed-off-by: Blake Johnson <bstier@chef.io>
Signed-off-by: Blake Johnson <bstier@chef.io>
Signed-off-by: Blake Johnson <bstier@chef.io>
@blakestier blakestier force-pushed the blake/a2-865/add_db_id_statements branch from 47e9a01 to 1c7c83a Compare July 15, 2019 15:49
@blakestier blakestier changed the title [A2 865]/add db id to statements [part of 832]/add db id to statements Jul 15, 2019
@blakestier blakestier merged commit 8fd19bf into master Jul 15, 2019
@blakestier blakestier deleted the blake/a2-865/add_db_id_statements branch July 15, 2019 17:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
auth-team anything that needs to be on the auth team board automate-auth
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants