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

Create versioned views WITH (security_invoker = true) #189

Merged
merged 3 commits into from
Oct 18, 2023

Conversation

andrew-farries
Copy link
Collaborator

Ensure that views (in Postgres 15 and 16) are created with security_invoker = true.

This ensures that any row-level security on the underlying table is applied according to the permissions of the invoker of the view rather than its owner.

Postgres 14 does not support the creation of views with security_invoker = true.

See:

Closes #179

pkg/roll/roll.go Outdated
pgConn: conn,
schema: schema,
state: state,
viewCreator: viewCreator,
Copy link
Member

Choose a reason for hiding this comment

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

not against this but I guess that we will do conditional statements based on the version in many places. Perhaps it would be good to store the version as a field here and read it from the createViews function to use one creation mode or another

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I created the interface and two implementations because I think it's cleaner to do this kind of version switching at the top-level with dependency injection rather than having switches deeper in the code.

It's not yet clear to me where/how many such switches we will have though.

I've rebased to store the postgres version and use it as a switch in createView, which is less new code.

LMK which approach you prefer.

run: go test ./...
env:
POSTGRES_VERSION: ${{ matrix.pgVersion }}

- name: Run pg14 tests
if: matrix.pgVersion == '14.8'
run: go test ./... --skip 'TestViewsAreCreatedWithSecurityInvokerTrue'
Copy link
Member

Choose a reason for hiding this comment

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

Could we skip the test from the test case instead? calling t.Skip or similar based on the postgres version

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I've rebased to skip the test as you suggest rather than skipping in github actions.

Copy link
Member

@exekias exekias left a comment

Choose a reason for hiding this comment

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

LGTM

My impression is that as we make progress (and new Postgres releases appear), we will do this more and more. Over the time more features will appear making zero downtime changes easier, or that I would hope

Having that into account, this approach looks good to me. As of today any option would work for me, my only worry is that if we solve this through dependency injection the Roll struct will only grow on logic that could be more local

columns := make([]string, 0, len(table.Columns))
for k, v := range table.Columns {
columns = append(columns, fmt.Sprintf("%s AS %s", pq.QuoteIdentifier(v.Name), pq.QuoteIdentifier(k)))
}

withOptions := ""
Copy link
Member

Choose a reason for hiding this comment

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

Could you add a comment here to explain security_invoker? 🙏

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done

@andrew-farries andrew-farries enabled auto-merge (squash) October 18, 2023 06:03
@andrew-farries andrew-farries merged commit bd53d10 into main Oct 18, 2023
21 of 22 checks passed
@andrew-farries andrew-farries deleted the security-invoker-views branch October 18, 2023 06:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use security_invoker views for temporary views created in the new schema
2 participants