From e435468e4419aef5730c31f21f8871981997b40f Mon Sep 17 00:00:00 2001 From: Artem Goncharov Date: Wed, 13 Aug 2025 20:12:12 +0200 Subject: [PATCH] feat: Start building container image --- .dockerignore | 6 ++ .github/workflows/container-build.yml | 61 ++++++++++++++++++++ .github/workflows/container-publish.yml | 77 +++++++++++++++++++++++++ Cargo.lock | 36 +++++++++++- Cargo.toml | 2 +- Dockerfile | 51 ++++++++++++++++ docker-compose.yaml | 54 +++++++++++++++++ src/bin/keystone.rs | 5 +- tools/Dockerfile.py-keystone | 12 ++++ tools/keystone.conf | 11 ++++ tools/start_keystone.sh | 9 +++ 11 files changed, 321 insertions(+), 3 deletions(-) create mode 100644 .dockerignore create mode 100644 .github/workflows/container-build.yml create mode 100644 .github/workflows/container-publish.yml create mode 100644 Dockerfile create mode 100644 docker-compose.yaml create mode 100644 tools/Dockerfile.py-keystone create mode 100644 tools/keystone.conf create mode 100755 tools/start_keystone.sh diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 00000000..3ce96ed5 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,6 @@ +Dockerfile +doc +.git/ +.gitignore +.github/ +target diff --git a/.github/workflows/container-build.yml b/.github/workflows/container-build.yml new file mode 100644 index 00000000..f21bc99c --- /dev/null +++ b/.github/workflows/container-build.yml @@ -0,0 +1,61 @@ +--- +# Build container image with the OpenStack cli +name: Build container image + +# Configures this workflow to run every time a change is pushed to the branch called `release`. +on: + pull_request: + paths: + - 'Cargo.toml' + - 'Cargo.lock' + - '.github/workflows/container-build.yml' + - '.github/workflows/container-publish.yml' + - 'keystone/**' + - 'Dockerfile' + - '.dockerignore' + + +permissions: + contents: read + +# Defines two custom environment variables for the workflow. These are used for the Container registry domain, and a name for the Docker image that this workflow builds. +env: + REGISTRY: ghcr.io + IMAGE_NAME: ${{ github.repository }} + +# There is a single job in this workflow. It's configured to run on the latest available version of Ubuntu. +jobs: + build-image: + runs-on: ubuntu-latest + # Sets the permissions granted to the `GITHUB_TOKEN` for the actions in this job. + permissions: + contents: read + id-token: write + # + steps: + - name: Harden Runner + uses: step-security/harden-runner@0634a2670c59f64b4a01f0f96f84700a4088b9f0 # v2.12.0 + with: + egress-policy: audit + + - name: Checkout repository + uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + + - name: Extract metadata (tags, labels) for Docker + id: meta + uses: docker/metadata-action@902fa8ec7d6ecbf8d84d538b9b233a880e428804 # v5.7.0 + with: + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} + tags: | + type=ref,event=branch + type=ref,event=pr + type=sha + + - name: Build and push Docker image + id: push + uses: docker/build-push-action@263435318d21b8e681c14492fe198d362a7d2c83 # v6.18.0 + with: + context: . + push: false + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} diff --git a/.github/workflows/container-publish.yml b/.github/workflows/container-publish.yml new file mode 100644 index 00000000..9122b14c --- /dev/null +++ b/.github/workflows/container-publish.yml @@ -0,0 +1,77 @@ +--- +# Build container image with the OpenStack cli +name: Create and publish container image + +# Configures this workflow to run every time a change is pushed to the branch called `release`. +on: + push: + branches: ['main'] + tags: + - 'keystone-v*' + +permissions: + contents: read + +# Defines two custom environment variables for the workflow. These are used for the Container registry domain, and a name for the Docker image that this workflow builds. +env: + REGISTRY: ghcr.io + IMAGE_NAME: ${{ github.repository }} + +# There is a single job in this workflow. It's configured to run on the latest available version of Ubuntu. +jobs: + build-and-push-image: + runs-on: ubuntu-latest + # Sets the permissions granted to the `GITHUB_TOKEN` for the actions in this job. + permissions: + contents: read + packages: write + attestations: write + id-token: write + # + steps: + - name: Harden Runner + uses: step-security/harden-runner@0634a2670c59f64b4a01f0f96f84700a4088b9f0 # v2.12.0 + with: + egress-policy: audit + + - name: Checkout repository + uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + + # Uses the `docker/login-action` action to log in to the Container registry registry using the account and password that will publish the packages. Once published, the packages are scoped to the account defined here. + - name: Log in to the Container registry + uses: docker/login-action@74a5d142397b4f367a81961eba4e8cd7edddf772 # v3.4.0 + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + # This step uses [docker/metadata-action](https://github.com/docker/metadata-action#about) to extract tags and labels that will be applied to the specified image. The `id` "meta" allows the output of this step to be referenced in a subsequent step. The `images` value provides the base name for the tags and labels. + - name: Extract metadata (tags, labels) for Docker + id: meta + uses: docker/metadata-action@902fa8ec7d6ecbf8d84d538b9b233a880e428804 # v5.7.0 + with: + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} + tags: | + type=ref,event=branch + type=ref,event=pr + type=sha + + # This step uses the `docker/build-push-action` action to build the image, based on your repository's `Dockerfile`. If the build succeeds, it pushes the image to GitHub Packages. + # It uses the `context` parameter to define the build's context as the set of files located in the specified path. For more information, see "[Usage](https://github.com/docker/build-push-action#usage)" in the README of the `docker/build-push-action` repository. + # It uses the `tags` and `labels` parameters to tag and label the image with the output from the "meta" step. + - name: Build and push Docker image + id: push + uses: docker/build-push-action@263435318d21b8e681c14492fe198d362a7d2c83 # v6.18.0 + with: + context: . + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} + + # This step generates an artifact attestation for the image, which is an unforgeable statement about where and how it was built. It increases supply chain security for people who consume the image. For more information, see "[AUTOTITLE](/actions/security-guides/using-artifact-attestations-to-establish-provenance-for-builds)." + - name: Generate artifact attestation + uses: actions/attest-build-provenance@e8998f949152b193b063cb0ec769d69d929409be # v2.4.0 + with: + subject-name: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} + subject-digest: ${{ steps.push.outputs.digest }} + push-to-registry: true diff --git a/Cargo.lock b/Cargo.lock index af2d568e..70b63894 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -17,6 +17,17 @@ version = "2.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa" +[[package]] +name = "aes" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b169f7a6d4742236a0a00c541b845991d0ac43e546831af1249753ab4c3aa3a0" +dependencies = [ + "cfg-if", + "cipher", + "cpufeatures", +] + [[package]] name = "ahash" version = "0.7.8" @@ -575,6 +586,15 @@ dependencies = [ "generic-array", ] +[[package]] +name = "block-padding" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a8894febbff9f758034a5b8e12d87918f56dfc64a8e1fe757d65e29041538d93" +dependencies = [ + "generic-array", +] + [[package]] name = "blocking" version = "1.6.2" @@ -691,6 +711,15 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "37b2a672a2cb129a2e41c10b1224bb368f9f37a2b16b612598138befd7b37eb5" +[[package]] +name = "cbc" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26b52a9543ae338f279b96b0b9fed9c8093744685043739079ce85cd58f289a6" +dependencies = [ + "cipher", +] + [[package]] name = "cc" version = "1.2.30" @@ -1609,10 +1638,14 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c66b725fe9483b9ee72ccaec072b15eb8ad95a3ae63a8c798d5748883b72fd33" dependencies = [ + "aes", "base64 0.22.1", "byteorder", + "cbc", "getrandom 0.2.16", - "openssl", + "hmac", + "sha2", + "subtle", "zeroize", ] @@ -2327,6 +2360,7 @@ version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "879f10e63c20629ecabbb64a8010319738c66a5cd0c29b02d63d272b03751d01" dependencies = [ + "block-padding", "generic-array", ] diff --git a/Cargo.toml b/Cargo.toml index 7bd17baa..250ac9a3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -34,7 +34,7 @@ config = { version = "0.15", features = ["ini"] } derive_builder = { version = "0.20" } dyn-clone = { version = "1.0" } eyre = { version = "0.6" } -fernet = { version = "0.2" } +fernet = { version = "0.2", default-features = false, features = ["rustcrypto"] } futures-util = { version = "0.3" } mockall_double = { version = "0.3" } opa-wasm = { version = "^0.1", optional = true } diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..c990d9e0 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,51 @@ +################ +##### Builder +FROM rust:1.89.0-slim-bookworm AS builder + +#RUN rustup target add x86_64-unknown-linux-gnu &&\ +RUN apt update &&\ + apt install -y openssl libssl-dev libssl3 pkg-config musl-tools musl-dev &&\ + update-ca-certificates + +WORKDIR /usr/src + +# Create blank project +RUN USER=root cargo new keystone + +# We want dependencies cached, so copy those first. +COPY Cargo.toml Cargo.lock /usr/src/keystone/ +RUN mkdir -p keystone/src/bin && touch keystone/src/lib.rs &&\ + cp keystone/src/main.rs keystone/src/bin/keystone.rs &&\ + cp keystone/src/main.rs keystone/src/bin/keystone_db.rs &&\ + mkdir -p keystone/benches && touch keystone/benches/fernet_token.rs + +# Set the working directory +WORKDIR /usr/src/keystone + +## This is a dummy build to get the dependencies cached. +#RUN cargo build --target x86_64-unknown-linux-musl --release +RUN cargo build --release + +# Now copy in the rest of the sources +COPY . /usr/src/keystone/ + +## Touch main.rs to prevent cached release build +RUN touch src/lib.rs && touch src/bin/keystone.rs + +# This is the actual application build. +RUN cargo build --release --bins + +################ +##### Runtime +FROM debian:bookworm-slim AS runtime + +LABEL maintainer="Artem Goncharov" + +#RUN apk add --no-cache bash openssl ca-certificates +RUN apt update && apt install -y ca-certificates libssl3 && update-ca-certificates + +# Copy application binary from builder image +COPY --from=builder /usr/src/keystone/target/release/keystone /usr/local/bin +COPY --from=builder /usr/src/keystone/target/release/keystone-db /usr/local/bin + +CMD ["/usr/local/bin/keystone"] diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 00000000..8a8e7285 --- /dev/null +++ b/docker-compose.yaml @@ -0,0 +1,54 @@ +services: + db: + image: "postgres" + environment: + POSTGRES_USER: keystone + POSTGRES_PASSWORD: password + PGDATA: /var/lib/postgresql/data/pgdata + healthcheck: + test: ["CMD-SHELL", "pg_isready -U user -d mydb"] + interval: 10s + timeout: 5s + retries: 5 + start_period: 30s + volumes: + - pg_data:/var/lib/postgresql/data + keystone_py: + image: keystone-py:dev + build: + context: tools/ + dockerfile: Dockerfile.py-keystone + depends_on: + db: + condition: service_healthy + restart: true + ports: + - "15001:5000" + volumes: + - fernet:/etc/keystone/fernet-keys:rw + - ${PWD}/tools/keystone.conf:/etc/keystone/keystone.conf + keystone_rust: + image: keystone-rust:dev + build: . + command: ["bash", "-c", "keystone-db up && keystone -c /etc/keystone/keystone.conf -vv"] + environment: + DATABASE_URL: postgresql://keystone:password@db/keystone + ports: + - "18080:8080" + depends_on: + db: + condition: service_healthy + restart: true + volumes: + - fernet:/etc/keystone/fernet-keys:rw + - ${PWD}/tools/keystone.conf:/etc/keystone/keystone.conf + opa: + image: openpolicyagent/opa:1.7.0 + ports: + - "18181:8181" + command: ["run", "-s", "/policy", "--addr", "0.0.0.0:8181", "--log-level", "debug"] + volumes: + - ${PWD}/policy:/policy +volumes: + pg_data: + fernet: diff --git a/src/bin/keystone.rs b/src/bin/keystone.rs index a876926a..613df035 100644 --- a/src/bin/keystone.rs +++ b/src/bin/keystone.rs @@ -33,7 +33,7 @@ use tower_http::{ request_id::{MakeRequestId, PropagateRequestIdLayer, RequestId, SetRequestIdLayer}, trace::{DefaultOnRequest, DefaultOnResponse, TraceLayer}, }; -use tracing::{Level, error, info, info_span, trace}; +use tracing::{Level, debug, error, info, info_span, trace}; use tracing_subscriber::{filter::*, prelude::*}; use utoipa::OpenApi; use utoipa_axum::router::OpenApiRouter; @@ -115,6 +115,8 @@ async fn main() -> Result<(), Report> { // build the tracing registry tracing_subscriber::registry().with(log_layer).init(); + info!("Starting Keystone..."); + let openapi = api::ApiDoc::openapi(); let (router, api) = OpenApiRouter::with_openapi(openapi.clone()) @@ -142,6 +144,7 @@ async fn main() -> Result<(), Report> { opt.sqlx_logging(false); } + debug!("Establishing the database connection..."); let conn = Database::connect(opt) .await .expect("Database connection failed"); diff --git a/tools/Dockerfile.py-keystone b/tools/Dockerfile.py-keystone new file mode 100644 index 00000000..39fab043 --- /dev/null +++ b/tools/Dockerfile.py-keystone @@ -0,0 +1,12 @@ +FROM python:3.12-bookworm + +RUN pip3 install keystone==27.0 uwsgi psycopg2-binary + +WORKDIR /app + +COPY start_keystone.sh /app/start.sh + +# Sadly podman-compose does not support service complete checks, so in order to +# have a more or less controlled order of the initialization steps we use the +# start script. +CMD ["/app/start.sh"] diff --git a/tools/keystone.conf b/tools/keystone.conf new file mode 100644 index 00000000..eddf2072 --- /dev/null +++ b/tools/keystone.conf @@ -0,0 +1,11 @@ +[DEFAULT] +debug = true + +[auth] +methods = password,token,openid,application_credential + +[database] +connection = postgresql://keystone:password@db/keystone + +[api_policy] +opa_base_url = http://opa:8181 diff --git a/tools/start_keystone.sh b/tools/start_keystone.sh new file mode 100755 index 00000000..f92d4e02 --- /dev/null +++ b/tools/start_keystone.sh @@ -0,0 +1,9 @@ +#!/usr/bin/bash -e + +keystone-manage db_sync + +keystone-manage fernet_setup --keystone-user root --keystone-group root + +keystone-manage bootstrap --bootstrap-user admin --bootstrap-password password --bootstrap-public-url http://localhost:15001 --bootstrap-internal-url http://localhost:18080 + +uwsgi --http-socket :5000 --module 'keystone.server.wsgi:initialize_public_application()' -b 65535