Skip to content

Commit fe79b27

Browse files
committed
chore: proposal
1 parent d36b69e commit fe79b27

18 files changed

Lines changed: 209 additions & 71 deletions
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
name: "Log in to the ECR pull-through cache (OIDC)"
2+
description: >
3+
Assumes the repo's OIDC role, logs Docker in to the ECR pull-through cache and
4+
exports BASE_REGISTRY so the native JNI build (src/main/jni/build-jni-lib.sh)
5+
pulls its base images from there instead of public.ecr.aws. Anonymous pulls
6+
from shared GitHub-hosted runner IPs regularly hit public.ecr.aws's 429
7+
"Data limit exceeded".
8+
9+
Pull requests from forks get neither secrets nor an OIDC token, so role-to-assume
10+
arrives empty. The action then does nothing, BASE_REGISTRY stays unset, and the
11+
build falls back to its public.ecr.aws default. The calling job needs
12+
`permissions: id-token: write`.
13+
14+
The role's credentials and region are only given to the login step; nothing is
15+
exported to the job environment, so later steps (make, mvn test, the docker
16+
builds) never see them.
17+
18+
inputs:
19+
role-to-assume:
20+
description: "ARN of the OIDC role allowed to pull through the cache. Empty skips the login."
21+
required: false
22+
default: ""
23+
aws-region:
24+
description: "Region of the pull-through cache."
25+
required: false
26+
default: ""
27+
aws-account-id:
28+
description: "Account that owns the pull-through cache."
29+
required: false
30+
default: ""
31+
32+
runs:
33+
using: composite
34+
steps:
35+
- name: Skip ECR login when no role is available
36+
if: inputs.role-to-assume == ''
37+
shell: bash
38+
run: echo "No ECR pull-through role (fork PR or unset secret); base images come from public.ecr.aws."
39+
40+
- name: Assume the ECR pull-through role
41+
id: creds
42+
if: inputs.role-to-assume != ''
43+
continue-on-error: true
44+
uses: aws-actions/configure-aws-credentials@7474bc4690e29a8392af63c5b98e7449536d5c3a # v4
45+
with:
46+
aws-region: ${{ inputs.aws-region }}
47+
role-to-assume: ${{ inputs.role-to-assume }}
48+
role-session-name: GitHubActionsEcrPullThrough
49+
output-env-credentials: false
50+
output-credentials: true
51+
52+
# BASE_REGISTRY is only exported once the login succeeds and both JNI base
53+
# images resolve through the cache (the first pull of a tag needs
54+
# BatchImportUpstreamImage). On any failure the build keeps the
55+
# public.ecr.aws default rather than failing on the private registry.
56+
- name: Log in and export BASE_REGISTRY
57+
if: inputs.role-to-assume != '' && steps.creds.outcome == 'success'
58+
shell: bash
59+
env:
60+
AWS_ACCESS_KEY_ID: ${{ steps.creds.outputs.aws-access-key-id }}
61+
AWS_SECRET_ACCESS_KEY: ${{ steps.creds.outputs.aws-secret-access-key }}
62+
AWS_SESSION_TOKEN: ${{ steps.creds.outputs.aws-session-token }}
63+
AWS_REGION: ${{ inputs.aws-region }}
64+
ECR_REGISTRY: ${{ inputs.aws-account-id }}.dkr.ecr.${{ inputs.aws-region }}.amazonaws.com
65+
run: |
66+
if ! password=$(aws ecr get-login-password --region "$AWS_REGION"); then
67+
echo "::warning::ECR login failed; base images come from public.ecr.aws."
68+
exit 0
69+
fi
70+
echo "::add-mask::$password"
71+
if ! docker login --username AWS --password-stdin "$ECR_REGISTRY" <<< "$password"; then
72+
echo "::warning::ECR login failed; base images come from public.ecr.aws."
73+
exit 0
74+
fi
75+
# Same images as src/main/jni/Dockerfile.glibc and Dockerfile.musl, plus the
76+
# CodeBuild agent (test/integration/codebuild-local/Dockerfile.agent) and the
77+
# local-builds image test_one.sh runs it with (tagged per architecture).
78+
if [ "$(uname -m)" = "aarch64" ]; then local_builds_tag=aarch64; else local_builds_tag=latest; fi
79+
for image in amazonlinux/amazonlinux:2 docker/library/alpine:3 amazoncorretto/amazoncorretto:8 \
80+
"codebuild/local-builds:$local_builds_tag"; do
81+
if ! docker buildx imagetools inspect "$ECR_REGISTRY/ecr-public/$image" > /dev/null; then
82+
echo "::warning::$image is not reachable through the ECR pull-through cache; base images come from public.ecr.aws."
83+
exit 0
84+
fi
85+
done
86+
# The integration tests run docker inside a CodeBuild local container, which
87+
# doesn't see this runner's docker login. test_one.sh hands it this
88+
# file so the container can log in itself.
89+
password_file="$RUNNER_TEMP/ecr-pull-through-password"
90+
(umask 077 && printf '%s' "$password" > "$password_file")
91+
echo "ECR_PASSWORD_FILE=$password_file" >> "$GITHUB_ENV"
92+
echo "BASE_REGISTRY=$ECR_REGISTRY/ecr-public" >> "$GITHUB_ENV"
93+
94+
- name: Warn on role assumption failure
95+
if: inputs.role-to-assume != '' && steps.creds.outcome != 'success'
96+
shell: bash
97+
run: echo "::warning::Could not assume the ECR pull-through role; base images come from public.ecr.aws."

