@@ -127,6 +127,10 @@ def build_zones(env):
127127 from web_update import get_web_domains
128128 www_redirect_domains = set (get_web_domains (env )) - set (get_web_domains (env , include_www_redirects = False ))
129129
130+ # For MTA-STS, we'll need to check if the PRIMARY_HOSTNAME certificate is
131+ # singned and valid. Check that now rather than repeatedly for each domain.
132+ env ["-primary-hostname-certificate-is-valid" ] = is_domain_cert_signed_and_valid (env ["PRIMARY_HOSTNAME" ], env )
133+
130134 # Build DNS records for each zone.
131135 for domain , zonefile in zonefiles :
132136 # Build the records to put in the zone.
@@ -322,24 +326,11 @@ def has_rec(qname, rtype, prefix=None):
322326 # certificate in use is not valid (e.g. because it is self-signed and a valid certificate has not
323327 # yet been provisioned). Since we cannot provision a certificate without A/AAAA records, we
324328 # always set them --- only the TXT records depend on there being valid certificates.
325- mta_sts_enabled = False
326329 mta_sts_records = [
327330 ("mta-sts" , "A" , env ["PUBLIC_IP" ], "Optional. MTA-STS Policy Host serving /.well-known/mta-sts.txt." ),
328331 ("mta-sts" , "AAAA" , env .get ('PUBLIC_IPV6' ), "Optional. MTA-STS Policy Host serving /.well-known/mta-sts.txt." ),
329332 ]
330- if domain in get_mail_domains (env ):
331- # Check that PRIMARY_HOSTNAME and the mta_sts domain both have valid certificates.
332- for d in (env ['PRIMARY_HOSTNAME' ], "mta-sts." + domain ):
333- cert = get_ssl_certificates (env ).get (d )
334- if not cert :
335- break # no certificate provisioned for this domain
336- cert_status = check_certificate (d , cert ['certificate' ], cert ['private-key' ])
337- if cert_status [0 ] != 'OK' :
338- break # certificate is not valid
339- else :
340- # 'break' was not encountered above, so both domains are good
341- mta_sts_enabled = True
342- if mta_sts_enabled :
333+ if domain in get_mail_domains (env ) and env ["-primary-hostname-certificate-is-valid" ] and is_domain_cert_signed_and_valid ("mta-sts." + domain , env ):
343334 # Compute an up-to-32-character hash of the policy file. We'll take a SHA-1 hash of the policy
344335 # file (20 bytes) and encode it as base-64 (28 bytes, using alphanumeric alternate characters
345336 # instead of '+' and '/' which are not allowed in an MTA-STS policy id) but then just take its
@@ -365,6 +356,13 @@ def has_rec(qname, rtype, prefix=None):
365356
366357 return records
367358
359+ def is_domain_cert_signed_and_valid (domain , env ):
360+ cert = get_ssl_certificates (env ).get (domain )
361+ if not cert : return False # no certificate provisioned
362+ cert_status = check_certificate (domain , cert ['certificate' ], cert ['private-key' ])
363+ print (domain , cert_status )
364+ return cert_status [0 ] == 'OK'
365+
368366########################################################################
369367
370368def build_tlsa_record (env ):
0 commit comments