11#!/usr/bin/env python3
22
3+ from functools import cached_property
34import os
45import sys
56from typing import List , Optional
@@ -17,8 +18,6 @@ class Route53DNSProvider(DNSProvider):
1718 CERTBOT_PLUGIN_MODULE = "certbot_dns_route53"
1819 CERTBOT_PACKAGE = "certbot-dns-route53==5.1.0"
1920 CERTBOT_PROPAGATION_SECONDS = None
20- AWS_CREDENTIALS_FILE = "~/.aws/credentials"
21- AWS_CONFIG_FILE = "~/.aws/config"
2221
2322 def __init__ (self ):
2423 super ().__init__ ()
@@ -42,6 +41,7 @@ def __init__(self):
4241 self .hosted_zone_id : Optional [str ] = None
4342 self .hosted_zone_name : Optional [str ] = None
4443
44+
4545 def setup_certbot_credentials (self ) -> bool :
4646 """Setup AWS credentials file for certbot.
4747
@@ -52,57 +52,9 @@ def setup_certbot_credentials(self) -> bool:
5252 Using this strategy we can impose least permissive and fast expiring access
5353 to our domain.
5454
55- Credentials are in environment variables, we'll create the credentials file.
5655 """
57- aws_access_key = os .getenv ("AWS_ACCESS_KEY_ID" )
58- aws_secret_key = os .getenv ("AWS_SECRET_ACCESS_KEY" )
59-
60- if not aws_access_key or not aws_secret_key :
61- # Assume IAM role or credentials file already exists
62- print ("Using existing AWS credentials (IAM role or credentials file)" )
63- return True
64-
65- credentials_file = os .path .expanduser (self .AWS_CREDENTIALS_FILE )
66- config_file = os .path .expanduser (self .AWS_CONFIG_FILE )
67-
68- aws_role_arn = os .getenv ('AWS_ROLE_ARN' )
69- aws_region = os .getenv ('AWS_REGION' , 'us-east-1' )
70-
71- credentials_dir = os .path .dirname (credentials_file )
7256
7357 try :
74- # Create credentials directory
75- os .makedirs (credentials_dir , exist_ok = True )
76-
77- if os .path .exists (credentials_file ):
78- print (f"AWS credentials file already exists: { credentials_file } " )
79-
80- else :
81- # Write credentials file in AWS INI format
82- with open (credentials_file , "w" ) as f :
83- f .write ("[certbot-source]\n " )
84- f .write (f"aws_access_key_id = { aws_access_key } \n " )
85- f .write (f"aws_secret_access_key = { aws_secret_key } \n " )
86-
87- # Set secure permissions
88- os .chmod (credentials_file , 0o600 )
89- print (f"AWS credentials file created: { credentials_file } " )
90-
91- if os .path .exists (credentials_file ):
92- print (f"AWS config file already exists: { config_file } " )
93-
94- else :
95- # Write config file in AWS INI format
96- with open (config_file , "w" ) as f :
97- f .write ("[profile certbot]\n " )
98- f .write (f"role_arn={ aws_role_arn } \n " )
99- f .write ("source_profile=certbot-source\n " )
100- f .write (f"region={ aws_region } \n " )
101-
102- # Set secure permissions
103- os .chmod (credentials_file , 0o600 )
104- print (f"AWS config file created: { config_file } " )
105-
10658 # Pre-fetch hosted zone ID if we have a domain
10759 domain = os .getenv ("DOMAIN" )
10860 if domain :
0 commit comments