Skip to content

Commit

Permalink
Fix subdomain level unexcepted error
Browse files Browse the repository at this point in the history
  • Loading branch information
tengattack committed Nov 24, 2018
1 parent f30a580 commit 8776bca
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 23 deletions.
57 changes: 37 additions & 20 deletions certbot_dns_aliyun/alidns.py
Expand Up @@ -12,13 +12,14 @@

import requests
from certbot import errors
from certbot.plugins import dns_common

API_ENDPOINT = 'https://alidns.aliyuncs.com/'

class AliError(Exception):
def __init__(self, message, code, request_id):
# Call the base class constructor with the parameters it needs
super(AliError, self).__init__(message)
super(AliError, self).__init__(message.rstrip('.'))

# Aliyun code...
self.Code = code
Expand All @@ -38,16 +39,36 @@ def __init__(self, access_key, access_key_secret, ttl = 600):
self._access_key_secret = access_key_secret
self._ttl = ttl

def get_domain_records(self, domain,
rr_keyword = '', type_keyword = '', value_keyword = ''):
return self._request('DescribeDomainRecords', {
def _find_domain_id(self, domain):
domain_name_guesses = dns_common.base_domain_name_guesses(domain)

for domain_name in domain_name_guesses:
r = self._request('DescribeDomains', {
'KeyWord': domain_name,
})
for d in r['Domains']['Domain']:
if d['DomainName'] == domain_name:
return domain_name

raise errors.PluginError('Unable to determine zone identifier for {0} using zone names: {1}'
.format(domain, domain_name_guesses))

def _find_domain_record_id(self, domain, rr = '', typ = '', value = ''):
records = self._request('DescribeDomainRecords', {
'DomainName': domain,
'RRKeyWord': rr_keyword,
'TypeKeyWord': type_keyword,
'ValueKeyWord': value_keyword,
'RRKeyWord': rr,
'TypeKeyWord': typ,
'ValueKeyWord': value,
})
for record in records['DomainRecords']['Record']:
if record['RR'] == rr:
return record['RecordId']
raise errors.PluginError('Unexpected error determining record identifier for {0}: {1}'
.format(rr, 'record not found'))

def add_txt_record(self, domain, rr, value):
def add_txt_record(self, domain, record_name, value):
domain = self._find_domain_id(domain)
rr = record_name[:record_name.rindex('.' + domain)]
self._request('AddDomainRecord', {
'DomainName': domain,
'RR': rr,
Expand All @@ -56,18 +77,14 @@ def add_txt_record(self, domain, rr, value):
'TTL': self._ttl,
})

def del_txt_record(self, domain, rr, value):
records = self.get_domain_records(domain, rr_keyword=rr, type_keyword='TXT')
record_id = ''
for record in records['DomainRecords']['Record']:
if record['RR'] == rr:
record_id = record['RecordId']
break
if record_id:
self._request('DeleteDomainRecord', {
'DomainName': domain,
'RecordId': record_id,
})
def del_txt_record(self, domain, record_name, value):
domain = self._find_domain_id(domain)
rr = record_name[:record_name.rindex('.' + domain)]
record_id = self._find_domain_record_id(domain, rr=rr, typ='TXT')
self._request('DeleteDomainRecord', {
'DomainName': domain,
'RecordId': record_id,
})

def _urlencode(self, s):
s = quote_plus(s)
Expand Down
6 changes: 3 additions & 3 deletions certbot_dns_aliyun/alidns_test.py
Expand Up @@ -7,7 +7,7 @@

ACCESS_KEY = '123'
ACCESS_KEY_SECRET = 'bar'
DOMAIN_NAME = 'example.com'
DOMAIN_NAME = 'foo.bar.example.com'

class AliDNSClientTest(unittest.TestCase):

Expand All @@ -18,10 +18,10 @@ def setUp(self):
self._client = alidns.AliDNSClient(ACCESS_KEY, ACCESS_KEY_SECRET)

def test_add_txt_record(self):
self._client.add_txt_record(DOMAIN_NAME, 'test.foobar.' + DOMAIN_NAME, 'test')
self._client.add_txt_record(DOMAIN_NAME, 'test.' + DOMAIN_NAME, 'test')

def test_del_txt_record(self):
self._client.del_txt_record(DOMAIN_NAME, 'test.foobar.' + DOMAIN_NAME, 'test')
self._client.del_txt_record(DOMAIN_NAME, 'test.' + DOMAIN_NAME, 'test')

if __name__ == "__main__":
unittest.main() # pragma: no cover

0 comments on commit 8776bca

Please sign in to comment.