Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CN/modules/ROOT/nav.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@
*** xref:master/ecosystem_components/set_user.adoc[set_user]
*** xref:master/ecosystem_components/pg_jieba.adoc[pg_jieba]
*** xref:master/ecosystem_components/pg_track_settings.adoc[pg_track_settings]
*** xref:master/ecosystem_components/postgresql_anonymizer.adoc[postgresql_anonymizer]
* 监控运维
** xref:master/getting-started/daily_monitoring.adoc[日常监控]
** xref:master/getting-started/daily_maintenance.adoc[日常维护]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ IvorySQL 作为一款兼容 Oracle 且基于 PostgreSQL 的高级开源数据库
| 30 | xref:master/ecosystem_components/set_user.adoc[set_user] | REL4_2_0 | PostgreSQL 安全审计扩展,可控角色切换,支持白名单、强制审计、拦截高危操作 | 可控角色切换、权限管理、审计日志
| 31 | xref:master/ecosystem_components/pg_jieba.adoc[pg_jieba] | master branch | 中文分词扩展,能够将连续的中文文本切分成有意义的词语序列,为中文全文搜索提供基础支持 | 全文搜索、自然语言处理
| 32 | xref:master/ecosystem_components/pg_track_settings.adoc[pg_track_settings] | 2.1.2 | PostgreSQL 配置跟踪扩展,记录配置变更历史 | 配置审计、变更追踪
| 33 | xref:master/ecosystem_components/postgresql_anonymizer.adoc[postgresql_anonymizer] | 3.2.3 | 对敏感数据进行动态或静态脱敏与匿名化 | 保障数据安全、隐私合规
|====

这些插件均经过 IvorySQL 团队的测试和适配,确保在 IvorySQL 环境下稳定运行。用户可以根据业务需求选择合适的插件,进一步提升数据库系统的能力和灵活性。
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@

:sectnums:
:sectnumlevels: 5

= postgresql_anonymizer

== 概述
postgresql_anonymizer 是一款 PostgreSQL 扩展插件,用于在数据库层对敏感数据进行动态或静态脱敏与匿名化,从而在保障数据安全的同时满足 GDPR 等隐私合规要求。

== 安装
本文档提供源码安装步骤。

[TIP]
源码安装环境为 Ubuntu 24.04(x86_64),环境中已经安装了 IvorySQL5 或以上版本,安装路径为/usr/ivory-5。

=== 源码安装
从 https://gitlab.com/dalibo/postgresql_anonymizer/-/archive/3.2.3/postgresql_anonymizer-3.2.3.tar.gz 下载 postgresql_anonymizer v3.2.3 源代码。

** 安装 rust 和 pgrx
[literal]
----
# 安装 rust
sudo curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

最新版本的 postgresql_anonymizer-3.2.3 要求 cargo-pgrx 的版本 0.19.1。

首先升级rustc
rustup update stable

安装0.19.1的 cargo-pgrx
cargo install cargo-pgrx --version 0.19.1 --locked

cargo pgrx init --pg18 /usr/ivory-5/bin/pg_config
----

** 编译安装 postgresql_anonymizer
[literal]
----
# 源代码解压缩后进入其目录
cd postgresql_anonymizer-3.2.3

# 编译代码
PGRX_PG_CONFIG_PATH=/usr/ivory-5/bin/pg_config make extension PG_CONFIG=/usr/ivory-5/bin/pg_config PGVER=pg18

# 安装插件
sudo make install PG_CONFIG=/usr/ivory-5/bin/pg_config PGVER=pg18
----

[TIP]
如果出现找不到xlocale.h的错误,需要手动修改 /usr/ivory-5/include/postgresql/server/pg_config.h
删除或者注释掉 #define HAVE_XLOCALE_H 1 这一行

== 创建插件
psql 连接到数据库,执行如下命令:
[literal]
----
ivorysql=# CREATE EXTENSION anon;
CREATE EXTENSION

ivorysql=# SELECT anon.init(); -- anon.init() loads the dataset required by the fake_* faking functions.

