Skip to content

Commit eb26b03

Browse files
authored
Merge pull request #266 from crazy-max/registry-identities-login-gate
build/bake: skip registry identity validation without registry login
2 parents 27ade87 + 874b69b commit eb26b03

6 files changed

Lines changed: 150 additions & 104 deletions

File tree

‎.github/workflows/.test-bake.yml‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,10 @@ jobs:
524524
context: test
525525
output: image
526526
push: false
527+
registry-identities: |
528+
- type: dockerhub
529+
username: dockereng
530+
connection_id: ${{ vars.MISSING_DOCKERHUB_OIDC_CONNECTIONID }}
527531
set: |
528532
*.args.VERSION={{meta.version}}
529533
target: hello-cross

‎.github/workflows/.test-build.yml‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,10 @@ jobs:
573573
output: image
574574
platforms: linux/amd64,linux/arm64
575575
push: false
576+
registry-identities: |
577+
- type: dockerhub
578+
username: dockereng
579+
connection_id: ${{ vars.MISSING_DOCKERHUB_OIDC_CONNECTIONID }}
576580
meta-images: ghcr.io/docker/github-builder-test
577581
meta-tags: |
578582
type=raw,value=build-${{ github.run_id }}

‎.github/workflows/bake.yml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ jobs:
195195
registry-identities:
196196
uses: ./.github/workflows/setup-registry-identities.yml
197197
with:
198+
registry-login: ${{ inputs.push && inputs.output == 'image' }}
198199
registry-identities: ${{ inputs.registry-identities }}
199200

200201
prepare:

‎.github/workflows/build.yml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ jobs:
206206
registry-identities:
207207
uses: ./.github/workflows/setup-registry-identities.yml
208208
with:
209+
registry-login: ${{ inputs.push && inputs.output == 'image' }}
209210
registry-identities: ${{ inputs.registry-identities }}
210211

211212
prepare:

‎.github/workflows/setup-registry-identities.yml‎

