Skip to content

Commit 8959642

Browse files
feat: add Devcontainers
Signed-off-by: Carlos Feria <2582866+carlosthe19916@users.noreply.github.com>
1 parent 2381bef commit 8959642

File tree

9 files changed

+317
-0
lines changed

9 files changed

+317
-0
lines changed

.devcontainer/Dockerfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
FROM mcr.microsoft.com/devcontainers/base:ubuntu
2+
3+
RUN sudo apt update && \
4+
sudo apt install -y build-essential pkg-config libssl-dev postgresql-client

.devcontainer/README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Devcontainers
2+
3+
Use [Devcontainers](https://code.visualstudio.com/docs/devcontainers/containers) to prepare a fully automated working environment.
4+
5+
### Docker
6+
7+
Docker defaults should work fine therefore there is nothing to do.
8+
9+
### Podman
10+
11+
Start Podman service for a regular user (rootless) and make it listen to a socket:
12+
13+
```shell
14+
systemctl --user enable --now podman.socket
15+
```
16+
17+
Restart your OS if necessary and verify that podman listens:
18+
19+
```shell
20+
systemctl --user status podman.socket
21+
```
22+
23+
## VSCode
24+
25+
Install the extension https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-containers
26+
27+
Only if you use podman, therefore Optional:
28+
29+
Go to the Extension Settings:
30+
31+
- `Dev › Containers: Docker Compose Path` set `podman-compose`
32+
- `Dev › Containers: Docker Path` set `podman`
33+
- `Dev › Containers: Docker Socket Path` set `/run/podman/podman.sock`
34+
35+
To open the repository with DevContainers do `Ctrl + Shift + P` and enter `Dev Containers: Rebuild and Reopen in Container` or `Dev Containers: Reopen in Container`. For more options see the Extension documentation.

.devcontainer/assets/demo.mp4

7.06 MB
Binary file not shown.

.devcontainer/devcontainer.json

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"name": "trustify",
3+
"dockerComposeFile": "./docker-compose.yml",
4+
"service": "trustify",
5+
"workspaceFolder": "/workspace",
6+
"initializeCommand": "bash .devcontainer/initializeCommand.sh ${devcontainerId}",
7+
"postCreateCommand": "bash .devcontainer/postCreateCommand.sh ${devcontainerId}",
8+
"features": {
9+
"ghcr.io/devcontainers/features/common-utils:2": {},
10+
"ghcr.io/devcontainers/features/rust:1": {}
11+
},
12+
"customizations": {
13+
"vscode": {
14+
"extensions": [
15+
"vadimcn.vscode-lldb",
16+
"rust-lang.rust-analyzer",
17+
"tamasfe.even-better-toml",
18+
"mtxr.sqltools",
19+
"mtxr.sqltools-driver-pg"
20+
],
21+
"settings": {
22+
"git.alwaysSignOff": true,
23+
"sqltools.connections": [
24+
{
25+
"server": "trustify-db",
26+
"database": "trustify",
27+
"username": "postgres",
28+
"password": "trustify",
29+
"port": 5432,
30+
"name": "db",
31+
"driver": "PostgreSQL"
32+
}
33+
]
34+
}
35+
}
36+
}
37+
}

