我正在使用AWS Aurora Postgres 14(14.5),并设置了此表(在铁 rust 中的柴油的帮助下)->;
create table contacts (
email TEXT NOT NULL,
user_id TEXT NOT NULL,
contact_data JSONB,
user_groups TEXT[],
tenant_groups TEXT[],
tags TEXT[],
PRIMARY KEY (email, user_id)
);
使用政策->;
ALTER TABLE contacts FORCE ROW LEVEL SECURITY;
CREATE POLICY select_contacts_policy ON contacts
FOR SELECT
USING (user_id = current_setting('myapp.user_id'));
ALTER TABLE contacts ENABLE ROW LEVEL SECURITY;
CREATE POLICY insert_contacts_policy ON contacts
FOR INSERT
WITH CHECK (true);
ALTER TABLE contacts ENABLE ROW LEVEL SECURITY;
在向该表插入新数据时,我首先设置了一个名为user_id
(字符串)的会话参数(使用迪塞尔的sql_query函数)->;
sql_query(format!("SET SESSION myapp.user_id = '{user_id}';")).execute(pg_conn)?;
然后我插入使用柴油->;
diesel::insert_into(contacts::table)
.values(&contacts) # a custom struct
.on_conflict((contacts::email, contacts::user_id))
.do_update()
.set(&contacts)
.execute(pg_conn)?;
在此查询之后,user_id
将恢复为默认的->;
sql_query("SET SESSION myapp.user_id = -1;").execute(pg_conn)?;
DIESEL函数调用转换为以下SQL查询(从Postgres日志(log)中获取)->;
INSERT INTO "contacts" ("email", "user_id", "contact_data", "user_groups", "tenant_groups", "tags") VALUES ($1, $2, $3, $4, $5, $6) ON CONFLICT ("email", "user_id") DO UPDATE SET "email" = $7, "user_id" = $8, "contact_data" = $9, "user_groups" = $10, "tenant_groups" = $11, "tags" = $12
在运行此查询时,当发生冲突并触发更新分支时,我会收到以下错误消息->;
new row violates row-level security policy (USING expression) for table "contacts"
个
我在本地有一个有点类似的postgres安装(没有多个用户,但是有相同的模式)(v14.8),它能够毫无问题地升级.
其他信息->;
在此操作的用户是另一个名为backend
User的用户
访问权限->;Snapshot of access privileges for the contacts table
我试图运行一个EXPLAIN ANALYZE
查询并将其记录下来,以查看错误发生在哪里,但该查询在完成之前就导致了违反策略.如果我在没有ANALYZE
标志的情况下运行,它只会打印查询计划(但我想知道在哪里以及是什么导致了该违规)