小言_互联网的博客

postgresql影子用户实践

275人阅读  评论(0)

作者:瀚高PG实验室 (Highgo PG Lab)- 禹晓

        在实际的生产环境 ,我们经常会碰到这样的情况:因为业务场景需要,本部门某些重要的业务数据表需要给予其他部门查看权限,因业务的扩展及调整,后期可能需要放开更多的表查询权限。为解决此种业务需求,我们可以采用创建视图的方式来解决,已可以通过创建影子用户的方式来满足需求,本文主要介绍影子用户的创建及授权方法。

场景1:只授予usage on schema 权限

session 1:
--创建readonly用户,并将test模式赋予readonly用户。


  
  1. postgres= # create user readonly with password 'postgres';
  2. CREATE ROLE
  3. postgres= # grant usage on schema test to readonly;
  4. GRANT
  5. postgres= # \dn
  6. List of schemas
  7. Name | Owner
  8. -------+-------
  9. test | postgres

session 2:

--登陆readonly用户可以查询test模式下现存的所有表。


  
  1. postgres=# \c postgres readonly
  2. You are now connected to database "postgres" as user "readonly" .
  3. postgres=> select * from test.emp ;
  4. empno | ename | job | mgr | hiredate | sal | comm | deptno
  5. -------+--------+-----------+------+------------+---------+---------+--------
  6. 7499 | ALLEN | SALESMAN | 7698 | 1981-02-20 | 1600.00 | 300.00 | 30
  7. 7521 | WARD | SALESMAN | 7698 | 1981-02-22 | 1250.00 | 500.00 | 30
  8. 7566 | JONES | MANAGER | 7839 | 1981-04-02 | 2975.00 | | 20
  9. 7654 | MARTIN | SALESMAN | 7698 | 1981-09-28 | 1250.00 | 1400.00 | 30
  10. 7698 | BLAKE | MANAGER | 7839 | 1981-05-01 | 2850.00 | | 30
  11. 7782 | CLARK | MANAGER | 7839 | 1981-06-09 | 2450.00 | | 10
  12. 7839 | KING | PRESIDENT | | 1981-11-17 | 5000.00 | | 10
  13. 7844 | TURNER | SALESMAN | 7698 | 1981-09-08 | 1500.00 | 0.00 | 30
  14. 7900 | JAMES | CLERK | 7698 | 1981-12-03 | 950.00 | | 30
  15. 7902 | FORD | ANALYST | 7566 | 1981-12-03 | 3000.00 | | 20
  16. 7934 | MILLER | CLERK | 7782 | 1982-01-23 | 1300.00 | | 10
  17. 7788 | test | ANALYST | 7566 | 1982-12-09 | 3000.00 | | 20
  18. 7876 | ADAMS | CLERK | 7788 | 1983-01-12 | 1100.00 | | 20
  19. 1111 | SMITH | CLERK | 7902 | 1980-12-17 | 800.00 | | 20
  20. (14 rows)

换到session 1创建新表t1


  
  1. postgres= # create table test.t1 as select * from test.emp;
  2. CREATE TABLE

 切换到session 2 readonly用户下,t1表无法查询


  
  1. postgres=> select * from test.t 1 ;
  2. 2021- 03- 02 15: 25: 33. 290 CST [21059] ERROR: permission denied for table t1
  3. 2021-03-02 15:25:33.290 CST [21059] STATEMENT: select * from test.t1 ;
  4. **ERROR: permission denied for table t1

结论:如果只授予 usage on schema 权限,readonly 只能查看 test 模式下已经存在的表和对象。在授予 usage on schema 权限之后创建的新表无法查看。

 

场景2:授予usage on schema 权限之后,再赋予 select on all tables in schema 权限

针对上个场景session 2 **ERROR: permission denied for table t1 错误的处理


  
  1. postgres=> select * from test.t1 ;
  2. ** ERROR: permission denied for table t1

session 1: 使用postgres用户授予readonly用户 select on all tables 权限

postgres=# grant select on all tables in schema test TO readonly ;

session 2: readonly用户查询 t1 表


  
  1. postgres=> select * from test.t1;
  2. empno | ename | job | mgr | hiredate | sal | comm | deptno
  3. -------+--------+-----------+------+------------+---------+---------+--------
  4. 7499 | ALLEN | SALESMAN | 7698 | 1981-02-20 | 1600.00 | 300.00 | 30
  5. 7521 | WARD | SALESMAN | 7698 | 1981-02-22 | 1250.00 | 500.00 | 30
  6. 7566 | JONES | MANAGER | 7839 | 1981-04-02 | 2975.00 | | 20
  7. 7654 | MARTIN | SALESMAN | 7698 | 1981-09-28 | 1250.00 | 1400.00 | 30
  8. 7698 | BLAKE | MANAGER | 7839 | 1981-05-01 | 2850.00 | | 30
  9. 7782 | CLARK | MANAGER | 7839 | 1981-06-09 | 2450.00 | | 10
  10. 7839 | KING | PRESIDENT | | 1981-11-17 | 5000.00 | | 10
  11. 7844 | TURNER | SALESMAN | 7698 | 1981-09-08 | 1500.00 | 0.00 | 30
  12. 7900 | JAMES | CLERK | 7698 | 1981-12-03 | 950.00 | | 30
  13. 7902 | FORD | ANALYST | 7566 | 1981-12-03 | 3000.00 | | 20
  14. 7934 | MILLER | CLERK | 7782 | 1982-01-23 | 1300.00 | | 10
  15. 7788 | test | ANALYST | 7566 | 1982-12-09 | 3000.00 | | 20
  16. 7876 | ADAMS | CLERK | 7788 | 1983-01-12 | 1100.00 | | 20
  17. 1111 | SMITH | CLERK | 7902 | 1980-12-17 | 800.00 | | 20
  18. (14 rows)

