diff --git a/.gitignore b/.gitignore index 648f401..a336355 100644 --- a/.gitignore +++ b/.gitignore @@ -31,4 +31,6 @@ # Debug files *.dSYM/ *.su -*~ \ No newline at end of file +*~ + +pgsodium_encrypted_root.key \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 1450ef4..93efe66 100644 --- a/Dockerfile +++ b/Dockerfile @@ -11,6 +11,7 @@ COPY . . RUN make && make install RUN ldconfig RUN cd `pg_config --sharedir`/extension/ -RUN cp pgsodium_getkey.sample `pg_config --sharedir`/extension/pgsodium_getkey +RUN cp getkey_scripts/pgsodium_getkey.sample `pg_config --sharedir`/extension/pgsodium_getkey RUN sed -i 's/exit//g' `pg_config --sharedir`/extension/pgsodium_getkey RUN chmod +x `pg_config --sharedir`/extension/pgsodium_getkey +RUN cp `pg_config --sharedir`/extension/pgsodium_getkey /getkey diff --git a/Dockerfile-debug b/Dockerfile-debug new file mode 100644 index 0000000..7330ccc --- /dev/null +++ b/Dockerfile-debug @@ -0,0 +1,28 @@ +ARG version +FROM postgres:${version} +ARG version + +# RUN apt-get update && apt-get install -y make git postgresql-server-dev-${version} curl build-essential gdb +RUN apt-get update && apt-get install -y make git curl build-essential gdb libreadline-dev bison flex zlib1g-dev tmux zile zip gawk + +RUN git clone --branch REL_${version}_STABLE https://github.com/postgres/postgres.git --depth=1 && \ + cd postgres && ./configure \ + --prefix=/usr/ \ + --enable-debug \ + --enable-depend --enable-cassert --enable-profiling \ + CFLAGS="-ggdb -Og -g3 -fno-omit-frame-pointer" \ +# CFLAGS="-O3" \ + && make -j 4 && make install + +RUN curl -s -L https://github.com/theory/pgtap/archive/v1.1.0.tar.gz | tar zxvf - && cd pgtap-1.1.0 && make && make install +RUN curl -s -L https://download.libsodium.org/libsodium/releases/libsodium-1.0.18.tar.gz | tar zxvf - && cd libsodium-1.0.18 && ./configure && make check && make install +RUN mkdir "/pgsodium" +WORKDIR "/pgsodium" +COPY . . +RUN make && make install +RUN ldconfig +RUN curl -O https://raw.githubusercontent.com/tvondra/gdbpg/master/gdbpg.py +RUN cd `pg_config --sharedir`/extension/ +RUN cp getkey_scripts/pgsodium_getkey.sample `pg_config --sharedir`/extension/pgsodium_getkey +RUN sed -i 's/exit//g' `pg_config --sharedir`/extension/pgsodium_getkey +RUN chmod +x `pg_config --sharedir`/extension/pgsodium_getkey diff --git a/README.md b/README.md index 94c9aea..0459004 100644 --- a/README.md +++ b/README.md @@ -100,12 +100,25 @@ used without putting it in `shared_preload_libraries`, you will simply need to provide your own key management. Skip ahead to the API usage section if you choose not to use server managed keys. -See the file [`pgsodium_getkey.sample`](./pgsodium_getkey.sample) for -an example script that returns a libsodium key. The script must emit -a hex encoded 32 byte (64 character) string on a single line. DO NOT -USE THIS FILE WITHOUT SUBSTITUTING YOUR OWN KEY. Edit the file to add -your own key and remove the `exit` line, remove the `.sample` suffix -and make the file executable (on unixen `chmod +x pgsodium_getkey`). +See the file +[`getkey_scripts/pgsodium_getkey.sample`](./pgsodium_getkey.sample) +for an example script that returns a libsodium key. The script must +emit a hex encoded 32 byte (64 character) string on a single line. DO +NOT USE THIS FILE WITHOUT SUBSTITUTING YOUR OWN KEY. Edit the file to +add your own key and remove the `exit` line, remove the `.sample` +suffix and make the file executable (on unixen `chmod +x +pgsodium_getkey`). + +pgsodium also comes with example scripts for: + + - [Amazon Web Service's Key Management + Service](getkey_scripts/pgsodium_getkey_aws.sh). + + - [Google Cloud's Cloud Key + Management](getkey_scripts/pgsodium_getkey_gcp.sh). + + - [Zymbit Zymkey 4i Hardware Security + Module]((getkey_scripts/pgsodium_getkey_zmk.sh). Next place `pgsodium` in your `shared_preload_libraries`. For docker containers, you can append this after the run: diff --git a/example/Dockerfile b/example/Dockerfile index 597a6cc..c2906e3 100644 --- a/example/Dockerfile +++ b/example/Dockerfile @@ -19,7 +19,7 @@ COPY . . RUN make && make install RUN ldconfig RUN cd `pg_config --sharedir`/extension/ -RUN cp pgsodium_getkey.sample `pg_config --sharedir`/extension/pgsodium_getkey +RUN cp getkey_scripts/pgsodium_getkey.sample `pg_config --sharedir`/extension/pgsodium_getkey RUN sed -i 's/exit//g' `pg_config --sharedir`/extension/pgsodium_getkey RUN chmod +x `pg_config --sharedir`/extension/pgsodium_getkey RUN chown -R postgres:postgres /pgsodium diff --git a/pgsodium_getkey.sample b/getkey_scripts/pgsodium_getkey.sample similarity index 100% rename from pgsodium_getkey.sample rename to getkey_scripts/pgsodium_getkey.sample diff --git a/getkey_scripts/pgsodium_getkey_aws.sh b/getkey_scripts/pgsodium_getkey_aws.sh new file mode 100755 index 0000000..03eaeb3 --- /dev/null +++ b/getkey_scripts/pgsodium_getkey_aws.sh @@ -0,0 +1,13 @@ +#!/bin/bash + +HERE=`pwd` +KEY_ID=${KEY_ID:-alias/pgsodium} +ENCRYPTED_ROOT_KEY_FILE=${ENCRYPTED_ROOT_KEY_FILE:-$HERE/pgsodium_encrypted_root.key} + +if [[ -f "$ENCRYPTED_ROOT_KEY_FILE" ]]; then + aws kms decrypt --ciphertext-blob fileb://$ENCRYPTED_ROOT_KEY_FILE --query Plaintext --output text | base64 --decode | hex +else + aws kms generate-data-key --number-of-bytes=32 --key-id=$KEY_ID --query CiphertextBlob --output text | base64 --decode > $ENCRYPTED_ROOT_KEY_FILE + aws kms decrypt --ciphertext-blob fileb://$ENCRYPTED_ROOT_KEY_FILE --query Plaintext --output text | base64 --decode | hex +fi + diff --git a/getkey_scripts/pgsodium_getkey_gcp.sh b/getkey_scripts/pgsodium_getkey_gcp.sh new file mode 100755 index 0000000..e23baf6 --- /dev/null +++ b/getkey_scripts/pgsodium_getkey_gcp.sh @@ -0,0 +1,40 @@ +#!/bin/bash + +HERE=`pwd` +KEY=${KEY:-pgsodium} +KEYRING=${KEYRING:-pgsodium} +LOCATION=${LOCATION:-global} +ROOT_KEY_FILE=${ROOT_KEY_FILE:-$HERE/pgsodium_encrypted_root.key} + +if [[ -f "$ROOT_KEY_FILE" ]]; then + gcloud kms decrypt \ + --key $KEY \ + --keyring $KEYRING \ + --location $LOCATION \ + --plaintext-file - \ + --ciphertext-file $ROOT_KEY_FILE +else + >&2 cat <&2 cat <