def arn_from_str()

in src/aws_encryption_sdk/internal/arn.py [0:0]


def arn_from_str(arn_str):  # noqa: C901
    """Parses an input string as an ARN.

    :param str arn_str: The string to parse.
    :returns: An ARN object representing the input string.
    :rtype: aws_encryption_sdk.internal.arn.Arn
    :raises MalformedArnError: if the string cannot be parsed as an ARN.
    """
    elements = arn_str.split(":", 5)

    try:
        if elements[0] != "arn":
            # //= compliance/framework/aws-kms/aws-kms-key-arn.txt#2.5
            # //# MUST start with string "arn"
            raise MalformedArnError("Missing 'arn' string")

        partition = elements[1]
        service = elements[2]
        region = elements[3]
        account = elements[4]

        if not partition:
            # //= compliance/framework/aws-kms/aws-kms-key-arn.txt#2.5
            # //# The partition MUST be a non-empty
            raise MalformedArnError("Missing partition")

        if not account:
            # //= compliance/framework/aws-kms/aws-kms-key-arn.txt#2.5
            # //# The account MUST be a non-empty string
            raise MalformedArnError("Missing account")

        if not region:
            # //= compliance/framework/aws-kms/aws-kms-key-arn.txt#2.5
            # //# The region MUST be a non-empty string
            raise MalformedArnError("Missing region")

        if service != "kms":
            # //= compliance/framework/aws-kms/aws-kms-key-arn.txt#2.5
            # //# The service MUST be the string "kms"
            raise MalformedArnError("Unknown service")

        resource = elements[5]
        if not resource:
            # //= compliance/framework/aws-kms/aws-kms-key-arn.txt#2.5
            # //# The resource section MUST be non-empty and MUST be split by a
            # //# single "/" any additional "/" are included in the resource id
            raise MalformedArnError("Missing resource")

        resource_elements = resource.split("/", 1)
        resource_type = resource_elements[0]
        resource_id = resource_elements[1]

        if resource_type not in ("alias", "key"):
            # //= compliance/framework/aws-kms/aws-kms-key-arn.txt#2.5
            # //# The resource type MUST be either "alias" or "key"
            raise MalformedArnError("Unknown resource type")

        if not resource_id:
            # //= compliance/framework/aws-kms/aws-kms-key-arn.txt#2.5
            # //# The resource id MUST be a non-empty string
            raise MalformedArnError("Missing resource id")

        return Arn(partition, service, region, account, resource_type, resource_id)
    except (IndexError, MalformedArnError) as exc:
        raise MalformedArnError("Resource {} could not be parsed as an ARN: {}".format(arn_str, exc.args[0]))