def test_create(domain_identity):
    name = domain_identity
    try:
        request = Request("Create", name)
        response = handler(request, {})
        assert response["Status"] == "SUCCESS", response["Reason"]
        check_dkim_tokens(name, "eu-west-1", response)

        # try duplicate create
        response = handler(request, {})
        assert response["Status"] == "SUCCESS", response["Reason"]
        check_dkim_tokens(name, "eu-west-1", response)

        request["RequestType"] = "Update"
        request["OldResourceProperties"] = {"Region": "eu-west-1"}
        request["ResourceProperties"]["Region"] = "eu-central-1"
        response = handler(request, {})
        assert response["Status"] == "SUCCESS", response["Reason"]
        check_dkim_tokens(name, "eu-central-1", response)

    finally:
        for region in ["eu-west-1", "eu-central-1"]:
            request = Request("Delete",
                              name,
                              region=region,
                              physical_resource_id="{name}@{region}")
            response = handler(request, {})
            assert response["Status"] == "SUCCESS", response["Reason"]
Example #2
0
def test_create():
    name = f"{uuid.uuid4()}.internal"
    try:
        request = Request("Create", name)
        response = handler(request, {})
        assert response["Status"] == "SUCCESS", response["Reason"]
        check_verification_response(name, "eu-west-1", response)

        # try duplicate create
        response = handler(request, {})
        assert response["Status"] == "FAILED", response["Reason"]
        assert (
            response["Reason"]
            == f"SES domain identity {name} already exists in region eu-west-1"
        )
        request["PhysicalResourceId"] = response.get("PhysicalResourceId")

        request["RequestType"] = "Update"
        request["OldResourceProperties"] = {"Region": "eu-west-1"}
        request["ResourceProperties"]["Region"] = "eu-central-1"
        response = handler(request, {})
        assert response["Status"] == "SUCCESS", response["Reason"]
        check_verification_response(name, "eu-central-1", response)

        # try duplicate update of existing record
        request["ResourceProperties"]["Region"] = "eu-central-1"
        request["OldResourceProperties"] = {"Region": "eu-central-1"}
        request["ResourceProperties"]["Region"] = "eu-west-1"
        response = handler(request, {})
        assert response["Status"] == "FAILED", response["Reason"]
        assert (
            response["Reason"]
            == f"cannot change domain identity to {name} as it already exists in region eu-west-1"
        )

    finally:
        for region in ["eu-west-1", "eu-central-1"]:
            request = Request(
                "Delete",
                name,
                region=region,
                physical_resource_id=f"{name.rstrip('.')}@{region}",
            )
            response = handler(request, {})
            assert response["Status"] == "SUCCESS", response["Reason"]
Example #3
0
def test_create():

    name = '%s.internal' % str(uuid.uuid4())
    hosted_zone_id = None
    try:
        response = route53.create_hosted_zone(Name=name, CallerReference=name)
        hosted_zone_id = response['HostedZone']['Id']
        wait_for_change_completion(response['ChangeInfo']['Id'])

        request = Request('Create', hosted_zone_id)
        response = handler(request, {})
        assert response['Status'] == 'SUCCESS', response['Reason']

        identities = filter(
            lambda i: i == name,
            ses.list_identities(IdentityType='Domain')['Identities'])
        assert len(
            identities) == 1, 'could not find domain %s as SES identity' % name

        physical_resource_id = response['PhysicalResourceId']
        # wait_for_change_completion(response['Data']['ChangeId'])

        records = route53.list_resource_record_sets(
            HostedZoneId=hosted_zone_id)['ResourceRecordSets']
        ses_verification_record = filter(
            lambda r: r['Name'] == '_amazonses.%s.' % name, records)
        dkim_verification_records = filter(
            lambda r: r['Name'].endswith('._domainkey.%s.' % name), records)
        assert len(ses_verification_record
                   ) == 1, 'could not find _amazonses.%s. record' % name
        assert len(dkim_verification_records
                   ) > 0, 'could not find any _domainkey.%s. records' % name

        request = Request('Update', hosted_zone_id, physical_resource_id)
        response = handler(request, {})
        assert response['Status'] == 'SUCCESS', response['Reason']
        assert physical_resource_id == response['PhysicalResourceId']
        # wait_for_change_completion(response['Data']['ChangeId'])

        request = Request('Delete', hosted_zone_id, physical_resource_id)
        response = handler(request, {})
        assert response['Status'] == 'SUCCESS', response['Reason']

        assert physical_resource_id == response['PhysicalResourceId']
        # wait_for_change_completion(response['Data']['ChangeId'])

        identities = filter(
            lambda i: i == name,
            ses.list_identities(IdentityType='Domain')['Identities'])
        assert len(
            identities
        ) == 0, 'domain %s is still present as a SES identity' % name

        records = route53.list_resource_record_sets(
            HostedZoneId=hosted_zone_id)['ResourceRecordSets']
        ses_verification_record = filter(
            lambda r: r['Name'] == '_amazonses.%s.' % name, records)
        dkim_verification_records = filter(
            lambda r: r['Name'].endswith('._domainkey.%s.' % name), records)
        assert len(ses_verification_record
                   ) == 0, '_amazonses.%s. record still present' % name
        assert len(dkim_verification_records
                   ) == 0, '_domainkey.%s. records still present' % name

    finally:
        if hosted_zone_id is not None:
            try:
                route53.delete_hosted_zone(Id=hosted_zone_id)
            except Exception as e:
                print e
