阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Oracle权限/角色赋予的差异理解

195次阅读
没有评论

共计 7627 个字符,预计需要花费 20 分钟才能阅读完成。

环境:Oracle 11.2.0.4
目的: 验证业务用户的权限/角色赋予的差异

现在创建两个用户 jingyu2 和 jingyu3;

SYS@jyzhao1> create user jingyu2 identified by jingyu2 DEFAULT tablespace tbs_jingyu;
SYS@jyzhao1> create user jingyu3 identified by jingyu3 DEFAULT tablespace tbs_jingyu;

SYS@jyzhao1> grant connect, resource to jingyu2, jingyu3;

模拟 jingyu2 用户下有一张表 T_jingyu2;jingyu3 用户下有一张表 T_jingyu3;

JINGYU2@jyzhao1> create table t_jingyu2 as select * from user_objects;

Table created.

JINGYU3@jyzhao1> create table t_jingyu3 as select * from user_objects;

Table created.

分别在两个用户的 session 下查询被赋予的角色/权限:

JINGYU2@jyzhao1>select * from session_privs;

PRIVILEGE
----------------------------------------
CREATE SESSION
UNLIMITED TABLESPACE
CREATE TABLE
CREATE CLUSTER
CREATE SEQUENCE
CREATE PROCEDURE
CREATE TRIGGER
CREATE TYPE
CREATE OPERATOR
CREATE INDEXTYPE

10 rows selected.

JINGYU2@jyzhao1>select * from session_roles;

ROLE
------------------------------
CONNECT
RESOURCE

JINGYU3 用户的会话权限和会话角色查询结果一致,输出略。
可以看到赋予 connect,resource 这两个最常被用于应用开发的角色之后,该用户具有上述 10 个权限,一般基础开发就够用了。

需求: jingyu2 用户访问 jingyu3 的表 jingyu3, 并创建同义词 jingyu3;

显然当前的这个需求,对于目前的角色/权限是不能满足需求的:

JINGYU2@jyzhao1>select count(1) from jingyu3.t_jingyu3;
select count(1) from jingyu3.t_jingyu3
                             *
ERROR at line 1:
ORA-00942: table or view does not exist


JINGYU2@jyzhao1>create synonym t_jingyu3 for jingyu3.t_jingyu3;
create synonym t_jingyu3 for jingyu3.t_jingyu3
*
ERROR at line 1:
ORA-01031: insufficient privileges

一是 jingyu2 用户不能访问其他用户 jingyu3 的表,二是 jingyu2 用户没有创建同义词的权限。

那么为了满足需求,考虑如何解决。

解决方案一:赋予缺少的权限(推荐使用)。
方案宗旨:根据业务需求,缺什么权限赋予什么权限,精确控制。

赋予缺少的权限:

JINGYU3@jyzhao1> grant select on t_jingyu3 to jingyu2;

Grant succeeded.

SYS@jyzhao1>grant create synonym to jingyu2;

Grant succeeded.    

此时赋予完权限后再次尝试, 发现已经可以正常满足需求。

JINGYU2@jyzhao1>select count(1) from jingyu3.t_jingyu3;

  COUNT(1)
----------
         1

JINGYU2@jyzhao1>create synonym t_jingyu3 for jingyu3.t_jingyu3;

Synonym created.

JINGYU2@jyzhao1>select count(1) from t_jingyu3;   

  COUNT(1)
----------
         1

查看此时的 session 权限/角色:

JINGYU2@jyzhao1>select * from session_privs;

PRIVILEGE
----------------------------------------
CREATE SESSION
UNLIMITED TABLESPACE
CREATE TABLE
CREATE CLUSTER
CREATE SYNONYM
CREATE SEQUENCE
CREATE PROCEDURE
CREATE TRIGGER
CREATE TYPE
CREATE OPERATOR
CREATE INDEXTYPE

11 rows selected.

JINGYU2@jyzhao1>select * from session_roles;