打开透明动态脱敏开关
ALTER DATABASE ivorysql SET anon.transparent_dynamic_masking = on;

修改pg配置文件,把 anon 加入到 shared_preload_libraries
shared_preload_libraries = 'anon';

重启数据库
----

== 使用
[literal]
----
-- 首先创建一张含敏感字段的测试表
CREATE TABLE people AS
SELECT 153478 AS id,
'Sarah' AS firstname,
'Conor' AS lastname,
'0609110911' AS phone;

-- 声明一个需要被脱敏的用户
CREATE ROLE skynet LOGIN;
SECURITY LABEL FOR anon ON ROLE skynet IS 'MASKED';
GRANT pg_read_all_data TO skynet;

-- 给字段挂脱敏规则
SECURITY LABEL FOR anon ON COLUMN people.lastname
IS 'MASKED WITH FUNCTION anon.dummy_last_name()';
SECURITY LABEL FOR anon ON COLUMN people.phone
IS 'MASKED WITH FUNCTION anon.partial(phone, 2, $$******$$, 2)';

验证动态脱敏(换一个被标记为 MASKED 的角色登录去查,结果应被自动打码):
\connect - skynet
SELECT * FROM people; -- 期望 lastname 变成随机假姓、phone 变成 06******11

id | firstname | lastname | phone
--------+-----------+----------+------------
153478 | Sarah | Gaylord | 06******11
(1 row)
----

[TIP]
在IvorySQL的兼容模式下,postgresql_anonymizer 的部分语句可能存在兼容问题

例如:
SECURITY LABEL FOR anon ON COLUMN people.phone
IS 'MASKED WITH FUNCTION anon.partial(phone, 2, $$******$$, 2)';

ERROR: Anon: `MASKED WITH FUNCTION anon.partial(phone, 2, $$******$$, 2)` is not a valid label for a column
DETAIL: anon.partial(phone, 2, $$******$$, 2) is not qualified

此时可以通过临时设置兼容模式为 pg 来解决。
1 change: 1 addition & 0 deletions EN/modules/ROOT/nav.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@
*** xref:master/ecosystem_components/set_user.adoc[set_user]
*** xref:master/ecosystem_components/pg_jieba.adoc[pg_jieba]
*** xref:master/ecosystem_components/pg_track_settings.adoc[pg_track_settings]
*** xref:master/ecosystem_components/postgresql_anonymizer_en.adoc[postgresql_anonymizer]
* Monitor and O&M
** xref:master/getting-started/daily_monitoring.adoc[Monitoring]
** xref:master/getting-started/daily_maintenance.adoc[Maintenance]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ IvorySQL, as an advanced open-source database compatible with Oracle and based o
|*30*| xref:master/ecosystem_components/set_user.adoc[set_user] | REL4_2_0 | PostgreSQL security auditing extension with controlled role switching, supporting allowlists, enforced auditing, and blocking of high-risk operations | Controlled role switching, privilege management, audit logging
|*31*| xref:master/ecosystem_components/pg_jieba.adoc[pg_jieba] | master branch | Chinese word segmentation extension, which segments continuous Chinese text into meaningful word sequences, providing fundamental support for full-text search | Full-text search and natural language processing (NLP)
|*32*| xref:master/ecosystem_components/pg_track_settings.adoc[pg_track_settings] | 2.1.2 | PostgreSQL configuration tracking extension that records configuration change history | Configuration auditing, change tracking
|*33*| xref:master/ecosystem_components/postgresql_anonymizer_en.adoc[postgresql_anonymizer] | 3.2.3 | provides dynamic or static masking and anonymization of sensitive data | protecting data, meeting privacy compliance requirements
|====

These plugins have all been tested and adapted by the IvorySQL team to ensure stable operation in the IvorySQL environment. Users can select appropriate plugins based on business needs to further enhance the capabilities and flexibility of the database system.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
:sectnums:
:sectnumlevels: 5

= postgresql_anonymizer

