aboutsummaryrefslogtreecommitdiff
path: root/ddns
diff options
context:
space:
mode:
authorMike Crute <mike@crute.us>2019-01-23 03:55:13 +0000
committerMike Crute <mike@crute.us>2019-01-23 03:55:13 +0000
commit932ef0bf02a5a3763a63f50e49f42df3eaf9508e (patch)
tree888f13674c28b3394bffd6ac92b09e403efe527f /ddns
parentae74acb5bf46f989ccf18e7640715c753190a5e6 (diff)
downloaddockerfiles-932ef0bf02a5a3763a63f50e49f42df3eaf9508e.tar.bz2
dockerfiles-932ef0bf02a5a3763a63f50e49f42df3eaf9508e.tar.xz
dockerfiles-932ef0bf02a5a3763a63f50e49f42df3eaf9508e.zip
Convert to boto3 and python3
Diffstat (limited to 'ddns')
-rwxr-xr-xddns/ddns.py69
1 files changed, 56 insertions, 13 deletions
diff --git a/ddns/ddns.py b/ddns/ddns.py
index 5ec92da..0bde4f7 100755
--- a/ddns/ddns.py
+++ b/ddns/ddns.py
@@ -1,18 +1,38 @@
1#!/usr/bin/env python 1#!/usr/bin/env python
2 2
3import os 3import os
4import json
5import boto3
4import flask 6import flask
5import hashlib 7import hashlib
6import functools 8import functools
7from boto.route53.record import ResourceRecordSets 9
8from boto.route53.connection import Route53Connection 10# pip install flask boto3
11
12# This is purely for documentation purposes
13__REQUIRED_IAM_POLICY__ = """
14{
15 "Version": "2012-10-17",
16 "Statement": [
17 {
18 "Effect": "Allow",
19 "Action": "ssm:GetParameter",
20 "Resource": "arn:aws:ssm:us-west-2::parameter/DDNS_CLIENTS"
21 },
22 {
23 "Effect": "Allow",
24 "Action": [
25 "route53:ListHostedZones",
26 "route53:ChangeResourceRecordSets"
27 ],
28 "Resource": "*"
29 }
30 ]
31}
32"""
9 33
10 34
11app = flask.Flask(__name__) 35app = flask.Flask(__name__)
12app.config.from_pyfile("/etc/ddns.cfg", silent=True)
13app.config.from_pyfile("ddns.cfg", silent=True)
14if "AMAZON_KEY_ID" not in app.config:
15 raise Exception("Not configured")
16 36
17 37
18def returns_plain_text(f): 38def returns_plain_text(f):
@@ -31,11 +51,25 @@ def get_ip():
31 51
32 52
33def update_record(zone, record, ip): 53def update_record(zone, record, ip):
34 conn = Route53Connection(app.config["AMAZON_KEY_ID"], 54 client = boto3.client("route53")
35 app.config["AMAZON_SECRET_KEY"]) 55 zones = client.list_hosted_zones()["HostedZones"]
36 change_set = ResourceRecordSets(conn, conn.get_zone(zone).id) 56
37 change_set.add_change("UPSERT", record, type="A", ttl=60).add_value(ip) 57 client.change_resource_record_sets(
38 change_set.commit() 58 HostedZoneId=[z["Id"] for z in zones if z["Name"] == zone][0],
59 ChangeBatch={
60 "Changes": [{
61 "Action": "UPSERT",
62 "ResourceRecordSet": {
63 "Name": ".".join((record, zone)),
64 "Type": "A",
65 "TTL": 60,
66 "ResourceRecords": [{
67 "Value": ip,
68 }]
69 }
70 }]
71 }
72 )
39 73
40 74
41@app.errorhandler(404) 75@app.errorhandler(404)
@@ -53,16 +87,25 @@ def new_secret():
53 return hashlib.sha256(os.urandom(100)).hexdigest() 87 return hashlib.sha256(os.urandom(100)).hexdigest()
54 88
55 89
90def get_client_config(client):
91 ssm = boto3.client("ssm")
92 clients = ssm.get_parameter(Name="DDNS_CLIENTS", WithDecryption=True)
93 config = json.loads(clients["Parameter"]["Value"])
94 return config.get(client)
95
96
56@app.route("/update", methods=["POST"]) 97@app.route("/update", methods=["POST"])
57def update_ip(): 98def update_ip():
58 key = flask.request.form.get("key") 99 key = flask.request.form.get("key")
59 config = app.config["CLIENTS"].get(key) 100 config = get_client_config(key)
60 101
61 if not config: 102 if not config:
62 flask.abort(404) 103 flask.abort(404)
63 104
105 resource, zone = config.split(".", 1)
106
64 try: 107 try:
65 update_record(config["zone"], config["resource"], get_ip()) 108 update_record(zone, resource, get_ip())
66 return "OK" 109 return "OK"
67 except: 110 except:
68 flask.abort(500) 111 flask.abort(500)