BAEL-6277 - A Guide To Spring Cloud Azure (#13704)
* BAEL-6255 - Run a Spring Boot application in AWS Lambda * BAEL-6255 - Run a Spring Boot application in AWS Lambda * fix on template.yaml * fix on template.yaml * removed log from test * resolved issues reported on PR * BAEL-6277 - A Guide To Spring Cloud Azure First commit * BAEL-6277 - A Guide To Spring Cloud Azure Added to README.md the steps to create the secrets * BAEL-6277 - A Guide To Spring Cloud Azure Added the integration Azure Key Vault Properties * BAEL-6277 - A Guide To Spring Cloud Azure Added the integration Azure Key Vault Properties * BAEL-6277 - A Guide To Spring Cloud Azure Key Vault Added one level package keyvault * BAEL-6277 - A Guide To Spring Cloud Azure Key Vault removed target release version * BAEL-6277 - A Guide To Spring Cloud Azure Key Vault fix compilation of NoSuchElementException * BAEL-6277 - A Guide To Spring Cloud Azure Key Vault fix pom.xml * Revert "BAEL-6277 - A Guide To Spring Cloud Azure Key Vault" This reverts commit 1cca1d0d692646001a6d7de106f3a37fb22839ce. * BAEL-6277 - A Guide To Spring Cloud Azure Key Vault fix pom.xml * BAEL-6277 - A Guide To Spring Cloud Azure Key Vault downgrade version to fix jenkins pipeline error * BAEL-6277 - A Guide To Spring Cloud Azure Key Vault comment run on main class --------- Co-authored-by: Cesare <cesare.valenti@hotmail.com>
This commit is contained in:
+36
@@ -0,0 +1,36 @@
|
||||
package com.baeldung.spring.cloud.azure.keyvault;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.CommandLineRunner;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
import com.azure.security.keyvault.secrets.models.KeyVaultSecret;
|
||||
import com.baeldung.spring.cloud.azure.keyvault.service.KeyVaultClient;
|
||||
import com.baeldung.spring.cloud.azure.keyvault.service.KeyVaultAutoconfiguredClient;
|
||||
|
||||
@SpringBootApplication
|
||||
public class Application implements CommandLineRunner {
|
||||
|
||||
@Value("${database.secret.value}")
|
||||
private String mySecret;
|
||||
|
||||
private final KeyVaultClient keyVaultClient;
|
||||
|
||||
public Application(@Qualifier(value = "KeyVaultAutoconfiguredClient") KeyVaultAutoconfiguredClient keyVaultAutoconfiguredClient) {
|
||||
this.keyVaultClient = keyVaultAutoconfiguredClient;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(String... args) throws Exception {
|
||||
//KeyVaultSecret keyVaultSecret = keyVaultClient.getSecret("my-secret");
|
||||
//System.out.println("Hey, our secret is here ->" + keyVaultSecret.getValue());
|
||||
//System.out.println("Hey, our secret is here from application properties file ->" + mySecret);
|
||||
}
|
||||
}
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
package com.baeldung.spring.cloud.azure.keyvault.data;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.boot.context.properties.ConstructorBinding;
|
||||
|
||||
@ConfigurationProperties("azure.keyvault")
|
||||
@ConstructorBinding
|
||||
public class KeyVaultProperties {
|
||||
private String vaultUrl;
|
||||
private String tenantId;
|
||||
private String clientId;
|
||||
private String clientSecret;
|
||||
|
||||
public KeyVaultProperties(String vaultUrl, String tenantId, String clientId, String clientSecret) {
|
||||
this.vaultUrl = vaultUrl;
|
||||
this.tenantId = tenantId;
|
||||
this.clientId = clientId;
|
||||
this.clientSecret = clientSecret;
|
||||
}
|
||||
|
||||
public String getVaultUrl() {
|
||||
return vaultUrl;
|
||||
}
|
||||
|
||||
public void setVaultUrl(String vaultUrl) {
|
||||
this.vaultUrl = vaultUrl;
|
||||
}
|
||||
|
||||
public String getTenantId() {
|
||||
return tenantId;
|
||||
}
|
||||
|
||||
public void setTenantId(String tenantId) {
|
||||
this.tenantId = tenantId;
|
||||
}
|
||||
|
||||
public String getClientId() {
|
||||
return clientId;
|
||||
}
|
||||
|
||||
public void setClientId(String clientId) {
|
||||
this.clientId = clientId;
|
||||
}
|
||||
|
||||
public String getClientSecret() {
|
||||
return clientSecret;
|
||||
}
|
||||
|
||||
public void setClientSecret(String clientSecret) {
|
||||
this.clientSecret = clientSecret;
|
||||
}
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.spring.cloud.azure.keyvault.service;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import com.azure.security.keyvault.secrets.SecretClient;
|
||||
|
||||
@Component("KeyVaultAutoconfiguredClient")
|
||||
public class KeyVaultAutoconfiguredClient implements KeyVaultClient {
|
||||
private final SecretClient secretClient;
|
||||
|
||||
public KeyVaultAutoconfiguredClient(SecretClient secretClient) {
|
||||
this.secretClient = secretClient;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SecretClient getSecretClient() {
|
||||
return secretClient;
|
||||
}
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
package com.baeldung.spring.cloud.azure.keyvault.service;
|
||||
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
import com.azure.security.keyvault.secrets.SecretClient;
|
||||
import com.azure.security.keyvault.secrets.models.KeyVaultSecret;
|
||||
|
||||
public interface KeyVaultClient {
|
||||
|
||||
SecretClient getSecretClient();
|
||||
|
||||
default KeyVaultSecret getSecret(String key) {
|
||||
KeyVaultSecret secret;
|
||||
try {
|
||||
secret = getSecretClient().getSecret(key);
|
||||
} catch (Exception ex) {
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
return secret;
|
||||
}
|
||||
}
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
package com.baeldung.spring.cloud.azure.keyvault.service;
|
||||
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import com.azure.identity.ClientSecretCredentialBuilder;
|
||||
import com.azure.security.keyvault.secrets.SecretClient;
|
||||
import com.azure.security.keyvault.secrets.SecretClientBuilder;
|
||||
import com.baeldung.spring.cloud.azure.keyvault.data.KeyVaultProperties;
|
||||
|
||||
@EnableConfigurationProperties(KeyVaultProperties.class)
|
||||
@Component("KeyVaultManuallyConfiguredClient")
|
||||
public class KeyVaultManuallyConfiguredClient implements KeyVaultClient {
|
||||
|
||||
private KeyVaultProperties keyVaultProperties;
|
||||
|
||||
private SecretClient secretClient;
|
||||
|
||||
@Override
|
||||
public SecretClient getSecretClient() {
|
||||
if (secretClient == null) {
|
||||
secretClient = new SecretClientBuilder().vaultUrl(keyVaultProperties.getVaultUrl())
|
||||
.credential(new ClientSecretCredentialBuilder().tenantId(keyVaultProperties.getTenantId())
|
||||
.clientId(keyVaultProperties.getClientId())
|
||||
.clientSecret(keyVaultProperties.getClientSecret())
|
||||
.build())
|
||||
.buildClient();
|
||||
}
|
||||
return secretClient;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
spring:
|
||||
cloud:
|
||||
azure:
|
||||
compatibility-verifier:
|
||||
enabled: false
|
||||
keyvault:
|
||||
secret:
|
||||
property-sources[0]:
|
||||
name: key-vault-property-source-1
|
||||
endpoint: https://spring-cloud-azure.vault.azure.net/
|
||||
property-source-enabled: true
|
||||
endpoint: https://spring-cloud-azure.vault.azure.net/
|
||||
azure:
|
||||
keyvault:
|
||||
vaultUrl: myVaultUrl
|
||||
tenantId: myTenantId
|
||||
clientId: myClientId
|
||||
clientSecret: myClientSecret
|
||||
database:
|
||||
secret:
|
||||
value: ${my-database-secret}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
package com.baeldung.spring.cloud.azure.keyvault;
|
||||
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
import com.baeldung.spring.cloud.azure.keyvault.service.KeyVaultAutoconfiguredClient;
|
||||
|
||||
@SpringBootTest(classes = Application.class)
|
||||
public class KeyVaultAutoconfiguredClientUnitTest {
|
||||
|
||||
@Autowired
|
||||
@Qualifier(value = "KeyVaultAutoconfiguredClient")
|
||||
private KeyVaultAutoconfiguredClient keyVaultAutoconfiguredClient;
|
||||
|
||||
@Test
|
||||
void whenANotExistingKeyIsProvided_thenShouldReturnAnError() {
|
||||
String secretKey = "mySecret";
|
||||
Assertions.assertThrows(NoSuchElementException.class, () -> keyVaultAutoconfiguredClient.getSecret(secretKey));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
spring:
|
||||
cloud:
|
||||
azure:
|
||||
compatibility-verifier:
|
||||
enabled: false
|
||||
keyvault:
|
||||
secret:
|
||||
endpoint: https://spring-cloud-azure.vault.azure.net/
|
||||
property-source-enabled: true
|
||||
property-sources:
|
||||
name: key-vault-property-source-1
|
||||
endpoint: https://spring-cloud-azure.vault.azure.net/
|
||||
azure:
|
||||
keyvault:
|
||||
vaultUrl: myVaultUrl
|
||||
tenantId: myTenantId
|
||||
clientId: myClientId
|
||||
clientSecret: myClientSecret
|
||||
database:
|
||||
secret:
|
||||
value: my-database-secret
|
||||
Reference in New Issue
Block a user