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

Verify SQL MERGE access control checks #13664

Merged

Conversation

djsstarburst
Copy link
Member

Description

This commit adds a MERGE test in BaseConnectorTest that verifies
that in order execute a MERGE, you must have appropriate access
control rights. Specifically, if there is a source table, you must
have SELECT on the table columns referenced; if the MERGE does
inserts, you must have INSERT on the target table; if the MERGE
does deletes you must have DELETE on the target table; and if
the MERGE does updates, you must have UPDATE on the target
table.

Is this change a fix, improvement, new feature, refactoring, or other?

It is an added test.

Is this a change to the core query engine, a connector, client library, or the SPI interfaces? (be specific)

None of the above.

How would you describe this change to a non-technical end user or system administrator?

Related issues, pull requests, and links

Documentation

(x) No documentation is needed.
( ) Sufficient documentation is included in this PR.
( ) Documentation PR is available with #prnumber.
( ) Documentation issue #issuenumber is filed, and can be handled later.

Release notes

(x) No release notes entries required.
( ) Release notes entries required with the following suggested text:

# Section
* Fix some things. ({issue}`issuenumber`)

Copy link
Member

@dain dain left a comment

Choose a reason for hiding this comment

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

Other than the formatting issue, looks good

@djsstarburst djsstarburst force-pushed the david.stryker/merge-access-control branch from ff18825 to 4560ff9 Compare August 13, 2022 19:26
Copy link
Member

@electrum electrum left a comment

Choose a reason for hiding this comment

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

These should move to TestAccessControl, as discussed offline. We don't need test data since we are only testing access control checks, not the merge functionality. I created #13665 which allows testing the positive case.

@djsstarburst djsstarburst force-pushed the david.stryker/merge-access-control branch from 4560ff9 to c342350 Compare August 13, 2022 20:19
@djsstarburst
Copy link
Member Author

djsstarburst commented Aug 13, 2022

@electrum suggested that I move this test to TestAccessControl. However, that test uses the Blackhole connector, which didn't support MERGE.

I added another commit to the start of my PR that moves the MERGE access control checks up to the top of StatementAnalysis.visitMerge. It a good change, but it's not sufficient to move the test to TestAccessControl. The problem is that we don't know which if any columns of the source and target tables will be SELECTed until StatementAnalysis.visitTable is called, and it make a call to get the merge rowId handle.

@electrum produced an ersatz implementation of MERGE for Blackhole, and maybe that makes sense as a followup. But at this point, I’m mildly in favor of merging my existing PR, now with the commit that does access checks earlier.

@djsstarburst
Copy link
Member Author

I created #13665 which allows testing the positive case.

I don't see how that PR can work, because the body of storeMergedRows is empty.

@electrum
Copy link
Member

It works because the Black Hole connector doesn't store or produce data (unless you ask it to produce nulls), so there is nothing to match or update. You can see that BlackHolePageSink ignores the provided page.

@djsstarburst djsstarburst force-pushed the david.stryker/merge-access-control branch from c342350 to fbec671 Compare August 14, 2022 00:47
@djsstarburst
Copy link
Member Author

I rebased on top of tip master, which has @electrum's ersatz implementation of MERGE for the Blackhole connector. That let me remove the test from BaseConnectorTest and move it to TestAccessControl, after I removed the final successful MERGE operation and verification, which Blackhole can't execute.

I think this now satisfies the concerns raised, and is ready to merge.

.findFirst()
.ifPresent(mergeCase -> accessControl.checkCanDeleteFromTable(session.toSecurityContext(), tableName));

ImmutableSet.Builder<String> allUpdateColumnNamesBuilder = ImmutableSet.builder();
Copy link
Member

Choose a reason for hiding this comment

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

Nit: we can use HashSet here with the variable named allUpdateColumnNames since we are just building this to pass to a function. Using an immutable builder is overkill and makes the code harder to read.

Copy link
Member Author

Choose a reason for hiding this comment

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

Changed.


assertUpdate(format("CREATE TABLE %s (nation_name VARCHAR, region_name VARCHAR)", targetTable));

assertUpdate(format("INSERT INTO %s (nation_name, region_name) VALUES ('FRANCE', 'EUROPE'), ('ALGERIA', 'AFRICA'), ('GERMANY', 'EUROPE')", targetTable), 3);
Copy link
Member

Choose a reason for hiding this comment

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

No need to insert data, since we aren't testing the actual merge functionality, and the blackhole connector will discard the data anyway. (so it's confusing to the reader as to why we do it)

Copy link
Member Author

Choose a reason for hiding this comment

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

Removed.

assertUpdate("DROP TABLE " + targetTable);
}

private void withPrivilegeDenied(String tableName, TestingPrivilegeType type, Runnable runnable)
Copy link
Member

Choose a reason for hiding this comment

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

We shouldn't need this method

Copy link
Member Author

Choose a reason for hiding this comment

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

Removed.


// Show that without SELECT on the source table, the MERGE fails regardless of which case is included
for (String mergeCase : ImmutableList.of(deleteCase, updateCase, insertCase)) {
withPrivilegeDenied(sourceTable, SELECT_COLUMN, () ->
Copy link
Member

Choose a reason for hiding this comment

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

Use assertAccessDenied, like the other tests in this class.

Copy link
Member Author

Choose a reason for hiding this comment

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

Changed.

Copy link
Member

@electrum electrum left a comment

Choose a reason for hiding this comment

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

Looks good overall. A few minor comments. Thanks for updating this.

@electrum
Copy link
Member

electrum commented Aug 15, 2022

Blackhole should be able to run the final MERGE using assertAccessAllowed. The existing merge test using assertAccessAllowed that this PR replaces runs.

@djsstarburst djsstarburst force-pushed the david.stryker/merge-access-control branch from fbec671 to 2574eb0 Compare August 15, 2022 15:04
@djsstarburst
Copy link
Member Author

Blackhole should be able to run the final MERGE using assertAccessAllowed. The existing merge test using assertAccessAllowed that this PR replaces runs.

Added.

This commit adds a MERGE test in TestAccessControl that verifies
that in order execute a MERGE, you must have appropriate access
control rights.  Specifically, if there is a source table, you must
have SELECT on the table columns referenced; if the MERGE does
inserts, you must have INSERT on the target table; if the MERGE
does deletes you must have DELETE on the target table; and if
the MERGE does updates, you must have UPDATE on the target
table.
@djsstarburst djsstarburst force-pushed the david.stryker/merge-access-control branch from 2574eb0 to 6ddca49 Compare August 15, 2022 15:47
@electrum electrum merged commit 121463c into trinodb:master Aug 15, 2022
@electrum
Copy link
Member

Thanks!

@github-actions github-actions bot added this to the 393 milestone Aug 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

3 participants