Skip to content

Commit 4e5b85b

Browse files
committed
Add SECURITY LABEL provider to acceptance test postgres db.
1 parent a961e75 commit 4e5b85b

File tree

6 files changed

+95
-1
lines changed

6 files changed

+95
-1
lines changed

tests/build/Dockerfile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
FROM postgres:${PGVERSION:-latest}
2+
3+
RUN apt-get update && apt-get install -y build-essential postgresql-server-dev-all
4+
COPY dummy_seclabel /opt/dummy_seclabel
5+
WORKDIR /opt/dummy_seclabel
6+
RUN make
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# src/test/modules/dummy_seclabel/Makefile
2+
3+
MODULES = dummy_seclabel
4+
PGFILEDESC = "dummy_seclabel - regression testing of the SECURITY LABEL statement"
5+
6+
EXTENSION = dummy_seclabel
7+
DATA = dummy_seclabel--1.0.sql
8+
9+
REGRESS = dummy_seclabel
10+
11+
PG_CONFIG = pg_config
12+
PGXS := $(shell $(PG_CONFIG) --pgxs)
13+
include $(PGXS)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/* src/test/modules/dummy_seclabel/dummy_seclabel--1.0.sql */
2+
3+
-- complain if script is sourced in psql, rather than via CREATE EXTENSION
4+
\echo Use "CREATE EXTENSION dummy_seclabel" to load this file. \quit
5+
6+
CREATE FUNCTION dummy_seclabel_dummy()
7+
RETURNS pg_catalog.void
8+
AS 'MODULE_PATHNAME' LANGUAGE C;
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* dummy_seclabel.c
3+
*
4+
* Dummy security label provider.
5+
*
6+
* This module does not provide anything worthwhile from a security
7+
* perspective, but allows regression testing independent of platform-specific
8+
* features like SELinux.
9+
*
10+
* Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group
11+
* Portions Copyright (c) 1994, Regents of the University of California
12+
*/
13+
#include "postgres.h"
14+
15+
#include "commands/seclabel.h"
16+
#include "fmgr.h"
17+
#include "miscadmin.h"
18+
#include "utils/rel.h"
19+
20+
PG_MODULE_MAGIC;
21+
22+
PG_FUNCTION_INFO_V1(dummy_seclabel_dummy);
23+
24+
static void
25+
dummy_object_relabel(const ObjectAddress *object, const char *seclabel)
26+
{
27+
if (seclabel == NULL ||
28+
strcmp(seclabel, "unclassified") == 0 ||
29+
strcmp(seclabel, "classified") == 0)
30+
return;
31+
32+
if (strcmp(seclabel, "secret") == 0 ||
33+
strcmp(seclabel, "top secret") == 0)
34+
{
35+
if (!superuser())
36+
ereport(ERROR,
37+
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
38+
errmsg("only superuser can set '%s' label", seclabel)));
39+
return;
40+
}
41+
ereport(ERROR,
42+
(errcode(ERRCODE_INVALID_NAME),
43+
errmsg("'%s' is not a valid security label", seclabel)));
44+
}
45+
46+
void
47+
_PG_init(void)
48+
{
49+
register_label_provider("dummy", dummy_object_relabel);
50+
}
51+
52+
/*
53+
* This function is here just so that the extension is not completely empty
54+
* and the dynamic library is loaded when CREATE EXTENSION runs.
55+
*/
56+
Datum
57+
dummy_seclabel_dummy(PG_FUNCTION_ARGS)
58+
{
59+
PG_RETURN_VOID();
60+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
comment = 'Test code for SECURITY LABEL feature'
2+
default_version = '1.0'
3+
module_pathname = '$libdir/dummy_seclabel'
4+
relocatable = true

tests/docker-compose.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@ version: "3"
22

33
services:
44
postgres:
5-
image: postgres:${PGVERSION:-latest}
5+
build: build
6+
# image: postgres:${PGVERSION:-latest}
67
user: postgres
78
command:
89
- "postgres"
910
- "-c"
1011
- "wal_level=logical"
1112
- "-c"
1213
- "max_replication_slots=10"
14+
- "-c"
15+
- "shared_preload_libraries=/opt/dummy_seclabel/dummy_seclabel"
1316
environment:
1417
POSTGRES_PASSWORD: ${PGPASSWORD}
1518
ports:

0 commit comments

Comments
 (0)