ROLE
------------------------------
CONNECT
RESOURCE

发现用户会话权限只多了一个 CREATE SYNONYM 权限,没有增加额外的风险。

解决方案二:赋予 dba 角色(不推荐)。
方案宗旨:为了操作简便,直接统一赋予 DBA 角色,满足一切应用潜在权限要求。

实际在很多应用场景中,尤其是开发测试环境,DBA 或是开发人员往往会为了方便直接赋予高权限的 dba 角色,避免麻烦。当然这是不推荐的方法。
但也是一种解决方案,下面是演示实验,且在过程中还发现了一些有意思的细节。

首先回收方案一的权限赋予:

SYS@jyzhao1>revoke create synonym from jingyu2;

Revoke succeeded.

JINGYU3@jyzhao1>revoke select on t_jingyu3 from jingyu2;

Revoke succeeded.

这样又恢复了初始环境.

然后我们尝试直接赋予 jingyu2 用户高大上的 dba 角色,理应一切 ok 了吧。

SYS@jyzhao1>grant dba to jingyu2;

Grant succeeded.

结果让人大跌眼镜,不可以!

JINGYU2@jyzhao1>select count(1) from jingyu3.t_jingyu3;
select count(1) from jingyu3.t_jingyu3
                             *
ERROR at line 1:
ORA-00942: table or view does not exist


JINGYU2@jyzhao1>create synonym t_jingyu3 for jingyu3.t_jingyu3;
create synonym t_jingyu3 for jingyu3.t_jingyu3
*
ERROR at line 1:
ORA-01031: insufficient privileges

至高无上的 dba 权限居然解决不了跨用户访问和创建同义词?
没搞错吧?
看到这里,你是怎么想的呢?

如果这时候你去尝试重新连接一个会话,会发现是可以成功实现需求的?
那么这样看来,oracle 对于权限的赋予是立即生效的,但是角色却需要重新连接会话才会生效。
到这里还不甘心?
来看看旧的会话权限:

JINGYU2@jyzhao1>select * from session_roles;

ROLE
------------------------------
CONNECT
RESOURCE

发现的确没有变化,那么有没有命令可以让旧的会话不重新连接就能够生效呢?
答案是肯定的,来看看 set role all 这条命令吧,可以在历史会话中执行,从而使得新赋予的角色生效。

JINGYU2@jyzhao1>set role all;

Role set.

JINGYU2@jyzhao1>select * from session_roles;

ROLE
------------------------------
CONNECT
RESOURCE
DBA
SELECT_CATALOG_ROLE
HS_ADMIN_SELECT_ROLE
EXECUTE_CATALOG_ROLE
HS_ADMIN_EXECUTE_ROLE
DELETE_CATALOG_ROLE
EXP_FULL_DATABASE
IMP_FULL_DATABASE
DATAPUMP_EXP_FULL_DATABASE
DATAPUMP_IMP_FULL_DATABASE
GATHER_SYSTEM_STATISTICS
SCHEDULER_ADMIN
WM_ADMIN_ROLE
JAVA_ADMIN
JAVA_DEPLOY
XDBADMIN
XDB_SET_INVOKER
OLAP_XS_ADMIN
OLAP_DBA

21 rows selected.

另外,可以看到只赋予了 DBA 的角色,却额外包含了很多角色,如果此时查下权限会发现:

JINGYU2@jyzhao1>select * from session_privs;

