Skip to content

Conversation

@homar
Copy link
Member

@homar homar commented Jan 13, 2025

Description

Propagate function lifecycle events sucha as create function and drop function to SystemSecurityMetadata

Additional context and related issues

Release notes

( ) This is not user-visible or is docs only, and no release notes are required.
( ) Release notes are required. Please propose a release note for me.
(x) Release notes are required, with the following suggested text:

## Section
* Add support for functions ownership 

@cla-bot cla-bot bot added the cla-signed label Jan 13, 2025
@homar homar force-pushed the homar/add_setFunctionOwner branch 4 times, most recently from f3b4bf0 to ceca747 Compare January 16, 2025 09:57
@homar homar changed the title test Add setFunctionOwner to SystemSecurityMetadata Jan 16, 2025
@homar homar marked this pull request as ready for review January 16, 2025 10:03
@homar homar requested review from Dith3r, kokosing and ksobolew January 16, 2025 10:03
Comment on lines 2715 to 2724
Copy link
Contributor

Choose a reason for hiding this comment

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

With views it's done differently - there's a tableCreated callback function, which is (presumably) used to update the ownership. The setXOwner methods are called in response to ALTER X SET AUTHORIZATION commands, instead (which we don't have here). Note that the actual function is created by the connector metadata, which is a factor too.

Copy link
Contributor

Choose a reason for hiding this comment

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

This test is fine, but it could take some inspiration for the existing tests for views. For example, those tests are also checking things like: the access to the underlying resources (here: my_test_function_inner) is only checked on query/execute, not during creation. Though it could be different for functions.

Copy link
Member

Choose a reason for hiding this comment

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

nit. ImmutableSet.of

Copy link
Member

Choose a reason for hiding this comment

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

I think you need to follow the convention here. Instead of saying what you want to do on event, you should simply emit the event and handle what you want in the implementation.

So you should emit events, when function is created, dropped or renamed. Like for any other object.

@homar homar force-pushed the homar/add_setFunctionOwner branch 2 times, most recently from d70bdfa to 9566b40 Compare January 21, 2025 12:44
@homar homar force-pushed the homar/add_setFunctionOwner branch 2 times, most recently from abc7ec8 to 6bd0903 Compare January 21, 2025 14:45
Copy link
Contributor

@ksobolew ksobolew left a comment

Choose a reason for hiding this comment

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

Apart from nits LGTM

Comment on lines 204 to 206
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: keep formatting consistent

Suggested change
@Override
public void functionCreated(Session session, CatalogSchemaFunctionName function, TrinoPrincipal principal)
{}
@Override
public void functionCreated(Session session, CatalogSchemaFunctionName function, TrinoPrincipal principal) {}

Comment on lines 2717 to 2722
Copy link
Contributor

Choose a reason for hiding this comment

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

These callbacks should be done as the last thing, specifically after the actual update (here: after metadata.createLanguageFunction is called)

Copy link
Member Author

Choose a reason for hiding this comment

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

It is called later, here we just prepare a lambda function, it is invoked much later.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm sorry, I don't see any lambda...

Copy link
Member Author

Choose a reason for hiding this comment

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

nvm, my bad i looked at a wrong place, sorry for the confusion

Comment on lines 267 to 269
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
@Override
public void functionCreated(Session session, CatalogSchemaFunctionName function, TrinoPrincipal principal)
{ }
@Override
public void functionCreated(Session session, CatalogSchemaFunctionName function, TrinoPrincipal principal) {}

@homar homar force-pushed the homar/add_setFunctionOwner branch 2 times, most recently from ffe40a9 to 48488b8 Compare January 22, 2025 21:17
@homar homar requested a review from ksobolew January 22, 2025 23:05
Comment on lines 2717 to 2724
Copy link
Contributor

Choose a reason for hiding this comment

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

I mean these two statements should be switched, per convention at least:

Suggested change
if (catalogMetadata.getSecurityManagement() == SYSTEM) {
systemSecurityMetadata.functionCreated(
session,
new CatalogSchemaFunctionName(catalogHandle.getCatalogName().toString(), schemaFunctionName),
new TrinoPrincipal(PrincipalType.USER, session.getUser()));
}
metadata.createLanguageFunction(session.toConnectorSession(catalogHandle), schemaFunctionName, function, replace);
metadata.createLanguageFunction(session.toConnectorSession(catalogHandle), schemaFunctionName, function, replace);
if (catalogMetadata.getSecurityManagement() == SYSTEM) {
systemSecurityMetadata.functionCreated(
session,
new CatalogSchemaFunctionName(catalogHandle.getCatalogName().toString(), schemaFunctionName),
new TrinoPrincipal(PrincipalType.USER, session.getUser()));
}

@homar homar force-pushed the homar/add_setFunctionOwner branch 2 times, most recently from bc2bc9f to 23c06c4 Compare January 23, 2025 10:18
Copy link
Member

Choose a reason for hiding this comment

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

Is there an operation which this method should be used by? There's not ALTER FUNCTION .. SET AUTHORIZATION for functions so I don't think there is such operation.

Copy link
Member

Choose a reason for hiding this comment

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

What's principal for? Isn't session enough to get the identity of the creator of the function?

Copy link
Member Author

Choose a reason for hiding this comment

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

following convention from other methods

Copy link
Member Author

Choose a reason for hiding this comment

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

ok i got it, fixed, thanks

@homar homar force-pushed the homar/add_setFunctionOwner branch 2 times, most recently from 8c12f07 to 35808fb Compare January 23, 2025 15:35
Copy link
Member

Choose a reason for hiding this comment

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

(identity, function)

Copy link
Member

Choose a reason for hiding this comment

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

Is this needed if there's also:

        assertAccessAllowed(
                functionOwnerSession,
                "CREATE FUNCTION memory.default.my_test_function_inner (x integer) RETURNS bigint RETURN x + 42");

?

Copy link
Member Author

Choose a reason for hiding this comment

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

yes, io.trino.security.TestingSystemSecurityMetadata#functionCreated is empty
This was suggested by @ksobolew to follow the convention

Copy link
Member Author

Choose a reason for hiding this comment

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

it was during offline conversation when I asked him about #24696 (comment) and #24696 (comment)

Copy link
Member

Choose a reason for hiding this comment

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

How about a check with the default session?

Copy link
Member Author

Choose a reason for hiding this comment

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

What would be the point of such a test? Verification if a function can be created is out of scope for this commit

Copy link
Member

Choose a reason for hiding this comment

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

It's a fairly complicated test case so maybe it's worth adding a few comments.
As I understand here the default session gets access denied because the function owner got access denied on the inner function, correct?

@homar homar force-pushed the homar/add_setFunctionOwner branch 2 times, most recently from 920046d to 8832645 Compare January 27, 2025 14:05
Copy link
Member

Choose a reason for hiding this comment

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

Please improve a commit message and pull request title and description. It is more about propagating events of function creation than handling onwnership.

@homar homar changed the title Add setFunctionOwner to SystemSecurityMetadata Propagate function lifecycle events to SystemSecurityMetadata Jan 27, 2025
@homar homar force-pushed the homar/add_setFunctionOwner branch from 8832645 to b6ded27 Compare January 27, 2025 17:28
Copy link
Member

@kokosing kokosing left a comment

Choose a reason for hiding this comment

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

Please fix the PR description to not to mention anything about ownerhship.

Copy link
Member

Choose a reason for hiding this comment

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

I don't think it is correct testing of the functionality you have introduced. You need to check if events are propagated. It has not much to do with "ownership". It is a different layer. You need a test similar to io.trino.execution.TestBeginQuery. Then you don't need any changes in TestingAccessControlManager too.

Copy link
Member

Choose a reason for hiding this comment

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

I have discussed this with @lukasz-walkiewicz and now I understand why it was added.

Please use functionCreated and functionDropped in testing access control and extract the case with DENY to inner function as separate test case. This test is getting quite complex as we are reusing entities for different test cases.

Copy link
Member Author

Choose a reason for hiding this comment

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

I was about to add another test

@homar homar force-pushed the homar/add_setFunctionOwner branch 3 times, most recently from fe976ee to 1d94fd5 Compare January 29, 2025 07:48
Copy link
Contributor

@ksobolew ksobolew left a comment

Choose a reason for hiding this comment

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

Looks like now we have more extensive tests for functions than for views :)

