Skip to content

Commit

Permalink
Add typing in sseconfig.py (#1351)
Browse files Browse the repository at this point in the history
Signed-off-by: Bala.FA <[email protected]>
  • Loading branch information
balamurugana authored Dec 9, 2023
1 parent 048bd6c commit d8276b2
Showing 1 changed file with 38 additions and 18 deletions.
56 changes: 38 additions & 18 deletions minio/sseconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,54 +16,71 @@

"""Request/response of PutBucketEncryption and GetBucketEncryption APIs."""

from __future__ import absolute_import
from __future__ import absolute_import, annotations

from abc import ABCMeta
from typing import Type, TypeVar, cast
from xml.etree import ElementTree as ET

from .xml import Element, SubElement, find, findtext

AES256 = "AES256"
AWS_KMS = "aws:kms"

A = TypeVar("A", bound="Rule")


class Rule:
"""Server-side encryption rule. """
__metaclass__ = ABCMeta

def __init__(self, sse_algorithm, kms_master_key_id=None):
def __init__(
self,
sse_algorithm: str,
kms_master_key_id: str | None = None,
):
self._sse_algorithm = sse_algorithm
self._kms_master_key_id = kms_master_key_id

@property
def sse_algorithm(self):
def sse_algorithm(self) -> str:
"""Get SSE algorithm."""
return self._sse_algorithm

@property
def kms_master_key_id(self):
def kms_master_key_id(self) -> str | None:
"""Get KMS master key ID."""
return self._kms_master_key_id

@classmethod
def new_sse_s3_rule(cls):
def new_sse_s3_rule(cls: Type[A]) -> A:
"""Create SSE-S3 rule."""
return cls(AES256)

@classmethod
def new_sse_kms_rule(cls, kms_master_key_id=None):
def new_sse_kms_rule(
cls: Type[A],
kms_master_key_id: str | None = None,
) -> A:
"""Create new SSE-KMS rule."""
return cls(AWS_KMS, kms_master_key_id)

@classmethod
def fromxml(cls, element):
def fromxml(cls: Type[A], element: ET.Element) -> A:
"""Create new object with values from XML element."""
element = find(element, "ApplyServerSideEncryptionByDefault")
sse_algorithm = findtext(element, "SSEAlgorithm", True)
kms_master_key_id = findtext(element, "KMSMasterKeyID")
return cls(sse_algorithm, kms_master_key_id)

def toxml(self, element):
element = cast(
ET.Element,
find(element, "ApplyServerSideEncryptionByDefault", True),
)
return cls(
cast(str, findtext(element, "SSEAlgorithm", True)),
findtext(element, "KMSMasterKeyID"),
)

def toxml(self, element: ET.Element | None) -> ET.Element:
"""Convert to XML."""
if element is None:
raise ValueError("element must be provided")
element = SubElement(element, "Rule")
tag = SubElement(element, "ApplyServerSideEncryptionByDefault")
SubElement(tag, "SSEAlgorithm", self._sse_algorithm)
Expand All @@ -72,26 +89,29 @@ def toxml(self, element):
return element


B = TypeVar("B", bound="SSEConfig")


class SSEConfig:
"""server-side encryption configuration."""

def __init__(self, rule):
def __init__(self, rule: Rule):
if not rule:
raise ValueError("rule must be provided")
self._rule = rule

@property
def rule(self):
def rule(self) -> Rule:
"""Get rule."""
return self._rule

@classmethod
def fromxml(cls, element):
def fromxml(cls: Type[B], element: ET.Element) -> B:
"""Create new object with values from XML element."""
element = find(element, "Rule")
element = cast(ET.Element, find(element, "Rule", True))
return cls(Rule.fromxml(element))

def toxml(self, element):
def toxml(self, element: ET.Element | None) -> ET.Element:
"""Convert to XML."""
element = Element("ServerSideEncryptionConfiguration")
self._rule.toxml(element)
Expand Down

0 comments on commit d8276b2

Please sign in to comment.