summaryrefslogtreecommitdiffstats
path: root/src/powerdns/pdns-backend-rgw.py
blob: db409b32a1f544313aa9aaec47b813d989b560b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#!/usr/bin/python
'''
A backend for PowerDNS to direct RADOS Gateway bucket traffic to the correct regions.

For example, two regions exist, US and EU.

EU: o.myobjects.eu
US: o.myobjects.us

A global domain o.myobjects.com exists.

Bucket 'foo' exists in the region EU and 'bar' in US.

foo.o.myobjects.com will return a CNAME to foo.o.myobjects.eu
bar.o.myobjects.com will return a CNAME to foo.o.myobjects.us

The HTTP Remote Backend from PowerDNS is used in this case: http://doc.powerdns.com/html/remotebackend.html

PowerDNS must be compiled with Remote HTTP backend support enabled, this is not default.

Configuration for PowerDNS:

launch=remote
remote-connection-string=http:url=http://localhost:6780/dns

Usage for this backend is showed by invoking with --help. See rgw-pdns.conf.in for a configuration example

The ACCESS and SECRET key pair requires the caps "metadata=read"

To test:

$ curl -X GET http://localhost:6780/dns/lookup/foo.o.myobjects.com/ANY

Should return something like:

{
 "result": [
  {
   "content": "foo.o.myobjects.eu",
   "qtype": "CNAME",
   "qname": "foo.o.myobjects.com",
   "ttl": 60
  }
 ]
}

'''

# Copyright: Wido den Hollander <wido@42on.com> 2014
# License:   LGPL2.1

from ConfigParser import SafeConfigParser, NoSectionError
from flask import abort, Flask, request, Response
from hashlib import sha1 as sha
from time import gmtime, strftime
from six.moves.urllib.parse import urlparse
import argparse
import base64
import hmac
import json
import pycurl
import StringIO
import urllib
import os
import sys

config_locations = ['rgw-pdns.conf', '~/rgw-pdns.conf', '/etc/ceph/rgw-pdns.conf']

# PowerDNS expects a 200 what ever happends and always wants
# 'result' to 'true' if the query fails
def abort_early():
    return json.dumps({'result': 'true'}) + "\n"

# Generate the Signature string for S3 Authorization with the RGW Admin API
def generate_signature(method, date, uri, headers=None):
    sign = "%s\n\n" % method

    if 'Content-Type' in headers:
        sign += "%s\n" % headers['Content-Type']
    else:
        sign += "\n"

    sign += "%s\n/%s/%s" % (date, config['rgw']['admin_entry'], uri)
    h = hmac.new(config['rgw']['secret_key'].encode('utf-8'), sign.encode('utf-8'), digestmod=sha)
    return base64.encodestring(h.digest()).strip()

def generate_auth_header(signature):
    return str("AWS %s:%s" % (config['rgw']['access_key'], signature.decode('utf-8')))

# Do a HTTP request to the RGW Admin API
def do_rgw_request(uri, params=None, data=None, headers=None):
    if headers == None:
        headers = {}

    headers['Date'] = strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime())
    signature = generate_signature("GET", headers['Date'], uri, headers)
    headers['Authorization'] = generate_auth_header(signature)

    query = None
    if params != None:
        query = '&'.join("%s=%s" % (key,val) for (key,val) in params.iteritems())

    c = pycurl.Curl()
    b = StringIO.StringIO()
    url = "http://" + config['rgw']['endpoint'] + "/" + config['rgw']['admin_entry'] + "/" + uri + "?format=json"
    if query != None:
        url += "&" + urllib.quote_plus(query)

    http_headers = []
    for header in headers.keys():
        http_headers.append(header + ": " + headers[header])

    c.setopt(pycurl.URL, str(url))
    c.setopt(pycurl.HTTPHEADER, http_headers)
    c.setopt(pycurl.WRITEFUNCTION, b.write)
    c.setopt(pycurl.FOLLOWLOCATION, 0)
    c.setopt(pycurl.CONNECTTIMEOUT, 5)
    c.perform()

    response = b.getvalue()
    if len(response) > 0:
        return json.loads(response)

    return None

def get_radosgw_metadata(key):
    return do_rgw_request('metadata', {'key': key})

# Returns a string of the region where the bucket is in
def get_bucket_region(bucket):
    meta = get_radosgw_metadata("bucket:%s" % bucket)
    bucket_id = meta['data']['bucket']['bucket_id']
    meta_instance = get_radosgw_metadata("bucket.instance:%s:%s" % (bucket, bucket_id))
    region = meta_instance['data']['bucket_info']['region']
    return region

