sseconfig.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. # -*- coding: utf-8 -*-
  2. # MinIO Python Library for Amazon S3 Compatible Cloud Storage, (C)
  3. # 2020 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. """Request/response of PutBucketEncryption and GetBucketEncryption APIs."""
  17. from __future__ import absolute_import, annotations
  18. from abc import ABC
  19. from dataclasses import dataclass
  20. from typing import Optional, Type, TypeVar, cast
  21. from xml.etree import ElementTree as ET
  22. from .xml import Element, SubElement, find, findtext
  23. AES256 = "AES256"
  24. AWS_KMS = "aws:kms"
  25. A = TypeVar("A", bound="Rule")
  26. @dataclass(frozen=True)
  27. class Rule(ABC):
  28. """Server-side encryption rule. """
  29. sse_algorithm: str
  30. kms_master_key_id: Optional[str] = None
  31. @classmethod
  32. def new_sse_s3_rule(cls: Type[A]) -> A:
  33. """Create SSE-S3 rule."""
  34. return cls(sse_algorithm=AES256)
  35. @classmethod
  36. def new_sse_kms_rule(
  37. cls: Type[A],
  38. kms_master_key_id: Optional[str] = None,
  39. ) -> A:
  40. """Create new SSE-KMS rule."""
  41. return cls(sse_algorithm=AWS_KMS, kms_master_key_id=kms_master_key_id)
  42. @classmethod
  43. def fromxml(cls: Type[A], element: ET.Element) -> A:
  44. """Create new object with values from XML element."""
  45. element = cast(
  46. ET.Element,
  47. find(element, "ApplyServerSideEncryptionByDefault", True),
  48. )
  49. return cls(
  50. sse_algorithm=cast(str, findtext(element, "SSEAlgorithm", True)),
  51. kms_master_key_id=findtext(element, "KMSMasterKeyID"),
  52. )
  53. def toxml(self, element: Optional[ET.Element]) -> ET.Element:
  54. """Convert to XML."""
  55. if element is None:
  56. raise ValueError("element must be provided")
  57. element = SubElement(element, "Rule")
  58. tag = SubElement(element, "ApplyServerSideEncryptionByDefault")
  59. SubElement(tag, "SSEAlgorithm", self.sse_algorithm)
  60. if self.kms_master_key_id is not None:
  61. SubElement(tag, "KMSMasterKeyID", self.kms_master_key_id)
  62. return element
  63. B = TypeVar("B", bound="SSEConfig")
  64. @dataclass(frozen=True)
  65. class SSEConfig:
  66. """server-side encryption configuration."""
  67. rule: Rule
  68. def __post_init__(self):
  69. if not self.rule:
  70. raise ValueError("rule must be provided")
  71. @classmethod
  72. def fromxml(cls: Type[B], element: ET.Element) -> B:
  73. """Create new object with values from XML element."""
  74. element = cast(ET.Element, find(element, "Rule", True))
  75. return cls(Rule.fromxml(element))
  76. def toxml(self, element: Optional[ET.Element]) -> ET.Element:
  77. """Convert to XML."""
  78. element = Element("ServerSideEncryptionConfiguration")
  79. self.rule.toxml(element)
  80. return element