[BAEL-2284] R2DBC Sample Project (#7314)
* [BAEL-2284] Sample project * [BAEL-2284] Sample project * [BAEL-2284] Sample code adjustments * [BAEL-2284] Sample code adjustments * [BAEL-2284] Sample code adjustments * [BAEL-2284] Add README
This commit is contained in:
committed by
Grzegorz Piwowarek
parent
c7433926c5
commit
76bb391379
@@ -42,6 +42,18 @@
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-jdbc</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-devtools</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<dependencyManagement>
|
||||
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package org.baeldung.spring.cloud.vaultsample;
|
||||
|
||||
import org.baeldung.spring.cloud.vaultsample.domain.Account;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface AccountRepo extends JpaRepository<Account, Long>{
|
||||
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
package org.baeldung.spring.cloud.vaultsample;
|
||||
|
||||
import org.baeldung.spring.cloud.vaultsample.domain.Account;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
public class AccountResource {
|
||||
@Autowired
|
||||
private AccountRepo repo;
|
||||
|
||||
@GetMapping("/account/{id}")
|
||||
public ResponseEntity<Account> getAccount(@PathVariable("id") Long id) {
|
||||
|
||||
Account acc = repo.findById(id).orElse(null);
|
||||
if ( acc != null ) {
|
||||
return new ResponseEntity<Account>(acc, HttpStatus.OK);
|
||||
}
|
||||
else {
|
||||
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
|
||||
}
|
||||
}
|
||||
}
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package org.baeldung.spring.cloud.vaultsample;
|
||||
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* @author Philippe
|
||||
*
|
||||
*/
|
||||
@RestController
|
||||
public class SecretResource {
|
||||
|
||||
@Autowired
|
||||
Environment env;
|
||||
|
||||
@GetMapping("/secret/{key}")
|
||||
public ResponseEntity<String> readSecret(@PathVariable("key") String key) {
|
||||
|
||||
String value = env.getProperty(key);
|
||||
|
||||
if ( value != null ) {
|
||||
return new ResponseEntity<String>(value, HttpStatus.OK);
|
||||
}
|
||||
else {
|
||||
return new ResponseEntity<String>("not found", HttpStatus.NOT_FOUND);
|
||||
}
|
||||
}
|
||||
}
|
||||
+133
@@ -0,0 +1,133 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package org.baeldung.spring.cloud.vaultsample.domain;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Table;
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
/**
|
||||
* @author Philippe
|
||||
*
|
||||
*/
|
||||
@Entity
|
||||
@Table(name = "account")
|
||||
public class Account {
|
||||
|
||||
@Id
|
||||
private Long id;
|
||||
|
||||
@NotNull
|
||||
private String name;
|
||||
|
||||
private Long branchId;
|
||||
private Long customerId;
|
||||
|
||||
/**
|
||||
* @return the id
|
||||
*/
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param id the id to set
|
||||
*/
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the name
|
||||
*/
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param name the name to set
|
||||
*/
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the branchId
|
||||
*/
|
||||
public Long getBranchId() {
|
||||
return branchId;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param branchId the branchId to set
|
||||
*/
|
||||
public void setBranchId(Long branchId) {
|
||||
this.branchId = branchId;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the customerId
|
||||
*/
|
||||
public Long getCustomerId() {
|
||||
return customerId;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param customerId the customerId to set
|
||||
*/
|
||||
public void setCustomerId(Long customerId) {
|
||||
this.customerId = customerId;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see java.lang.Object#hashCode()
|
||||
*/
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + ((branchId == null) ? 0 : branchId.hashCode());
|
||||
result = prime * result + ((customerId == null) ? 0 : customerId.hashCode());
|
||||
result = prime * result + ((id == null) ? 0 : id.hashCode());
|
||||
result = prime * result + ((name == null) ? 0 : name.hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see java.lang.Object#equals(java.lang.Object)
|
||||
*/
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
Account other = (Account) obj;
|
||||
if (branchId == null) {
|
||||
if (other.branchId != null)
|
||||
return false;
|
||||
} else if (!branchId.equals(other.branchId))
|
||||
return false;
|
||||
if (customerId == null) {
|
||||
if (other.customerId != null)
|
||||
return false;
|
||||
} else if (!customerId.equals(other.customerId))
|
||||
return false;
|
||||
if (id == null) {
|
||||
if (other.id != null)
|
||||
return false;
|
||||
} else if (!id.equals(other.id))
|
||||
return false;
|
||||
if (name == null) {
|
||||
if (other.name != null)
|
||||
return false;
|
||||
} else if (!name.equals(other.name))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,6 +1,11 @@
|
||||
spring:
|
||||
application:
|
||||
name: fakebank
|
||||
|
||||
datasource:
|
||||
url: jdbc:mysql://localhost:3306/fakebank
|
||||
spring:
|
||||
application:
|
||||
name: fakebank
|
||||
|
||||
datasource:
|
||||
url: jdbc:mysql://localhost:3306/fakebank?serverTimezone=GMT-3
|
||||
hikari:
|
||||
connection-test-query: select 1
|
||||
idle-timeout: 5000
|
||||
max-lifetime: 120000
|
||||
maximum-pool-size: 5
|
||||
|
||||
@@ -4,8 +4,6 @@ spring:
|
||||
uri: https://localhost:8200
|
||||
connection-timeout: 5000
|
||||
read-timeout: 15000
|
||||
config:
|
||||
order: -10
|
||||
|
||||
ssl:
|
||||
trust-store: classpath:/vault.jks
|
||||
@@ -15,17 +13,19 @@ spring:
|
||||
enabled: true
|
||||
application-name: fakebank
|
||||
|
||||
kv:
|
||||
enabled: false
|
||||
backend: kv
|
||||
application-name: fakebank
|
||||
|
||||
# kv:
|
||||
# enabled: false
|
||||
# backend: kv
|
||||
# application-name: fakebank
|
||||
#
|
||||
database:
|
||||
enabled: true
|
||||
role: fakebank-accounts-rw
|
||||
# backend: database
|
||||
# username-property: spring.datasource.username
|
||||
# password-property: spring.datasource.password
|
||||
backend: database
|
||||
username-property: spring.datasource.username
|
||||
password-property: spring.datasource.password
|
||||
#
|
||||
#
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -17,4 +17,7 @@ listener "tcp" {
|
||||
tls_key_file = "./src/test/vault-config/localhost.key"
|
||||
}
|
||||
|
||||
// Audit to stdout
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -5,16 +5,13 @@
|
||||
2. Open another shell and execute the command below:
|
||||
> vault operator init
|
||||
|
||||
Vault will output the unseal keys and root token: STORE THEM SAFELY !!!
|
||||
Unseal Key 1: Iwvpd4IVofhcmQ2HEIPs5HMUbz4tz6JhqmLZ6+1MhAPx
|
||||
Unseal Key 2: ANQDXUFGGtLtt6grX25YsdmeKELhM/ioKWzwFukJIe2f
|
||||
Unseal Key 3: 8MHyzFnOvlwVQzdWYJ3BIN4xPDOn8a4VemZ/Qe5HgurU
|
||||
Unseal Key 4: ywT9YR9OfxIpA4l1RniNNCvSZWAuNZsAEFRyD7aqFOrp
|
||||
Unseal Key 5: q1c7M+lnlT72jGLoCH+jjri6KGSBhc5lCzlT0I1R9URU
|
||||
|
||||
Example output:
|
||||
Unseal Key 1: OfCseaSZzjTZmrxhfx+5clKobwLGCNiJdAlfixSG9E3o
|
||||
Unseal Key 2: iplVLPTHW0n0WL5XuI6QWwyNtWbKTek1SoKcG0gR7vdT
|
||||
Unseal Key 3: K0TleK3OYUvWFF+uIDsQuf5a+/gkv1PtZ3O47ornzRoF
|
||||
Unseal Key 4: +5zhysLAO4hIdZs0kiZpkrRovw11uQacfloiBwnZBJA/
|
||||
Unseal Key 5: GDwSq18lXV3Cw4MoHsKIH137kuI0mdl36UiD9WxOdulc
|
||||
|
||||
Initial Root Token: d341fdaf-1cf9-936a-3c38-cf5eec94b5c0
|
||||
Initial Root Token: dee7107a-8819-0719-62a3-cea3ea854589
|
||||
|
||||
...
|
||||
|
||||
@@ -73,8 +70,8 @@ flush privileges;
|
||||
> vault write database/roles/fakebank-accounts-rw ^
|
||||
db_name=mysql-fakebank ^
|
||||
creation_statements="CREATE USER '{{name}}'@'%' IDENTIFIED BY '{{password}}';GRANT SELECT,INSERT,UPDATE ON fakebank.* TO '{{name}}'@'%';" ^
|
||||
default_ttl="1m" ^
|
||||
max_ttl="2m"
|
||||
default_ttl="5m" ^
|
||||
max_ttl="30m"
|
||||
|
||||
=== Get credentials
|
||||
> vault read database/creds/fakebank-accounts-rw
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
|
||||
call %~dp0%/vault-env.bat
|
||||
|
||||
vault operator unseal OfCseaSZzjTZmrxhfx+5clKobwLGCNiJdAlfixSG9E3o
|
||||
vault operator unseal iplVLPTHW0n0WL5XuI6QWwyNtWbKTek1SoKcG0gR7vdT
|
||||
vault operator unseal iplVLPTHW0n0WL5XuI6QWwyNtWbKTek1SoKcG0gR7vdT
|
||||
vault operator unseal K0TleK3OYUvWFF+uIDsQuf5a+/gkv1PtZ3O47ornzRoF
|
||||
vault operator unseal Iwvpd4IVofhcmQ2HEIPs5HMUbz4tz6JhqmLZ6+1MhAPx
|
||||
vault operator unseal ANQDXUFGGtLtt6grX25YsdmeKELhM/ioKWzwFukJIe2f
|
||||
vault operator unseal 8MHyzFnOvlwVQzdWYJ3BIN4xPDOn8a4VemZ/Qe5HgurU
|
||||
|
||||
|
||||
Reference in New Issue
Block a user