@@ -152,15 +152,14 @@ def deserialize(s3_uri: str, bytes_to_deserialize: bytes) -> Any:
152152
153153# TODO: use dask serializer in case dask distributed is installed in users' environment.
154154def serialize_func_to_s3 (
155- func : Callable , sagemaker_session : Session , s3_uri : str , hmac_key : str , s3_kms_key : str = None
155+ func : Callable , sagemaker_session : Session , s3_uri : str , s3_kms_key : str = None
156156):
157157 """Serializes function and uploads it to S3.
158158
159159 Args:
160160 sagemaker_session (sagemaker.session.Session):
161161 The underlying Boto3 session which AWS service calls are delegated to.
162162 s3_uri (str): S3 root uri to which resulting serialized artifacts will be uploaded.
163- hmac_key (str): Key used to calculate hmac-sha256 hash of the serialized func.
164163 s3_kms_key (str): KMS key used to encrypt artifacts uploaded to S3.
165164 func: function to be serialized and persisted
166165 Raises:
@@ -169,14 +168,13 @@ def serialize_func_to_s3(
169168
170169 _upload_payload_and_metadata_to_s3 (
171170 bytes_to_upload = CloudpickleSerializer .serialize (func ),
172- hmac_key = hmac_key ,
173171 s3_uri = s3_uri ,
174172 sagemaker_session = sagemaker_session ,
175173 s3_kms_key = s3_kms_key ,
176174 )
177175
178176
179- def deserialize_func_from_s3 (sagemaker_session : Session , s3_uri : str , hmac_key : str ) -> Callable :
177+ def deserialize_func_from_s3 (sagemaker_session : Session , s3_uri : str ) -> Callable :
180178 """Downloads from S3 and then deserializes data objects.
181179
182180 This method downloads the serialized training job outputs to a temporary directory and
@@ -186,7 +184,6 @@ def deserialize_func_from_s3(sagemaker_session: Session, s3_uri: str, hmac_key:
186184 sagemaker_session (sagemaker.session.Session):
187185 The underlying sagemaker session which AWS service calls are delegated to.
188186 s3_uri (str): S3 root uri to which resulting serialized artifacts will be uploaded.
189- hmac_key (str): Key used to calculate hmac-sha256 hash of the serialized func.
190187 Returns :
191188 The deserialized function.
192189 Raises:
@@ -198,32 +195,26 @@ def deserialize_func_from_s3(sagemaker_session: Session, s3_uri: str, hmac_key:
198195
199196 bytes_to_deserialize = _read_bytes_from_s3 (f"{ s3_uri } /payload.pkl" , sagemaker_session )
200197
201- _perform_integrity_check (
202- expected_hash_value = metadata .sha256_hash , secret_key = hmac_key , buffer = bytes_to_deserialize
203- )
198+ _perform_integrity_check (expected_hash_value = metadata .sha256_hash , buffer = bytes_to_deserialize )
204199
205200 return CloudpickleSerializer .deserialize (f"{ s3_uri } /payload.pkl" , bytes_to_deserialize )
206201
207202
208- def serialize_obj_to_s3 (
209- obj : Any , sagemaker_session : Session , s3_uri : str , hmac_key : str , s3_kms_key : str = None
210- ):
203+ def serialize_obj_to_s3 (obj : Any , sagemaker_session : Session , s3_uri : str , s3_kms_key : str = None ):
211204 """Serializes data object and uploads it to S3.
212205
213206 Args:
214207 sagemaker_session (sagemaker.session.Session):
215208 The underlying Boto3 session which AWS service calls are delegated to.
216209 s3_uri (str): S3 root uri to which resulting serialized artifacts will be uploaded.
217210 s3_kms_key (str): KMS key used to encrypt artifacts uploaded to S3.
218- hmac_key (str): Key used to calculate hmac-sha256 hash of the serialized obj.
219211 obj: object to be serialized and persisted
220212 Raises:
221213 SerializationError: when fail to serialize object to bytes.
222214 """
223215
224216 _upload_payload_and_metadata_to_s3 (
225217 bytes_to_upload = CloudpickleSerializer .serialize (obj ),
226- hmac_key = hmac_key ,
227218 s3_uri = s3_uri ,
228219 sagemaker_session = sagemaker_session ,
229220 s3_kms_key = s3_kms_key ,
@@ -270,14 +261,13 @@ def json_serialize_obj_to_s3(
270261 )
271262
272263
273- def deserialize_obj_from_s3 (sagemaker_session : Session , s3_uri : str , hmac_key : str ) -> Any :
264+ def deserialize_obj_from_s3 (sagemaker_session : Session , s3_uri : str ) -> Any :
274265 """Downloads from S3 and then deserializes data objects.
275266
276267 Args:
277268 sagemaker_session (sagemaker.session.Session):
278269 The underlying sagemaker session which AWS service calls are delegated to.
279270 s3_uri (str): S3 root uri to which resulting serialized artifacts will be uploaded.
280- hmac_key (str): Key used to calculate hmac-sha256 hash of the serialized obj.
281271 Returns :
282272 Deserialized python objects.
283273 Raises:
@@ -290,15 +280,13 @@ def deserialize_obj_from_s3(sagemaker_session: Session, s3_uri: str, hmac_key: s
290280
291281 bytes_to_deserialize = _read_bytes_from_s3 (f"{ s3_uri } /payload.pkl" , sagemaker_session )
292282
293- _perform_integrity_check (
294- expected_hash_value = metadata .sha256_hash , secret_key = hmac_key , buffer = bytes_to_deserialize
295- )
283+ _perform_integrity_check (expected_hash_value = metadata .sha256_hash , buffer = bytes_to_deserialize )
296284
297285 return CloudpickleSerializer .deserialize (f"{ s3_uri } /payload.pkl" , bytes_to_deserialize )
298286
299287
300288def serialize_exception_to_s3 (
301- exc : Exception , sagemaker_session : Session , s3_uri : str , hmac_key : str , s3_kms_key : str = None
289+ exc : Exception , sagemaker_session : Session , s3_uri : str , s3_kms_key : str = None
302290):
303291 """Serializes exception with traceback and uploads it to S3.
304292
@@ -307,7 +295,6 @@ def serialize_exception_to_s3(
307295 The underlying Boto3 session which AWS service calls are delegated to.
308296 s3_uri (str): S3 root uri to which resulting serialized artifacts will be uploaded.
309297 s3_kms_key (str): KMS key used to encrypt artifacts uploaded to S3.
310- hmac_key (str): Key used to calculate hmac-sha256 hash of the serialized exception.
311298 exc: Exception to be serialized and persisted
312299 Raises:
313300 SerializationError: when fail to serialize object to bytes.
@@ -316,7 +303,6 @@ def serialize_exception_to_s3(
316303
317304 _upload_payload_and_metadata_to_s3 (
318305 bytes_to_upload = CloudpickleSerializer .serialize (exc ),
319- hmac_key = hmac_key ,
320306 s3_uri = s3_uri ,
321307 sagemaker_session = sagemaker_session ,
322308 s3_kms_key = s3_kms_key ,
@@ -325,7 +311,6 @@ def serialize_exception_to_s3(
325311
326312def _upload_payload_and_metadata_to_s3 (
327313 bytes_to_upload : Union [bytes , io .BytesIO ],
328- hmac_key : str ,
329314 s3_uri : str ,
330315 sagemaker_session : Session ,
331316 s3_kms_key ,
@@ -334,15 +319,14 @@ def _upload_payload_and_metadata_to_s3(
334319
335320 Args:
336321 bytes_to_upload (bytes): Serialized bytes to upload.
337- hmac_key (str): Key used to calculate hmac-sha256 hash of the serialized obj.
338322 s3_uri (str): S3 root uri to which resulting serialized artifacts will be uploaded.
339323 sagemaker_session (sagemaker.session.Session):
340324 The underlying Boto3 session which AWS service calls are delegated to.
341325 s3_kms_key (str): KMS key used to encrypt artifacts uploaded to S3.
342326 """
343327 _upload_bytes_to_s3 (bytes_to_upload , f"{ s3_uri } /payload.pkl" , s3_kms_key , sagemaker_session )
344328
345- sha256_hash = _compute_hash (bytes_to_upload , secret_key = hmac_key )
329+ sha256_hash = _compute_hash (bytes_to_upload )
346330
347331 _upload_bytes_to_s3 (
348332 _MetaData (sha256_hash ).to_json (),
@@ -352,14 +336,13 @@ def _upload_payload_and_metadata_to_s3(
352336 )
353337
354338
355- def deserialize_exception_from_s3 (sagemaker_session : Session , s3_uri : str , hmac_key : str ) -> Any :
339+ def deserialize_exception_from_s3 (sagemaker_session : Session , s3_uri : str ) -> Any :
356340 """Downloads from S3 and then deserializes exception.
357341
358342 Args:
359343 sagemaker_session (sagemaker.session.Session):
360344 The underlying sagemaker session which AWS service calls are delegated to.
361345 s3_uri (str): S3 root uri to which resulting serialized artifacts will be uploaded.
362- hmac_key (str): Key used to calculate hmac-sha256 hash of the serialized exception.
363346 Returns :
364347 Deserialized exception with traceback.
365348 Raises:
@@ -372,9 +355,7 @@ def deserialize_exception_from_s3(sagemaker_session: Session, s3_uri: str, hmac_
372355
373356 bytes_to_deserialize = _read_bytes_from_s3 (f"{ s3_uri } /payload.pkl" , sagemaker_session )
374357
375- _perform_integrity_check (
376- expected_hash_value = metadata .sha256_hash , secret_key = hmac_key , buffer = bytes_to_deserialize
377- )
358+ _perform_integrity_check (expected_hash_value = metadata .sha256_hash , buffer = bytes_to_deserialize )
378359
379360 return CloudpickleSerializer .deserialize (f"{ s3_uri } /payload.pkl" , bytes_to_deserialize )
380361
@@ -399,18 +380,18 @@ def _read_bytes_from_s3(s3_uri, sagemaker_session):
399380 ) from e
400381
401382
402- def _compute_hash (buffer : bytes , secret_key : str ) -> str :
403- """Compute the hmac- sha256 hash"""
404- return hmac . new ( secret_key . encode (), msg = buffer , digestmod = hashlib .sha256 ).hexdigest ()
383+ def _compute_hash (buffer : bytes ) -> str :
384+ """Compute the sha256 hash"""
385+ return hashlib .sha256 ( buffer ).hexdigest ()
405386
406387
407- def _perform_integrity_check (expected_hash_value : str , secret_key : str , buffer : bytes ):
388+ def _perform_integrity_check (expected_hash_value : str , buffer : bytes ):
408389 """Performs integrity checks for serialized code/arguments uploaded to s3.
409390
410391 Verifies whether the hash read from s3 matches the hash calculated
411392 during remote function execution.
412393 """
413- actual_hash_value = _compute_hash (buffer = buffer , secret_key = secret_key )
394+ actual_hash_value = _compute_hash (buffer = buffer )
414395 if not hmac .compare_digest (expected_hash_value , actual_hash_value ):
415396 raise DeserializationError (
416397 "Integrity check for the serialized function or data failed. "
0 commit comments