MogDB/openGauss 數據庫中關于用戶角色這部分有一個大家熟知的特性:三權分立,即從安全性考慮將系統管理的權限分離出安全管理員和審計管理員,這里暫時先不考慮三權分立這個特性,僅從user/role 開發運維層面進行整理。
MogDB數據庫版本:3.0.0
定義
角色是擁有數據庫對象和權限的實體。在不同的環境中角色可以認為是一個用戶,一個組或者兼顧兩者。
從創建用戶和角色的語義(create user/role)上看也沒有區別,唯一的區別就是用戶默認帶有login權限。官方文檔里也明確的指出 
查詢用戶相關信息可以查看視圖pg_user,查看角色相關信息可以查看pg_roles,但如果你查看pg_user和pg_roles的視圖定義,會發現這兩個視圖都來源于基表pg_authid。
我們可以認為在MogDB/openGauss里,用戶就是帶有login屬性的角色。
角色管理
私有用戶
角色的屬性有很多,可以通過\h create user/role來查看,也可以直接在pg_authid系統表中查看。
這里主要介紹一個比較重要的屬性:INDEPENDENT,即在非三權分立模式下,創建具有INDEPENDENT屬性的私有用戶,
針對該私有用戶的對象,系統管理員和擁有CREATEROLE屬性的安全管理員在未經其授權前,只能進行控制操作(DROP、ALTER、TRUNCATE),無權進行INSERT、DELETE、SELECT、UPDATE、COPY、GRANT、REVOKE、ALTER OWNER操作。
--創建私有用戶,不會立即創建同名schema,在創建對象的時候會創建
MogDB=# create user inu independent password 'inu@1234';
NOTICE: The encrypted password contains MD5 ciphertext, which is not secure.
WARNING: Please carefully use independent user as it need more self-management.
HINT: Self-management include logical backup, password manage and so on.
CREATE ROLE
MogDB=# select * from information_schema.schemata where schema_name='inu';
catalog_name | schema_name | schema_owner | default_character_set_catalog | default_character_set_schema | default_character_set_name | sql_path
--------------+-------------+--------------+-------------------------------+------------------------------+----------------------------+----------
(0 rows)
MogDB=# \c postgres inu
Password for user inu:
Non-SSL connection (SSL connection is recommended when requiring high-security)
You are now connected to database "postgres" as user "inu".
MogDB=> create table it(id int);
CREATE TABLE
MogDB=> select * from information_schema.schemata where schema_name='inu';
catalog_name | schema_name | schema_owner | default_character_set_catalog | default_character_set_schema | default_character_set_name | sql_path
--------------+-------------+--------------+-------------------------------+------------------------------+----------------------------+----------
postgres | inu | inu | | | |
(1 row)
--超級管理員用戶無法取消independent屬性,只有私有用戶自己有權限
MogDB=# alter user inu noindependent;
ERROR: Only user himself can remove his own independent attribute.
MogDB=# \c postgres inu
Password for user inu:
Non-SSL connection (SSL connection is recommended when requiring high-security)
You are now connected to database "postgres" as user "inu".
MogDB=> alter user inu noindependent;
ALTER ROLE
--超級管理員無法訪問私有用戶對象數據
MogDB=> insert into it values(1);
INSERT 0 1
MogDB=> select * from it;
id
----
1
(1 row)
MogDB=> \c postgres omm
Non-SSL connection (SSL connection is recommended when requiring high-security)
You are now connected to database "postgres" as user "omm".
MogDB=# select * from inu.it;
ERROR: permission denied for relation it
DETAIL: N/A
角色權限繼承
在角色級別授予或撤消權限時,這些更改將作用到角色下的所有成員。刪除角色時只刪除角色本身,不會刪除角色中的成員用戶。
MogDB提供了一個隱式定義的擁有所有角色的組PUBLIC,所有創建的用戶和角色默認擁有PUBLIC所擁有的權限。要撤銷或重新授予用戶和角色對PUBLIC的權限, 可通過在GRANT和REVOKE指定關鍵字PUBLIC實現。
添加/移除角色成員
--添加角色
grant role to user;
--刪除角色
revoke role from user;
賬號安全策略
失敗登錄次數:failed_login_attempts=10
密碼鎖定時間:password_lock_time=1(d)
手工鎖定:ALTER USER name ACCOUNT LOCK;
手工解鎖:ALTER USER name ACCOUNT unlock;
密碼安全策略
密碼加密方式:password_encryption_type=1
是否開啟密碼策略:password_policy=1
密碼重用時間:password_reuse_time
密碼重用次數:password_reuse_max
密碼有效期:password_effect_time
密碼到期提醒:password_notify_time




