diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..141ac2e --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,58 @@ +name: Release image + +on: + release: + types: [published] + +permissions: + contents: read + packages: write + +env: + IMAGE: ghcr.io/mathworks-ref-arch/container-images/oauth2-auth-code-proxy + +jobs: + publish: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Verify release tag is on main + run: | + git merge-base --is-ancestor "$GITHUB_SHA" origin/main || { + echo "::error::Release ${GITHUB_REF_NAME} does not point to a commit on main" + exit 1 + } + + - uses: docker/setup-buildx-action@v3 + + - uses: docker/login-action@v3 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - id: meta + uses: docker/metadata-action@v5 + with: + images: ${{ env.IMAGE }} + tags: | + type=semver,pattern={{version}} + type=semver,pattern={{major}}.{{minor}} + type=raw,value=latest + + - id: ts + run: echo "value=$(date -u +%Y-%m-%dT%H:%M:%SZ)" >> "$GITHUB_OUTPUT" + + - uses: docker/build-push-action@v6 + with: + context: . + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} + build-args: | + VERSION=${{ github.ref_name }} + BUILD_TIMESTAMP=${{ steps.ts.outputs.value }} + GROUP_ID=${{ vars.GROUP_ID }} diff --git a/AGENTS.md b/AGENTS.md index 452fa06..016648e 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -82,6 +82,7 @@ These require Go 1.26.3+ locally or can be run inside the builder container: ```bash make build # produce oauth2-auth-code-proxy binary (requires Go 1.26.3+ locally) make docker # build Docker image locally (recommended — bundles correct toolchain) +make docker-local # Dockerfile.local: creds.yaml + certs/ baked in, local use only (never push) make test # go test -race with coverage make lint # golangci-lint v2 make mocks # regenerate mockery mocks diff --git a/Dockerfile b/Dockerfile index a35aec9..3e4b456 100644 --- a/Dockerfile +++ b/Dockerfile @@ -19,8 +19,6 @@ FROM scratch COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ COPY --from=builder /oauth2-auth-code-proxy /oauth2-auth-code-proxy -COPY certs/tls.crt /etc/oauth2-auth-code-proxy/tls.crt -COPY certs/tls.key /etc/oauth2-auth-code-proxy/tls.key EXPOSE 80 30045 diff --git a/Dockerfile.local b/Dockerfile.local new file mode 100644 index 0000000..3d93b79 --- /dev/null +++ b/Dockerfile.local @@ -0,0 +1,13 @@ +# Copyright 2024-2026 The MathWorks, Inc. +# Local-only image: bakes creds.yaml and certs/ into the public image. +# Contains secrets — never push this image. Build with `make docker-local`. +ARG BASE_IMAGE=ghcr.io/mathworks-ref-arch/container-images/oauth2-auth-code-proxy:latest +FROM ${BASE_IMAGE} + +COPY creds.yaml /etc/oauth2-auth-code-proxy/creds.yaml +COPY certs/tls.crt /etc/oauth2-auth-code-proxy/certs/tls.crt +COPY certs/tls.key /etc/oauth2-auth-code-proxy/certs/tls.key + +ENV OAUTH_CREDS_FILE=/etc/oauth2-auth-code-proxy/creds.yaml \ + OAUTH_TLS_CERT_FILE=/etc/oauth2-auth-code-proxy/certs/tls.crt \ + OAUTH_TLS_KEY_FILE=/etc/oauth2-auth-code-proxy/certs/tls.key diff --git a/Makefile b/Makefile index 8427e11..eb17508 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ GROUP_ID ?= PKG := $(MODULE)/internal/app LDFLAGS := -X '$(PKG).Version=$(VERSION)' -X '$(PKG).BuildTime=$(BUILD_TS)' -X '$(PKG).ModuleName=$(MODULE)' -.PHONY: all build test lint cyclo fmt mocks swagger licenses coverage coverage-html coverage-report clean docker i18n certs +.PHONY: all build test lint cyclo fmt mocks swagger licenses coverage coverage-html coverage-report clean docker docker-local i18n certs all: fmt build test lint cyclo @@ -47,7 +47,7 @@ coverage-report: test @echo "Summary:" @go tool cover -func=coverage.out | grep total: -docker: certs +docker: docker build \ --build-arg VERSION=$(VERSION) \ --build-arg BUILD_TIMESTAMP=$(BUILD_TS) \ @@ -55,6 +55,13 @@ docker: certs -t ghcr.io/mathworks-ref-arch/container-images/oauth2-auth-code-proxy:$(VERSION) \ -t ghcr.io/mathworks-ref-arch/container-images/oauth2-auth-code-proxy:latest . +# Local-only image with creds.yaml and certs/ baked in. Never push it. +docker-local: docker certs + @test -f creds.yaml || { echo "creds.yaml not found at repo root (see README \"Credentials File\")"; exit 1; } + docker build -f Dockerfile.local \ + --build-arg BASE_IMAGE=ghcr.io/mathworks-ref-arch/container-images/oauth2-auth-code-proxy:$(VERSION) \ + -t oauth2-auth-code-proxy-local:latest . + licenses: @go-licenses report . --include_tests 2>/dev/null | grep -v "$(shell go list -m)" | sort > /tmp/licenses.csv @echo "# Third-Party Software Notices" > THIRD_PARTY_NOTICES.md diff --git a/README.md b/README.md index 55fbbbf..0991b60 100644 --- a/README.md +++ b/README.md @@ -57,7 +57,57 @@ The container listens on port 30045 by default (matching existing OAuth redirect docker build -t oauth2-auth-code-proxy . ``` -The Dockerfile is a self-contained multi-stage build: it compiles the Go binary from source and produces a minimal `scratch`-based image (~10 MB) containing only the static binary and CA certificates. +The Dockerfile is a self-contained multi-stage build: it compiles the Go binary from source and produces a minimal `scratch`-based image (~10 MB) containing only the static binary and CA certificates. It contains no credentials or TLS certificates — provide those at runtime via mounts and environment variables. + +### Local Image with Your Own Credentials and Certs + +For local testing, `Dockerfile.local` builds on top of the public image and bakes in your `creds.yaml` and TLS certificate, so the container runs without mounts or environment variables. + +> **Warning:** this image contains your OAuth client secret and TLS private key. Never push or share it. + +Place the files at the repo root (both paths are gitignored): + +``` +oauth2-auth-code-proxy/ +├── creds.yaml # OAuth app config (see Credentials File below) +└── certs/ + ├── tls.crt # certificate (PEM) + └── tls.key # private key (PEM) +``` + +To use your own certificate, copy it in under exactly these names; its SANs must match the hostname clients connect to. If `certs/` is absent, `make docker-local` generates a self-signed certificate for `localhost` and `127.0.0.1`. + +Build and run: + +```bash +make docker-local + +docker run -d --name oauth2-proxy -p 80:80 oauth2-auth-code-proxy-local:latest +``` + +Without `make`, build the public image first, then the local image on top of it: + +```bash +docker build -t oauth2-auth-code-proxy:base . + +docker build -f Dockerfile.local \ + --build-arg BASE_IMAGE=oauth2-auth-code-proxy:base \ + -t oauth2-auth-code-proxy-local:latest . +``` + +To skip compiling, set `BASE_IMAGE` to a published release instead, e.g. `ghcr.io/mathworks-ref-arch/container-images/oauth2-auth-code-proxy:`. + +To generate a self-signed certificate without `make` (in Git Bash, prefix the command with `MSYS_NO_PATHCONV=1`): + +```bash +mkdir -p certs +openssl req -x509 -newkey ec -pkeyopt ec_paramgen_curve:prime256v1 \ + -keyout certs/tls.key -out certs/tls.crt -days 3650 -nodes \ + -subj "/CN=oauth2-auth-code-proxy" \ + -addext "subjectAltName=DNS:localhost,IP:127.0.0.1" +``` + +Rebuild the image whenever `creds.yaml` or the certificate changes. ### Standalone Binary @@ -351,6 +401,7 @@ make swagger # regenerate OpenAPI 3.1 spec make mocks # regenerate mockery mocks make coverage-html # generate coverage.html make docker # build container image locally +make docker-local # local-only image with creds.yaml + certs/ baked in (never push) make fmt # format code make clean # remove build artifacts ``` diff --git a/doc/MATLAB.md b/doc/MATLAB.md index 4f4d967..0f5a345 100644 --- a/doc/MATLAB.md +++ b/doc/MATLAB.md @@ -17,9 +17,9 @@ Generate a local TLS certificate (one-time, idempotent): make certs ``` -This creates `certs/tls.crt` and `certs/tls.key` with SANs for `localhost` and `127.0.0.1`. The cert is gitignored but persists across rebuilds. +This creates `certs/tls.crt` and `certs/tls.key` with SANs for `localhost` and `127.0.0.1`. The cert is gitignored and is not baked into the Docker image — mount it at runtime. -Build and run the Docker image (the `docker` target depends on `certs`): +Build and run the Docker image, mounting the cert directory: ```bash make docker @@ -27,12 +27,21 @@ make docker docker run -d --name oauth2-proxy-test \ -p 30045:30045 -p 80:80 \ -v "$(pwd)/creds.yaml:/etc/oauth2-auth-code-proxy/creds.yaml:ro" \ + -v "$(pwd)/certs:/etc/oauth2-auth-code-proxy/certs:ro" \ -e OAUTH_CREDS_FILE=/etc/oauth2-auth-code-proxy/creds.yaml \ - -e OAUTH_TLS_CERT_FILE=/etc/oauth2-auth-code-proxy/tls.crt \ - -e OAUTH_TLS_KEY_FILE=/etc/oauth2-auth-code-proxy/tls.key \ + -e OAUTH_TLS_CERT_FILE=/etc/oauth2-auth-code-proxy/certs/tls.crt \ + -e OAUTH_TLS_KEY_FILE=/etc/oauth2-auth-code-proxy/certs/tls.key \ oauth2-auth-code-proxy:latest ``` +Alternatively, `make docker-local` builds a local-only image (`Dockerfile.local`) with `creds.yaml` and `certs/` baked in, so no mounts or env vars are needed. It runs `make certs` automatically. That image contains your secrets, so never push it: + +```bash +make docker-local + +docker run -d --name oauth2-proxy-test -p 30045:30045 -p 80:80 oauth2-auth-code-proxy-local:latest +``` + Connect from MATLAB using the local cert file: ```matlab