1- import configparser
21import boto3
3- from botocore . exceptions import ClientError
2+ import json
43
4+ # Define the Secrets Manager client
5+ secrets_client = boto3 .client ('secretsmanager' )
56
6- def get_secret ():
7+ # Define the name of the secret containing the database details
8+ secret_name = "rds!db-8f23e7a9-e9f8-4b50-a059-f0e310e18134"
79
8- secret_name = "rds!db-8f23e7a9-e9f8-4b50-a059-f0e310e18134"
9- region_name = "us-west-2"
10-
11- session = boto3 .session .Session ()
12- client = session .client (
13- service_name = 'secretsmanager' ,
14- region_name = region_name
15- )
10+ endpoint_port = "database-1.cvgrek461osh.us-west-2.rds.amazonaws.com:3306"
1611
17- try :
18- get_secret_value_response = client .get_secret_value (
19- SecretId = secret_name
20- )
21- except ClientError as e :
22- raise e
12+ # Retrieve the secret value
13+ response = secrets_client .get_secret_value (SecretId = secret_name )
2314
24- db_credentials = get_secret_value_response ['SecretString' ]
15+ # Parse the secret value as a JSON object
16+ secret_value = response ['SecretString' ]
2517
26- PROPERTIES_FILE_NAME = "application.properties"
18+ # Parse the JSON object to retrieve the database details
19+ database_details = json .loads (secret_value )
2720
28- config = configparser . ConfigParser ()
29- config . read ( PROPERTIES_FILE_NAME )
21+ # Define the path to the application.properties file
22+ file_path = "/opt/application.properties"
3023
31- config ['database' ]['url' ] = db_credentials ['url' ]
32- config ['database' ]['username' ] = db_credentials ['username' ]
33- config ['database' ]['password' ] = db_credentials ['password' ]
24+ # Open the file in read mode
25+ with open (file_path , 'r' ) as f :
26+ # Read the contents of the file into a string
27+ file_contents = f .read ()
3428
35- with open (PROPERTIES_FILE_NAME , 'w' ) as configfile :
36- config .write (configfile )
29+ # Replace the database details in the string
30+ file_contents = file_contents .replace ("spring.datasource.url=jdbc:mysql://localhost:3306/petclinic" , f"spring.datasource.url={ (endpoint_port )} " )
31+ file_contents = file_contents .replace ("spring.datasource.username=petclinic" , f"spring.datasource.username={ database_details ['username' ]} " )
32+ file_contents = file_contents .replace ("spring.datasource.password=petclinic" , f"spring.datasource.password={ database_details ['password' ]} " )
33+
34+ # Open the file in write mode
35+ with open (file_path , 'w' ) as f :
36+ # Write the updated contents of the file to the file
37+ f .write (file_contents )
0 commit comments