Comment on lines 567 to 568
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
.setIdentity(Identity.forUser(functionOwner)
.build())
.setIdentity(Identity.ofUser(functionOwner))

Comment on lines +598 to +599
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: just to be extra-safe, you could add an assertion that with the same denied privilege you are not allowed to call the inner function directly

Copy link
Contributor

Choose a reason for hiding this comment

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

Did you mean to deny it here?

Copy link
Member Author

Choose a reason for hiding this comment

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

fixed

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure this is what @kokosing had in mind, but this name is not right. It's more about roles than denying.

Comment on lines 675 to 676
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
.setIdentity(Identity.forUser(functionOwner1)
.build())
.setIdentity(Identity.ofUser(functionOwner1))

Comment on lines 681 to 682
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
.setIdentity(Identity.forUser(functionOwner2)
.build())
.setIdentity(Identity.ofUser(functionOwner2))

Copy link
Contributor

Choose a reason for hiding this comment

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

Isn't this test duplicated now?

@homar homar force-pushed the homar/add_setFunctionOwner branch from 1d94fd5 to c55ff9f Compare January 29, 2025 09:59
Copy link
Contributor

@ksobolew ksobolew left a comment

Choose a reason for hiding this comment

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

Re-approved from my side :)

@Test
public void testAllowCallFunction()
{
reset();
Copy link
Member

Choose a reason for hiding this comment

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

nit. it already pre-exisiting, but we could do this as part of @Before method. Maybe an idea for a follow-up?

Copy link
Member Author

Choose a reason for hiding this comment

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

PTAL #24852

@kokosing kokosing merged commit 5e3e900 into trinodb:master Jan 30, 2025
94 checks passed
@kokosing
Copy link
Member

Thank you! Merged.

@github-actions github-actions bot added this to the 470 milestone Jan 30, 2025
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.

5 participants