‎.github/workflows/runtime-interface-client_merge_to_main.yml‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ on:
1717
paths:
1818
- 'aws-lambda-java-runtime-interface-client/**'
1919
- '.github/workflows/runtime-interface-client_*.yml'
20+
- '.github/actions/**'
2021

2122
jobs:
2223

@@ -47,7 +48,14 @@ jobs:
4748

4849
- name: Available buildx platforms
4950
run: echo ${{ steps.buildx.outputs.platforms }}
50-
51+
52+
- name: Log in to the ECR pull-through cache
53+
uses: ./.github/actions/ecr-pull-through-login
54+
with:
55+
role-to-assume: ${{ secrets.AWS_ROLE_ECR_PULL_THROUGH }}
56+
aws-region: ${{ vars.AWS_REGION_MAVEN_RELEASE }}
57+
aws-account-id: ${{ secrets.AWS_ACCOUNT_ID }}
58+
5159
- name: Build and install serialization dependency locally
5260
working-directory: ./aws-lambda-java-serialization
5361
run: mvn clean install
@@ -96,6 +104,7 @@ jobs:
96104
# codecov/project status on identical code.
97105
coverage:
98106
permissions:
107+
id-token: write # ECR pull-through login
99108
contents: read
100109
strategy:
101110
fail-fast: false
@@ -130,6 +139,13 @@ jobs:
130139
working-directory: ./aws-lambda-java-serialization
131140
run: mvn clean install
132141

142+
- name: Log in to the ECR pull-through cache
143+
uses: ./.github/actions/ecr-pull-through-login
144+
with:
145+
role-to-assume: ${{ secrets.AWS_ROLE_ECR_PULL_THROUGH }}
146+
aws-region: ${{ vars.AWS_REGION_MAVEN_RELEASE }}
147+
aws-account-id: ${{ secrets.AWS_ACCOUNT_ID }}
148+
133149
- name: Build Runtime Interface Client - Run 'build-${{ matrix.arch }}' target
134150
working-directory: ./aws-lambda-java-runtime-interface-client
135151
run: make build-${{ matrix.arch }}

‎.github/workflows/runtime-interface-client_pr.yml‎

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ permissions:
1616
jobs:
1717

1818
smoke-test-arch:
19+
permissions:
20+
id-token: write # ECR pull-through login; forks don't get a token
21+
contents: read
1922
strategy:
2023
fail-fast: true
2124
matrix:
@@ -44,13 +47,23 @@ jobs:
4447
working-directory: ./aws-lambda-java-serialization
4548
run: mvn clean install
4649

50+
- name: Log in to the ECR pull-through cache
51+
uses: ./.github/actions/ecr-pull-through-login
52+
with:
53+
role-to-assume: ${{ secrets.AWS_ROLE_ECR_PULL_THROUGH }}
54+
aws-region: ${{ vars.AWS_REGION_MAVEN_RELEASE }}
55+
aws-account-id: ${{ secrets.AWS_ACCOUNT_ID }}
56+
4757
- name: Runtime Interface Client smoke tests - Run 'pr-${{ matrix.arch }}' target
4858
working-directory: ./aws-lambda-java-runtime-interface-client
4959
run: make pr-${{ matrix.arch }}
5060
env:
5161
IS_JAVA_8: true
5262

