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

MDEV-30908 Make SESSION_USER() comparable with CURRENT_USER() #2985

Open
wants to merge 1 commit into
base: 11.4
Choose a base branch
from

Conversation

christian7007
Copy link
Contributor

@christian7007 christian7007 commented Jan 8, 2024

  • The Jira issue number for this PR is: MDEV-30908

Description

Update SESSION_USER() behaviour to be comparable with CURRENT_USER(). SESSION_USER() will return the user and host columns from mysql.user used to authenticate the user when the session was created.

Historically SESSION_USER() was an alias of USER() function. The main difference with USER() behaviour after this change is that SESSION_USER() now returns the host column from mysql.user instead of the client host or ip.

This will allow to retrieve the user and host from the session used to call a stored procedure when SQL SECURITY DEFINER is set. With the previous behaviour the host used by the client was returned instead of the value of the host column from mysql.user table making it harder to know exactly which row from mysql.user was used to authenticate the user.

Below there is an example of the output of this function compared with the existing alternatives, if we create mysql.user_function_demo() like:

CREATE PROCEDURE mysql.user_function_demo() SQL SECURITY DEFINER BEGIN
SELECT CURRENT_USER(), USER(), SESSION_USER();
END

use the command below to connect to the server:

$ mariadb -u test_user -h 127.0.0.1   # force connection to be over TCP, not Unix socket

and then execute the procedure:

CURRENT_USER()	USER()	                   SESSION_USER()
root@localhost	[email protected]:12345  test_user@%

NOTE: SESSION_USER_IS_USER old mode is added to make the change backward compatible.

How can this PR be tested?

This PR adds a new MTR test for testing this new function.

Basing the PR against the correct MariaDB version

  • This is a new feature and the PR is based against the latest MariaDB development branch.

PR quality check

  • I checked the CODING_STANDARDS.md file and my PR conforms to this where appropriate.
  • For any trivial modifications to the PR, I am ok with the reviewer making the changes themselves.

Copyright

All new code of the whole pull request, including one or several files
that are either new files or modified ones, are contributed under the
BSD-new license. I am contributing on behalf of my employer
Amazon Web Services, Inc.

@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@christian7007
Copy link
Contributor Author

The changes on this PR were previously discussed with @vuvova and @grooverdan at: #2928

@christian7007 christian7007 force-pushed the MDEV-30908-SESSION_USER branch 3 times, most recently from 4588115 to db9b3e4 Compare January 9, 2024 22:54
@christian7007
Copy link
Contributor Author

@vuvova regarding your comment about optional parenthesis here: christian7007@4c9ec81#r135239559.

The first version I pushed for this PR allowing option parenthesis broke main.parser test at:

create table SESSION_USER(a int);

Maybe I'm missing something, but after some research my understanding is that the failure came from supporting SESSION_USER with optional parenthesis (if we add SESSION_USER_SYM as keyword_sp_var_and_label using optional_braces it will cause ambiguity and will make the parser building to fail). This was not supported before, and as adding this will break backward compatibility I pushed a new version for the changes making parenthesis mandatory for SESSION_USER() again.

Please, let me know if you think I'm missing something or if you think there is a better way of addressing this.

@christian7007
Copy link
Contributor Author

I also updated some MTR tests, there are still some CI failures but after looking at them I don't think they are related with my changes.

@vuvova vuvova changed the title Make SESSION_USER() comparable with CURRENT_USER() MDEV-30908 Make SESSION_USER() comparable with CURRENT_USER() Jan 12, 2024
@vuvova vuvova self-requested a review January 12, 2024 21:54
@vuvova
Copy link
Member

vuvova commented Jan 12, 2024

I've marked MDEV-30908 as being IN-REVIEW, assigned to myself. But I'll only look at it next month. There's still enough time for it to get into the next release (deadline is mid-March).

Update `SESSION_USER()` behaviour to be comparable with `CURRENT_USER()`.
`SESSION_USER()` will return the user and host columns from `mysql.user`
used to authenticate the user when the session was created.

Historically `SESSION_USER()` was an alias of `USER()` function. The
main difference with `USER()` behaviour after this changes is that
`SESSION_USER()` now returns the host column from `mysql.user` instead of
the client host or ip.

NOTE: `SESSION_USER_IS_USER` old mode is added to make the change
backward compatible.

All new code of the whole pull request, including one or several files
that are either new files or modified ones, are contributed under the
BSD-new license. I am contributing on behalf of my employer
Amazon Web Services, Inc.
@ottok
Copy link
Contributor

ottok commented Apr 7, 2024

We know you @vuvova are busy, but perhaps you could do a superficial review to let us know if you agree with this change in principle?

Copy link
Member

@vuvova vuvova left a comment

Choose a reason for hiding this comment

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

Looks fine to me. Note a couple of minor comments. And please rebase on top of 11.6, you'll have old-mode related conflicts.

@@ -3889,6 +3889,7 @@ static const char *old_mode_names[]=
"COMPAT_5_1_CHECKSUM", // deprecated since 11.3
"NO_NULL_COLLATION_IDS", // deprecated since 11.3
"LOCK_ALTER_TABLE_COPY", // deprecated since 11.3
"SESSION_USER_IS_USER",
Copy link
Member

Choose a reason for hiding this comment

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

when you'll rebase it on top of 11.6, you'll see a conflict here, see how the new old mode OLD_FLUSH_STATUS was added and add yours in the same way with a correct deprecation version.

{
return mark_unsupported_function(fully_qualified_func_name(), arg,
VCOL_SESSION_FUNC);
}
Copy link
Member

Choose a reason for hiding this comment

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

get_copy() ?

}
const char *fully_qualified_func_name() const override
{ return "session_user()"; }
bool check_vcol_func_processor(void *arg) override
Copy link
Member

Choose a reason for hiding this comment

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

doesn't seem to be necessary? Item_func_sysconst (two classes up the hierarchy) has exactly that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
4 participants