Skip to content

Commit 3ad9ce3

Browse files
istvanoIstvan Orban
andauthored
Feature/docker (#7)
* add Dockerfile for development build * add documentation for usage with docker * add more examples --------- Co-authored-by: Istvan Orban <istvan.orbann@gmail.com>
1 parent 7bbb686 commit 3ad9ce3

2 files changed

Lines changed: 154 additions & 0 deletions

File tree

Dockerfile

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Stage 1: Build the Vault plugin
2+
FROM golang:1.19 as builder
3+
4+
ARG PLUGIN_NAME=vault-plugin-database-couchbasecapella
5+
ARG PLUGIN_DIR=/vault/plugins/
6+
7+
# Install Git
8+
RUN apt-get update && \
9+
apt-get install -y git
10+
11+
# Set up the Go workspace
12+
WORKDIR /go/src/github.com/couchbaselabs/${PLUGIN_NAME}
13+
14+
# Clone the plugin repository
15+
ADD . .
16+
17+
# Build the plugin
18+
RUN CGO_ENABLED=0 GOOS=linux go build -o ${PLUGIN_DIR}/${PLUGIN_NAME} "./cmd/couchbasecapella-database-plugin"
19+
RUN shasum -a 256 "${PLUGIN_DIR}/${PLUGIN_NAME}" | cut -d " " -f1 > ${PLUGIN_DIR}/${PLUGIN_NAME}.sha256
20+
21+
# Stage 2: Add the plugin to the Vault image
22+
FROM hashicorp/vault:1.14
23+
24+
ARG PLUGIN_NAME=vault-plugin-database-couchbasecapella
25+
ARG PLUGIN_DIR=/vault/plugins/
26+
27+
ENV PLUGIN_NAME=$PLUGIN_NAME
28+
ENV PLUGIN_DIR=$PLUGIN_DIR
29+
30+
# Set environment variables for Vault
31+
ENV VAULT_ADDR=http://127.0.0.1:8200
32+
ENV VAULT_API_ADDR=http://127.0.0.1:8200
33+
34+
# Copy the plugin binary from the builder stage
35+
COPY --from=builder ${PLUGIN_DIR}/${PLUGIN_NAME} ${PLUGIN_DIR}/${PLUGIN_NAME}
36+
COPY --from=builder ${PLUGIN_DIR}/${PLUGIN_NAME}.sha256 /vault/${PLUGIN_NAME}.sha256
37+
38+
COPY <<EOF /vault/password_policy.hcl
39+
length=64
40+
41+
rule "charset" {
42+
charset = "abcdefghijklmnopqrstuvwxyz"
43+
min-chars = 1
44+
}
45+
46+
rule "charset" {
47+
charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
48+
min-chars = 1
49+
}
50+
51+
rule "charset" {
52+
charset = "0123456789"
53+
min-chars = 1
54+
}
55+
56+
rule "charset" {
57+
charset = "#@%!"
58+
min-chars = 1
59+
}
60+
EOF
61+
62+
# Update Vault's configuration to load the plugin
63+
RUN echo '{"plugin_directory": "/vault/plugins", "storage": {"file": {"path": "/vault/file"}}, "default_lease_ttl": "168h", "max_lease_ttl": "720h", "ui": true}' > /vault/config/config.json
64+
65+
# Expose Vault ports
66+
EXPOSE 8200

docker.md

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# vault-plugin-database-couchbasecapella with Docker
2+
3+
Please note this build is not recommended for Production use. It is designed for testing using Docker.
4+
5+
## Build
6+
7+
You can build a test Vault image where this plugin will be copied to the `/vault/plugins` folder
8+
9+
```bash
10+
docker build -t vault:with-cb-capella-plugin .
11+
```
12+
13+
## Setup
14+
15+
The following command will start Vault in development mode with a root token `password`. This can be used to test the plugin
16+
17+
```bash
18+
docker run --cap-add=IPC_LOCK --name="couchbase_vault" --rm \
19+
-e VAULT_DEV_LISTEN_ADDRESS=0.0.0.0:8200 \
20+
-e VAULT_ADDR=http://0.0.0.0:8200 \
21+
-p 8200:8200 \
22+
vault:with-cb-capella-plugin \
23+
vault server -dev -dev-root-token-id="password" \
24+
-log-level=debug -config=/vault/config/config.json
25+
```
26+
27+
### Enable Database secrets
28+
29+
```bash
30+
docker exec -it "couchbase_vault" /bin/ash -c "vault login password && vault secrets enable database"
31+
```
32+
### Register the plugin
33+
34+
The multi-stage docker container first builds the plugin. It also generates the SHA256 and saves it the following file:
35+
*/vault/vault-plugin-database-couchbasecapella.sha256*
36+
37+
```bash
38+
docker exec -it "couchbase_vault" /bin/ash -c "SHA256=\$(cat /vault/vault-plugin-database-couchbasecapella.sha256) && vault login password && vault write sys/plugins/catalog/database/vault-plugin-database-couchbasecapella sha256=\$SHA256 command=vault-plugin-database-couchbasecapella"
39+
```
40+
41+
You can check if the plugin was registered by listing the installed plugins with the following command
42+
43+
```bash
44+
docker exec -it "couchbase_vault" /bin/ash -c "vault login password && vault plugin list"
45+
```
46+
47+
### Upload password policy
48+
49+
Couchbase provides a Vault password policy file that can be used with the plugin. The policy can be found at */vault/password_policy.hcl*
50+
51+
```bash
52+
docker exec -it "couchbase_vault" /bin/ash -c "vault login password && vault write sys/policies/password/couchbasecapella policy=@/vault/password_policy.hcl"
53+
```
54+
55+
## Testing
56+
57+
### Create database config
58+
59+
You can use the following command to create a database config that sets up the connection to your Capella cluster.
60+
Make sure to replace the variables.
61+
62+
```bash
63+
docker exec -it "couchbase_vault" /bin/ash -c 'vault login password && vault write database/config/vault-plugin-database-couchbasecapella plugin_name="vault-plugin-database-couchbasecapella" cloud_api_base_url="https://cloudapi.cloud.couchbase.com/v4" organization_id="$your_capella_organization_id" project_id="$your_capella_project_id" cluster_id="$your_capella_cluster_id" username="$your_capella_access_key_name" password="$your_capella_access_key_secret" password_policy="couchbasecapella" allowed_roles="*"'
64+
```
65+
> Please note: it uses the password policy we registered before
66+
67+
### Rotate root credentials
68+
69+
The plugin supports rotating the root credentials that was used to initialize the database config
70+
71+
```bash
72+
docker exec -it "couchbase_vault" /bin/ash -c "vault login password && vault write -force database/rotate-root/vault-plugin-database-couchbasecapella"
73+
```
74+
### Create a dynamic role
75+
76+
```bash
77+
docker exec -it "couchbase_vault" /bin/ash -c 'vault login password && vault write database/roles/dynamicrole1 db_name="vault-plugin-database-couchbasecapella" creation_statements='\''{"access": [ { "privileges": [ "data_reader", "data_writer" ], "resources": { "buckets": [ { "name": "vault-bucket-1", "scopes": [ { "name": "vault-bucket-1-scope-1", "collections": [ "*" ] } ] } ] } } ]}'\'' default_ttl="5m" max_ttl="1h"'
78+
```
79+
80+
> Please note: this example assumes you have a bucket called: *vault-bucket-1* and a scope called: *vault-bucket-1-scope-1*
81+
82+
### Create a new credential
83+
84+
Using the dynamic role setup in the earlier step, we can ask Vault to create a new set of database credentials
85+
86+
```bash
87+
docker exec -it "couchbase_vault" /bin/ash -c 'vault login password && vault read database/creds/dynamicrole1'
88+
```

0 commit comments

Comments
 (0)