PRIVILEGE
----------------------------------------
ALTER SYSTEM
AUDIT SYSTEM
CREATE SESSION
ALTER SESSION
RESTRICTED SESSION
CREATE TABLESPACE
ALTER TABLESPACE
MANAGE TABLESPACE
DROP TABLESPACE
UNLIMITED TABLESPACE
CREATE USER
BECOME USER
ALTER USER
DROP USER
CREATE ROLLBACK SEGMENT
ALTER ROLLBACK SEGMENT
DROP ROLLBACK SEGMENT
CREATE TABLE
CREATE ANY TABLE
ALTER ANY TABLE
BACKUP ANY TABLE
DROP ANY TABLE
LOCK ANY TABLE
COMMENT ANY TABLE
SELECT ANY TABLE
INSERT ANY TABLE
UPDATE ANY TABLE
DELETE ANY TABLE
CREATE CLUSTER
CREATE ANY CLUSTER
ALTER ANY CLUSTER
DROP ANY CLUSTER
CREATE ANY INDEX
ALTER ANY INDEX
DROP ANY INDEX
CREATE SYNONYM
CREATE ANY SYNONYM
DROP ANY SYNONYM
CREATE PUBLIC SYNONYM
DROP PUBLIC SYNONYM
CREATE VIEW
CREATE ANY VIEW
DROP ANY VIEW
CREATE SEQUENCE
CREATE ANY SEQUENCE
ALTER ANY SEQUENCE
DROP ANY SEQUENCE

PRIVILEGE
----------------------------------------
SELECT ANY SEQUENCE
CREATE DATABASE LINK
CREATE PUBLIC DATABASE LINK
DROP PUBLIC DATABASE LINK
CREATE ROLE
DROP ANY ROLE
GRANT ANY ROLE
ALTER ANY ROLE
AUDIT ANY
ALTER DATABASE
FORCE TRANSACTION
FORCE ANY TRANSACTION
CREATE PROCEDURE
CREATE ANY PROCEDURE
ALTER ANY PROCEDURE
DROP ANY PROCEDURE
EXECUTE ANY PROCEDURE
CREATE TRIGGER
CREATE ANY TRIGGER
ALTER ANY TRIGGER
DROP ANY TRIGGER
CREATE PROFILE
ALTER PROFILE
DROP PROFILE
ALTER RESOURCE COST
ANALYZE ANY
GRANT ANY PRIVILEGE
CREATE MATERIALIZED VIEW
CREATE ANY MATERIALIZED VIEW
ALTER ANY MATERIALIZED VIEW
DROP ANY MATERIALIZED VIEW
CREATE ANY DIRECTORY
DROP ANY DIRECTORY
CREATE TYPE
CREATE ANY TYPE
ALTER ANY TYPE
DROP ANY TYPE
EXECUTE ANY TYPE
UNDER ANY TYPE
CREATE LIBRARY
CREATE ANY LIBRARY
ALTER ANY LIBRARY
DROP ANY LIBRARY
EXECUTE ANY LIBRARY
CREATE OPERATOR
CREATE ANY OPERATOR
ALTER ANY OPERATOR

PRIVILEGE
----------------------------------------
DROP ANY OPERATOR
EXECUTE ANY OPERATOR
CREATE INDEXTYPE
CREATE ANY INDEXTYPE
ALTER ANY INDEXTYPE
DROP ANY INDEXTYPE
UNDER ANY VIEW
QUERY REWRITE
GLOBAL QUERY REWRITE
EXECUTE ANY INDEXTYPE
UNDER ANY TABLE
CREATE DIMENSION
CREATE ANY DIMENSION
ALTER ANY DIMENSION
DROP ANY DIMENSION
MANAGE ANY QUEUE
ENQUEUE ANY QUEUE
DEQUEUE ANY QUEUE
CREATE ANY CONTEXT
DROP ANY CONTEXT
CREATE ANY OUTLINE
ALTER ANY OUTLINE
DROP ANY OUTLINE
ADMINISTER RESOURCE MANAGER
ADMINISTER DATABASE TRIGGER
MERGE ANY VIEW
ON COMMIT REFRESH
RESUMABLE
SELECT ANY DICTIONARY
DEBUG CONNECT SESSION
DEBUG ANY PROCEDURE
FLASHBACK ANY TABLE
GRANT ANY OBJECT PRIVILEGE
CREATE EVALUATION CONTEXT
CREATE ANY EVALUATION CONTEXT
ALTER ANY EVALUATION CONTEXT
DROP ANY EVALUATION CONTEXT
EXECUTE ANY EVALUATION CONTEXT
CREATE RULE SET
CREATE ANY RULE SET
ALTER ANY RULE SET
DROP ANY RULE SET
EXECUTE ANY RULE SET
EXPORT FULL DATABASE
IMPORT FULL DATABASE
CREATE RULE
CREATE ANY RULE

