[BAEL-6165] AWS Secrets Manager integration with Spring Boot for DB. (#13632)

* [BAEL-6165] AWS Secrets Manager integration with Spring Boot for DB.

* [BAEL-6165] Add other types of secrets as example

* [BAEL-6165] externalize version to properties

* [BTL-6165] create UserController

---------

Co-authored-by: Lukasz Wlodkowski <lukasz.w@bluestonepim.com>
This commit is contained in:
Łukasz Włodkowski 2023-04-11 21:08:05 +02:00 committed by GitHub
parent 67d6b1533b
commit 2765db78ae
6 changed files with 143 additions and 2 deletions

View File

@ -25,8 +25,13 @@
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<groupId>com.amazonaws.secretsmanager</groupId>
<artifactId>aws-secretsmanager-jdbc</artifactId>
<version>${aws.secrets.manager.jdbc}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
@ -34,6 +39,11 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.awspring.cloud</groupId>
<artifactId>spring-cloud-starter-aws-secrets-manager-config</artifactId>
<version>${aws.secrets.manager.config}</version>
</dependency>
</dependencies>
<build>
@ -45,4 +55,9 @@
</plugins>
</build>
<properties>
<aws.secrets.manager.config>2.4.4</aws.secrets.manager.config>
<aws.secrets.manager.jdbc>1.0.11</aws.secrets.manager.jdbc>
</properties>
</project>

View File

@ -0,0 +1,29 @@
package com.baeldung.startdbwithawssecretsmanager;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Profile;
import javax.annotation.PostConstruct;
@SpringBootApplication
@Profile("aws")
public class StartWithAWSSecretsManagerApplication {
@Value("${api-key1}")
private String apiKeyValue1;
@Value("${api-key2}")
private String apiKeyValue2;
@PostConstruct
private void postConstruct() {
System.out.println(apiKeyValue1);
System.out.println(apiKeyValue2);
}
public static void main(String[] args) {
SpringApplication.run(StartWithAWSSecretsManagerApplication.class, args);
}
}

View File

@ -0,0 +1,38 @@
package com.baeldung.startdbwithawssecretsmanager.controller;
import com.baeldung.startdbwithawssecretsmanager.model.UserEntity;
import com.baeldung.startdbwithawssecretsmanager.repository.UserRepository;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("users")
public class UserController {
private final UserRepository userRepository;
public UserController(UserRepository userRepository) {
this.userRepository = userRepository;
}
@GetMapping(value = "/{id}", produces = "application/json")
public @ResponseBody UserEntity getUser(@PathVariable Long id) {
return userRepository.findById(id).get();
}
@PostMapping
public UserEntity createUser(@RequestBody UserEntity userEntity) {
return userRepository.save(userEntity);
}
@DeleteMapping(value = "/{id}")
public void removeUser(@PathVariable Long id) {
userRepository.deleteById(id);
}
}

View File

@ -0,0 +1,40 @@
package com.baeldung.startdbwithawssecretsmanager.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class UserEntity {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
public UserEntity() {
}
public UserEntity(Long id, String name) {
this.id = id;
this.name = name;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

View File

@ -0,0 +1,10 @@
package com.baeldung.startdbwithawssecretsmanager.repository;
import com.baeldung.startdbwithawssecretsmanager.model.UserEntity;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface UserRepository
extends CrudRepository<UserEntity, Long> {
}

View File

@ -0,0 +1,9 @@
spring.datasource.driver-class-name=com.amazonaws.secretsmanager.sql.AWSSecretsManagerMySQLDriver
spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect
spring.datasource.url=jdbc-secretsmanager:mysql://database-1.cwhqvgjbpgfw.eu-central-1.rds.amazonaws.com:3306/test
spring.datasource.username=rds/credentials
#Overwriting application.properties configuration back to default.
spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults=true
spring.config.import=aws-secretsmanager:test/secret/