Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Dockerfile
doc
.git/
.gitignore
.github/
target
61 changes: 61 additions & 0 deletions .github/workflows/container-build.yml
Original file line number Diff line number Diff line change
@@ -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 }}
77 changes: 77 additions & 0 deletions .github/workflows/container-publish.yml
Original file line number Diff line number Diff line change
@@ -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
36 changes: 35 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
51 changes: 51 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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"]
54 changes: 54 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -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:
5 changes: 4 additions & 1 deletion src/bin/keystone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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");
Expand Down
12 changes: 12 additions & 0 deletions tools/Dockerfile.py-keystone
Original file line number Diff line number Diff line change
@@ -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"]
11 changes: 11 additions & 0 deletions tools/keystone.conf
Original file line number Diff line number Diff line change
@@ -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
9 changes: 9 additions & 0 deletions tools/start_keystone.sh
Original file line number Diff line number Diff line change
@@ -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