.devcontainer/docker-compose.yml

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
networks:
2+
trustify:
3+
name: "trustify"
4+
5+
volumes:
6+
trustify-postgres-data:
7+
8+
services:
9+
trustify:
10+
image: localhost/devcontainer-trustify:latest
11+
build:
12+
dockerfile: ./Dockerfile
13+
security_opt:
14+
- "label=disable"
15+
userns_mode: "keep-id"
16+
environment:
17+
TRUSTD_DB_HOST: trustify-db
18+
HTTP_SERVER_BIND_ADDR: "::"
19+
UI_ISSUER_URL: http://localhost:9090/realms/trustd
20+
UI_CLIENT_ID: ui
21+
AUTHENTICATOR_OIDC_ISSUER_URL: http://trustify-keycloak:8080/realms/trustd
22+
AUTHENTICATOR_OIDC_CLIENT_IDS: ui,cli
23+
ports:
24+
- "8080:8080"
25+
- "8090:8090"
26+
- "9010:9010"
27+
command: tail -f /dev/null
28+
volumes:
29+
- ..:/workspace:cached
30+
networks:
31+
- trustify
32+
depends_on:
33+
trustify-db:
34+
condition: service_started
35+
36+
trustify-db:
37+
image: postgres:17
38+
restart: unless-stopped
39+
volumes:
40+
- trustify-postgres-data:/var/lib/postgresql/data
41+
environment:
42+
POSTGRES_USER: postgres
43+
POSTGRES_PASSWORD: trustify
44+
POSTGRES_DB: trustify
45+
POSTGRES_HOSTNAME: localhost
46+
POSTGRES_PORT: 5432
47+
networks:
48+
- trustify
49+
50+
trustify-keycloak:
51+
image: quay.io/keycloak/keycloak:latest
52+
environment:
53+
KEYCLOAK_ADMIN: admin
54+
KEYCLOAK_ADMIN_PASSWORD: admin
55+
KC_HOSTNAME_BACKCHANNEL_DYNAMIC: true
56+
KC_HOSTNAME: http://localhost:9090
57+
ports:
58+
- "9090:8080"
59+
command: [ "start-dev" ]
60+
networks:
61+
- trustify
62+
63+
trustify-keycloak-wait:
64+
image: docker.io/alpine/curl:latest
65+
volumes:
66+
- ./keycloak/kc-wait.sh:/tmp/kc-wait.sh:Z
67+
entrypoint: [ "/bin/sh" ]
68+
command: /tmp/kc-wait.sh
69+
depends_on:
70+
trustify-keycloak:
71+
condition: service_started
72+
networks:
73+
- trustify
74+
75+
trustify-keycloak-init:
76+
image: quay.io/keycloak/keycloak:latest
77+
volumes:
78+
- ./keycloak/kc-init.sh:/tmp/kc-init.sh:Z
79+
entrypoint: [ "/bin/sh" ]
80+
command: /tmp/kc-init.sh
81+
depends_on:
82+
trustify-keycloak-wait:
83+
condition: service_completed_successfully
84+
networks:
85+
- trustify

.devcontainer/initializeCommand.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/bash
2+
3+
# custom initialization goes here - runs outside of the dev container
4+
# just before the container is launched but after the container is created
5+
6+
echo "devcontainerID ${1}"