5363
build-arch:
64+
permissions:
65+
id-token: write # ECR pull-through login; forks don't get a token
66+
contents: read
5467
strategy:
5568
fail-fast: true
5669
matrix:
@@ -84,6 +97,13 @@ jobs:
8497
working-directory: ./aws-lambda-java-serialization
8598
run: mvn clean install
8699

100+
- name: Log in to the ECR pull-through cache
101+
uses: ./.github/actions/ecr-pull-through-login
102+
with:
103+
role-to-assume: ${{ secrets.AWS_ROLE_ECR_PULL_THROUGH }}
104+
aws-region: ${{ vars.AWS_REGION_MAVEN_RELEASE }}
105+
aws-account-id: ${{ secrets.AWS_ACCOUNT_ID }}
106+
87107
- name: Test Runtime Interface Client build - Run 'build-${{ matrix.arch }}' target
88108
working-directory: ./aws-lambda-java-runtime-interface-client
89109
run: make build-${{ matrix.arch }}
@@ -107,12 +127,16 @@ jobs:
107127
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
108128

109129
integration-test-matrix:
130+
permissions:
131+
id-token: write # ECR pull-through login; forks don't get a token
132+
contents: read
110133
runs-on: ${{ matrix.arch.runner }}
111134
strategy:
112135
# Run every OS/arch combination to completion so one failure doesn't mask the others.
113136
fail-fast: false
114-
# Every shard pulls its images anonymously from public.ecr.aws, and shards
115-
# started together trip its rate limit (429), so cap how many run at once.
137+
# Fork PRs get no OIDC token for the ECR pull-through cache, so their shards
138+
# pull anonymously from public.ecr.aws, and shards started together trip
139+
# its rate limit (429). Cap how many run at once.
116140
max-parallel: 4
117141
matrix:
118142
buildspec:
@@ -146,6 +170,13 @@ jobs:
146170
with:
147171
install: true
148172

173+
- name: Log in to the ECR pull-through cache
174+
uses: ./.github/actions/ecr-pull-through-login
175+
with:
176+
role-to-assume: ${{ secrets.AWS_ROLE_ECR_PULL_THROUGH }}
177+
aws-region: ${{ vars.AWS_REGION_MAVEN_RELEASE }}
178+
aws-account-id: ${{ secrets.AWS_ACCOUNT_ID }}
179+
149180
- name: Run OS integration test - 'test-integ' target
150181
working-directory: ./aws-lambda-java-runtime-interface-client
151182
run: make test-integ BUILDSPEC=test/integration/codebuild/${{ matrix.buildspec }}

‎aws-lambda-java-runtime-interface-client/Makefile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ test:
2828
setup-codebuild-agent:
2929
test/integration/codebuild-local/docker-retry.sh docker build --load -t codebuild-agent \
3030
--build-arg ARCHITECTURE=$(ARCHITECTURE_ALIAS) \
31+
--build-arg BASE_REGISTRY=$(or $(BASE_REGISTRY),public.ecr.aws) \
3132
-f test/integration/codebuild-local/Dockerfile.agent \
3233
test/integration/codebuild-local
3334

‎aws-lambda-java-runtime-interface-client/test/integration/codebuild-local/Dockerfile.agent‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
# syntax=docker/dockerfile:1.4
2-
FROM public.ecr.aws/amazoncorretto/amazoncorretto:8
1+
# No "# syntax=" directive: it would pull the frontend from Docker Hub, and the
2+
# built-in one already supports the heredoc below.
3+
# Set to the ECR pull-through cache by .github/actions/ecr-pull-through-login.
4+
ARG BASE_REGISTRY=public.ecr.aws
5+
FROM ${BASE_REGISTRY}/amazoncorretto/amazoncorretto:8
36

47
# Install docker and buildx extension.
58
# gnupg2 provides gpg/gpgv/gpg-agent so build-jni-lib.sh can GPG-verify the prebuilt assets.

