forked from aws/aws-cdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cdk-assets): externally-configured Docker credentials (aws#15290)
Currently, `cdk-assets` does a single `docker login` with credentials fetched from ECR's `getAuthorizationToken` API. This enables access to (typically) the assets in the environment's ECR repo (`*--container-assets-*`). A pain point for users today is throttling when using images from other sources, especially from DockerHub when using unauthenticated calls. This change introduces a new configuration file at a well-known location (and overridable via the CDK_DOCKER_CREDS_FILE environment variable), which allows specifying per-domain login credentials via either the default ECR auth tokens or via a secret in SecretsManager. If the credentials file is present, a Docker credential helper (docker-credential-cdk-assets) will be set up for each of the configured domains, and used for the `docker build` commands to enable fetching images from both DockerHub or configured ECR repos. Then the "normal" credentials will be assumed for the final publishing step. For backwards compatibility, if no credentials file is present, the existing `docker login` will be done prior to the build step as usual. This PR will be shortly followed by a corresponding PR for the cdk pipelines library to enable users to specify registries and credentials to be fed into this credentials file during various stages of the pipeline (e.g., build/synth, self-update, and asset publishing). Two refactorings here: - Moved obtainEcrCredentials from docker.ts to docker-credentials-ts. - Moved DefaultAwsClient from bin/publish.ts to lib/aws.ts related aws#10999 related aws#11774 ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
- Loading branch information
Showing
15 changed files
with
605 additions
and
134 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#!/usr/bin/env node | ||
require('./docker-credential-cdk-assets.js'); |
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,48 @@ | ||
/** | ||
* Docker Credential Helper to retrieve credentials based on an external configuration file. | ||
* Supports loading credentials from ECR repositories and from Secrets Manager, | ||
* optionally via an assumed role. | ||
* | ||
* The only operation currently supported by this credential helper at this time is the `get` | ||
* command, which receives a domain name as input on stdin and returns a Username/Secret in | ||
* JSON format on stdout. | ||
* | ||
* IMPORTANT - The credential helper must not output anything else besides the final credentials | ||
* in any success case; doing so breaks docker's parsing of the output and causes the login to fail. | ||
*/ | ||
|
||
import * as fs from 'fs'; | ||
import { DefaultAwsClient } from '../lib'; | ||
|
||
import { cdkCredentialsConfig, cdkCredentialsConfigFile, fetchDockerLoginCredentials } from '../lib/private/docker-credentials'; | ||
|
||
async function main() { | ||
// Expected invocation is [node, docker-credential-cdk-assets, get] with input fed via STDIN | ||
// For other valid docker commands (store, list, erase), we no-op. | ||
if (process.argv.length !== 3 || process.argv[2] !== 'get') { | ||
process.exit(0); | ||
} | ||
|
||
const config = cdkCredentialsConfig(); | ||
if (!config) { | ||
throw new Error(`unable to find CDK Docker credentials at: ${cdkCredentialsConfigFile()}`); | ||
} | ||
|
||
// Read the domain to fetch from stdin | ||
let rawDomain = fs.readFileSync(0, { encoding: 'utf-8' }).trim(); | ||
// Paranoid handling to ensure new URL() doesn't throw if the schema is missing. | ||
// Not convinced docker will ever pass in a url like 'index.docker.io/v1', but just in case... | ||
rawDomain = rawDomain.includes('://') ? rawDomain : `https://${rawDomain}`; | ||
const domain = new URL(rawDomain).hostname; | ||
|
||
const credentials = await fetchDockerLoginCredentials(new DefaultAwsClient(), config, domain); | ||
|
||
// Write the credentials back to stdout | ||
fs.writeFileSync(1, JSON.stringify(credentials)); | ||
} | ||
|
||
main().catch(e => { | ||
// eslint-disable-next-line no-console | ||
console.error(e.stack); | ||
process.exitCode = 1; | ||
}); |
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
Oops, something went wrong.