PRIVILEGE
----------------------------------------
ALTER ANY RULE
DROP ANY RULE
EXECUTE ANY RULE
ANALYZE ANY DICTIONARY
ADVISOR
CREATE JOB
CREATE ANY JOB
EXECUTE ANY PROGRAM
EXECUTE ANY CLASS
MANAGE SCHEDULER
SELECT ANY TRANSACTION
DROP ANY SQL PROFILE
ALTER ANY SQL PROFILE
ADMINISTER SQL TUNING SET
ADMINISTER ANY SQL TUNING SET
CREATE ANY SQL PROFILE
MANAGE FILE GROUP
MANAGE ANY FILE GROUP
READ ANY FILE GROUP
CHANGE NOTIFICATION
CREATE EXTERNAL JOB
CREATE ANY EDITION
DROP ANY EDITION
ALTER ANY EDITION
CREATE ASSEMBLY
CREATE ANY ASSEMBLY
ALTER ANY ASSEMBLY
DROP ANY ASSEMBLY
EXECUTE ANY ASSEMBLY
EXECUTE ASSEMBLY
CREATE MINING MODEL
CREATE ANY MINING MODEL
DROP ANY MINING MODEL
SELECT ANY MINING MODEL
ALTER ANY MINING MODEL
COMMENT ANY MINING MODEL
CREATE CUBE DIMENSION
ALTER ANY CUBE DIMENSION
CREATE ANY CUBE DIMENSION
DELETE ANY CUBE DIMENSION
DROP ANY CUBE DIMENSION
INSERT ANY CUBE DIMENSION
SELECT ANY CUBE DIMENSION
CREATE CUBE
ALTER ANY CUBE
CREATE ANY CUBE
DROP ANY CUBE

PRIVILEGE
----------------------------------------
SELECT ANY CUBE
UPDATE ANY CUBE
CREATE MEASURE FOLDER
CREATE ANY MEASURE FOLDER
DELETE ANY MEASURE FOLDER
DROP ANY MEASURE FOLDER
INSERT ANY MEASURE FOLDER
CREATE CUBE BUILD PROCESS
CREATE ANY CUBE BUILD PROCESS
DROP ANY CUBE BUILD PROCESS
UPDATE ANY CUBE BUILD PROCESS
UPDATE ANY CUBE DIMENSION
ADMINISTER SQL MANAGEMENT OBJECT
FLASHBACK ARCHIVE ADMINISTER

202 rows selected.

很可怕吧,赋予 DBA 角色后居然让会话的权限从原来的 10 个变成了 202 个,这也是为什么不建议赋予 DBA 角色的原因。因为这对于数据库来说,普通应用用户的权限这么高,安全隐患太大了。

总结:

  • 1. 赋予权限时,无论是是否是之前连接的会话,都立即生效;赋予角色时,新连接会话生效,历史连接会话如果无法重新连接,就需要使用 set role all 才可以生效。
  • 2. 对于数据库的应用用户而言,建议最好可以严格控制角色/权限。
    一般来说,对于应用而言,connect 和 resource 角色已经可以满足大部分应用开发的需求,若有其他特殊需求,建议单独授予,强烈不建议直接赋予具有 N 多权限的 DBA 角色。

更多 Oracle 相关信息见 Oracle 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=12

本文永久更新链接地址 :http://www.linuxidc.com/Linux/2017-12/149982.htm

正文完
星哥玩云-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-22发表,共计7627字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中