# Returns the correct host for the bucket based on the regionmap
def get_bucket_host(bucket, region_map):
    region = get_bucket_region(bucket)
    return bucket + "." + region_map[region]

# This should support multiple endpoints per region!
def parse_region_map(map):
    regions = {}
    for region in map['regions']:
        url = urlparse(region['val']['endpoints'][0])
        regions.update({region['key']: url.netloc})

    return regions

def str2bool(s):
    return s.lower() in ("yes", "true", "1")

def init_config():
    parser = argparse.ArgumentParser()
    parser.add_argument("--config", help="The configuration file to use.", action="store")

    args = parser.parse_args()

    defaults = {
                   'listen_addr': '127.0.0.1',
                   'listen_port': '6780',
                   'dns_zone': 'rgw.local.lan',
                   'dns_soa_record': 'dns1.icann.org. hostmaster.icann.org. 2012080849 7200 3600 1209600 3600',
                   'dns_soa_ttl': '3600',
                   'dns_default_ttl': '60',
                   'rgw_endpoint': 'localhost:8080',
                   'rgw_admin_entry': 'admin',
                   'rgw_access_key': 'access',
                   'rgw_secret_key': 'secret',
                   'debug': False
               }

    cfg = SafeConfigParser(defaults)
    if args.config == None:
        cfg.read(config_locations)
    else:
        if not os.path.isfile(args.config):
            print("Could not open configuration file %s" % args.config)
            sys.exit(1)

        cfg.read(args.config)

    config_section = 'powerdns'

    try:
        return {
            'listen': {
                'port': cfg.getint(config_section, 'listen_port'),
                'addr': cfg.get(config_section, 'listen_addr')
                },
            'dns': {
                'zone': cfg.get(config_section, 'dns_zone'),
                'soa_record': cfg.get(config_section, 'dns_soa_record'),
                'soa_ttl': cfg.get(config_section, 'dns_soa_ttl'),
                'default_ttl': cfg.get(config_section, 'dns_default_ttl')
            },
            'rgw': {
                'endpoint': cfg.get(config_section, 'rgw_endpoint'),
                'admin_entry': cfg.get(config_section, 'rgw_admin_entry'),
                'access_key': cfg.get(config_section, 'rgw_access_key'),
                'secret_key': cfg.get(config_section, 'rgw_secret_key')
            },
            'debug': str2bool(cfg.get(config_section, 'debug'))
        }

    except NoSectionError:
         return None

def generate_app(config):
    # The Flask App
    app = Flask(__name__)

    # Get the RGW Region Map
    region_map = parse_region_map(do_rgw_request('config'))

    @app.route('/')
    def index():
        abort(404)

    @app.route("/dns/lookup/<qname>/<qtype>")
    def bucket_location(qname, qtype):
        if len(qname) == 0:
            return abort_early()

        split = qname.split(".", 1)
        if len(split) != 2:
            return abort_early()

        bucket = split[0]
        zone = split[1]

        # If the received qname doesn't match our zone we abort
        if zone != config['dns']['zone']:
            return abort_early()

        # We do not serve MX records
        if qtype == "MX":
            return abort_early()

        # The basic result we always return, this is what PowerDNS expects.
        response = {'result': 'true'}
        result = {}

        # A hardcoded SOA response (FIXME!)
        if qtype == "SOA":
            result.update({'qtype': qtype})
            result.update({'qname': qname})
            result.update({'content': config['dns']['soa_record']})
            result.update({'ttl': config['dns']['soa_ttl']})
        else:
            region_hostname = get_bucket_host(bucket, region_map)
            result.update({'qtype': 'CNAME'})
            result.update({'qname': qname})
            result.update({'content': region_hostname})
            result.update({'ttl': config['dns']['default_ttl']})

        if len(result) > 0:
            res = []
            res.append(result)
            response['result'] = res

        return json.dumps(response, indent=1) + "\n"

    return app


# Initialize the configuration and generate the Application
config = init_config()
if config == None:
    print("Could not parse configuration file. "
          "Tried to parse %s" % config_locations)
    sys.exit(1)

app = generate_app(config)
app.debug = config['debug']

# Only run the App if this script is invoked from a Shell
if __name__ == '__main__':
    app.run(host=config['listen']['addr'], port=config['listen']['port'])

# Otherwise provide a variable called 'application' for mod_wsgi
else:
    application = app