Skip to content

Commit 5174943

Browse files
committed
Replace RDS password-based authentication with IAM role authentication
- Updated Python Django app to use IAM role auth instead of base64 decoded password - Updated Java Spring Boot app to use IAM role auth with useAWSIam=true parameter - Updated Node.js app to use IAM role auth with SSL configuration - Removed RDS_MYSQL_CLUSTER_PASSWORD environment variables from all Terraform configurations - Removed password-related variables from Terraform variable files - All applications now use IAM role authentication for RDS connections
1 parent 81f92f9 commit 5174943

File tree

10 files changed

+23
-38
lines changed

10 files changed

+23
-38
lines changed

sample-apps/java/springboot-main-service/src/main/java/com/amazon/sampleapp/FrontendServiceController.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import org.apache.http.impl.client.CloseableHttpClient;
3333
import org.apache.http.impl.client.HttpClients;
3434
import org.apache.http.util.EntityUtils;
35-
import org.apache.tomcat.util.codec.binary.Base64;
3635
import org.slf4j.Logger;
3736
import org.slf4j.LoggerFactory;
3837
import org.springframework.beans.factory.annotation.Autowired;
@@ -151,12 +150,15 @@ public String asyncService() {
151150
@ResponseBody
152151
public String mysql() {
153152
logger.info("mysql received");
154-
final String rdsMySQLClusterPassword = new String(new Base64().decode(System.getenv("RDS_MYSQL_CLUSTER_PASSWORD").getBytes()));
155153
try {
154+
// Use IAM role authentication instead of password
155+
String connectionUrl = System.getenv("RDS_MYSQL_CLUSTER_CONNECTION_URL") +
156+
"?useSSL=true&requireSSL=true&verifyServerCertificate=false" +
157+
"&allowPublicKeyRetrieval=true&useAWSIam=true";
156158
Connection connection = DriverManager.getConnection(
157-
System.getenv("RDS_MYSQL_CLUSTER_CONNECTION_URL"),
159+
connectionUrl,
158160
System.getenv("RDS_MYSQL_CLUSTER_USERNAME"),
159-
rdsMySQLClusterPassword);
161+
null); // No password needed for IAM auth
160162
Statement statement = connection.createStatement();
161163
statement.executeQuery("SELECT * FROM tables LIMIT 1;");
162164
} catch (SQLException e) {

sample-apps/node/frontend-service/index.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,12 +121,18 @@ app.get('/client-call', (req, res) => {
121121
});
122122

123123
app.get('/mysql', (req, res) => {
124-
// Create a connection to the MySQL database
124+
// Create a connection to the MySQL database using IAM role authentication
125125
const connection = mysql.createConnection({
126126
host: process.env.RDS_MYSQL_CLUSTER_ENDPOINT,
127127
user: process.env.RDS_MYSQL_CLUSTER_USERNAME,
128-
password: process.env.RDS_MYSQL_CLUSTER_PASSWORD,
129128
database: process.env.RDS_MYSQL_CLUSTER_DATABASE,
129+
ssl: {
130+
ca: require('fs').readFileSync('/opt/rds-ca-2019-root.pem', 'utf8'),
131+
rejectUnauthorized: false
132+
},
133+
authPlugins: {
134+
mysql_clear_password: () => () => Buffer.alloc(0)
135+
}
130136
});
131137

132138
// Connect to the database
Binary file not shown.

sample-apps/python/django_frontend_service/frontend_service_app/views.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
## SPDX-License-Identifier: Apache-2.0
33
import logging
44
import os
5-
import base64
65
import threading
76
import time
87

@@ -113,14 +112,16 @@ def get_xray_trace_id():
113112
def mysql(request):
114113
logger.info("mysql received")
115114

116-
encoded_password = os.environ["RDS_MYSQL_CLUSTER_PASSWORD"]
117-
decoded_password = base64.b64decode(encoded_password).decode('utf-8')
118-
119115
try:
120-
connection = pymysql.connect(host=os.environ["RDS_MYSQL_CLUSTER_ENDPOINT"],
121-
user=os.environ["RDS_MYSQL_CLUSTER_USERNAME"],
122-
password=decoded_password,
123-
database=os.environ["RDS_MYSQL_CLUSTER_DATABASE"])
116+
# Use IAM role authentication instead of password
117+
connection = pymysql.connect(
118+
host=os.environ["RDS_MYSQL_CLUSTER_ENDPOINT"],
119+
user=os.environ["RDS_MYSQL_CLUSTER_USERNAME"],
120+
database=os.environ["RDS_MYSQL_CLUSTER_DATABASE"],
121+
ssl={'ca': '/opt/rds-ca-2019-root.pem'},
122+
auth_plugin_map={'mysql_clear_password': ''},
123+
connect_timeout=10
124+
)
124125
with connection:
125126
with connection.cursor() as cursor:
126127
cursor.execute("SELECT * FROM tables LIMIT 1;")

terraform/java/eks/main.tf

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,6 @@ resource "kubernetes_deployment" "sample_app_deployment" {
119119
name = "RDS_MYSQL_CLUSTER_USERNAME"
120120
value = var.rds_mysql_cluster_username
121121
}
122-
env {
123-
name = "RDS_MYSQL_CLUSTER_PASSWORD"
124-
value = var.rds_mysql_cluster_password
125-
}
126122
env {
127123
name = "OTEL_INSTRUMENTATION_COMMON_EXPERIMENTAL_CONTROLLER_TELEMETRY_ENABLED"
128124
value = "true"

terraform/java/eks/variables.tf

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,6 @@ variable "rds_mysql_cluster_username" {
5757
default = "username"
5858
}
5959

60-
variable "rds_mysql_cluster_password" {
61-
default = "password"
62-
}
63-
6460
variable "account_id" {
6561
default = "<AWS_ACCOUNT_ID>"
6662
}

terraform/node/eks/main.tf

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,6 @@ resource "kubernetes_deployment" "sample_app_deployment" {
126126
name = "RDS_MYSQL_CLUSTER_USERNAME"
127127
value = var.rds_mysql_cluster_username
128128
}
129-
env {
130-
name = "RDS_MYSQL_CLUSTER_PASSWORD"
131-
value = var.rds_mysql_cluster_password
132-
}
133129
port {
134130
container_port = 8000
135131
}

terraform/node/eks/variables.tf

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,6 @@ variable "rds_mysql_cluster_username" {
5757
default = "username"
5858
}
5959

60-
variable "rds_mysql_cluster_password" {
61-
default = "password"
62-
}
63-
6460
variable "account_id" {
6561
default = "<AWS_ACCOUNT_ID>"
6662
}

terraform/python/eks/main.tf

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,6 @@ resource "kubernetes_deployment" "python_app_deployment" {
131131
name = "RDS_MYSQL_CLUSTER_USERNAME"
132132
value = var.rds_mysql_cluster_username
133133
}
134-
env {
135-
name = "RDS_MYSQL_CLUSTER_PASSWORD"
136-
value = var.rds_mysql_cluster_password
137-
}
138134
port {
139135
container_port = 8000
140136
}

terraform/python/eks/variables.tf

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,6 @@ variable "rds_mysql_cluster_username" {
6161
default = "username"
6262
}
6363

64-
variable "rds_mysql_cluster_password" {
65-
default = "password"
66-
}
67-
6864
variable "account_id" {
6965
default = "<AWS_ACCOUNT_ID>"
7066
}

0 commit comments

Comments
 (0)