Lines changed: 119 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ on:
77
type: string
88
description: "Keyless registry identity configuration as YAML objects"
99
required: false
10+
registry-login:
11+
type: boolean
12+
description: "Whether registry login is enabled"
13+
required: false
14+
default: true
1015
outputs:
1116
aws-ecr-enabled:
1217
description: "Whether an AWS ECR registry identity was configured"
@@ -86,14 +91,22 @@ jobs:
8691
INPUT_RUNTIME-MODULE: ${{ env.RUNTIME_MODULE }}
8792
INPUT_RUNTIME-INSTALL-ARGS: ${{ env.RUNTIME_INSTALL_ARGS }}
8893
INPUT_REGISTRY-IDENTITIES: ${{ inputs.registry-identities }}
94+
INPUT_REGISTRY-LOGIN: ${{ inputs.registry-login }}
8995
with:
9096
script: |
97+
const registryLogin = core.getBooleanInput('registry-login');
98+
if (!registryLogin) {
99+
core.info('Registry authentication not required; skipping npm install');
100+
return;
101+
}
102+
91103
const registryIdentities = core.getInput('registry-identities', {trimWhitespace: false});
92104
if (!registryIdentities.trim()) {
93105
core.info('No registry identities configured; skipping npm install');
94106
return;
95107
}
96108
109+
core.info('Registry identities configured; installing validation dependencies');
97110
const npmArgs = ['install', ...core.getMultilineInput('runtime-install-args'), core.getInput('runtime-module')];
98111
const maxAttempts = 3;
99112
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
@@ -115,6 +128,7 @@ jobs:
115128
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
116129
env:
117130
INPUT_REGISTRY-IDENTITIES: ${{ inputs.registry-identities }}
131+
INPUT_REGISTRY-LOGIN: ${{ inputs.registry-login }}
118132
with:
119133
script: |
120134
const setEmptyOutputs = () => {
@@ -133,6 +147,13 @@ jobs:
133147
core.setOutput('dockerhub-oidc-connection-id', '');
134148
};
135149
150+
const registryLogin = core.getBooleanInput('registry-login');
151+
if (!registryLogin) {
152+
core.info('Registry authentication not required; skipping validation');
153+
setEmptyOutputs();
154+
return;
155+
}
156+
136157
const registryIdentities = core.getInput('registry-identities', {trimWhitespace: false});
137158
if (!registryIdentities.trim()) {
138159
core.info('No registry identities configured; skipping validation');
@@ -150,116 +171,130 @@ jobs:
150171
const fail = message => {
151172
throw new Error(`Invalid registry-identities input: ${message}`);
152173
};
174+
153175
const ensureObject = (value, path) => {
154176
if (!value || typeof value !== 'object' || Array.isArray(value)) {
155177
fail(`${path} must be an object`);
156178
}
157179
};
180+
158181
const requireString = (entry, key, path) => {
159182
const value = entry[key];
160183
if (typeof value !== 'string' || !value.trim()) {
161184
fail(`${path}.${key} must be a non-empty string`);
162185
}
163186
return value.trim();
164187
};
188+
165189
const optionalString = (entry, key, path, defaultValue) => {
166190
if (!Object.prototype.hasOwnProperty.call(entry, key)) {
167191
return defaultValue;
168192
}
169193
return requireString(entry, key, path);
170194
};
171-
let parsed;
195+
172196
try {
173-
parsed = yaml.load(registryIdentities);
174-
} catch (error) {
175-
fail(error.message);
176-
}
177-
if (parsed === null || parsed === undefined) {
178-
setEmptyOutputs();
179-
return;
180-
}
197+
let parsed;
198+
try {
199+
parsed = yaml.load(registryIdentities);
200+
} catch (error) {
201+
fail(error.message);
202+
}
203+
if (parsed === null || parsed === undefined) {
204+
core.info('Registry identities input is empty after parsing; disabling registry identity outputs');
205+
setEmptyOutputs();
206+
return;
207+
}
181208
182-
const entries = Array.isArray(parsed) ? parsed : [parsed];
183-
if (entries.length === 0) {
184-
setEmptyOutputs();
185-
return;
186-
}
209+
const entries = Array.isArray(parsed) ? parsed : [parsed];
210+
if (entries.length === 0) {
211+
core.info('No registry identity entries parsed; disabling registry identity outputs');
212+
setEmptyOutputs();
213+
return;
214+
}
215+
core.info(`Validating ${entries.length} registry identity ${entries.length === 1 ? 'entry' : 'entries'}`);
187216
188-
let awsEcr;
189-
let gcpWif;
190-
let dockerhubOidc;
191-
entries.forEach((entry, index) => {
192-
const path = `registry-identities[${index}]`;
193-
ensureObject(entry, path);
194-
const type = requireString(entry, 'type', path);
195-
switch (type) {
196-
case 'aws-ecr': {
197-
const allowedKeys = new Set(['type', 'registry', 'role-to-assume', 'region']);
198-
for (const key of Object.keys(entry)) {
199-
if (!allowedKeys.has(key)) {
200-
fail(`${path}.${key} is not supported for aws-ecr`);
217+
let awsEcr;
218+
let gcpWif;
219+
let dockerhubOidc;
220+
entries.forEach((entry, index) => {
221+
const path = `registry-identities[${index}]`;
222+
ensureObject(entry, path);
223+
const type = requireString(entry, 'type', path);
224+
switch (type) {
225+
case 'aws-ecr': {
226+
const allowedKeys = new Set(['type', 'registry', 'role-to-assume', 'region']);
227+
for (const key of Object.keys(entry)) {
228+
if (!allowedKeys.has(key)) {
229+
fail(`${path}.${key} is not supported for aws-ecr`);
230+
}
201231
}
202-
}
203-
if (awsEcr) {
204-
fail('only one aws-ecr registry identity is supported');
205-
}
206-
awsEcr = {
207-
registry: requireString(entry, 'registry', path),
208-
roleToAssume: requireString(entry, 'role-to-assume', path),
209-
region: requireString(entry, 'region', path)
210-
};
211-
break;
212-
}
213-
case 'gcp-wif': {
214-
const allowedKeys = new Set(['type', 'registry', 'workload_identity_provider', 'service_account', 'project_id']);
215-
for (const key of Object.keys(entry)) {
216-
if (!allowedKeys.has(key)) {
217-
fail(`${path}.${key} is not supported for gcp-wif`);
232+
if (awsEcr) {
233+
fail('only one aws-ecr registry identity is supported');
218234
}
235+
awsEcr = {
236+
registry: requireString(entry, 'registry', path),
237+
roleToAssume: requireString(entry, 'role-to-assume', path),
238+
region: requireString(entry, 'region', path)
239+
};
240+
core.info(`Configured aws-ecr registry identity for ${awsEcr.registry}`);
241+
break;
219242
}
220-
if (gcpWif) {
221-
fail('only one gcp-wif registry identity is supported');
222-
}
223-
gcpWif = {
224-
registry: requireString(entry, 'registry', path),
225-
workloadIdentityProvider: requireString(entry, 'workload_identity_provider', path),
226-
serviceAccount: requireString(entry, 'service_account', path),
227-
projectId: requireString(entry, 'project_id', path)
228-
};
229-
break;
230-
}
231-
case 'dockerhub': {
232-
const allowedKeys = new Set(['type', 'registry', 'username', 'connection_id']);
233-
for (const key of Object.keys(entry)) {
234-
if (!allowedKeys.has(key)) {
235-
fail(`${path}.${key} is not supported for dockerhub`);
243+
case 'gcp-wif': {
244+
const allowedKeys = new Set(['type', 'registry', 'workload_identity_provider', 'service_account', 'project_id']);
245+
for (const key of Object.keys(entry)) {
246+
if (!allowedKeys.has(key)) {
247+
fail(`${path}.${key} is not supported for gcp-wif`);
248+
}
236249
}
250+
if (gcpWif) {
251+
fail('only one gcp-wif registry identity is supported');
252+
}
253+
gcpWif = {
254+
registry: requireString(entry, 'registry', path),
255+
workloadIdentityProvider: requireString(entry, 'workload_identity_provider', path),
256+
serviceAccount: requireString(entry, 'service_account', path),
257+
projectId: optionalString(entry, 'project_id', path, '')
258+
};
259+
core.info(`Configured gcp-wif registry identity for ${gcpWif.registry}`);
260+
break;
237261
}
238-
if (dockerhubOidc) {
239-
fail('only one dockerhub registry identity is supported');
262+
case 'dockerhub': {
263+
const allowedKeys = new Set(['type', 'registry', 'username', 'connection_id']);
264+
for (const key of Object.keys(entry)) {
265+
if (!allowedKeys.has(key)) {
266+
fail(`${path}.${key} is not supported for dockerhub`);
267+
}
268+
}
269+
if (dockerhubOidc) {
270+
fail('only one dockerhub registry identity is supported');
271+
}
272+
dockerhubOidc = {
273+
registry: optionalString(entry, 'registry', path, 'docker.io'),
274+
username: requireString(entry, 'username', path),
275+
connectionID: requireString(entry, 'connection_id', path)
276+
};
277+
core.info(`Configured dockerhub registry identity for ${dockerhubOidc.registry}`);
278+
break;
240279
}
241-
dockerhubOidc = {
242-
registry: optionalString(entry, 'registry', path, 'docker.io'),
243-
username: requireString(entry, 'username', path),
244-
connectionID: requireString(entry, 'connection_id', path)
245-
};
246-
break;
280+
default:
281+
fail(`${path}.type has unsupported provider ${type}`);
247282
}
248-
default:
249-
fail(`${path}.type has unsupported provider ${type}`);
250-
}
251-
});
283+
});
252284
253-
core.setOutput('aws-ecr-enabled', awsEcr ? 'true' : 'false');
254-
core.setOutput('aws-ecr-registry', awsEcr?.registry || '');
255-
core.setOutput('aws-ecr-role-to-assume', awsEcr?.roleToAssume || '');
256-
core.setOutput('aws-ecr-region', awsEcr?.region || '');
257-
core.setOutput('gcp-wif-enabled', gcpWif ? 'true' : 'false');
258-
core.setOutput('gcp-wif-registry', gcpWif?.registry || '');
259-
core.setOutput('gcp-wif-workload-identity-provider', gcpWif?.workloadIdentityProvider || '');
260-
core.setOutput('gcp-wif-service-account', gcpWif?.serviceAccount || '');
261-
core.setOutput('gcp-wif-project-id', gcpWif?.projectId || '');
262-
core.setOutput('dockerhub-oidc-enabled', dockerhubOidc ? 'true' : 'false');
263-
core.setOutput('dockerhub-oidc-registry', dockerhubOidc?.registry || '');
264-
core.setOutput('dockerhub-oidc-username', dockerhubOidc?.username || '');
265-
core.setOutput('dockerhub-oidc-connection-id', dockerhubOidc?.connectionID || '');
285+
core.setOutput('aws-ecr-enabled', awsEcr ? 'true' : 'false');
286+
core.setOutput('aws-ecr-registry', awsEcr?.registry || '');
287+
core.setOutput('aws-ecr-role-to-assume', awsEcr?.roleToAssume || '');
288+
core.setOutput('aws-ecr-region', awsEcr?.region || '');
289+
core.setOutput('gcp-wif-enabled', gcpWif ? 'true' : 'false');
290+
core.setOutput('gcp-wif-registry', gcpWif?.registry || '');
291+
core.setOutput('gcp-wif-workload-identity-provider', gcpWif?.workloadIdentityProvider || '');
292+
core.setOutput('gcp-wif-service-account', gcpWif?.serviceAccount || '');
293+
core.setOutput('gcp-wif-project-id', gcpWif?.projectId || '');
294+
core.setOutput('dockerhub-oidc-enabled', dockerhubOidc ? 'true' : 'false');
295+
core.setOutput('dockerhub-oidc-registry', dockerhubOidc?.registry || '');
296+
core.setOutput('dockerhub-oidc-username', dockerhubOidc?.username || '');
297+
core.setOutput('dockerhub-oidc-connection-id', dockerhubOidc?.connectionID || '');
298+
} catch (error) {
299+
core.setFailed(error.message || error);
300+
}

0 commit comments

Comments
 (0)