local-letsencrypt/server/backends.py

92 lines
2.8 KiB
Python

import requests
import json
class BackendException(Exception):
pass
class Backend(object):
def __init__(self, config):
self.config = config
def add(self, name, value, type_):
raise NotImplemented
def delete(self, name, type_):
raise NotImplemented
class OVHBackend(Backend):
def __init__(self, config):
import ovh
import ovh.exceptions
self.config = config
self.client = ovh.Client()
try:
self.client.get('/auth/currentCredential')
except (ovh.exceptions.InvalidKey, ovh.exceptions.InvalidCredential):
req = ovh.Client().request_consumerkey([
{'method': 'GET', 'path': '/domain/zone/*/record'},
{'method': 'POST', 'path': '/domain/zone/*/record'},
{'method': 'POST', 'path': '/domain/zone/*/refresh'},
{'method': 'DELETE', 'path': '/domain/zone/*/record/*'},
])
print(req)
def add(self, name, value, type_):
if not name.endswith(self.config['OVH_ZONE']):
raise Exception()
v = self.client.post('/domain/zone/%s/record' % (self.config['OVH_ZONE'],),
fieldType=type_, subDomain=name+'.', target=value)
print(v)
self.client.post('/domain/zone/%s/refresh' % (self.config['OVH_ZONE'],))
def delete(self, name, type_):
ids = self.client.get(
'/domain/zone/%s/record' % (self.config['OVH_ZONE'],), fieldType=type_, subDomain=name+'.')
for i in ids:
print('Removing', i)
self.client.delete('/domain/zone/%s/record/%d' % (self.config['OVH_ZONE'], i))
self.client.post('/domain/zone/%s/refresh' % (self.config['OVH_ZONE'],))
class PowerDNSBackend(Backend):
def do_patch(self, reqdata):
api_url = '%s/api/v1/servers/localhost/zones/%s.' % (
self.config.get('PDNS_URL', 'http://127.0.0.1:8081'),
self.config['PDNS_ZONE'],
)
resp = requests.patch(api_url, data=json.dumps(reqdata), headers={
'X-API-Key': self.config['PDNS_TOKEN']
}).json()
if 'error' in resp:
raise BackendException(resp['error'])
def add(self, name, value, type_):
self.do_patch({
"rrsets": [{
"name": name + ".",
"type": type_,
"ttl": 120,
"changetype": "REPLACE",
"records": [{
"content": value,
"disabled": False
}]
}]
})
def delete(self, name, type_):
self.do_patch({
"rrsets": [{
"name": name + ".",
"type": type_,
"changetype": "DELETE",
}]
})