.devcontainer/keycloak/kc-init.sh

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
SERVER_URL="http://trustify-keycloak:8080"
2+
USERNAME="admin"
3+
PASSWORD="admin"
4+
REALM="master"
5+
6+
# Login
7+
/opt/keycloak/bin/kcadm.sh config credentials \
8+
--server ${SERVER_URL} \
9+
--user ${USERNAME} \
10+
--password ${PASSWORD} \
11+
--realm ${REALM}
12+
13+
# Start working on Trustify Realm
14+
TRUSTIFY_REALM="trustd"
15+
TRUSTIFY_ROLE="admin"
16+
TRUSTIFY_USERNAME="admin"
17+
18+
# Realm
19+
/opt/keycloak/bin/kcadm.sh create realms -s realm=${TRUSTIFY_REALM} -s enabled=true
20+
21+
# Realm roles
22+
/opt/keycloak/bin/kcadm.sh create roles -r ${TRUSTIFY_REALM} -s name=${TRUSTIFY_ROLE} -o
23+
admin_role_id=$(/opt/keycloak/bin/kcadm.sh get roles -r "${TRUSTIFY_REALM}" --fields id,name --format csv --noquotes | grep ",${TRUSTIFY_ROLE}" | sed 's/,.*//')
24+
25+
# Scopes
26+
for scope in read:document create:document update:document delete:document; do
27+
/opt/keycloak/bin/kcadm.sh create client-scopes -r "${TRUSTIFY_REALM}" -s "name=$scope" -s protocol=openid-connect
28+
done
29+
30+
# Roles scope mappings
31+
for scope in read:document create:document update:document delete:document; do
32+
scope_id=$(/opt/keycloak/bin/kcadm.sh get client-scopes -r "${TRUSTIFY_REALM}" --fields id,name --format csv --noquotes | grep ",${scope}" | sed 's/,.*//')
33+
/opt/keycloak/bin/kcadm.sh create "client-scopes/${scope_id}/scope-mappings/realm" -r "${TRUSTIFY_REALM}" -b '[{"name":"'"${TRUSTIFY_ROLE}"'", "id":"'"${admin_role_id}"'"}]'
34+
done
35+
36+
# Users
37+
/opt/keycloak/bin/kcadm.sh create users -r ${TRUSTIFY_REALM} -s username=${TRUSTIFY_USERNAME} -s enabled=true -s firstName="admin" -s lastName="admin" -s email="admin@trustify.org" -o
38+
/opt/keycloak/bin/kcadm.sh set-password -r ${TRUSTIFY_REALM} --username admin --new-password admin
39+
40+
/opt/keycloak/bin/kcadm.sh add-roles -r ${TRUSTIFY_REALM} --uusername ${TRUSTIFY_USERNAME} --rolename ${TRUSTIFY_ROLE}
41+
42+
# Clients
43+
/opt/keycloak/bin/kcadm.sh create clients -r ${TRUSTIFY_REALM} -f - << EOF
44+
{
45+
"clientId": "ui",
46+
"publicClient": true,
47+
"webOrigins": [
48+
"*"
49+
],
50+
"redirectUris": [
51+
"*"
52+
],
53+
"defaultClientScopes": [
54+
"acr",
55+
"basic",
56+
"email",
57+
"profile",
58+
"roles",
59+
"create:document",
60+
"read:document",
61+
"update:document",
62+
"delete:document"
63+
],
64+
"optionalClientScopes": [
65+
"address",
66+
"microprofile-jwt",
67+
"offline_access",
68+
"phone"
69+
]
70+
}
71+
EOF
72+
73+
/opt/keycloak/bin/kcadm.sh create clients -r ${TRUSTIFY_REALM} -f - << EOF
74+
{
75+
"clientId": "cli",
76+
"publicClient": false,
77+
"standardFlowEnabled": false,
78+
"serviceAccountsEnabled": true,
79+
"secret": "secret",
80+
"defaultClientScopes": [
81+
"acr",
82+
"basic",
83+
"email",
84+
"profile",
85+
"roles",
86+
"create:document",
87+
"read:document",
88+
"update:document",
89+
"delete:document"
90+
],
91+
"optionalClientScopes": [
92+
"address",
93+
"microprofile-jwt",
94+
"offline_access",
95+
"phone"
96+
]
97+
}
98+
EOF
99+
100+
# Assign roles to service-account
101+
cli_client="cli"
102+
103+
adminRoleId=$(/opt/keycloak/bin/kcadm.sh get roles -r ${TRUSTIFY_REALM} --fields id,name --format csv --noquotes | grep ",${TRUSTIFY_ROLE}" | sed 's/,.*//')
104+
105+
cliClientId=$(/opt/keycloak/bin/kcadm.sh get clients -r ${TRUSTIFY_REALM} --fields id,clientId --format csv --noquotes | grep ",${cli_client}" | sed 's/,.*//')
106+
serviceAccountId=$(/opt/keycloak/bin/kcadm.sh get clients/${cliClientId}/service-account-user -r ${TRUSTIFY_REALM} --fields id,username --format csv --noquotes | grep ",service-account-${cli_client}" | sed 's/,.*//')
107+
108+
/opt/keycloak/bin/kcadm.sh create users/${serviceAccountId}/role-mappings/realm -r ${TRUSTIFY_REALM} -f - << EOF
109+
[
110+
{
111+
"id": "${adminRoleId}",
112+
"name": "${TRUSTIFY_ROLE}",
113+
"clientRole": false,
114+
"composite": false
115+
}
116+
]
117+
EOF

.devcontainer/keycloak/kc-wait.sh

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
SERVER_URL="http://trustify-keycloak:8080"
2+
TIMEOUT=120
3+
4+
attempt_counter=0
5+
interval=3
6+
max_attempts=$(($TIMEOUT/interval));
7+
8+
## Wait until server is ready to continue
9+
echo "Waiting for $SERVER_URL"
10+
until (curl --output /dev/null --silent --head --fail $SERVER_URL); do
11+
if [ ${attempt_counter} -eq ${max_attempts} ];then
12+
echo "Max attempts reached"
13+
exit 1
14+
fi
15+
16+
printf '.'
17+
attempt_counter=$(($attempt_counter+1))
18+
sleep $interval
19+
done
20+
21+
echo "Server ready to listen"

.devcontainer/postCreateCommand.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/bash
2+
3+
# Custom initialization goes here if needed.
4+
# Runs inside the dev container after the container is created
5+
6+
################################################################################
7+
# When using docker we will not be root inside the container
8+
# the following steps are then required
9+
################################################################################
10+
11+
echo "alias start:dev='cargo run --bin trustd db migrate && cargo run --bin trustd api'" >> ~/.bashrc
12+
echo "alias psql:postgres='env PGPASSWORD=trustify psql -U postgres -d postgres -h trustify-db -p 5432'" >> ~/.bashrc

0 commit comments

Comments
 (0)