Skip to content

Commit fd6a339

Browse files
authored
Merge pull request #157 from cipherstash/feat/optionally-trigger-eql-install-on-container-start
feat: optionally trigger EQL install on container start
2 parents 1e93feb + 1fd1613 commit fd6a339

File tree

6 files changed

+103
-21
lines changed

6 files changed

+103
-21
lines changed

README.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,27 @@ TODO: Add instructions for running Proxy locally
208208

209209
### Setting up the database schema
210210

211-
TODO: Add instructions for setting up the database schema
211+
Under the hood, Proxy uses [CipherStash Encrypt Query Language](https://github.com/cipherstash/encrypt-query-language/) to index and search encrypted data.
212+
213+
When you start the Proxy container, you can install EQL by setting the `CS_DATABASE__INSTALL_EQL` environment variable:
214+
215+
```bash
216+
CS_DATABASE__INSTALL_EQL=true
217+
```
218+
219+
This will install the version of EQL bundled with the Proxy container.
220+
The version of EQL bundled with the Proxy container is tested to work with that version of Proxy.
221+
222+
If you are following the [getting started](#getting-started) guide above, EQL is automatically installed for you.
223+
You can also install EQL by running [the installation script](https://github.com/cipherstash/encrypt-query-language/releases) as a database migration in your application.
224+
225+
Once you have installed EQL, you can see what version is installed by querying the database:
226+
227+
```sql
228+
SELECT cs_eql_version();
229+
```
230+
231+
This will output the version of EQL installed.
212232

213233
#### Creating columns with the right types
214234

docker-compose.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ services:
3939
- CS_DATABASE__HOST=postgres
4040
- CS_DATABASE__PORT=5432
4141
- CS_PROMETHEUS__ENABLED=${CS_PROMETHEUS__ENABLED:-true}
42+
- CS_DATABASE__INSTALL_EQL=true # install EQL into the PostgreSQL database we start above
4243
networks:
4344
- cipherstash
4445

docker-entrypoint.sh

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,34 @@
11
#!/usr/bin/env bash
22
set -eu
33

4+
DATABASE_URL="postgresql://${CS_DATABASE__USERNAME}:${CS_DATABASE__PASSWORD}@${CS_DATABASE__HOST}:${CS_DATABASE__PORT}/${CS_DATABASE__NAME}"
5+
6+
postgres_ready () {
7+
psql ${DATABASE_URL} -c "SELECT 1" > /dev/null 2>&1
8+
}
9+
10+
wait_for_postgres_or_exit() {
11+
host=${CS_DATABASE__HOST}
12+
port=${CS_DATABASE__PORT}
13+
max_retries=20
14+
interval=0.5
15+
attempt=1
16+
echo "Testing presence of PostgreSQL at ${host}:${port} with a maximum of ${max_retries} retries"
17+
18+
until postgres_ready
19+
do
20+
if [ $attempt -lt $max_retries ]; then
21+
echo "Waiting for ${host}:${port}"
22+
sleep $interval
23+
attempt=$(expr $attempt + 1)
24+
else
25+
echo "Unable to connect to ${host}:${port} after ${max_retries} attempts"
26+
exit 64
27+
fi
28+
done
29+
echo "Connected to ${host}:${port} after ${attempt} attempts"
30+
}
31+
432
: "${CS_DATABASE__AWS_BUNDLE_PATH:=./aws-rds-global-bundle.pem}"
533

634
# Optionally pull in the AWS RDS global certificate bundle. This is required
@@ -30,4 +58,31 @@ case "${CS_DATABASE__INSTALL_AWS_RDS_CERT_BUNDLE:-}" in
3058
;;
3159
esac
3260

61+
# Optionally install EQL in the target database
62+
case "${CS_DATABASE__INSTALL_EQL:-}" in
63+
"true") ;&
64+
"yes") ;&
65+
"1")
66+
>&2 echo "Installing EQL in target PostgreSQL database..."
67+
68+
if [ ! -f "/opt/cipherstash-eql.sql" ]; then
69+
>&2 echo "error: unable to find EQL installer at: /opt/cipherstash-eql.sql"
70+
exit 1
71+
fi
72+
73+
# Wait for postgres to become available
74+
wait_for_postgres_or_exit
75+
76+
# Attempt to install EQL
77+
psql --file=/opt/cipherstash-eql.sql --quiet $DATABASE_URL > /dev/null 2>&1
78+
if [ $? != 0 ]; then
79+
>&2 echo "error: unable to install EQL in target PostgreSQL database!"
80+
exit 2
81+
fi
82+
;;
83+
*)
84+
>&2 echo "Not installing EQL in target PostgreSQL database."
85+
;;
86+
esac
87+
3388
exec cipherstash-proxy "$@"

