gpt4 book ai didi

mysql - 仅向用户提供 GRANT EXECUTE 'procedure'(无选择或插入权限)

转载 作者:行者123 更新时间:2023-11-29 06:05:58 25 4
gpt4 key购买 nike

我想知道是否可以授予SELECTINSERT等权限给用户EXECUTE权限. 对运行过程的表的权限?

将其用于 Web 应用程序的登录表。 MySQL 在 Docker 容器中运行。用于创建过程的 SQL 作为 docker 构建过程的一部分被复制(运行时,sql 在 entrypoint.sh 中使用)。 Login_db 在运行容器时创建(-e 标志)。

我想从下面删除 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;

这并不能解决问题 - 因为作为表所有者会暴露相同的权限:

Execute stored proc fails with GRANT EXECUTE because of table permissions

这也许可以解释为什么我不能,但是表名对我来说有点奇怪(MySQL 新手 - 我的印象是 mysql.proc 是一个系统表,所以不是确定它是否适用):

How to grant execute on specific stored procedure to user

会不会是 root 在创建过程时没有 SELECT 权限,所以登录用户无法运行它? (因为 Docker MySQL 运行 entrypoint.sh 然后环境变量)?

过程代码在这里(我知道,不是最优雅的)- 我可以 GRANT 然后 REVOKE 权限给 logins 用户在此,考虑到 DEFINER 是 root ?

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;

最佳答案

是的,您可以在声明 stored procedure 时使用 sql security definer 来做到这一点:

The SQL SECURITY characteristic can be DEFINER or INVOKER to specify the security context; that is, whether the routine executes using the privileges of the account named in the routine DEFINER clause or the user who invokes it. This account must have permission to access the database with which the routine is associated. The default value is 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.

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.

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(). The default DEFINER value is the user who executes the CREATE PROCEDURE or CREATE FUNCTION statement. This is the same as specifying DEFINER = CURRENT_USER explicitly.

总结一下:definer子句中的用户必须对这个ase中的底层表有select/insert权限,而执行stored proc的用户必须有stored proc的执行权限。

关于mysql - 仅向用户提供 GRANT EXECUTE 'procedure'(无选择或插入权限),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41665626/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com