Skip to content

Commit 6fbd100

Browse files
committed
inject constructed aws creds in entrypoint
1 parent 4e4c9b4 commit 6fbd100

File tree

2 files changed

+22
-50
lines changed

2 files changed

+22
-50
lines changed

custom-domain/dstack-ingress/scripts/dns_providers/route53.py

Lines changed: 2 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/usr/bin/env python3
22

3+
from functools import cached_property
34
import os
45
import sys
56
from 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:

custom-domain/dstack-ingress/scripts/entrypoint.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,26 @@ setup_certbot_env() {
6565
# shellcheck disable=SC1091
6666
source /opt/app-venv/bin/activate
6767

68+
if [ "${DNS_PROVIDER}" = "route53" ]; then
69+
mkdir -p /root/.aws
70+
71+
cat <<EOF >/root/.aws/config
72+
[profile certbot]
73+
role_arn=${AWS_ROLE_ARN}
74+
source_profile=certbot-source
75+
region=${AWS_REGION:-us-east-1}
76+
EOF
77+
78+
cat <<EOF >/root/.aws/credentials
79+
[certbot-source]
80+
aws_access_key_id=${AWS_ACCESS_KEY_ID}
81+
aws_secret_access_key=${AWS_SECRET_ACCESS_KEY}
82+
EOF
83+
84+
unset AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY AWS_SESSION_TOKEN
85+
export AWS_PROFILE=certbot
86+
fi
87+
6888
# Use the unified certbot manager to install plugins and setup credentials
6989
echo "Installing DNS plugins and setting up credentials"
7090
certman.py setup

0 commit comments

Comments
 (0)