I use kms.decrypt() method from boto3 package. For typing support I use the boto3-stubs package.
The decrypt method has attribute EncryptionAlgorithm, which is typed as
EncryptionAlgorithmSpecType = Literal["RSAES_OAEP_SHA_1", "RSAES_OAEP_SHA_256", "SYMMETRIC_DEFAULT"]
I parse the encryption algorithm via regular expression from input string so in my case the EncryptionAlgorithm value is str, not a Literal. Mypy complaint for it.
I do not want to disable type check for this line and the only solution I was found is the helper method which convert the str value to literal in this way:
import boto3
from mypy_boto3_kms.literals import EncryptionAlgorithmSpecType
def getEncryptionAlgorithmLiteral(algorithm: str) -> EncryptionAlgorithmSpecType:
result: EncryptionAlgorithmSpecType
if algorithm == "RSAES_OAEP_SHA_1":
result = "RSAES_OAEP_SHA_1"
elif algorithm == "RSAES_OAEP_SHA_256":
result = "RSAES_OAEP_SHA_256"
elif algorithm == "SYMMETRIC_DEFAULT":
result = "SYMMETRIC_DEFAULT"
else:
raise Exception(f"Unexpected algorithm '{algorithm}'. It must be one of {EncryptionAlgorithmSpecType}")
return result
def main(binaryEncData: bytes, keyId: str, algorithm: str):
kms = boto3.client('kms')
kmsResult = kms.decrypt(CiphertextBlob=binaryEncData,
KeyId=keyId,
EncryptionAlgorithm=getEncryptionAlgorithmLiteral(algorithm))
I wonder if there is some better way how to achieve the conversion in getEncryptionAlgorithmLiteral() method so I do not need to type all these values twice. Ideally I would like to use values directly from EncryptionAlgorithmSpecType type instead of typing them again to my code.