Alvin
Alvin

Reputation: 2543

Anyway to get dkims records for verifying ses domain in boto?

Tinkering around with verifying a couple of domains and found the manual process rather tedius. My DNS controller offers API access so I figured why not script the whole thing.

Trick is I can't figure out how to access the required TXT & CNAME records for DKIMS verification from boto, when I punch in

dkims = conn.verify_domain_dkim('DOMAIN.COM')

it adds DOMAIN.COM to the list of domains pending verification but doesn't provide the needed records, the returned value of dkims is

{'VerifyDomainDkimResponse': {
    'ResponseMetadata': {'RequestId': 'REQUEST_ID_STRING'},
    'VerifyDomainDkimResult': {'DkimTokens': {
        'member': 'DKIMS_TOKEN_STRING'}}}}

Is there some undocumented way to take the REQUEST_ID or TOKEN_STRING to pull up these records?


UPDATE

If you have an aws account you can see the records I'm after at

https://console.aws.amazon.com/ses/home?region=us-west-2#verified-senders:domain

tab: Details:: Record Type: TXT (Text)

tab: DKIM:: DNS Record 1, 2, 3

these are the records required to add to the DNS controller to validate & allow DKIM signatures to take place

Upvotes: 0

Views: 973

Answers (2)

Lisandro
Lisandro

Reputation: 311

This is how I do it with python.

DOMINIO = 'mydomain.com'

from boto3 import Session
session = Session(
    aws_access_key_id=MY_AWS_ACCESS_KEY_ID,
    aws_secret_access_key=MY_AWS_SECRET_ACCESS_KEY,
    region_name=MY_AWS_REGION_NAME)
client = session.client('ses')

# gets VerificationToken for the domain, that will be used to add a TXT record to the DNS
result = client.verify_domain_identity(Domain=DOMINIO)
txt = result.get('VerificationToken')

# gets DKIM tokens that will be used to add 3 CNAME records
result = client.verify_domain_dkim(Domain=DOMINIO)
dkim_tokens = result.get('DkimTokens')  # this is a list

At the end of the code, you will have "txt" and "dkim_tokens" variables, a string and a list respectively.

You will need to add a TXT record to your dns, where the host name is "_amazonses" and the value is the value of "txt" variable.

Also you will need to add 3 CNAME records to your dns, one for each token present in "dkim_tokens" list, where the host name of each record is of the form of [dkimtoken]._domainkey and the target is [dkimtoken].dkim.amazonses.com

After adding the dns records, after some minutes (maybe a couple of hours), Amazon will detect and verify the domain, and will send you an email notification. After that, you can enable Dkim signature by doing this call:

client.set_identity_dkim_enabled(Identity=DOMINIO, DkimEnabled=True)

The methods used here are verify_domain_identity, verify_domain_dkim and set_identity_dkim_enabled. You may also want to take a look a get_identity_verification_attributes and get_identity_dkim_attributes.

Upvotes: 2

garnaat
garnaat

Reputation: 45846

I think the get_identity_dkim_attributes method will return the information you are looking for. You pass in the domain name(s) you are interested in and it returns the status for that identity as well as the DKIM tokens.

Upvotes: 0

Related Questions