-
Notifications
You must be signed in to change notification settings - Fork 378
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Securely build and push Docker images #292
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
--- | ||
# This action is dangerous and should be handled with a lot of care to avoid security problems. We use | ||
# `pull_request_target` event to give pull requests access to `GITHUB_TOKEN` secret with permissions | ||
# to publish Docker images. We want to do that to allow Dance to test those PRs. But rogue PR authors could | ||
# try to steal our secrets. We prevent that with the following: | ||
# | ||
# * We require approval for PRs from first-time contributors. That's a built-in feature for all actions. | ||
# * After reviewing changes, we require the `trust` label to be assigned to PRs by FerretDB maintainers. | ||
# Only a few trusted people have permission to do that. | ||
# * Thanks to the way `pull_request_target` trigger works, PR changes in the workflow itself are not run | ||
# until they are merged. | ||
# * We use a short-living automatic `GITHUB_TOKEN` instead of a long-living personal access token (PAT). | ||
# It also has minimal permissions. | ||
# * We publish Docker images from PRs as a separate package that should not be run by users. | ||
# * We limit what third-party actions can be used. | ||
|
||
# We also tried a different approach: build Docker image in one normal, secure `pull_request` workflow, | ||
# upload artifact, and the download and publish in another workflow that has access to secrets, but treats | ||
# artifact as passive data. We use buildx for building multi-platform images, and there is a way to export | ||
# multi-platform OCI tarball: https://docs.docker.com/engine/reference/commandline/buildx_build/#output | ||
# Unfortunately, it seems that there is no way to import that tarball in another workflow and publish it | ||
# as a Docker image, as strange as it sounds: https://github.com/docker/buildx/issues/186 | ||
|
||
# Relevant GitHub documentation is scattered. The first article gives a good overview: | ||
# * https://securitylab.github.com/research/github-actions-preventing-pwn-requests/ | ||
# * https://docs.github.com/en/actions/security-guides/automatic-token-authentication | ||
# * https://docs.github.com/en/actions/security-guides/security-hardening-for-github-actions | ||
# * https://docs.github.com/en/packages/learn-github-packages/configuring-a-packages-access-control-and-visibility | ||
# * https://docs.github.com/en/packages/managing-github-packages-using-github-actions-workflows/publishing-and-installing-a-package-with-github-action | ||
# * https://docs.github.com/en/packages/working-with-a-github-packages-registry/working-with-the-container-registry | ||
|
||
name: Docker | ||
on: | ||
pull_request_target: | ||
types: | ||
- labeled | ||
- opened | ||
- reopened | ||
- synchronize | ||
push: | ||
branches: | ||
- main | ||
schedule: | ||
- cron: '42 4 * * *' | ||
|
||
env: | ||
GOPATH: /home/runner/go | ||
GOCACHE: /home/runner/go/cache | ||
GOMODCACHE: /home/runner/go/cache/mod | ||
GOPROXY: https://proxy.golang.org # remove direct | ||
|
||
jobs: | ||
build: | ||
name: Build | ||
runs-on: ubuntu-20.04 | ||
|
||
if: github.event_name != 'pull_request_target' || contains(github.event.pull_request.labels.*.name, 'trust') | ||
|
||
permissions: | ||
packages: write | ||
|
||
steps: | ||
- name: Checkout code | ||
if: github.event_name != 'pull_request_target' | ||
uses: actions/checkout@v2 | ||
with: | ||
fetch-depth: 0 | ||
|
||
- name: Checkout pull request code | ||
if: github.event_name == 'pull_request_target' | ||
uses: actions/checkout@v2 | ||
with: | ||
fetch-depth: 0 | ||
ref: ${{ github.event.pull_request.head.sha }} | ||
|
||
- name: Setup Go | ||
uses: FerretDB/github-actions/setup-go@main | ||
with: | ||
cache-key: build | ||
|
||
- name: Run init | ||
run: make init | ||
|
||
- name: Initialize Docker Buildx builder | ||
run: make docker-init | ||
|
||
- name: Extract Docker image name and tag | ||
id: extract | ||
uses: FerretDB/github-actions/extract-docker-tag@main | ||
|
||
- name: Login to GitHub Container Registry | ||
uses: docker/login-action@v1 | ||
with: | ||
registry: ghcr.io | ||
username: ${{ github.actor }} | ||
password: ${{ secrets.GITHUB_TOKEN }} | ||
|
||
- name: Build Docker image ${{ steps.extract.outputs.ghcr }} | ||
run: make docker-push | ||
env: | ||
DOCKER_IMAGE: ${{ steps.extract.outputs.ghcr }} | ||
|
||
- name: Check dirty | ||
run: | | ||
git status | ||
git diff --exit-code |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ package version | |
|
||
import ( | ||
_ "embed" | ||
"fmt" | ||
"runtime/debug" | ||
"strconv" | ||
"strings" | ||
|
@@ -31,6 +32,9 @@ var ( | |
//go:embed version.txt | ||
version string | ||
|
||
//go:embed commit.txt | ||
commit string | ||
|
||
//go:embed branch.txt | ||
branch string | ||
) | ||
|
@@ -53,6 +57,7 @@ func Get() *Info { | |
func init() { | ||
info = &Info{ | ||
Version: strings.TrimSpace(version), | ||
Commit: strings.TrimSpace(commit), | ||
Branch: strings.TrimSpace(branch), | ||
} | ||
|
||
|
@@ -64,9 +69,12 @@ func init() { | |
info.BuildEnvironment = types.MustNewDocument() | ||
for _, s := range buildInfo.Settings { | ||
info.BuildEnvironment.Set(s.Key, s.Value) | ||
|
||
switch s.Key { | ||
case "vcs.revision": | ||
info.Commit = s.Value | ||
if s.Value != info.Commit { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like the way that this ensures that the version info which is used for builds is consistent with git. |
||
panic(fmt.Sprintf("commit.txt value %q != vcs.revision value %q", info.Commit, s.Value)) | ||
} | ||
case "vcs.modified": | ||
info.Dirty, _ = strconv.ParseBool(s.Value) | ||
case "-race": | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.