File: cloudflare.py

package info (click to toggle)
ddupdate 0.7.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 420 kB
  • sloc: python: 1,899; sh: 38; makefile: 35
file content (236 lines) | stat: -rw-r--r-- 7,878 bytes parent folder | download
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
"""
ddupdate plugin updating data on cloudflare.com.

See: ddupdate(8)
See: https://api.cloudflare.com

"""


REQUESTS_NOT_FOUND = """
The Cloudflare plugin uses the python3-requests package which cannot be found.
Please install python-requests or python3-requests. Giving up.
"""

# pylint: disable=wrong-import-position
from ddupdate.ddplugin import ServicePlugin, ServiceError
from ddupdate.ddplugin import get_netrc_auth, dict_of_opts

try:
    from requests import Request, Session
    from requests.auth import AuthBase
except (ImportError, ModuleNotFoundError):
    import sys
    print(REQUESTS_NOT_FOUND, file=sys.stderr)
    sys.exit(1)


def _call(session, request):
    """Call Cloudflare V4 API."""
    try:
        prepped = session.prepare_request(request)
        res = session.send(prepped)

        if res.status_code / 100 != 2:
            raise ServiceError("Error retrieving %s: status %d" %
                               (request.url, res.status_code))
        json = res.json()
        if not json['success']:
            raise ServiceError("Error retrieving %s: errors %s" %
                               (request.url, json['errors']))
        return json['result']
    except ValueError as err:
        raise ServiceError("Error parsing response %s: %s" %
                           (request.url, err)) from None


def _get_ipv4_from_dnsrecords(dnsrecords):
    """
    Find the A record in dns records, and return tuple (id, address).
    """
    for rec in dnsrecords:
        if 'type' in rec:
            if rec['type'] == 'A':
                return (rec['id'], rec['content'])
    return (None, None)


def _get_ipv6_from_dnsrecords(dnsrecords):
    """
    Find the AAAA record in dns records, and return tuple (id, address).
    """
    for rec in dnsrecords:
        if 'type' in rec:
            if rec['type'] == 'AAAA':
                return (rec['id'], rec['content'])
    return (None, None)


class CloudflareAuth(AuthBase):
    """
    Cloudflare Custom Authentication.

    Attaches a Cloudflare X-Auth-Email/Key authentication scheme to the given
    Request object.
    """

    # pylint: disable=too-few-public-methods
    def __init__(self, email, auth_key):
        """Email and auth_key are required."""
        self.email = email
        self.auth_key = auth_key

    def __call__(self, r):
        """Implement AuthBase."""
        r.headers['X-Auth-Email'] = self.email
        r.headers['X-Auth-Key'] = self.auth_key
        return r


class CloudflarePlugin(ServicePlugin):
    """
    Update a dns entry on cloudflare.com.

    Supports address plugins that define the IP, including default-web-ip
    and default-if. The ip-disabled plugin is not supported.
    ipv6 is supported

    Access to the service requires an API token and login email. This is
    available in the web interface. Also required is the name of the zone.

    netrc: Use a line like
        machine api.cloudflare.com login <email> password <authkey>
    Options:
        zone = Cloudflare Zone name (mandatory)
    """

    _name = 'cloudflare.com'
    _oneliner = 'Updates on https://cloudflare.com'
    _url = "https://api.cloudflare.com/client/v4"
    _auth = None

    def _get_zoneid(self, session, opts):
        """Retrieve an identifier for a given zone name."""
        zone = opts['zone']
        params = {
            'name': zone,
            'per_page': 1
        }
        request = Request(
            'GET',
            self._url + "/zones",
            params=params,
            auth=self._auth)
        res = _call(session, request)
        if res and len(res) == 1 and 'id' in res[0] and res[0]['id']:
            return res[0]['id']
        raise ServiceError("Zone %s not found" % zone)

    def _get_dnsrecords(self, session, hostname, opts):
        """Retrieve all dns records for a given hostname."""
        zone_id = opts['zone_id']
        params = {
            'name': hostname,
            'match': 'all',
        }
        request = Request(
            'GET',
            self._url + "/zones/{0}/dns_records".format(zone_id),
            params=params,
            auth=self._auth)
        return _call(session, request)

    def _create_dnsrecord(self, session, record, opts):
        """Create a new dns record."""
        zone_id = opts['zone_id']
        request = Request(
            'POST',
            self._url + "/zones/{0}/dns_records".format(zone_id),
            json=record,
            auth=self._auth)
        res = _call(session, request)
        return (res['id'], res['content'])

    def _update_dnsrecord(self, session, record_id, record, opts):
        """Update existing dns record."""
        zone_id = opts['zone_id']
        request = Request(
            'PUT',
            self._url + "/zones/{0}/dns_records/{1}".format(zone_id,
                                                            record_id),
            json=record,
            auth=self._auth)
        res = _call(session, request)
        return (res['id'], res['content'])

    def _init_auth(self):
        """Initialize Custom Authentication for Cloudflare v4 API."""
        user, password = get_netrc_auth('api.cloudflare.com')
        self._auth = CloudflareAuth(user, password)

    def register(self, log, hostname, ip, options):
        """Implement ServicePlugin.register()."""
        if not ip:
            raise ServiceError("IP must be defined.")

        self._init_auth()
        opts = dict_of_opts(options)

        if 'zone' not in opts:
            raise ServiceError('Required option zone= missing, giving up.')

        session = Session()

        opts['zone_id'] = self._get_zoneid(session, opts)

        dnsrecords = self._get_dnsrecords(session, hostname, opts)
        ipv4_id, ipv4 = _get_ipv4_from_dnsrecords(dnsrecords)
        ipv6_id, ipv6 = _get_ipv6_from_dnsrecords(dnsrecords)
        log.debug("host=%s existing_ipv4=%s existing_ipv6=%s",
                  hostname, ipv4, ipv6)

        if ip.v4:
            if ipv4 != ip.v4:
                record = {
                    'type': 'A',
                    'name': hostname,
                    'content': ip.v4
                }
                if ipv4_id:
                    log.debug(
                        "method=update_A host=%s existing=%s expected=%s",
                        hostname, ipv4, ip.v4)
                    ipv4_id, ipv4 = \
                        self._update_dnsrecord(session, ipv4_id, record, opts)
                else:
                    log.debug(
                        "method=create_A host=%s existing=%s expected=%s",
                        hostname, ipv4, ip.v4)
                    ipv4_id, ipv4 = \
                        self._create_dnsrecord(session, record, opts)
                log.debug("ipv4_id=%s updated_ipv4=%s", ipv4_id, ipv4)
            else:
                log.info("Existing ipv4 record matches, skipping update")

        if ip.v6:
            if ipv6 != ip.v6:
                record = {
                    'type': 'AAAA',
                    'name': hostname,
                    'content': ip.v6
                }
                if ipv6_id:
                    log.debug(
                        "method=update_AAAA host=%s existing=%s expected=%s",
                        hostname, ipv6, ip.v6)
                    ipv6_id, ipv6 = \
                        self._update_dnsrecord(session, ipv6_id, record, opts)
                else:
                    log.debug(
                        "method=create_AAAA host=%s existing=%s expected=%s",
                        hostname, ipv6, ip.v6)
                    ipv6_id, ipv6 = \
                        self._create_dnsrecord(session, record, opts)
                log.debug("ipv6_id=%s updated_ipv6=%s", ipv6_id, ipv6)
            else:
                log.info("Existing ipv6 record matches, skipping update")