forked from alex/letsencrypt-aws
-
Notifications
You must be signed in to change notification settings - Fork 0
/
letsencrypt-aws.py
606 lines (511 loc) · 19.6 KB
/
letsencrypt-aws.py
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
import datetime
import json
import os
import sys
import time
import acme.challenges
import acme.client
import acme.jose
import click
from cryptography import x509
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes, serialization
from cryptography.hazmat.primitives.asymmetric import ec, rsa
import boto3
import OpenSSL.crypto
import rfc3986
DEFAULT_ACME_DIRECTORY_URL = "https://acme-v01.api.letsencrypt.org/directory"
CERTIFICATE_EXPIRATION_THRESHOLD = datetime.timedelta(days=45)
# One day
PERSISTENT_SLEEP_INTERVAL = 60 * 60 * 24
DNS_TTL = 30
class Logger(object):
def __init__(self):
self._out = sys.stdout
def emit(self, event, **data):
formatted_data = " ".join(
"{}={!r}".format(k, v) for k, v in data.items()
)
self._out.write("{} [{}] {}\n".format(
datetime.datetime.utcnow().replace(microsecond=0),
event,
formatted_data
))
self._out.flush()
def _get_iam_certificate(iam_client, certificate_id):
paginator = iam_client.get_paginator("list_server_certificates")
for page in paginator.paginate():
for server_certificate in page["ServerCertificateMetadataList"]:
if server_certificate["Arn"] == certificate_id:
cert_name = server_certificate["ServerCertificateName"]
response = iam_client.get_server_certificate(
ServerCertificateName=cert_name,
)
return x509.load_pem_x509_certificate(
response["ServerCertificate"]["CertificateBody"].encode(),
default_backend(),
)
class CertificateRequest(object):
def __init__(self, cert_location, dns_challenge_completer, hosts,
key_type):
self.cert_location = cert_location
self.dns_challenge_completer = dns_challenge_completer
self.hosts = hosts
self.key_type = key_type
class ELBCertificate(object):
def __init__(self, elb_client, iam_client, elb_name, elb_port):
self.elb_client = elb_client
self.iam_client = iam_client
self.elb_name = elb_name
self.elb_port = elb_port
def get_current_certificate(self):
response = self.elb_client.describe_load_balancers(
LoadBalancerNames=[self.elb_name]
)
[description] = response["LoadBalancerDescriptions"]
[elb_listener] = [
listener["Listener"]
for listener in description["ListenerDescriptions"]
if listener["Listener"]["LoadBalancerPort"] == self.elb_port
]
if "SSLCertificateId" not in elb_listener:
raise ValueError(
"A certificate must already be configured for the ELB"
)
return _get_iam_certificate(
self.iam_client, elb_listener["SSLCertificateId"]
)
def update_certificate(self, logger, hosts, private_key, pem_certificate,
pem_certificate_chain):
logger.emit(
"updating-elb.upload-iam-certificate", elb_name=self.elb_name
)
response = self.iam_client.upload_server_certificate(
ServerCertificateName=generate_certificate_name(
hosts,
x509.load_pem_x509_certificate(
pem_certificate, default_backend()
)
),
PrivateKey=private_key.private_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PrivateFormat.TraditionalOpenSSL,
encryption_algorithm=serialization.NoEncryption(),
),
CertificateBody=pem_certificate.decode(),
CertificateChain=pem_certificate_chain.decode(),
)
new_cert_arn = response["ServerCertificateMetadata"]["Arn"]
# Sleep before trying to set the certificate, it appears to sometimes
# fail without this.
time.sleep(15)
logger.emit("updating-elb.set-elb-certificate", elb_name=self.elb_name)
self.elb_client.set_load_balancer_listener_ssl_certificate(
LoadBalancerName=self.elb_name,
SSLCertificateId=new_cert_arn,
LoadBalancerPort=self.elb_port,
)
class Route53ChallengeCompleter(object):
def __init__(self, route53_client):
self.route53_client = route53_client
def _find_zone_id_for_domain(self, domain):
paginator = self.route53_client.get_paginator("list_hosted_zones")
zones = []
for page in paginator.paginate():
for zone in page["HostedZones"]:
if (
domain.endswith(zone["Name"]) or
(domain + ".").endswith(zone["Name"])
) and not zone["Config"]["PrivateZone"]:
zones.append((zone["Name"], zone["Id"]))
if not zones:
raise ValueError(
"Unable to find a Route53 hosted zone for {}".format(domain)
)
# Order the zones that are suffixes for our desired to domain by
# length, this puts them in an order like:
# ["foo.bar.baz.com", "bar.baz.com", "baz.com", "com"]
# And then we choose the last one, which will be the most specific.
zones.sort(key=lambda z: len(z[0]), reverse=True)
return zones[0][1]
def _change_txt_record(self, action, zone_id, domain, value):
response = self.route53_client.change_resource_record_sets(
HostedZoneId=zone_id,
ChangeBatch={
"Changes": [
{
"Action": action,
"ResourceRecordSet": {
"Name": domain,
"Type": "TXT",
"TTL": DNS_TTL,
"ResourceRecords": [
# For some reason TXT records need to be
# manually quoted.
{"Value": '"{}"'.format(value)}
],
}
}
]
}
)
return response["ChangeInfo"]["Id"]
def create_txt_record(self, host, value):
zone_id = self._find_zone_id_for_domain(host)
change_id = self._change_txt_record(
"CREATE",
zone_id,
host,
value,
)
return (zone_id, change_id)
def delete_txt_record(self, change_id, host, value):
zone_id, _ = change_id
self._change_txt_record(
"DELETE",
zone_id,
host,
value
)
def wait_for_change(self, change_id):
_, change_id = change_id
while True:
response = self.route53_client.get_change(Id=change_id)
if response["ChangeInfo"]["Status"] == "INSYNC":
return
time.sleep(5)
def generate_rsa_private_key():
return rsa.generate_private_key(
public_exponent=65537, key_size=2048, backend=default_backend()
)
def generate_ecdsa_private_key():
return ec.generate_private_key(ec.SECP256R1(), backend=default_backend())
def generate_csr(private_key, hosts):
csr_builder = x509.CertificateSigningRequestBuilder().subject_name(
# This is the same thing the official letsencrypt client does.
x509.Name([
x509.NameAttribute(x509.NameOID.COMMON_NAME, hosts[0]),
])
).add_extension(
x509.SubjectAlternativeName([
x509.DNSName(host)
for host in hosts
]),
# TODO: change to `critical=True` when Let's Encrypt supports it.
critical=False,
)
return csr_builder.sign(private_key, hashes.SHA256(), default_backend())
def find_dns_challenge(authz):
for combo in authz.body.resolved_combinations:
if (
len(combo) == 1 and
isinstance(combo[0].chall, acme.challenges.DNS01)
):
yield combo[0]
def generate_certificate_name(hosts, cert):
return "{serial}-{expiration}-{hosts}".format(
serial=cert.serial,
expiration=cert.not_valid_after.date(),
hosts="-".join(h.replace(".", "_") for h in hosts),
)[:128]
class AuthorizationRecord(object):
def __init__(self, host, authz, dns_challenge, change_id):
self.host = host
self.authz = authz
self.dns_challenge = dns_challenge
self.change_id = change_id
def start_dns_challenge(logger, acme_client, dns_challenge_completer,
elb_name, host):
logger.emit(
"updating-elb.request-acme-challenge", elb_name=elb_name, host=host
)
authz = acme_client.request_domain_challenges(
host, acme_client.directory.new_authz
)
[dns_challenge] = find_dns_challenge(authz)
logger.emit(
"updating-elb.create-txt-record", elb_name=elb_name, host=host
)
change_id = dns_challenge_completer.create_txt_record(
dns_challenge.validation_domain_name(host),
dns_challenge.validation(acme_client.key),
)
return AuthorizationRecord(
host,
authz,
dns_challenge,
change_id,
)
def complete_dns_challenge(logger, acme_client, dns_challenge_completer,
elb_name, authz_record):
logger.emit(
"updating-elb.wait-for-route53",
elb_name=elb_name, host=authz_record.host
)
dns_challenge_completer.wait_for_change(authz_record.change_id)
response = authz_record.dns_challenge.response(acme_client.key)
logger.emit(
"updating-elb.local-validation",
elb_name=elb_name, host=authz_record.host
)
verified = response.simple_verify(
authz_record.dns_challenge.chall,
authz_record.host,
acme_client.key.public_key()
)
if not verified:
raise ValueError("Failed verification")
logger.emit(
"updating-elb.answer-challenge",
elb_name=elb_name, host=authz_record.host
)
acme_client.answer_challenge(authz_record.dns_challenge, response)
def request_certificate(logger, acme_client, elb_name, authorizations, csr):
logger.emit("updating-elb.request-cert", elb_name=elb_name)
cert_response, _ = acme_client.poll_and_request_issuance(
acme.jose.util.ComparableX509(
OpenSSL.crypto.load_certificate_request(
OpenSSL.crypto.FILETYPE_ASN1,
csr.public_bytes(serialization.Encoding.DER),
)
),
authzrs=[authz_record.authz for authz_record in authorizations],
)
pem_certificate = OpenSSL.crypto.dump_certificate(
OpenSSL.crypto.FILETYPE_PEM, cert_response.body
)
pem_certificate_chain = b"\n".join(
OpenSSL.crypto.dump_certificate(OpenSSL.crypto.FILETYPE_PEM, cert)
for cert in acme_client.fetch_chain(cert_response)
)
return pem_certificate, pem_certificate_chain
def update_cert(logger, acme_client, force_issue, cert_request):
logger.emit("updating-elb", elb_name=cert_request.cert_location.elb_name)
current_cert = cert_request.cert_location.get_current_certificate()
if current_cert is not None:
logger.emit(
"updating-elb.certificate-expiration",
elb_name=cert_request.cert_location.elb_name,
expiration_date=current_cert.not_valid_after
)
days_until_expiration = (
current_cert.not_valid_after - datetime.datetime.today()
)
try:
san_extension = current_cert.extensions.get_extension_for_class(
x509.SubjectAlternativeName
)
except x509.ExtensionNotFound:
# Handle the case where an old certificate doesn't have a SAN
# extension and always reissue in that case.
current_domains = []
else:
current_domains = san_extension.value.get_values_for_type(
x509.DNSName
)
if (
days_until_expiration > CERTIFICATE_EXPIRATION_THRESHOLD and
# If the set of hosts we want for our certificate changes, we
# update even if the current certificate isn't expired.
sorted(current_domains) == sorted(cert_request.hosts) and
not force_issue
):
return
if cert_request.key_type == "rsa":
private_key = generate_rsa_private_key()
elif cert_request.key_type == "ecdsa":
private_key = generate_ecdsa_private_key()
else:
raise ValueError(
"Invalid key_type: {!r}".format(cert_request.key_type)
)
csr = generate_csr(private_key, cert_request.hosts)
authorizations = []
try:
for host in cert_request.hosts:
authz_record = start_dns_challenge(
logger, acme_client, cert_request.dns_challenge_completer,
cert_request.cert_location.elb_name, host,
)
authorizations.append(authz_record)
for authz_record in authorizations:
complete_dns_challenge(
logger, acme_client, cert_request.dns_challenge_completer,
cert_request.cert_location.elb_name, authz_record
)
pem_certificate, pem_certificate_chain = request_certificate(
logger, acme_client, cert_request.cert_location.elb_name,
authorizations, csr
)
cert_request.cert_location.update_certificate(
logger, cert_request.hosts,
private_key, pem_certificate, pem_certificate_chain
)
finally:
for authz_record in authorizations:
logger.emit(
"updating-elb.delete-txt-record",
elb_name=cert_request.cert_location.elb_name,
host=authz_record.host
)
dns_challenge = authz_record.dns_challenge
cert_request.dns_challenge_completer.delete_txt_record(
authz_record.change_id,
dns_challenge.validation_domain_name(authz_record.host),
dns_challenge.validation(acme_client.key),
)
def update_certs(logger, acme_client, force_issue, certificate_requests):
for cert_request in certificate_requests:
update_cert(
logger,
acme_client,
force_issue,
cert_request,
)
def setup_acme_client(s3_client, acme_directory_url, acme_account_key):
uri = rfc3986.urlparse(acme_account_key)
if uri.scheme == "file":
if uri.host is None:
path = uri.path
elif uri.path is None:
path = uri.host
else:
path = os.path.join(uri.host, uri.path)
with open(path) as f:
key = f.read()
elif uri.scheme == "s3":
# uri.path includes a leading "/"
response = s3_client.get_object(Bucket=uri.host, Key=uri.path[1:])
key = response["Body"].read()
else:
raise ValueError(
"Invalid acme account key: {!r}".format(acme_account_key)
)
key = serialization.load_pem_private_key(
key.encode("utf-8"), password=None, backend=default_backend()
)
return acme_client_for_private_key(acme_directory_url, key)
def acme_client_for_private_key(acme_directory_url, private_key):
return acme.client.Client(
# TODO: support EC keys, when acme.jose does.
acme_directory_url, key=acme.jose.JWKRSA(key=private_key)
)
@click.group()
def cli():
pass
@cli.command(name="update-certificates")
@click.option(
"--persistent", is_flag=True, help="Runs in a loop, instead of just once."
)
@click.option(
"--force-issue", is_flag=True, help=(
"Issue a new certificate, even if the old one isn't close to "
"expiration."
)
)
def update_certificates(persistent=False, force_issue=False):
logger = Logger()
logger.emit("startup")
if persistent and force_issue:
raise ValueError("Can't specify both --persistent and --force-issue")
# Set up basic session, in case credentials are stored in S3
session = boto3.Session()
s3_client = session.client("s3")
# Collect various config stuff
config = json.loads(os.environ["LETSENCRYPT_AWS_CONFIG"])
domains = config["domains"]
acme_directory_url = config.get(
"acme_directory_url", DEFAULT_ACME_DIRECTORY_URL
)
acme_account_key = config["acme_account_key"]
acme_client = setup_acme_client(
s3_client, acme_directory_url, acme_account_key
)
# Find appropriate credentials
creds_default = os.environ["AWS_SHARED_CREDENTIALS_FILE"]
try:
creds_route53 = os.environ["AWS_ROUTE53_CREDENTIALS_FILE"]
except KeyError:
creds_route53 = ''
# Set up other clients
elb_client = session.client("elb")
iam_client = session.client("iam")
# Set up route53 clients
route53_client_basic = session.client("route53")
if creds_route53:
os.environ["AWS_SHARED_CREDENTIALS_FILE"] = creds_route53
route53_session = boto3.Session()
route53_client_other = route53_session.client("route53")
os.environ["AWS_SHARED_CREDENTIALS_FILE"] = creds_default
certificate_requests = []
for domain in domains:
if "elb" in domain:
cert_location = ELBCertificate(
elb_client, iam_client,
domain["elb"]["name"], int(domain["elb"].get("port", 443))
)
else:
raise ValueError(
"Unknown certificate location: {!r}".format(domain)
)
try:
if domain["Route53Type"] == 'route53':
if not route53_client_other:
raise ValueError("Error: route53 creds requested, but not found")
route53_client = route53_client_other
else:
route53_client = route53_client_basic
except KeyError:
route53_client = route53_client_basic
certificate_requests.append(CertificateRequest(
cert_location,
Route53ChallengeCompleter(route53_client),
domain["hosts"],
domain.get("key_type", "rsa"),
))
if persistent:
logger.emit("running", mode="persistent")
while True:
update_certs(
logger, acme_client,
force_issue, certificate_requests
)
# Sleep before we check again
logger.emit("sleeping", duration=PERSISTENT_SLEEP_INTERVAL)
time.sleep(PERSISTENT_SLEEP_INTERVAL)
else:
logger.emit("running", mode="single")
update_certs(
logger, acme_client,
force_issue, certificate_requests
)
@cli.command()
@click.argument("email")
@click.option(
"--out",
type=click.File("w"),
default="-",
help="Where to write the private key to. Defaults to stdout."
)
def register(email, out):
logger = Logger()
config = json.loads(os.environ.get("LETSENCRYPT_AWS_CONFIG", "{}"))
acme_directory_url = config.get(
"acme_directory_url", DEFAULT_ACME_DIRECTORY_URL
)
logger.emit("acme-register.generate-key")
private_key = generate_rsa_private_key()
acme_client = acme_client_for_private_key(acme_directory_url, private_key)
logger.emit("acme-register.register", email=email)
registration = acme_client.register(
acme.messages.NewRegistration.from_data(email=email)
)
logger.emit("acme-register.agree-to-tos")
acme_client.agree_to_tos(registration)
out.write(private_key.private_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PrivateFormat.TraditionalOpenSSL,
encryption_algorithm=serialization.NoEncryption(),
))
if __name__ == "__main__":
cli()