Skip to content

Commit ba00abd

Browse files
nachorpaezampagent
andauthored
ci: authenticate Docker Hub with OIDC (SEC-4519) (#1417)
* ci: authenticate Docker Hub with OIDC (SEC-4519) Use the configured Docker OIDC connection instead of static Docker credentials, preserving release triggers and publishing behavior. Linear: https://linear.app/sourcegraph/issue/SEC-4519/configure-oidc-in-github-actions-that-log-in-to-docker Amp-Thread-ID: https://ampcode.com/threads/T-01a2180e-b11b-76a6-b0cd-520034d574a0 Co-authored-by: Amp <amp@ampcode.com> * ci: use SHA-pinned Go setup dependencies Update the shared Go setup action to its upstream full-SHA pinning fix. Amp-Thread-ID: https://ampcode.com/threads/T-01a2180e-b11b-76a6-b0cd-520034d574a0 Co-authored-by: Amp <amp@ampcode.com> * ci: scope Docker OIDC credentials to the published image Preserve default credentials for public Docker Hub base-image pulls. SEC-4519 Amp-Thread-ID: https://ampcode.com/threads/T-01a2180e-b11b-76a6-b0cd-520034d574a0 Co-authored-by: Amp <amp@ampcode.com> --------- Co-authored-by: Amp <amp@ampcode.com>
1 parent f2901d1 commit ba00abd

4 files changed

Lines changed: 23 additions & 23 deletions

File tree

‎.github/workflows/docker.yml‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ on:
1414
jobs:
1515
publish:
1616
runs-on: ubuntu-26.04
17+
permissions:
18+
contents: read
19+
id-token: write
1720
steps:
1821
- name: Checkout
1922
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
@@ -29,7 +32,12 @@ jobs:
2932
with:
3033
platforms: arm64
3134

32-
- run: ./docker/batch-change-volume-workspace/push.py -d ./docker/batch-change-volume-workspace/Dockerfile -i sourcegraph/src-batch-change-volume-workspace -p linux/amd64,linux/arm64,linux/386 --readme ./docker/batch-change-volume-workspace/README.md
35+
- name: Login to Docker Hub
36+
uses: docker/login-action@8aa5831fb8d48d8f76594c7ddde61ae58019f550 # v4.8.0
37+
with:
38+
username: sourcegraph
39+
scope: sourcegraph/src-batch-change-volume-workspace
3340
env:
34-
DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }}
35-
DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }}
41+
DOCKERHUB_OIDC_CONNECTIONID: 9f14c248-6ae5-4c2a-ad0c-3af4ca55a16e
42+
43+
- run: ./docker/batch-change-volume-workspace/push.py -d ./docker/batch-change-volume-workspace/Dockerfile -i sourcegraph/src-batch-change-volume-workspace -p linux/amd64,linux/arm64,linux/386 --readme ./docker/batch-change-volume-workspace/README.md

‎.github/workflows/goreleaser.yml‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,9 @@ jobs:
142142
name: Run goreleaser
143143
runs-on: ubuntu-latest
144144
needs: [release_type, goreleaser_pre]
145+
permissions:
146+
contents: read
147+
id-token: write
145148
# By default, this job will be skipped if either "needs" job is skipped. This tells
146149
# GitHub actions to always run it, so long as the previous jobs that ran didn't fail.
147150
if: |
@@ -178,15 +181,17 @@ jobs:
178181
id: mise
179182
run: echo "go-version=$(grep 'golang' mise.toml | cut -d'"' -f2)" >> $GITHUB_OUTPUT
180183
- name: Set up Go
181-
uses: sourcegraph/actions/go-setup@4430dc1e4dde002387a4b655b41f6431ca75b1c1 # main
184+
uses: sourcegraph/actions/go-setup@4c889fc8e48c06d4bbcb42a4b3a4965c2a83dccb # main
182185
with:
183186
go-version: ${{ steps.mise.outputs.go-version }}
184187
- run: go test ./...
185188
- run: go test -race -v ./...
186-
- run: echo "${DOCKER_PASSWORD}" | docker login -u=$DOCKER_USERNAME --password-stdin
189+
- name: Login to Docker Hub
190+
uses: docker/login-action@8aa5831fb8d48d8f76594c7ddde61ae58019f550 # v4.8.0
191+
with:
192+
username: sourcegraph
187193
env:
188-
DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }}
189-
DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }}
194+
DOCKERHUB_OIDC_CONNECTIONID: 9f14c248-6ae5-4c2a-ad0c-3af4ca55a16e
190195
- name: Run GoReleaser
191196
uses: goreleaser/goreleaser-action@a4f02ab5355a589de60d917a659ce617ed19b3de # v7.3.0
192197
with:

‎.github/workflows/scip.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jobs:
1111
id: mise
1212
run: echo "go-version=$(grep 'golang' mise.toml | cut -d'"' -f2)" >> $GITHUB_OUTPUT
1313
- name: Set up Go
14-
uses: sourcegraph/actions/go-setup@4430dc1e4dde002387a4b655b41f6431ca75b1c1 # main
14+
uses: sourcegraph/actions/go-setup@4c889fc8e48c06d4bbcb42a4b3a4965c2a83dccb # main
1515
with:
1616
go-version: ${{ steps.mise.outputs.go-version }}
1717

‎docker/batch-change-volume-workspace/push.py‎

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
# This is a very simple script to build and push the Docker image used by the
44
# Docker volume workspace driver. It's normally run from the "Publish Docker
55
# image dependencies" GitHub Action, but can be run locally if necessary.
6+
# It reuses Docker's existing authentication, set up by the workflow's OIDC
7+
# login or by running docker login locally.
68
#
79
# This script requires Python 3.8 or later, and Docker 19.03 (for buildx). You
810
# are strongly encouraged to use Black to format this file after modifying it.
@@ -87,14 +89,6 @@ def docker_cli_build(
8789
run(args, stdin=dockerfile)
8890

8991

90-
def docker_cli_login(username: str, password: str):
91-
run(
92-
["docker", "login", f"-u={username}", "--password-stdin"],
93-
input=password,
94-
text=True,
95-
)
96-
97-
9892
def run(args: Sequence[str], /, **kwargs) -> subprocess.CompletedProcess:
9993
print(f"+ {' '.join(args)}")
10094
return subprocess.run(args, check=True, **kwargs)
@@ -127,13 +121,6 @@ def main():
127121
tags = calculate_tags(args.ref)
128122
print(f"will push tags: {', '.join(tags)}")
129123

130-
print("logging into Docker Hub")
131-
try:
132-
docker_cli_login(os.environ["DOCKER_USERNAME"], os.environ["DOCKER_PASSWORD"])
133-
except KeyError as e:
134-
print(f"error retrieving environment variables: {e}")
135-
raise
136-
137124
print("building and pushing image")
138125
docker_cli_build(open(args.dockerfile, "rb"), args.platform, args.image, tags)
139126

0 commit comments

Comments
 (0)