mise.toml

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -52,21 +52,8 @@ run = """
5252
{% set target = arch() ~ "-unknown-linux-gnu" | replace(from="arm64", to="aarch64") | replace(from="x64", to="x86_64") %}
5353
{% set docker_platform = "linux/" ~ arch() | replace(from="x64", to="amd64") %}
5454
55-
{# If we are on macos, cross-compile for Linux, so we can run the binary in a Docker container. #}
56-
{# Only supports Apple Silicon. #}
57-
{% if os() == "macos" %}
58-
if ! which {{ target }}-gcc ; then
59-
brew install MaterializeInc/crosstools/aarch64-unknown-linux-gnu
60-
fi
61-
export CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER={{ target }}-gcc
62-
{% endif %}
63-
64-
# cross-compile
65-
rustup target add --toolchain stable {{ target }}
66-
67-
cargo build --locked --target {{ target }} --release --package cipherstash-proxy
68-
69-
cp {{config_root}}/target/{{ target }}/release/cipherstash-proxy {{config_root}}/
55+
# build a binary
56+
mise run build:binary --target {{target}}
7057
7158
# build a new container
7259
mise run build:docker --platform {{docker_platform}}
@@ -514,8 +501,29 @@ mise run build:docker --platform {{option(name="platform",default=default_platfo
514501
[tasks."build:binary"]
515502
description = "Build a releasable binary for cipherstash-proxy"
516503
run = """
517-
cargo build --locked --release --package cipherstash-proxy
518-
cp -v target/release/cipherstash-proxy .
504+
#!/bin/bash
505+
{% set default_target_arch = arch() | replace(from="arm64", to="aarch64") | replace(from="x64", to="x86_64") %}
506+
{% set default_target_os = os() | replace(from="linux", to="unknown-linux-gnu") | replace(from="macos", to="apple-darwin") %}
507+
{% set default_target = default_target_arch ~ "-" ~ default_target_os %}
508+
{% set target = option(name="target", default=default_target) %}
509+
510+
{# If we are on macos and are cross-compiling for Linux, set up a linker and toolchain. #}
511+
{# Only supports cross-compiling to Linux/ARM64. #}
512+
{% if os() == "macos" %}
513+
if [[ "{{option(name="target", default=default_target)}}" =~ "linux" ]]; then
514+
if ! which {{ target }}-gcc ; then
515+
brew install MaterializeInc/crosstools/aarch64-unknown-linux-gnu
516+
fi
517+
export CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER={{ target }}-gcc
518+
fi
519+
{% endif %}
520+
521+
# cross-compile
522+
rustup target add --toolchain stable {{ target }}
523+
524+
cargo build --locked --target {{ target }} --release --package cipherstash-proxy
525+
526+
cp -v {{config_root}}/target/{{ target }}/release/cipherstash-proxy {{config_root}}/
519527
"""
520528

521529
[tasks."build:docker"]

proxy.Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ RUN apt update && apt install -y ca-certificates postgresql-client curl
66

77
# Copy binary
88
COPY cipherstash-proxy /usr/local/bin/cipherstash-proxy
9+
# Copy entrypoint, for handling Proxy startup
910
COPY docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh
1011

1112
# Copy EQL install scripts

tests/docker-compose.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,7 @@ services:
5757
- CS_DEFAULT_KEYSET_ID=${CS_DEFAULT_KEYSET_ID}
5858
- CS_CLIENT_KEY=${CS_CLIENT_KEY}
5959
- CS_CLIENT_ID=${CS_CLIENT_ID}
60-
6160
- CS_PROMETHEUS__ENABLED=${CS_PROMETHEUS__ENABLED:-true}
62-
63-
6461
networks:
6562
- postgres
6663

0 commit comments

Comments
 (0)