Skip to content

Commit a124c0b

Browse files
authored
Update runtime for new version of SageMaker shim (#4377)
Closes DIAGNijmegen/rse-roadmap#443
1 parent d1a913c commit a124c0b

3 files changed

Lines changed: 19 additions & 27 deletions

File tree

app/grandchallenge/components/backends/amazon_sagemaker_base.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import logging
22
import re
3-
import time
43
from abc import ABC, abstractmethod
54
from datetime import timedelta
65
from json import JSONDecodeError
@@ -719,8 +718,6 @@ def _handle_failed_job(self, *, event):
719718
"Out of Memory. Please use a larger instance",
720719
):
721720
try:
722-
# Allow time for the log file to be written
723-
time.sleep(10)
724721
users_process_exit_code = self._get_task_return_code()
725722
except UncleanExit:
726723
users_process_exit_code = None

app/grandchallenge/components/backends/base.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -411,8 +411,6 @@ def invocation_environment(self):
411411
"LOG_LEVEL": "INFO",
412412
"PYTHONUNBUFFERED": "1",
413413
"no_proxy": "amazonaws.com",
414-
"GRAND_CHALLENGE_COMPONENT_WRITABLE_DIRECTORIES": "/opt/ml/output/data:/opt/ml/model:/opt/ml/input/data/ground_truth:/opt/ml/checkpoints:/tmp",
415-
"GRAND_CHALLENGE_COMPONENT_POST_CLEAN_DIRECTORIES": "/opt/ml/output/data:/opt/ml/model:/opt/ml/input/data/ground_truth",
416414
"GRAND_CHALLENGE_COMPONENT_MAX_MEMORY_MB": str(
417415
self._max_memory_mb
418416
),
@@ -820,17 +818,14 @@ def _get_task_return_code(self):
820818

821819
body = response["Body"].read()
822820

823-
signature_hmac_sha256 = response["Metadata"].get(
824-
"signature_hmac_sha256"
825-
)
821+
signature_hmac_sha256 = response["Metadata"]["signature_hmac_sha256"]
826822
body_signature_hmac_sha256 = hmac.new(
827823
key=self._signing_key, msg=body, digestmod=hashlib.sha256
828824
).hexdigest()
829825

830-
if signature_hmac_sha256 and not secrets.compare_digest(
826+
if not secrets.compare_digest(
831827
body_signature_hmac_sha256, signature_hmac_sha256
832828
):
833-
# TODO The signature should always be present when all images use sagemaker shim >= 0.5.0
834829
logger.error(
835830
"The invocation response object has been tampered with"
836831
)

app/tests/components_tests/test_amazon_sagemaker_training_backend.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import hashlib
2+
import hmac
13
import io
24
import json
35
from datetime import datetime, timedelta, timezone
@@ -202,8 +204,6 @@ def test_invocation_json(settings):
202204
"LOG_LEVEL": "INFO",
203205
"PYTHONUNBUFFERED": "1",
204206
"no_proxy": "amazonaws.com",
205-
"GRAND_CHALLENGE_COMPONENT_WRITABLE_DIRECTORIES": "/opt/ml/output/data:/opt/ml/model:/opt/ml/input/data/ground_truth:/opt/ml/checkpoints:/tmp",
206-
"GRAND_CHALLENGE_COMPONENT_POST_CLEAN_DIRECTORIES": "/opt/ml/output/data:/opt/ml/model:/opt/ml/input/data/ground_truth",
207207
"GRAND_CHALLENGE_COMPONENT_MAX_MEMORY_MB": "7168",
208208
"GRAND_CHALLENGE_COMPONENT_SIGNING_KEY_HEX": "746f74616c6c79736563726574",
209209
},
@@ -556,23 +556,23 @@ def test_handle_completed_job():
556556
time_limit=60,
557557
requires_gpu_type=GPUTypeChoices.NO_GPU,
558558
use_warm_pool=False,
559-
signing_key=b"",
559+
signing_key=b"itsasecret",
560560
)
561561

562-
return_code = 0
563-
564-
with io.BytesIO() as f:
565-
f.write(
566-
json.dumps(
567-
{"return_code": return_code, "pk": f"algorithms-job-{pk}"}
568-
).encode("utf-8")
569-
)
570-
f.seek(0)
571-
executor._s3_client.upload_fileobj(
572-
Fileobj=f,
573-
Bucket=settings.COMPONENTS_OUTPUT_BUCKET_NAME,
574-
Key=executor._result_key,
575-
)
562+
content = json.dumps(
563+
{"return_code": 0, "pk": f"algorithms-job-{pk}"}
564+
).encode("utf-8")
565+
signature = hmac.new(
566+
key=b"itsasecret", msg=content, digestmod=hashlib.sha256
567+
).hexdigest()
568+
executor._s3_client.upload_fileobj(
569+
Fileobj=io.BytesIO(content),
570+
Bucket=settings.COMPONENTS_OUTPUT_BUCKET_NAME,
571+
Key=executor._result_key,
572+
ExtraArgs={
573+
"Metadata": {"signature_hmac_sha256": signature},
574+
},
575+
)
576576

577577
assert executor._handle_completed_job() is None
578578

0 commit comments

Comments
 (0)