‎aws-lambda-java-runtime-interface-client/test/integration/codebuild-local/test_one.sh‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,15 @@ function usage {
1818
>&2 echo " env Additional environment variables file."
1919
}
2020

21-
# codebuild/local-builds images are not multi-architectural
21+
# codebuild/local-builds images are not multi-architectural.
22+
# BASE_REGISTRY points at the ECR pull-through cache when
23+
# .github/actions/ecr-pull-through-login has logged this runner in.
2224
function get_local_agent_image() {
25+
local registry="${BASE_REGISTRY:-public.ecr.aws}"
2326
if [[ "$(arch)" == "aarch64" ]]; then
24-
echo "public.ecr.aws/codebuild/local-builds:aarch64"
27+
echo "$registry/codebuild/local-builds:aarch64"
2528
else
26-
echo "public.ecr.aws/codebuild/local-builds:latest"
29+
echo "$registry/codebuild/local-builds:latest"
2730
fi
2831
}
2932

@@ -58,6 +61,16 @@ main() {
5861
echo "PLATFORM=$PLATFORM"
5962
} >> "$ENVFILE"
6063

64+
# Set by .github/actions/ecr-pull-through-login. The build container has its
65+
# own docker config, so it logs in to the pull-through cache with this token
66+
# (see the install phase of the buildspecs).
67+
if [ -n "${BASE_REGISTRY:-}" ] && [ -f "${ECR_PASSWORD_FILE:-}" ]; then
68+
{
69+
echo "BASE_REGISTRY=$BASE_REGISTRY"
70+
echo "ECR_PASSWORD=$(cat "$ECR_PASSWORD_FILE")"
71+
} >> "$ENVFILE"
72+
fi
73+
6174
ARTIFACTS_DIR="$CODEBUILD_TEMP_DIR/artifacts"
6275
mkdir -p "$ARTIFACTS_DIR"
6376

‎aws-lambda-java-runtime-interface-client/test/integration/codebuild/buildspec.os.alpine.yml‎

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,8 @@ batch:
2525
phases:
2626
install:
2727
commands:
28-
- >
29-
if [[ -z "${DOCKERHUB_USERNAME}" && -z "${DOCKERHUB_PASSWORD}" ]];
30-
then
31-
echo "DockerHub credentials not set as CodeBuild environment variables. Continuing without docker login."
32-
else
33-
echo "Performing DockerHub login . . ."
34-
docker login -u $DOCKERHUB_USERNAME -p $DOCKERHUB_PASSWORD
35-
fi
28+
# Sourced, not run: it unsets BASE_REGISTRY here when the ECR login fails.
29+
- . aws-lambda-java-runtime-interface-client/test/integration/codebuild/scripts/docker_login.sh
3630
- aws-lambda-java-runtime-interface-client/test/integration/codebuild/scripts/configure_multi_arch_env.sh
3731
pre_build:
3832
commands:
@@ -75,7 +69,8 @@ phases:
7569
-t "${IMAGE_TAG}" \
7670
--platform="${PLATFORM}" \
7771
--build-arg RUNTIME_VERSION="${RUNTIME_VERSION}" \
78-
--build-arg DISTRO_VERSION="${DISTRO_VERSION}"
72+
--build-arg DISTRO_VERSION="${DISTRO_VERSION}" \
73+
--build-arg BASE_REGISTRY="${BASE_REGISTRY:-public.ecr.aws}"
7974
build:
8075
commands:
8176
- aws-lambda-java-runtime-interface-client/test/integration/codebuild/scripts/run_invocation_test.sh