session1 :postgres用户的test模式下创建新表 t2


  
  1. postgres=# create table test.t2 as select * from test.emp;
  2. SELECT 14

session 2:readonly用户查询 t2 表权限不足


  
  1. postgres=> select * from test.t2 ;
  2. ERROR: permission denied for table t2

session 1:再次赋予 grant select on all tables

postgres=# grant select on all tables in schema test TO readonly ;

session 2:readonly用户又可以查看 T2 表


  
  1. postgres=> select * from test.t2 ;
  2. empno | ename | job | mgr | hiredate | sal | comm | deptno
  3. -------+--------+-----------+------+------------+---------+---------+--------
  4. 7499 | ALLEN | SALESMAN | 7698 | 1981-02-20 | 1600.00 | 300.00 | 30
  5. 7521 | WARD | SALESMAN | 7698 | 1981-02-22 | 1250.00 | 500.00 | 30
  6. 7566 | JONES | MANAGER | 7839 | 1981-04-02 | 2975.00 | | 20
  7. 7654 | MARTIN | SALESMAN | 7698 | 1981-09-28 | 1250.00 | 1400.00 | 30
  8. 7698 | BLAKE | MANAGER | 7839 | 1981-05-01 | 2850.00 | | 30
  9. 7782 | CLARK | MANAGER | 7839 | 1981-06-09 | 2450.00 | | 10
  10. 7839 | KING | PRESIDENT | | 1981-11-17 | 5000.00 | | 10
  11. 7844 | TURNER | SALESMAN | 7698 | 1981-09-08 | 1500.00 | 0.00 | 30
  12. 7900 | JAMES | CLERK | 7698 | 1981-12-03 | 950.00 | | 30
  13. 7902 | FORD | ANALYST | 7566 | 1981-12-03 | 3000.00 | | 20
  14. 7934 | MILLER | CLERK | 7782 | 1982-01-23 | 1300.00 | | 10
  15. 7788 | test | ANALYST | 7566 | 1982-12-09 | 3000.00 | | 20
  16. 7876 | ADAMS | CLERK | 7788 | 1983-01-12 | 1100.00 | | 20
  17. 1111 | SMITH | CLERK | 7902 | 1980-12-17 | 800.00 | | 20
  18. (14 rows)

 

影子用户创建

如果想让readonly只读用户不在每次  postgres用户在test模式中创建新表后都要手工赋予 grant select on all tables in schema test TO readonly  权限。则需要授予对test默认的访问权限,对于test模式新创建的也生效。

session 1:未来访问test模式下所有新建的表赋权,创建 t5 表。


  
  1. postgres= # alter default privileges in schema test grant select on tables to readonly ;
  2. ALTER DEFAULT PRIVILEGES
  3. postgres= # create table test.t5 as select * from test.emp;
  4. CREATE TABLE

session 2:查询readonly用户


  
  1. postgres=> select * from test.t5;
  2. empno | ename | job | mgr | hiredate | sal | comm | deptno
  3. -------+--------+-----------+------+------------+---------+---------+--------
  4. 7499 | ALLEN | SALESMAN | 7698 | 1981-02-20 | 1600.00 | 300.00 | 30
  5. 7521 | WARD | SALESMAN | 7698 | 1981-02-22 | 1250.00 | 500.00 | 30
  6. 7566 | JONES | MANAGER | 7839 | 1981-04-02 | 2975.00 | | 20
  7. 7654 | MARTIN | SALESMAN | 7698 | 1981-09-28 | 1250.00 | 1400.00 | 30
  8. 7698 | BLAKE | MANAGER | 7839 | 1981-05-01 | 2850.00 | | 30
  9. 7782 | CLARK | MANAGER | 7839 | 1981-06-09 | 2450.00 | | 10
  10. 7839 | KING | PRESIDENT | | 1981-11-17 | 5000.00 | | 10
  11. 7844 | TURNER | SALESMAN | 7698 | 1981-09-08 | 1500.00 | 0.00 | 30
  12. 7900 | JAMES | CLERK | 7698 | 1981-12-03 | 950.00 | | 30
  13. 7902 | FORD | ANALYST | 7566 | 1981-12-03 | 3000.00 | | 20
  14. 7934 | MILLER | CLERK | 7782 | 1982-01-23 | 1300.00 | | 10
  15. 7788 | test | ANALYST | 7566 | 1982-12-09 | 3000.00 | | 20
  16. 7876 | ADAMS | CLERK | 7788 | 1983-01-12 | 1100.00 | | 20
  17. 1111 | SMITH | CLERK | 7902 | 1980-12-17 | 800.00 | | 20
  18. (14 rows)

 

总结:影子用户创建的步骤


  
  1. --创建影子用户
  2. create user readonly with password 'postgres';
  3. --将schema中usage权限赋予给readonly用户,访问所有已存在的表
  4. grant usage on schema test to readonly;
  5. grant select on all tables in schema test to readonly;
  6. --未来访问test模式下所有新建的表
  7. alter default privileges in schema test grant select on tables to readonly ;

 


转载:https://blog.csdn.net/pg_hgdb/article/details/114313062
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场