sse.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. # -*- coding: utf-8 -*-
  2. # MinIO Python Library for Amazon S3 Compatible Cloud Storage,
  3. # (C) 2018 MinIO, Inc.
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. """
  17. minio.sse
  18. ~~~~~~~~~~~~~~~~~~~
  19. This module contains core API parsers.
  20. :copyright: (c) 2018 by MinIO, Inc.
  21. :license: Apache 2.0, see LICENSE for more details.
  22. """
  23. from __future__ import absolute_import, annotations
  24. import base64
  25. import json
  26. from abc import ABC, abstractmethod
  27. from typing import Any, cast
  28. class Sse(ABC):
  29. """Server-side encryption base class."""
  30. @abstractmethod
  31. def headers(self) -> dict[str, str]:
  32. """Return headers."""
  33. def tls_required(self) -> bool: # pylint: disable=no-self-use
  34. """Return TLS required to use this server-side encryption."""
  35. return True
  36. def copy_headers(self) -> dict[str, str]: # pylint: disable=no-self-use
  37. """Return copy headers."""
  38. return {}
  39. class SseCustomerKey(Sse):
  40. """ Server-side encryption - customer key type."""
  41. def __init__(self, key: bytes):
  42. if len(key) != 32:
  43. raise ValueError(
  44. "SSE-C keys must be exactly 256 bits (32 bytes) long. "
  45. "Pass raw bytes, not the base64 encoded value.",
  46. )
  47. b64key = base64.b64encode(key).decode()
  48. from .helpers import \
  49. md5sum_hash # pylint: disable=import-outside-toplevel
  50. md5key = cast(str, md5sum_hash(key))
  51. self._headers: dict[str, str] = {
  52. "X-Amz-Server-Side-Encryption-Customer-Algorithm": "AES256",
  53. "X-Amz-Server-Side-Encryption-Customer-Key": b64key,
  54. "X-Amz-Server-Side-Encryption-Customer-Key-MD5": md5key,
  55. }
  56. self._copy_headers: dict[str, str] = {
  57. "X-Amz-Copy-Source-Server-Side-Encryption-Customer-Algorithm":
  58. "AES256",
  59. "X-Amz-Copy-Source-Server-Side-Encryption-Customer-Key": b64key,
  60. "X-Amz-Copy-Source-Server-Side-Encryption-Customer-Key-MD5":
  61. md5key,
  62. }
  63. def headers(self) -> dict[str, str]:
  64. return self._headers.copy()
  65. def copy_headers(self) -> dict[str, str]:
  66. return self._copy_headers.copy()
  67. class SseKMS(Sse):
  68. """Server-side encryption - KMS type."""
  69. def __init__(self, key: str, context: dict[str, Any]):
  70. self._headers = {
  71. "X-Amz-Server-Side-Encryption-Aws-Kms-Key-Id": key,
  72. "X-Amz-Server-Side-Encryption": "aws:kms"
  73. }
  74. if context:
  75. data = bytes(json.dumps(context), "utf-8")
  76. self._headers["X-Amz-Server-Side-Encryption-Context"] = (
  77. base64.b64encode(data).decode()
  78. )
  79. def headers(self) -> dict[str, str]:
  80. return self._headers.copy()
  81. class SseS3(Sse):
  82. """Server-side encryption - S3 type."""
  83. def headers(self) -> dict[str, str]:
  84. return {
  85. "X-Amz-Server-Side-Encryption": "AES256"
  86. }
  87. def tls_required(self) -> bool:
  88. return False