== Overview
postgresql_anonymizer is a PostgreSQL extension that provides dynamic or static masking and anonymization of sensitive data at the database level, helping protect data while meeting privacy compliance requirements such as the GDPR.

== Installation
This document describes how to install the extension from source.

[TIP]
The source installation described here uses Ubuntu 24.04 (x86_64), with IvorySQL 5 or later already installed in /usr/ivory-5.

=== Installing from Source
Download the postgresql_anonymizer v3.2.3 source code from https://gitlab.com/dalibo/postgresql_anonymizer/-/archive/3.2.3/postgresql_anonymizer-3.2.3.tar.gz.

** Install Rust and pgrx
[literal]
----
# Install Rust
sudo curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

The latest version of postgresql_anonymizer 3.2.3 requires cargo-pgrx 0.19.1.

First, update rustc
rustup update stable

Install cargo-pgrx 0.19.1
cargo install cargo-pgrx --version 0.19.1 --locked

cargo pgrx init --pg18 /usr/ivory-5/bin/pg_config
----

** Build and install postgresql_anonymizer
[literal]
----
# Enter the source directory after extracting the archive
cd postgresql_anonymizer-3.2.3

# Build the source code
PGRX_PG_CONFIG_PATH=/usr/ivory-5/bin/pg_config make extension PG_CONFIG=/usr/ivory-5/bin/pg_config PGVER=pg18

# Install the extension
sudo make install PG_CONFIG=/usr/ivory-5/bin/pg_config PGVER=pg18
----

[TIP]
If an error reports that xlocale.h cannot be found, manually edit /usr/ivory-5/include/postgresql/server/pg_config.h and delete or comment out the following line: #define HAVE_XLOCALE_H 1

== Creating the Extension
Connect to the database with psql and run the following commands:
[literal]
----
ivorysql=# CREATE EXTENSION anon;
CREATE EXTENSION

ivorysql=# SELECT anon.init(); -- anon.init() loads the dataset required by the fake_* faking functions.

Enable transparent dynamic masking
ALTER DATABASE ivorysql SET anon.transparent_dynamic_masking = on;

Edit the PostgreSQL configuration file and add anon to shared_preload_libraries
shared_preload_libraries = 'anon';

Restart the database
----

== Usage
[literal]
----
-- First, create a test table containing sensitive columns
CREATE TABLE people AS
SELECT 153478 AS id,
'Sarah' AS firstname,
'Conor' AS lastname,
'0609110911' AS phone;

-- Declare a user whose data access must be masked
CREATE ROLE skynet LOGIN;
SECURITY LABEL FOR anon ON ROLE skynet IS 'MASKED';
GRANT pg_read_all_data TO skynet;

-- Apply masking rules to the columns
SECURITY LABEL FOR anon ON COLUMN people.lastname
IS 'MASKED WITH FUNCTION anon.dummy_last_name()';
SECURITY LABEL FOR anon ON COLUMN people.phone
IS 'MASKED WITH FUNCTION anon.partial(phone, 2, $$******$$, 2)';

Verify dynamic masking (connect as a role marked MASKED and query the table; the results should be masked automatically):
\connect - skynet
SELECT * FROM people; -- Expected: lastname is replaced with a random fake surname, and phone becomes 06******11

id | firstname | lastname | phone
--------+-----------+----------+------------
153478 | Sarah | Gaylord | 06******11
(1 row)
----

[TIP]
In IvorySQL compatibility mode, some postgresql_anonymizer statements may encounter compatibility issues.

For example:
SECURITY LABEL FOR anon ON COLUMN people.phone
IS 'MASKED WITH FUNCTION anon.partial(phone, 2, $$******$$, 2)';

ERROR: Anon: `MASKED WITH FUNCTION anon.partial(phone, 2, $$******$$, 2)` is not a valid label for a column
DETAIL: anon.partial(phone, 2, $$******$$, 2) is not qualified

In this case, temporarily setting the compatibility mode to pg resolves the issue.
Loading