-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMakefile
326 lines (244 loc) · 7.85 KB
/
Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
##############################################################################
#
# Makefile
#
###############################################################################
##
## V A R I A B L E S
##
# This Makefile has many input variables,
# see link below for the standard vars offered by PGXS
# https://www.postgresql.org/docs/current/extend-pgxs.html
# The input variable below are optional
# PSQL : psql client ( default = local psql )
# PGDUMP : pg_dump tool ( default = docker )
# REGRESS : run a specific test ( e.g. `REGRESS=noise make installcheck` )
# PG_TEST_EXTRA : extra tests to be run by `installcheck` ( default = none )
##
## C O N F I G
##
MODULES = anon
EXTENSION = anon
EXTENSION_VERSION=$(shell grep default_version $(EXTENSION).control | sed -e "s/default_version[[:space:]]*=[[:space:]]*'\([^']*\)'/\1/")
DATA = anon/*
PG_CFLAGS = -Wno-unused-variable
# Use this var to add more tests
#PG_TEST_EXTRA ?= ""
REGRESS_TESTS = init extschema detection
REGRESS_TESTS+= get_function_schema trusted_schemas
REGRESS_TESTS+= copy pg_dump
REGRESS_TESTS+= masking_expressions
#REGRESS_TESTS+= multiple_masking_policies
REGRESS_TESTS+= sampling
REGRESS_TESTS+= destruction noise shuffle random faking partial
REGRESS_TESTS+= pseudonymization hashing dynamic_masking
REGRESS_TESTS+= anonymize privacy_by_default
#REGRESS_TESTS+= restore
REGRESS_TESTS+= hasmask masked_roles masking masking_search_path masking_foreign_tables
REGRESS_TESTS+= generalization k_anonymity
REGRESS_TESTS+= permissions_owner permissions_masked_role injection syntax_checks
REGRESS_TESTS+=$(PG_TEST_EXTRA)
# This can be overridden by an env variable
REGRESS?=$(REGRESS_TESTS)
MODULEDIR=extension/anon
REGRESS_OPTS = --inputdir=tests
EXTRA_CLEAN = anon _build _venv $(ZIPBALL)
OBJS = anon.o
##
## Mandatory PGXS stuff
## see https://github.com/postgres/postgres/blob/master/src/makefiles/pgxs.mk
##
PG_CONFIG ?= pg_config
PGXS := $(shell $(PG_CONFIG) --pgxs)
include $(PGXS)
BUILD?= _build
all: extension
##
## H E L P
##
# @echo "Available targets for $(MODULE) $(EXTENSION_VERSION):"
default:: help
help:: #: display this message.
@echo
@echo "Available targets for $(EXTENSION) $(EXTENSION_VERSION):"
@echo
@gawk 'match($$0, /([^:]*):.+#'': (.*)/, m) { printf " %-16s%s\n", m[1], m[2]}' $(MAKEFILE_LIST) | sort
@echo
##
## I N S T A L L
##
BINDIR ?= $(shell $(PG_CONFIG) --bindir)
install: install-bin
install-bin:
install -d $(DESTDIR)$(BINDIR)
install -m 0755 bin/pg_dump_anon.sh $(DESTDIR)$(BINDIR)
install-go: _build/linux/amd64/pg_dump_anon/pg_dump_anon
install -d $(DESTDIR)$(BINDIR)
install -m 0755 $^ $(DESTDIR)$(BINDIR)
##
## L I N T
##
.PHONY: lint
lint: lint-sh lint-md lint-sql lint-py
.PHONY: lint-sh
lint-sh: #: check the shell script syntax
shellcheck bin/pg_dump_anon.sh
.PHONY: lint-md
lint-md: #: check the markdown syntax
mdl docs/*.md *.md
.PHONY: lint-sql
lint-sql: #: check the SQL syntax
sqlint anon.sql
sqlint demo/*.sql
.PHONY: lint-py
lint-py: | _venv #: Check the python syntax
_venv/bin/pip install -r python/development.txt
_venv/bin/python -m flake8 python/*.py
.PHONY: lint-go
lint-go: #: Check the golang syntax
$(MAKE) -C pg_dump_anon lint-go
##
## pg_dump_anon
##
.PHONY: pg_dump_anon
pg_dump_anon: #: Build the pg_dump_anon command
$(MAKE) -C pg_dump_anon DEST=../_build
##
## B U I L D
##
.PHONY: extension
extension: | anon #: build the extension
cp anon.sql anon/anon--$(EXTENSION_VERSION).sql
cp data/*.csv anon/
anon:
mkdir -p $@
PG_DUMP?=docker exec postgresqlanonymizer_PostgreSQL_1 pg_dump -U postgres --insert --no-owner
SED1=sed 's/public.//'
SED2=sed 's/SELECT.*search_path.*//'
SED3=sed 's/^SET idle_in_transaction_session_timeout.*//'
SED4=sed 's/^SET row_security.*//'
SEDI=$(shell sed --version >/dev/null 2>&1 && echo 'sed -i --' || echo 'sed -i ""')
sql/tables/%.sql:
$(PG_DUMP) --table $* | $(SED1) | $(SED2) | $(SED3) | $(SED4) > $@
PSQL?=PGPASSWORD=CHANGEME psql -U postgres -h 0.0.0.0 -p54322
PGRGRSS=docker exec postgresqlanonymizer_PostgreSQL_1 /usr/lib/postgresql/10/lib/pgxs/src/test/regress/pg_regress --outputdir=tests/ --inputdir=./ --bindir='/usr/lib/postgresql/10/bin' --inputdir=tests --dbname=contrib_regression --user=postgres unit
##
## D O C K E R
##
ifneq ($(PG_MAJOR_VERSION),)
BUILD_ARG := --build-arg PG_MAJOR_VERSION=$(PG_MAJOR_VERSION)
endif
docker_image: docker/Dockerfile #: build the docker image
docker build -t registry.gitlab.com/dalibo/postgresql_anonymizer . --file $^ $(BUILD_ARG)
docker_push: #: push the docker image to the registry
docker push registry.gitlab.com/dalibo/postgresql_anonymizer
docker_bash: #: enter the docker image (useful for testing)
docker exec -it docker-PostgreSQL-1 bash
COMPOSE=docker compose --file docker/docker-compose.yml
docker_init: #: start a docker container
$(COMPOSE) down
$(COMPOSE) up -d
@echo "The Postgres server may take a few seconds to start. Please wait."
.PHONY: expected
expected : tests/expected/unit.out
tests/expected/unit.out:
$(PGRGRSS)
cp tests/results/unit.out tests/expected/unit.out
##
## S T A N D A L O N E
##
# This is the schema the required extension (for instance tsm_system_rows)
# will be installed
EXTSCHEMA?=public
##
## L O A D
##
FAKE_DATA_TABLES?=address city company country email first_name iban last_name lorem_ipsum postcode siret
FAKE_DATA_LINES?=1000
FAKE_DATA_LOCALES?=en
FAKE_DATA_SEED?=0
FAKE_DATA_CSV_FILES=$(addprefix data/, $(addsuffix .csv, $(FAKE_DATA_TABLES)))
.PHONY: fake_data
fake_data: $(FAKE_DATA_CSV_FILES) #: generate the fake data tables
data/%.csv: | _venv
_venv/bin/python python/populate.py \
--table $* \
--lines $(FAKE_DATA_LINES) \
--locales $(FAKE_DATA_LOCALES) \
--seed $(FAKE_DATA_SEED) \
> $@
_venv:
python3 -m venv $@
$@/bin/pip install --upgrade pip
$@/bin/pip install -r python/requirements.txt
clean_fake_data:
rm $(FAKE_DATA_CSV_FILES)
##
## D E M O & T E S T S
##
#.PHONY: demo_masking demo_perf demo_random demo_partial
demo_in := $(wildcard demo/*.sql)
demo_out = $(demo_in:.sql=.out)
.PHONY: demo
demo:: $(demo_out) demo_blackbox #: launch the demo scripts
demo/%.out: demo/%.sql
$(PSQL) -c 'CREATE DATABASE demo;'
$(PSQL) --echo-all demo < $^ > $@ 2>&1
$(PSQL) -c 'DROP DATABASE demo;'
cat $@
demo_blackbox:
./demo/blackbox.sh
clean_demo:
rm $(demo_out)
tests/sql/%.sql:
$(PSQL) -f $@
##
## C I
##
.PHONY: ci_local
ci_local:
gitlab-ci-multi-runner exec docker make
##
## P G X N
##
ZIPBALL:=_build/linux/$(EXTENSION)-$(EXTENSION_VERSION).zip
.PHONY: pgxn
$(ZIPBALL): pgxn
pgxn: #: build the PGXN package
# required by CI : https://gitlab.com/gitlab-com/support-forum/issues/1351
git clone --bare https://gitlab.com/dalibo/postgresql_anonymizer.git
git -C postgresql_anonymizer.git archive --format zip --prefix=$(EXTENSION)_$(EXTENSION_VERSION)/ --output ../$(ZIPBALL) master
# open the package
unzip $(ZIPBALL)
# remove the zipball because we will rebuild it from scratch
rm -fr $(ZIPBALL)
# copy artefact into the package
cp -pr anon ./$(EXTENSION)_$(EXTENSION_VERSION)/
# remove folders and files that are useless in the PGXN package
rm -fr ./$(EXTENSION)_$(EXTENSION_VERSION)/images
rm -fr ./$(EXTENSION)_$(EXTENSION_VERSION)/docker
rm -fr ./$(EXTENSION)_$(EXTENSION_VERSION)/docs
# rebuild the package
zip -r $(ZIPBALL) ./$(EXTENSION)_$(EXTENSION_VERSION)/
# clean up
rm -fr ./$(EXTENSION)_$(EXTENSION_VERSION) ./postgresql_anonymizer.git/
##
## Windows ZIP
##
WINZIPBALL:=_build/windows/$(EXTENSION)-$(EXTENSION_VERSION)-win2016.zip
.PHONY: windows_zip
$WINZIPBALL: windows_zip
windows_zip: #: build the Windows package
zip -r $(WINZIPBALL) . -x "./git/*"
##
## Debian Packages
##
PG_MAJOR_VERSION ?= $(shell pg_config --sharedir | sed s,.*/,,)
.PHONY: debian
debian: debian/control
dpkg-buildpackage --build=binary --no-check-builddeps
mkdir -p $(BUILD)/debian
cp ../postgresql-*.deb $(BUILD)/debian
lintian
debian/control: debian/control.in
pg_buildext updatecontrol