8000 Automate images re-spinning in GH actions by andreaTP · Pull Request #12144 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content
Closed
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
22 changes: 22 additions & 0 deletions .github/scripts/need-respin.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#! /bin/bash
set -euxo pipefail

image=$1
# Get Latest Keycloak release tag
tag=$(curl -sL http://github.com/keycloak/keycloak/releases/latest -H "Accept: application/json" | jq -r '.tag_name')

docker pull ${image}:${tag}

base_image_url=$(docker inspect ${image}:${tag} | jq -r '.[0].Config.Labels.url')

base_image_name=$(docker inspect ${image}:${tag} | jq -r '.[0].Config.Labels.url' | sed 's/.*#\///' | sed 's/\/images.*//')

docker pull ${base_image_name}:latest

latest_base_image_url=$(docker inspect ${base_image_name}:latest | jq -r '.[0].Config.Labels.url')
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is possible to access the manifests without downloading the images. It's a little tricky, as each repo seems to implement the dockerhttp API slightly differently (particularly around the access tokens), but it could save a lot of time.

Quay needs an anonymous token with the right service and scope parameters:

host=quay.io
repository=keycloak/keycloak
tag=18.0.0
token="$(curl -sSfL 'https://$host/v2/auth?service=$host&scope=repository:$repository:pull' | jq -r '.token')"
curl -sSfL -H "Authorization: Bearer $token" "https://$host/v2/$repository/manifests/$tag" | jq -r '.history[0].v1Compatibility' | jq -r '.config.Labels.url'

Which returns https://access.redhat.com/containers/#/registry.access.redhat.com/ubi8-minimal/images/8.6-751

Red Hat's repo provides tokenless anonymous access:

```bash
host=registry.access.redhat.com
repository=ubi8-minimal
tag=latest
curl -sSfL "https://$host/v2/$repository/manifests/$tag" | jq -r '.history[0].v1Compatibility' | jq -r '.config.Labels.url'

Which returns https://access.redhat.com/containers/#/registry.access.redhat.com/ubi8-minimal/images/8.6-751

So you could make your approach to the comparison faster based of off this method. It won't be as generic an approach as you're trying to write. However, the assumption around the url label isn't generic either, it's something that happens to work for UBI

That got me thinking, this check could also be done by comparing the layer hashes. The code for that is simpler, compared to parsing the label, but does require you to hardcode the parent URL:

curl -sSfL -H 'Authorization: Bearer $token' https://quay.io/v2/keycloak/keycloak/manifests/18.0.0 | jq -r '.fsLayers | .[] | .blobSum' > /tmp/layers-kc
curl -sSfL https://registry.access.redhat.com/v2/ubi8-minimal/manifests/latest | jq -r '.fsLayers | .[] | .blobSum' > /tmp/layers-ubi
grep -qxF -f /tmp/layers-kc /tmp/layers-ubi

Which would return exit code 0 if the latest ubi layers were present in the keycloak layers (no respin required), or exit code 1 if they were not (respin required)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the accurate review @ASzc !

With this implementation, I was working under the assumption that the job will run pretty seldom and I was preferring (as you noticed) "portability" over "speed".

You are correct that the code is not completely portable as it relies on RedHat specific UBI image labels but, at least, it will run on both quay.io and dockerhub which is convenient for testing IMHO; also, we are not likely to use an image that is not a UBI any time soon AFAIK.

I think I'm still inclined to keep the original implementation using docker pull, mostly because it still looks simpler (more dumb 🙂 ) to me (i.e. doesn't require knowledge about the docker HTTP API nor tokens etc.), but I'm happy to hear more feedback in this regard.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, it's a tradeoff :)

I think for a once-per-day check, the performance hit of downloading several hundred megabytes of data shouldn't be too bad


if [ "${base_image_url}" = "${latest_base_image_url}" ]; then
@abstractj abstractj Jun 6, 2022 •
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@andreaTP, thanks for submitting those changes. If I understood it correctly, the script will check if the base image had changed to perform the respin on the daily basis.

If that's the case, I'd suggest to respin the build of those images more frequently to fix known CVEs that will eventually emerge.

The latest UBI was released 27 days ago, that means that if there's a new CVE, the only way to remediate those CVEs is waiting until the next release to benefit from the fix, instead of trigger microdnf update.

Does it make some sense?

//cc @ASzc

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might have understood it wrongly, but, my impression was that @stianst expressed an opinion toward less frequent re-spins (e.g. max once a day) without quickly reacting to upstream to give users a more "stable" experience.

I'm open to discuss the tradeoffs and eventually steer the PR in the desired direction.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@andreaTP no problem, we can wait for more feedback. Even with less frequent respins, which is a valid concern. We need some agreement about when to patch those CVEs. As you are already aware, we have 2 alternatives:

  1. Patch CVEs only when there's a new UBI image
  2. Patch when there are some dependency updates + a new UBI image

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@abstractj agreed, my personal vote goes to option 1, leaving 2 "covered" by manual triggers.

A third option would be to check for dependencies updates at a "slower pace" e.g. once a week/month.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In theory UBI should be maintained at a Health Score of A at all times, so there shouldn't be significant public CVEs whenever you happen to pull the latest UBI, even though it could be many weeks old (29 days as of this comment, with no CVEs listed).

I don't think microdnf update would be required at any point to fix a CVE from UBI. Anything that's embargoed wouldn't show up in the updates, and it would be embargoed until a new UBI is published

exit -1 # no need respin
else
exit 0
fi
9 changes: 9 additions & 0 deletions .github/workflows/release-container.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
push:
tags:
- '[0-9]+.[0-9]+.[0-9]+'
schedule:
- cron: '7 0 * * *'
concurrency: keycloak-container-release

jobs:
Expand All @@ -14,6 +16,12 @@
- name: Checkout
uses: actions/checkout@v3

- name: Check respin
id: respin
continue-on-error: true
run: |
./.github/scripts/need-respin.sh quay.io/keycloak/keycloak

- name: Set up QEMU
uses: docker/setup-qemu-action@v2

Expand Down Expand Up @@ -46,6 +54,7 @@
type=semver,pattern={{major}}.{{minor}}

- name: Build and push the image
if: ${{ ( github.event_name != 'schedule' ) || ( github.event_name == 'schedule' && steps.respin.conclusion == 'success' ) }}
uses: docker/build-push-action@v3
with:
context: quarkus/container
Expand Down
9 changes: 9 additions & 0 deletions .github/workflows/release-operator-container.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ on:
push:
tags:
- '[0-9]+.[0-9]+.[0-9]+'
schedule:
- cron: '7 0 * * *'
concurrency: keycloak-operator-container-release

env:
Expand All @@ -17,6 +19,12 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Check respin
id: respin
continue-on-error: true
run: |
./.github/scripts/need-respin.sh quay.io/keycloak/keycloak-operator

- uses: actions/setup-java@v3
with:
Expand Down Expand Up @@ -56,6 +64,7 @@ jobs:

- name: Build and push the operator image
working-directory: operator
if: ${{ ( github.event_name != 'schedule' ) || ( github.event_name == 'schedule' && steps.respin.conclusion == 'success' ) }}
run: |
echo "${{ steps.meta.outputs.tags }}" | xargs -I {} \
mvn clean package \
Expand Down
0