Skip to content

Commit 39b74cc

Browse files
committed
fix: local mode printing of credentials during docker login closes aws#2180
1 parent 30b4ce2 commit 39b74cc

File tree

2 files changed

+41
-12
lines changed

2 files changed

+41
-12
lines changed

src/sagemaker/local/image.py

+24-5
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,11 @@ def __init__(
112112
self.container = None
113113

114114
def process(
115-
self, processing_inputs, processing_output_config, environment, processing_job_name
115+
self,
116+
processing_inputs,
117+
processing_output_config,
118+
environment,
119+
processing_job_name,
116120
):
117121
"""Run a processing job locally using docker-compose.
118122
@@ -139,7 +143,11 @@ def process(
139143
for host in self.hosts:
140144
_create_processing_config_file_directories(self.container_root, host)
141145
self.write_processing_config_files(
142-
host, environment, processing_inputs, processing_output_config, processing_job_name
146+
host,
147+
environment,
148+
processing_inputs,
149+
processing_output_config,
150+
processing_job_name,
143151
)
144152

145153
self._generate_compose_file(
@@ -381,7 +389,12 @@ def retrieve_artifacts(self, compose_data, output_data_config, job_name):
381389
return os.path.join(output_data, "model.tar.gz")
382390

383391
def write_processing_config_files(
384-
self, host, environment, processing_inputs, processing_output_config, processing_job_name
392+
self,
393+
host,
394+
environment,
395+
processing_inputs,
396+
processing_output_config,
397+
processing_job_name,
385398
):
386399
"""Write the config files for the processing containers.
387400
@@ -1080,8 +1093,14 @@ def _ecr_login_if_needed(boto_session, image):
10801093
token = raw_token.decode("utf-8").strip("AWS:")
10811094
ecr_url = auth["authorizationData"][0]["proxyEndpoint"]
10821095

1083-
cmd = "docker login -u AWS -p %s %s" % (token, ecr_url)
1084-
subprocess.check_output(cmd.split())
1096+
# Log in to ecr, but use communicate to not print creds to the console
1097+
cmd = f"docker login {ecr_url} -u AWS --password-stdin".split()
1098+
proc = subprocess.Popen(
1099+
cmd,
1100+
stdin=subprocess.PIPE,
1101+
)
1102+
1103+
proc.communicate(input=token.encode())
10851104

10861105
return True
10871106

tests/unit/test_image.py

+17-7
Original file line numberDiff line numberDiff line change
@@ -753,8 +753,9 @@ def test_ecr_login_image_exists(_check_output, image):
753753
assert result is False
754754

755755

756-
@patch("subprocess.check_output", return_value="".encode("utf-8"))
757-
def test_ecr_login_needed(check_output):
756+
@patch("subprocess.Popen", return_value=Mock(autospec=subprocess.Popen))
757+
@patch("sagemaker.local.image._check_output", return_value="")
758+
def test_ecr_login_needed(mock_check_output, popen):
758759
session_mock = Mock()
759760

760761
token = "very-secure-token"
@@ -775,13 +776,22 @@ def test_ecr_login_needed(check_output):
775776
}
776777
session_mock.client("ecr").get_authorization_token.return_value = response
777778
image = "520713654638.dkr.ecr.us-east-1.amazonaws.com/image-i-need:1.1"
778-
result = sagemaker.local.image._ecr_login_if_needed(session_mock, image)
779+
# What a sucessful login would look like
780+
popen.return_value.communicate.return_value = (None, None)
779781

780-
expected_command = (
781-
"docker login -u AWS -p %s https://520713654638.dkr.ecr.us-east-1.amazonaws.com" % token
782-
)
782+
result = sagemaker.local.image._ecr_login_if_needed(session_mock, image)
783783

784-
check_output.assert_called_with(expected_command.split())
784+
mock_check_output.assert_called_with(f"docker images -q {image}")
785+
expected_command = [
786+
"docker",
787+
"login",
788+
"https://520713654638.dkr.ecr.us-east-1.amazonaws.com",
789+
"-u",
790+
"AWS",
791+
"--password-stdin",
792+
]
793+
popen.assert_called_with(expected_command, stdin=subprocess.PIPE)
794+
popen.return_value.communicate.assert_called_with(input=token.encode())
785795
session_mock.client("ecr").get_authorization_token.assert_called_with(
786796
registryIds=["520713654638"]
787797
)

0 commit comments

Comments
 (0)