环境: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角色。