Skip to content

Commit

Permalink
resetAuthPlugins simply resets to default
Browse files Browse the repository at this point in the history
  • Loading branch information
njlynch committed Jun 25, 2021
1 parent 084155e commit d45eeb2
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 14 deletions.
15 changes: 5 additions & 10 deletions packages/cdk-assets/lib/private/docker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,10 @@ export class Docker {
const config = cdkCredentialsConfig();
if (!config) { return false; }

this.resetAuthPlugins();
// Should never happen; means resetAuthPlugins is broken.
if (!this.configDir) { throw new Error('no active docker config directory selected'); }
this.configDir = fs.mkdtempSync(path.join(os.tmpdir(), 'cdkDockerConfig'));

const domains = Object.keys(config.domainCredentials);
const credHelpers = domains.reduce(function(map: Record<string, string>, domain) {
const credHelpers = domains.reduce((map: Record<string, string>, domain) => {
map[domain] = 'cdk-assets'; // Use docker-credential-cdk-assets for this domain
return map;
}, {});
Expand All @@ -114,16 +112,13 @@ export class Docker {
}

/**
* Creates a new empty Docker config directory.
* All future commands (e.g., `build`, `push`) will use this config.
* Removes any configured Docker config directory.
* All future commands (e.g., `build`, `push`) will use the default config.
*
* This is useful after calling `configureCdkCredentials` to reset to default credentials.
*
* @returns the path to the directory
*/
public resetAuthPlugins() {
this.configDir = fs.mkdtempSync(path.join(os.tmpdir(), 'cdkDockerConfig'));
return this.configDir;
this.configDir = undefined;
}

private async execute(args: string[], options: ShellOptions = {}) {
Expand Down
7 changes: 3 additions & 4 deletions packages/cdk-assets/test/docker-images.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,6 @@ test('when external credentials are present, explicit Docker config directories
// Setup -- Mock that we have CDK credentials, and mock fs operations.
jest.spyOn(dockercreds, 'cdkCredentialsConfig').mockReturnValue({ version: '0.1', domainCredentials: {} });
jest.spyOn(fs, 'mkdtempSync').mockImplementationOnce(() => '/tmp/mockedTempDir');
jest.spyOn(fs, 'mkdtempSync').mockImplementationOnce(() => '/tmp/otherMockedTempDir');
jest.spyOn(fs, 'writeFileSync').mockImplementation(jest.fn());

let pub = new AssetPublishing(AssetManifest.fromPath('/simple/cdk.out'), { aws });
Expand All @@ -243,9 +242,9 @@ test('when external credentials are present, explicit Docker config directories
{ commandLine: ['docker', '--config', '/tmp/mockedTempDir', 'inspect', 'cdkasset-theasset'], exitCode: 1 },
{ commandLine: ['docker', '--config', '/tmp/mockedTempDir', 'build', '--tag', 'cdkasset-theasset', '.'], cwd: absoluteDockerPath },
{ commandLine: ['docker', '--config', '/tmp/mockedTempDir', 'tag', 'cdkasset-theasset', '12345.amazonaws.com/repo:abcdef'] },
// Prior to push, change to a new (fresh) config directory
{ commandLine: ['docker', '--config', '/tmp/otherMockedTempDir', 'login'], prefix: true },
{ commandLine: ['docker', '--config', '/tmp/otherMockedTempDir', 'push', '12345.amazonaws.com/repo:abcdef'] },
// Prior to push, revert to the default config directory
{ commandLine: ['docker', 'login'], prefix: true },
{ commandLine: ['docker', 'push', '12345.amazonaws.com/repo:abcdef'] },
);

await pub.publish();
Expand Down

0 comments on commit d45eeb2

Please sign in to comment.