简体   繁体   English

仅向用户提供GRANT EXECUTE“过程”(无选择或插入权限)

[英]Provide only GRANT EXECUTE 'procedure' to a user (No select or insert permissions)

I was wondering if it's possible to grant EXECUTE permissions to a user without granting SELECT , INSERT etc. permissions on the table that a procedure runs on? 我想知道是否可以向用户授予EXECUTE权限而无需授予对过程运行的表的SELECTINSERT等权限?

Using it for a Logins table for a webapp. 将其用于Web应用程序的登录表。 MySQL is running in a Docker container. MySQL在Docker容器中运行。 SQL for creating procedures is copied across as part of the docker build process (when run, the sql is used in entrypoint.sh). 创建过程的SQL在docker构建过程中被复制(运行时,在entrypoint.sh中使用该sql)。 Login_db is created when running the container (-e flag). 运行容器时将创建Login_db(-e标志)。

I'd like to remove the GRANT SELECT line from below so, no matter what happens, the webapp server can never run a SELECT query - such as doing SELECT * FROM logins . 我想从下面删除GRANT SELECT行,因此无论发生什么情况,webapp服务器都永远无法运行SELECT查询-例如执行SELECT * FROM logins

CREATE USER 'logins'@'172.24.0.7' IDENTIFIED BY 'some-password';
GRANT SELECT, INSERT, UPDATE on logins_db.login TO 'logins'@'172.24.0.7';
GRANT EXECUTE ON PROCEDURE logins_db.sp_login16 TO 'logins'@'172.24.0.7';
FLUSH PRIVILEGES;

This doesn't solve it - as being the table owner would expose the same privileges: 这不能解决问题-因为作为表所有者会暴露相同的特权:

Execute stored proc fails with GRANT EXECUTE because of table permissions 由于表权限的原因,执行存储的过程失败并显示GRANT EXECUTE

This might explain why I can't, but the table names are a bit odd to me (MySQL newbie - I'm under the impression that mysql.proc is a system table, so not sure if it applies): 这可能可以解释为什么我不能这样做,但是表名对我来说有点奇怪(MySQL新手-我给人的印象是mysql.proc是系统表,因此不确定是否适用):

How to grant execute on specific stored procedure to user 如何授予用户执行特定存储过程的权限

Could it be that root doesn't have SELECT privileges when creating the procedure and so the the logins user cannot run it? 可能是root用户在创建过程时没有SELECT特权,因此登录用户无法运行它吗? (Because Docker MySQL runs entrypoint.sh and then the environment variable)? (因为Docker MySQL先运行entrypoint.sh,然后运行环境变量)?

The procedure code is here (I know, not the most elegant) - could I GRANT and then REVOKE privileges for the logins user within this, considering the DEFINER is root ? 过程代码在这里(我知道,不是最优雅的)-考虑到DEFINER是root用户,我可以在其中授予logins用户的GRANT权限然后REVOKE权限吗?

CREATE DEFINER=`root`@`localhost` PROCEDURE `sp_login16`(
IN p_email VARCHAR(120),
IN p_password VARCHAR(120))
BEGIN
SELECT user_id,user_password FROM login WHERE user_email = p_email;

Yes, you can do this by using sql security definer while declaring the stored procedure : 是的,您可以在声明存储过程时使用sql security definer 程序来执行此操作:

The SQL SECURITY characteristic can be DEFINER or INVOKER to specify the security context; SQL SECURITY特性可以是DEFINER或INVOKER来指定安全性上下文。 that is, whether the routine executes using the privileges of the account named in the routine DEFINER clause or the user who invokes it. 也就是说,例程是否使用例程DEFINER子句中命名的帐户的特权或调用该例程的用户的特权来执行。 This account must have permission to access the database with which the routine is associated. 该帐户必须有权访问与该例程关联的数据库。 The default value is DEFINER. 默认值为DEFINER。 The user who invokes the routine must have the EXECUTE privilege for it, as must the DEFINER account if the routine executes in definer security context. 如果例程在定义程序安全性上下文中执行,则调用该例程的用户必须对其具有EXECUTE特权,对于DEFINER帐户也必须具有该特权。

The DEFINER clause specifies the MySQL account to be used when checking access privileges at routine execution time for routines that have the SQL SECURITY DEFINER characteristic. DEFINER子句指定在例程执行时检查具有SQL SECURITY DEFINER特征的例程的访问权限时要使用的MySQL帐户。

If a user value is given for the DEFINER clause, it should be a MySQL account specified as 'user_name'@'host_name', CURRENT_USER, or CURRENT_USER(). 如果为DEFINER子句提供了一个用户值,则它应该是一个指定为'user_name'@'host_name',CURRENT_USER或CURRENT_USER()的MySQL帐户。 The default DEFINER value is the user who executes the CREATE PROCEDURE or CREATE FUNCTION statement. 默认的DEFINER值是执行CREATE PROCEDURE或CREATE FUNCTION语句的用户。 This is the same as specifying DEFINER = CURRENT_USER explicitly. 这与显式指定DEFINER = CURRENT_USER相同。

To sum it up: the user in the definer clause has to have the select / insert privileges to the underlying table in this ase, while the user who executes the stored proc must have execute privileges to the stored proc. 概括起来:在这种情况下,definer子句中的用户必须具有对基础表的选择/插入特权,而执行存储的proc的用户必须具有对存储的proc的执行特权。

Added a new user ADMIN@localhost with SELECT , INSERT and UPDATE privileges. 添加了具有SELECTINSERTUPDATE特权的新用户ADMIN@localhost ADMIN then became the DEFINER for all the procedures, with 'logins'@'172.24.0.7' only being granted EXECUTE permissions. ADMIN然后成为了DEFINER所有程序,用'logins'@'172.24.0.7'仅被授予EXECUTE权限。 Runs perfectly now! 现在运行完美!

Apparently you can't use root in the way I was trying to. 显然,您不能以我尝试的方式使用root。 Kudos to @Shadow for pointing me in the right direction. 感谢@Shadow向我指出正确的方向。

Final SQL here: 此处的最终SQL:

https://github.com/dijksterhuis/Notflix/tree/master/mysql/logins/sql https://github.com/dijksterhuis/Notflix/tree/master/mysql/logins/sql

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM