從ORA-01950報錯聊起——令人困惑的Resource角色和隱含unlimited tablespace系統許可權

Hoegh發表於2016-10-26

相信大家一定對Resource 角色不會陌生,Resource 角色是授予開發人員的,能在自己的方案中建立表、序列、檢視等。很多DBA習慣在建立新使用者後直接賦予Connect和Resource 角色,這樣就可以在資料庫裡執行建立表等操作了。


最近在測試過程中發現一些奇怪的現象,有時候擁有Connect和Resource 角色的使用者會提示“ORA-01950: no privileges on tablespace 'USERS'”錯誤,也就是說沒有操作表空間的許可權,這是怎麼回事呢?

透過一系列的測試發現,unlimited tablespace是隱含在resource角色中的一個系統許可權,當使用者得到resource的角色時,unlimited tablespace系統許可權也隱式授權給使用者。但是需要注意的是,unlimited tablespace系統許可權只能授予使用者,不能被授予角色;也不會隨著resource角色被授予role而級聯授予給使用者。

首先,我們瞭解一下和unlimited tablespace系統許可權的一個概念QUOTA,然後透過若干測試來驗證以上結論。

關於QUOTA

對於一個新建的使用者,如果沒有分配給unlimited tablespace系統許可權的使用者,必須先給他們指定限額,之後他們才能在表空間中建立物件。

限額是指定標空間中允許的空間容量,預設的情況下,使用者在任何表空間中都是沒有限額的,可以使用以下三個選項來為使用者提供表空間限額:

A、無限制的:允許使用者最大限度的使用表空間中的可用空間

B、值:使用者可以使用的表空間,以千位元組或者兆位元組為單位。但是這並不能保證會為使用者保留該空間。

C、UNLIMITED TABLESPACE系統許可權:此係統許可權會覆蓋所有的單個表空間限額,並向使用者提供所有表空間(包括SYSTEM和SYSAUX)的無限制限額(注:授予resource角色的時候也會授予此許可權)

如果需要為一個使用者指定一個限額,可以有兩種方法:

1、在建立使用者的時候指定限額:

點選(此處)摺疊或開啟

  1. CREATE USER hoegh IDENTIFIED BY hoegh
  2. DEFAULT TABLESPACE users
  3. TEMPORARY TABLESPACE TEMP
  4. QUOTA 3M ON users;

2、在建立使用者完成之後,對使用者限額進行指定:

點選(此處)摺疊或開啟

  1. CREATE USER hoegh IDENTIFIED BY hoegh
  2. DEFAULT TABLESPACE TEST;
  3. ALTER USER hoegh QUOTA 3M ON users;

測試1 授予connect和resource角色

建立新使用者hoegh1,授予connect和resource角色,嘗試建表和插入操作,透過查詢user_sys_privs資料字典來驗證使用者的系統許可權。

點選(此處)摺疊或開啟

  1. SYS@HOEGH> create user hoegh1 identified by hoegh1;

  2. User created.

  3. SYS@HOEGH>
  4. SYS@HOEGH> grant connect,resource to hoegh1;

  5. Grant succeeded.

  6. SYS@HOEGH> conn hoegh1/hoegh1
  7. Connected.
  8. hoegh1@HOEGH>
  9. hoegh1@HOEGH> create table test(id number);

  10. Table created.

  11. hoegh1@HOEGH> insert into test values(1);

  12. 1 row created.

  13. hoegh1@HOEGH>
  14. hoegh1@HOEGH> select privilege from user_sys_privs;

  15. PRIVILEGE
  16. ----------------------------------------
  17. UNLIMITED TABLESPACE

  18. hoegh1@HOEGH> select username,GRANTED_ROLE,ADMIN_OPTION from user_role_privs;

  19. USERNAME GRANTED_ROLE ADM
  20. ------------------------------ ------------------------------ ---
  21. HOEGH1 CONNECT NO
  22. HOEGH1 RESOURCE NO

  23. hoegh1@HOEGH>
我們看到hoegh1使用者擁有了unlimited tablespace系統許可權,插入記錄成功。也就是說,當使用者hoegh1得到resource的角色時,unlimited tablespace系統許可權也隱式授權給使用者。


測試2 逐條授予resource角色包含的系統許可權

建立新使用者hoegh2,授予connect逐條授予resource角色包含的系統許可權嘗試建表和插入操作,透過查user_sys_privs資料字典來驗證使用者的系統許可權。

點選(此處)摺疊或開啟

  1. SYS@HOEGH> create user hoegh2 identified by hoegh2;

  2. User created.

  3. SYS@HOEGH> grant connect to hoegh2;

  4. Grant succeeded.

  5. SYS@HOEGH>
  6. SYS@HOEGH> select privilege from role_sys_privs
  7. where role='RESOURCE'; 2

  8. PRIVILEGE
  9. ----------------------------------------
  10. CREATE SEQUENCE
  11. CREATE TRIGGER
  12. CREATE CLUSTER
  13. CREATE PROCEDURE
  14. CREATE TYPE
  15. CREATE OPERATOR
  16. CREATE TABLE
  17. CREATE INDEXTYPE

  18. 8 rows selected.

  19. SYS@HOEGH>
  20. SYS@HOEGH> select 'grant '||PRIVILEGE||' to hoegh2;' from role_sys_privs
  21. where role='RESOURCE'; 2

  22. 'GRANT'||PRIVILEGE||'TOhoegh2;'
  23. -----------------------------------------------------
  24. grant CREATE SEQUENCE to hoegh2;
  25. grant CREATE TRIGGER to hoegh2;
  26. grant CREATE CLUSTER to hoegh2;
  27. grant CREATE PROCEDURE to hoegh2;
  28. grant CREATE TYPE to hoegh2;
  29. grant CREATE OPERATOR to hoegh2;
  30. grant CREATE TABLE to hoegh2;
  31. grant CREATE INDEXTYPE to hoegh2;

  32. 8 rows selected.

  33. SYS@HOEGH> grant CREATE SEQUENCE to hoegh2;
  34. grant CREATE TRIGGER to hoegh2;
  35. grant CREATE CLUSTER to hoegh2;
  36. grant CREATE PROCEDURE to hoegh2;
  37. grant CREATE TYPE to hoegh2;
  38. grant CREATE OPERATOR to hoegh2;
  39. grant CREATE TABLE to hoegh2;
  40. grant CREATE INDEXTYPE to hoegh2;

  41. Grant succeeded.

  42. SYS@HOEGH>
  43. Grant succeeded.

  44. SYS@HOEGH>
  45. Grant succeeded.

  46. SYS@HOEGH>
  47. Grant succeeded.

  48. SYS@HOEGH>
  49. Grant succeeded.

  50. SYS@HOEGH>
  51. Grant succeeded.

  52. SYS@HOEGH>
  53. Grant succeeded.

  54. SYS@HOEGH>
  55. Grant succeeded.

  56. SYS@HOEGH>
  57. SYS@HOEGH>
  58. SYS@HOEGH> conn hoegh2/hoegh2
  59. Connected.
  60. hoegh2@HOEGH> create table test(id number);

  61. Table created.

  62. hoegh2@HOEGH> insert into test values(1);
  63. insert into test values(1)
  64.             *
  65. ERROR at line 1:
  66. ORA-01950: no privileges on tablespace 'USERS'


  67. hoegh2@HOEGH>
  68. hoegh2@HOEGH>
  69. hoegh2@HOEGH> select privilege from user_sys_privs;

  70. PRIVILEGE
  71. ----------------------------------------
  72. CREATE TABLE
  73. CREATE CLUSTER
  74. CREATE TYPE
  75. CREATE TRIGGER
  76. CREATE PROCEDURE
  77. CREATE OPERATOR
  78. CREATE INDEXTYPE
  79. CREATE SEQUENCE

  80. 8 rows selected.

  81. hoegh2@HOEGH> select username,GRANTED_ROLE,ADMIN_OPTION from user_role_privs;

  82. USERNAME GRANTED_ROLE ADM
  83. ------------------------------ ------------------------------ ---
  84. HOEGH2 CONNECT NO

  85. hoegh2@HOEGH>
  86. hoegh2@HOEGH>
