如何在Snowflake中为角色授予数据库全量权限(含未来对象)
问题根因
Snowflake的权限是严格按层级隔离的,你之前执行的grant all on database test to developer;只会给角色授予数据库层面的基础权限(比如查看数据库、在库内创建schema的权限),不会自动继承库内已有schema、表、stage等对象的操作权限,默认也不会对后续新建的对象生效,所以会出现只能看到数据库、没法访问内部对象的问题。
正确授权方案
要实现覆盖全类型对象、自动适配未来新建资源的全操作权限(读/建/改/删),需要按层级依次授权,同时配置未来对象的自动赋权规则:
- 第一步:授予数据库本身的全量权限
grant all privileges on database test to role developer;
- 第二步:授予库内所有已存在schema的全量权限
grant all privileges on all schemas in database test to role developer;
- 第三步:授予所有现有schema下各类型对象的全量权限
-- 表、视图全权限 grant all privileges on all tables in database test to role developer; grant all privileges on all views in database test to role developer; -- 内部/外部stage全权限 grant all privileges on all stages in database test to role developer; -- 存储过程、自定义函数、序列等常用对象全权限 grant all privileges on all procedures in database test to role developer; grant all privileges on all functions in database test to role developer; grant all privileges on all sequences in database test to role developer;
注意:存储集成(storage integration)是账户级对象,不属于单个数据库下的资源,无法通过数据库级授权覆盖,需要单独给对应角色授权:
-- 将实际使用的存储集成替换语句中的占位符即可 grant usage on integration <你实际使用的存储集成名称> to role developer;
- 第四步:配置未来对象自动赋权规则,保证库内后续新建的所有对应对象自动给developer角色开放全权限,无需重复手动授权
-- 未来新建schema自动赋权 grant all privileges on future schemas in database test to role developer; -- 未来各schema下新建的表、视图自动赋权 grant all privileges on future tables in database test to role developer; grant all privileges on future views in database test to role developer; -- 未来新建stage自动赋权 grant all privileges on future stages in database test to role developer; -- 未来新建存储过程、函数、序列自动赋权 grant all privileges on future procedures in database test to role developer; grant all privileges on future functions in database test to role developer; grant all privileges on future sequences in database test to role developer;
权限校验方式
授权完成后可切换到developer角色执行测试语句,确认权限正常生效:
use role developer; use database test; -- 验证schema创建权限 create schema if not exists perm_check; -- 验证表创建、写入、查询、删除权限 create table if not exists perm_check.t1(id int); insert into perm_check.t1 values(1); select * from perm_check.t1; drop table perm_check.t1; drop schema perm_check;
补充说明:如果目标数据库是通过数据共享创建的只读数据库,部分对象权限会受共享提供方的权限限制,无法通过本地授权获取。
内容的提问来源于stack exchange,提问作者h33
相关产品推荐
相关产品推荐

