-
Notifications
You must be signed in to change notification settings - Fork 3.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: preserve original docker config if credentialhelpers exists #30750
Open
gyalai-aws
wants to merge
8
commits into
aws:main
Choose a base branch
from
gyalai-aws:cdkasset-docker-config
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9bedc44
fix: preserve original docker config if credentialhelpers exists
gyalai-aws ee16cb1
Merge branch 'main' into cdkasset-docker-config
gyalai-aws 34e2878
fix: remove testing workaround
gyalai-aws 43ee88c
chore: linting findings corrected
gyalai-aws dd7b91b
fix: creating public getter for configDirectory
gyalai-aws f1a8103
Merge branch 'main' into cdkasset-docker-config
gyalai-aws c806ea5
Merge branch 'main' into cdkasset-docker-config
gyalai-aws 403697f
Merge branch 'main' into cdkasset-docker-config
gyalai-aws File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,14 @@ | ||
import { readFileSync } from 'fs'; | ||
import * as os from 'os'; | ||
import * as path from 'path'; | ||
import * as mockfs from 'mock-fs'; | ||
import { Docker } from '../../lib/private/docker'; | ||
import { ShellOptions, ProcessFailedError } from '../../lib/private/shell'; | ||
|
||
type ShellExecuteMock = jest.SpyInstance<ReturnType<Docker['execute']>, Parameters<Docker['execute']>>; | ||
|
||
const _ENV = process.env; | ||
|
||
describe('Docker', () => { | ||
describe('exists', () => { | ||
let docker: Docker; | ||
|
@@ -42,9 +48,9 @@ describe('Docker', () => { | |
test('throws when the error is a shell failure but the exit code is unrecognized', async () => { | ||
makeShellExecuteMock(() => { | ||
throw new (class extends Error implements ProcessFailedError { | ||
public readonly code = 'PROCESS_FAILED' | ||
public readonly exitCode = 47 | ||
public readonly signal = null | ||
public readonly code = 'PROCESS_FAILED'; | ||
public readonly exitCode = 47; | ||
public readonly signal = null; | ||
|
||
constructor() { | ||
super('foo'); | ||
|
@@ -58,9 +64,9 @@ describe('Docker', () => { | |
test('returns false when the error is a shell failure and the exit code is 1 (Docker)', async () => { | ||
makeShellExecuteMock(() => { | ||
throw new (class extends Error implements ProcessFailedError { | ||
public readonly code = 'PROCESS_FAILED' | ||
public readonly exitCode = 1 | ||
public readonly signal = null | ||
public readonly code = 'PROCESS_FAILED'; | ||
public readonly exitCode = 1; | ||
public readonly signal = null; | ||
|
||
constructor() { | ||
super('foo'); | ||
|
@@ -76,9 +82,9 @@ describe('Docker', () => { | |
test('returns false when the error is a shell failure and the exit code is 125 (Podman)', async () => { | ||
makeShellExecuteMock(() => { | ||
throw new (class extends Error implements ProcessFailedError { | ||
public readonly code = 'PROCESS_FAILED' | ||
public readonly exitCode = 125 | ||
public readonly signal = null | ||
public readonly code = 'PROCESS_FAILED'; | ||
public readonly exitCode = 125; | ||
public readonly signal = null; | ||
|
||
constructor() { | ||
super('foo'); | ||
|
@@ -91,4 +97,164 @@ describe('Docker', () => { | |
expect(imageExists).toBe(false); | ||
}); | ||
}); | ||
|
||
describe('dockerConfigFile', () => { | ||
let docker: Docker; | ||
|
||
beforeEach(() => { | ||
docker = new Docker(); | ||
process.env = { ..._ENV }; | ||
}); | ||
|
||
afterEach(() => { | ||
process.env = _ENV; | ||
}); | ||
|
||
test('Can be overridden by CDK_DOCKER_CONFIG_FILE', () => { | ||
const configFile = '/tmp/insertfilenamehere_docker_config.json'; | ||
process.env.CDK_DOCKER_CONFIG_FILE = configFile; | ||
|
||
expect(docker.dockerConfigFile()).toEqual(configFile); | ||
}); | ||
|
||
test('Uses homedir if no process env is set', () => { | ||
expect(docker.dockerConfigFile()).toEqual(path.join(os.userInfo().homedir, '.docker', 'config.json')); | ||
}); | ||
}); | ||
|
||
describe('dockerConfig', () => { | ||
let docker: Docker; | ||
const configFile = '/tmp/foo/bar/does/not/exist/config.json'; | ||
afterEach(() => { | ||
jest.resetModules(); | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
beforeEach(() => { | ||
docker = new Docker(); | ||
process.env.CDK_DOCKER_CONFIG_FILE = configFile; | ||
}); | ||
|
||
afterEach(() => { | ||
mockfs.restore(); | ||
process.env = { ..._ENV }; | ||
}); | ||
|
||
test('returns empty object if no config exists', () => { | ||
expect(docker.dockerConfig()).toEqual({}); | ||
}); | ||
|
||
test('returns parsed config if it exists', () => { | ||
mockfs({ | ||
[configFile]: JSON.stringify({ | ||
proxies: { | ||
default: { | ||
httpProxy: 'http://proxy.com', | ||
httpsProxy: 'https://proxy.com', | ||
noProxy: '.amazonaws.com', | ||
}, | ||
}, | ||
}), | ||
}); | ||
|
||
const config = docker.dockerConfig(); | ||
expect(config).toBeDefined(); | ||
expect(config.proxies).toEqual({ | ||
default: { | ||
httpProxy: 'http://proxy.com', | ||
httpsProxy: 'https://proxy.com', | ||
noProxy: '.amazonaws.com', | ||
}, | ||
}); | ||
}); | ||
}); | ||
|
||
describe('configureCdkCredentials', () => { | ||
let docker: Docker; | ||
const credsFile = '/tmp/foo/bar/does/not/exist/config-cred.json'; | ||
const configFile = '/tmp/foo/bar/does/not/exist/config.json'; | ||
|
||
afterEach(() => { | ||
jest.resetModules(); | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
beforeEach(() => { | ||
docker = new Docker(); | ||
process.env.CDK_DOCKER_CREDS_FILE = credsFile; | ||
process.env.CDK_DOCKER_CONFIG_FILE = configFile; | ||
}); | ||
|
||
afterEach(() => { | ||
mockfs.restore(); | ||
process.env = { ..._ENV }; | ||
}); | ||
|
||
test('returns false if no cred config exists', () => { | ||
expect(docker.configureCdkCredentials()).toBeFalsy(); | ||
}); | ||
Comment on lines
+193
to
+195
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Was this expected behavior before, or is this new? If not new, would this have been covered by one of the above tests - i.e. would this have resulted in a shell error? |
||
|
||
test('returns true if cred config exists', () => { | ||
mockfs({ | ||
[credsFile]: JSON.stringify({ | ||
version: '0.1', | ||
domainCredentials: { | ||
'test1.example.com': { secretsManagerSecretId: 'mySecret' }, | ||
'test2.example.com': { ecrRepository: 'arn:aws:ecr:bar' }, | ||
}, | ||
}), | ||
}); | ||
|
||
expect(docker.configureCdkCredentials()).toBeTruthy(); | ||
|
||
const config = JSON.parse(readFileSync(path.join(docker.configDirectory!, 'config.json'), 'utf-8')); | ||
expect(config).toBeDefined(); | ||
expect(config).toEqual({ | ||
credHelpers: { | ||
'test1.example.com': 'cdk-assets', | ||
'test2.example.com': 'cdk-assets', | ||
}, | ||
}); | ||
}); | ||
|
||
test('returns true if cred config and docker config exists', () => { | ||
mockfs({ | ||
[credsFile]: JSON.stringify({ | ||
version: '0.1', | ||
domainCredentials: { | ||
'test1.example.com': { secretsManagerSecretId: 'mySecret' }, | ||
'test2.example.com': { ecrRepository: 'arn:aws:ecr:bar' }, | ||
}, | ||
}), | ||
[configFile]: JSON.stringify({ | ||
proxies: { | ||
default: { | ||
httpProxy: 'http://proxy.com', | ||
httpsProxy: 'https://proxy.com', | ||
noProxy: '.amazonaws.com', | ||
}, | ||
}, | ||
}), | ||
}); | ||
|
||
expect(docker.configureCdkCredentials()).toBeTruthy(); | ||
|
||
const config = JSON.parse(readFileSync(path.join(docker.configDirectory!, 'config.json'), 'utf-8')); | ||
expect(config).toBeDefined(); | ||
expect(config).toEqual({ | ||
credHelpers: { | ||
'test1.example.com': 'cdk-assets', | ||
'test2.example.com': 'cdk-assets', | ||
}, | ||
proxies: { | ||
default: { | ||
httpProxy: 'http://proxy.com', | ||
httpsProxy: 'https://proxy.com', | ||
noProxy: '.amazonaws.com', | ||
}, | ||
}, | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this worth throwing an error for? Since this is new behavior, we should consider falling back to the old behavior on failures. We can still give a warning.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe this is not a big concern since it is only when the config file exists and is invalid. That would indicate the user wants to use the config file, but it is not written correctly. This case also seems rare.
Still something to think about for backwards compatability.