def create_for_domain(hosted_zone_name, hosted_zone_id, domain_name):
    dkim_domain = (
        domain_name.rstrip(".") if domain_name else hosted_zone_name.rstrip(".")
    )
    if domain_name:
        request = Request("Create", hosted_zone_id, domain_name)
    else:
        request = Request("Create", hosted_zone_id)

    response = handler(request, {})
    assert response["Status"] == "SUCCESS", response["Reason"]

    identities = list(
        filter(
            lambda i: i == dkim_domain,
            ses.list_identities(IdentityType="Domain")["Identities"],
        )
    )
    assert len(identities) == 1, (
        "could not find domain %s as SES identity" % dkim_domain
    )

    physical_resource_id = response["PhysicalResourceId"]
    if domain_name and dkim_domain != hosted_zone_name.rstrip("."):
        assert physical_resource_id == f"{dkim_domain}@{hosted_zone_id}"
    else:
        assert physical_resource_id == hosted_zone_id

    records = route53.list_resource_record_sets(HostedZoneId=hosted_zone_id)[
        "ResourceRecordSets"
    ]
    ses_verification_record = list(
        filter(lambda r: r["Name"] == "_amazonses.%s." % dkim_domain, records)
    )
    dkim_verification_records = list(
        filter(lambda r: r["Name"].endswith("._domainkey.%s." % dkim_domain), records)
    )
    assert len(ses_verification_record) == 1, (
        "could not find _amazonses.%s record" % dkim_domain
    )
    assert len(dkim_verification_records) > 0, (
        "could not find any _domainkey.%s records" % dkim_domain
    )

    ## re-insert of existing domain should fail
    response = handler(request, {})
    assert response["Status"] == "FAILED", response["Reason"]
    assert response["Reason"] == f"SES domain identity {dkim_domain} already exists"

    if not domain_name:
        request = Request("Create", hosted_zone_id, dkim_domain)
        response = handler(request, {})
        assert response["Status"] == "FAILED", response["Reason"]
        assert response["Reason"] == f"SES domain identity {dkim_domain} already exists"

    request = Request(
        "Update", hosted_zone_id, physical_resource_id=physical_resource_id
    )
    response = handler(request, {})
    assert response["Status"] == "SUCCESS", response["Reason"]
    if domain_name and dkim_domain != hosted_zone_name.rstrip("."):
        assert physical_resource_id == f"{dkim_domain}@{hosted_zone_id}"
    else:
        assert physical_resource_id == hosted_zone_id

    request = Request(
        "Delete", hosted_zone_id, physical_resource_id=physical_resource_id
    )
    response = handler(request, {})
    assert response["Status"] == "SUCCESS", response["Reason"]

    if domain_name and dkim_domain != hosted_zone_name.rstrip("."):
        assert physical_resource_id == f"{dkim_domain}@{hosted_zone_id}"
    else:
        assert physical_resource_id == hosted_zone_id

    identities = list(
        filter(
            lambda i: i == dkim_domain,
            ses.list_identities(IdentityType="Domain")["Identities"],
        )
    )
    assert len(identities) == 0, (
        "domain %s is still present as a SES identity" % dkim_domain
    )

    records = route53.list_resource_record_sets(HostedZoneId=hosted_zone_id)[
        "ResourceRecordSets"
    ]
    ses_verification_record = list(
        filter(lambda r: r["Name"] == "_amazonses.%s." % dkim_domain, records)
    )
    dkim_verification_records = list(
        filter(lambda r: r["Name"].endswith("._domainkey.%s." % dkim_domain), records)
    )
    assert len(ses_verification_record) == 0, (
        "_amazonses.%s record still present" % dkim_domain
    )
    assert len(dkim_verification_records) == 0, (
        "_domainkey.%s records still present" % dkim_domain
    )