当前位置:   article > 正文

PostgreSQL数据安全配置_pg create policy

pg create policy

行级访问限制

准备数据

  1. [postgres@localhost ~]$ cd resoruce/
  2. [postgres@localhost resoruce]$ vi RLSData.sql
  3. [postgres@localhost resoruce]$ psql -U pgadmin -d postgres -p 5666 < RLSData.sql
  1. create table passwd(
  2. user_name text unique not null
  3. ,pwhas text
  4. ,uid int primary key
  5. ,gid int not null
  6. ,rel_name text not null
  7. ,home_phone text
  8. ,extra_info text
  9. ,home_dir text not null
  10. ,shell text not null
  11. );
  12. insert into passwd values('appuser','xxx',0,0,'appuser','111-222-333',null,'/root','/bin/dash');
  13. insert into passwd values('appuser1','xxx',1,1,'appuser1','211-222-333',null,'/home/appuser1','/bin/zsh');
  14. insert into passwd values('appuser2','xxx',2,1,'appuser2','311-222-333',null,'/home/appuser2','/bin/zsh');
  15. create user appuser1 login password '1qaz@WSX';
  16. create user appuser2 login password '1qaz@WSX';
  17. grant select ,insert,update,delete on passwd to appuser1;
  18. grant select ,insert,update,delete on passwd to appuser2;
  19. grant select ,insert,update,delete on passwd to readonlyuser;

创建一个策略,使得用户只能访问属于自己的行

  1. postgres=# alter table passwd enable row level security ;
  2. postgres=# create policy appuser1_select_passwd on passwd for select to appuser1 using(current_user=user_name);
  1. [postgres@localhost resoruce]$ psql -U pgadmin -d postgres
  2. psql (12.0)
  3. Type "help" for help.
  4. postgres=# \d
  5. List of relations
  6. Schema | Name | Type | Owner
  7. --------+--------+-------+---------
  8. public | passwd | table | pgadmin
  9. (1 row)
  10. postgres=# alter table passwd enable row level security ;
  11. ALTER TABLE
  12. postgres=# create policy appuser1_select_passwd on passwd for select to appuser1 using(current_user=user_name);
  13. CREATE POLICY
  14. postgres=# \c postgres appuser1
  15. You are now connected to database "postgres" as user "appuser1".
  16. postgres=> select * from passwd;
  17. user_name | pwhas | uid | gid | rel_name | home_phone | extra_info | home_dir | shell
  18. -----------+-------+-----+-----+----------+-------------+------------+----------------+----------
  19. appuser1 | xxx | 1 | 1 | appuser1 | 211-222-333 | | /home/appuser1 | /bin/zsh
  20. (1 row)
  21. postgres=> \c postgres postgres
  22. You are now connected to database "postgres" as user "postgres".
  23. postgres=# select * from passwd;
  24. user_name | pwhas | uid | gid | rel_name | home_phone | extra_info | home_dir | shell
  25. -----------+-------+-----+-----+----------+-------------+------------+----------------+-----------
  26. appuser | xxx | 0 | 0 | appuser | 111-222-333 | | /root | /bin/dash
  27. appuser1 | xxx | 1 | 1 | appuser1 | 211-222-333 | | /home/appuser1 | /bin/zsh
  28. appuser2 | xxx | 2 | 1 | appuser2 | 311-222-333 | | /home/appuser2 | /bin/zsh
  29. (3 rows)

创建一个策略,使得用户可以向有行限制的表中插入数据

  1. postgres=# drop policy appuser1_insert_passwd on passwd;
  2. postgres=# create policy appuser1_insert_passwd on passwd for insert to appuser1 with check(true);

列级加密

创建加密扩展  这里需不需要编译 后面再验证一下 

  1. postgres=> \c postgres pgadmin
  2. postgres=# create extension pgcrypto ;

准备数据 使用crypt函数对数据加密

  1. create table test_user(
  2. id serial
  3. ,user_name text
  4. ,password text
  5. );
  6. insert into test_user(user_name,password) values('appuser1',crypt('123456',gen_salt('md5')));
  7. insert into test_user(user_name,password) values('appuser2',crypt('223456',gen_salt('md5')));

验证数据是否加密

  1. postgres=# select * from test_user;
  2. id | user_name | password
  3. ----+-----------+------------------------------------
  4. 1 | appuser1 | $1$YiUide3K$Z6z2pIGyi8HsB7q3IFK1c1
  5. 2 | appuser2 | $1$k0GXH0mZ$KzhzcTGsQAl3I/1FnCr4G0
  6. postgres=# select (password=crypt('123456',password)) from test_user;
  7. ?column?
  8. ----------
  9. t
  10. f
  11. postgres=# select * from test_user where password=crypt('123456',password);
  12. id | user_name | password
  13. ----+-----------+------------------------------------
  14. 1 | appuser1 | $1$YiUide3K$Z6z2pIGyi8HsB7q3IFK1c1

使用PG_AUDIT插件进行审计

下载地址 https://github.com/pgaudit/pgaudit

编译pg_audit模块

  1. [postgres@localhost contrib]$ cd ~/resoruce/
  2. [postgres@localhost resoruce]$ unzip pgaudit-REL_12_STABLE.zip
  3. [postgres@localhost resoruce]$ cp -r pgaudit-REL_12_STABLE ~/resoruce/postgresql-12.0/contrib/
  4. [postgres@localhost resoruce]$ cd ~/resoruce/postgresql-12.0/contrib/pgaudit-REL_12_STABLE/
  5. [postgres@localhost pgaudit-REL_12_STABLE]$ make
  6. [postgres@localhost pgaudit-REL_12_STABLE]$ make install
  7. [postgres@localhost pgaudit-REL_12_STABLE]$ cp pgaudit.so ~/soft/lib/

修改配置文件 添加预加载库

  1. [postgres@localhost pgaudit-REL_12_STABLE]$ vi $PGDATA/postgresql.conf
  2. --最后一行添加
  3. shared_preload_libraries = 'pgaudit'

重启数据库 创建audit扩展

  1. [postgres@localhost pgaudit-REL_12_STABLE]$ pg_ctl restart
  2. [postgres@localhost pgaudit-REL_12_STABLE]$ psql -U pgadmin -d postgres
  3. psql (12.0)
  4. Type "help" for help.
  5. postgres=# create extension pgaudit;

创建audit用户

  1. postgres=# create user pgaudituser with password '1qaz@WSX';
  2. CREATE ROLE
  3. postgres=# alter system set pgaudit.role = 'pgaudituser';
  4. ALTER SYSTEM
  5. postgres=# select pg_reload_conf();
  6. pg_reload_conf
  7. ----------------
  8. t
  9. (1 row)
  10. postgres=# show pgaudit.role
  11. ;
  12. pgaudit.role
  13. --------------
  14. pgaudituser
  15. (1 row)

将要审计的表给audit用户赋权

  1. postgres=# \c appdb appuser
  2. appdb=> grant select,delete on table appuser.app to pgaudituser;

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/1018883
推荐阅读
相关标签
  

闽ICP备14008679号