我們看到hoegh2使用者雖然擁有了resource角色下的所有系統許可權,但是卻沒有unlimited tablespace系統許可權,插入記錄失敗。

測試3 將connect和resource角色授予新的角色

建立角色hoegh,並將connect和resource角色授予這個角色;然後建立新使用者hoegh3,將hoegh角色授予使用者hoegh3,嘗試建表和插入操作。

點選(此處)摺疊或開啟

  1. SYS@HOEGH>
  2. SYS@HOEGH> create user hoegh3 identified by hoegh3;

  3. User created.

  4. SYS@HOEGH>
  5. SYS@HOEGH> create role hoegh;

  6. Role created.

  7. SYS@HOEGH> grant connect,resource to hoegh;

  8. Grant succeeded.

  9. SYS@HOEGH> grant hoegh to hoegh3;

  10. Grant succeeded.

  11. SYS@HOEGH>
  12. SYS@HOEGH> conn hoegh3/hoegh3
  13. Connected.
  14. hoegh3@HOEGH>
  15. hoegh3@HOEGH> create table test(id number);

  16. Table created.

  17. hoegh3@HOEGH> insert into test values(1);
  18. insert into test values(1)
  19.             *
  20. ERROR at line 1:
  21. ORA-01950: no privileges on tablespace 'USERS'


  22. hoegh3@HOEGH>
  23. hoegh3@HOEGH> select privilege from user_sys_privs;

  24. no rows selected

  25. hoegh3@HOEGH>
  26. hoegh3@HOEGH> select username,GRANTED_ROLE,ADMIN_OPTION from user_role_privs;

  27. USERNAME GRANTED_ROLE ADM
  28. ------------------------------ ------------------------------ ---
  29. HOEGH3 HOEGH NO

  30. hoegh3@HOEGH>
  31. hoegh3@HOEGH>
我們看到hoegh3使用者雖然擁有了hoegh角色,並且hoegh角色包含了connect角色resource角色,但是hoegh3使用者並沒有unlimited tablespace系統許可權,插入記錄失敗。也就是說,unlimited tablespace系統許可權不會隨著resource角色被授予hoegh角色而級聯授予給使用者hoegh3。

測試4 直接授予使用者unlimited tablespace系統許可權

建立新使用者hoegh4,授予connect角色後,直接授予create table和unlimited tablespace系統許可權,嘗試建表和插入操作。

點選(此處)摺疊或開啟

  1. SYS@HOEGH> create user hoegh4 identified by hoegh4;

  2. User created.

  3. SYS@HOEGH> grant connect to hoegh4;

  4. Grant succeeded.

  5. SYS@HOEGH> grant create table to hoegh4;

  6. Grant succeeded.

  7. SYS@HOEGH> grant unlimited tablespace to hoegh4;

  8. Grant succeeded.

  9. SYS@HOEGH>
  10. SYS@HOEGH> conn hoegh4/hoegh4
  11. Connected.
  12. hoegh4@HOEGH>
  13. hoegh4@HOEGH> create table test(id number);

  14. Table created.

  15. hoegh4@HOEGH> insert into test values(1);

  16. 1 row created.

  17. hoegh4@HOEGH>
  18. hoegh4@HOEGH> select privilege from user_sys_privs;

  19. PRIVILEGE
  20. ----------------------------------------
  21. CREATE TABLE
  22. UNLIMITED TABLESPACE

  23. hoegh4@HOEGH> select username,GRANTED_ROLE,ADMIN_OPTION from user_role_privs;

  24. USERNAME GRANTED_ROLE ADM
  25. ------------------------------ ------------------------------ ---
  26. HOEGH4 CONNECT NO

  27. hoegh4@HOEGH>
  28. hoegh4@HOEGH>
我們看到hoegh4使用者擁有了unlimited tablespace系統許可權,插入記錄成功。



                                                                                            ~~~~~~~ the end~~~~~~~~~
                                                                                                                                                                                                               hoegh
                                                                                                                                                                                                           2016.10.26

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/30162081/viewspace-2127149/,如需轉載,請註明出處,否則將追究法律責任。

相關文章