‎aws-lambda-java-runtime-interface-client/test/integration/codebuild/buildspec.os.amazoncorretto.yml‎

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,8 @@ batch:
2626
phases:
2727
install:
2828
commands:
29-
- >
30-
if [[ -z "${DOCKERHUB_USERNAME}" && -z "${DOCKERHUB_PASSWORD}" ]];
31-
then
32-
echo "DockerHub credentials not set as CodeBuild environment variables. Continuing without docker login."
33-
else
34-
echo "Performing DockerHub login . . ."
35-
docker login -u $DOCKERHUB_USERNAME -p $DOCKERHUB_PASSWORD
36-
fi
29+
# Sourced, not run: it unsets BASE_REGISTRY here when the ECR login fails.
30+
- . aws-lambda-java-runtime-interface-client/test/integration/codebuild/scripts/docker_login.sh
3731
- aws-lambda-java-runtime-interface-client/test/integration/codebuild/scripts/configure_multi_arch_env.sh
3832
pre_build:
3933
commands:
@@ -73,7 +67,8 @@ phases:
7367
-t "${IMAGE_TAG}" \
7468
--platform="${PLATFORM}" \
7569
--build-arg RUNTIME_VERSION="${RUNTIME_VERSION}" \
76-
--build-arg DISTRO_VERSION="${DISTRO_VERSION}"
70+
--build-arg DISTRO_VERSION="${DISTRO_VERSION}" \
71+
--build-arg BASE_REGISTRY="${BASE_REGISTRY:-public.ecr.aws}"
7772
build:
7873
commands:
7974
- aws-lambda-java-runtime-interface-client/test/integration/codebuild/scripts/run_invocation_test.sh

‎aws-lambda-java-runtime-interface-client/test/integration/codebuild/buildspec.os.amazonlinux.1.yml‎

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,8 @@ batch:
2222
phases:
2323
install:
2424
commands:
25-
- >
26-
if [[ -z "${DOCKERHUB_USERNAME}" && -z "${DOCKERHUB_PASSWORD}" ]];
27-
then
28-
echo "DockerHub credentials not set as CodeBuild environment variables. Continuing without docker login."
29-
else
30-
echo "Performing DockerHub login . . ."
31-
docker login -u $DOCKERHUB_USERNAME -p $DOCKERHUB_PASSWORD
32-
fi
25+
# Sourced, not run: it unsets BASE_REGISTRY here when the ECR login fails.
26+
- . aws-lambda-java-runtime-interface-client/test/integration/codebuild/scripts/docker_login.sh
3327
pre_build:
3428
commands:
3529
# Log some environment variables for troubleshooting
@@ -59,7 +53,8 @@ phases:
5953
-f "${SCRATCH_DIR}/Dockerfile.function.${OS_DISTRIBUTION}.tmp" \
6054
-t "${IMAGE_TAG}" \
6155
--build-arg RUNTIME_VERSION="${RUNTIME_VERSION}" \
62-
--build-arg DISTRO_VERSION="${DISTRO_VERSION}"
56+
--build-arg DISTRO_VERSION="${DISTRO_VERSION}" \
57+
--build-arg BASE_REGISTRY="${BASE_REGISTRY:-public.ecr.aws}"
6358
build:
6459
commands:
6560
- aws-lambda-java-runtime-interface-client/test/integration/codebuild/scripts/run_invocation_test.sh

‎aws-lambda-java-runtime-interface-client/test/integration/codebuild/buildspec.os.amazonlinux.2.yml‎

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,8 @@ batch:
2525
phases:
2626
install:
2727
commands:
28-
- >
29-
if [[ -z "${DOCKERHUB_USERNAME}" && -z "${DOCKERHUB_PASSWORD}" ]];
30-
then
31-
echo "DockerHub credentials not set as CodeBuild environment variables. Continuing without docker login."
32-
else
33-
echo "Performing DockerHub login . . ."
34-
docker login -u $DOCKERHUB_USERNAME -p $DOCKERHUB_PASSWORD
35-
fi
28+
# Sourced, not run: it unsets BASE_REGISTRY here when the ECR login fails.
29+
- . aws-lambda-java-runtime-interface-client/test/integration/codebuild/scripts/docker_login.sh
3630
- aws-lambda-java-runtime-interface-client/test/integration/codebuild/scripts/configure_multi_arch_env.sh
3731
pre_build:
3832
commands:
@@ -72,7 +66,8 @@ phases:
7266
-t "${IMAGE_TAG}" \
7367
--platform="${PLATFORM}" \
7468
--build-arg RUNTIME_VERSION="${RUNTIME_VERSION}" \
75-
--build-arg DISTRO_VERSION="${DISTRO_VERSION}"
69+
--build-arg DISTRO_VERSION="${DISTRO_VERSION}" \
70+
--build-arg BASE_REGISTRY="${BASE_REGISTRY:-public.ecr.aws}"
7671
build:
7772
commands:
7873
- aws-lambda-java-runtime-interface-client/test/integration/codebuild/scripts/run_invocation_test.sh

0 commit comments

Comments
 (0)