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:
parent
4027e83023
commit
51caf72619
@ -54,7 +54,8 @@
|
||||
<module>spring-cloud-bus</module>
|
||||
<module>spring-cloud-data-flow</module>
|
||||
<module>spring-cloud-sleuth</module>
|
||||
<module>spring-cloud-open-telemetry</module>
|
||||
<module>spring-cloud-open-telemetry</module>
|
||||
<module>spring-cloud-azure</module>
|
||||
</modules>
|
||||
|
||||
<build>
|
||||
|
13
spring-cloud-modules/spring-cloud-azure/README.md
Normal file
13
spring-cloud-modules/spring-cloud-azure/README.md
Normal file
@ -0,0 +1,13 @@
|
||||
# Spring Cloud Azure
|
||||
|
||||
# Relevant Articles
|
||||
|
||||
# Azure KeyVault:
|
||||
In order to create the secrets, follow these steps:
|
||||
- create an Azure account
|
||||
- install the Azure Cli an run the following commands
|
||||
- login on Azure -> _az-login_
|
||||
- create a resource group: _az group create --name spring_cloud_azure --location eastus_
|
||||
- create a keyvault storage: _az keyvault create --name new_keyvault --resource-group spring_cloud_azure --location eastus_
|
||||
- create the secrets: > az keyvault secret set --name my-database-secret --value my-database-secret-value --vault-name new_keyvault,> az keyvault secret set --name my-secret --value my-secret-value --vault-name new_keyvault
|
||||
```
|
59
spring-cloud-modules/spring-cloud-azure/pom.xml
Normal file
59
spring-cloud-modules/spring-cloud-azure/pom.xml
Normal file
@ -0,0 +1,59 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung.spring.cloud</groupId>
|
||||
<artifactId>spring-cloud-azure</artifactId>
|
||||
<name>spring-cloud-azure</name>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
<packaging>jar</packaging>
|
||||
<description>Spring Cloud Azure Examples</description>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung.spring.cloud</groupId>
|
||||
<artifactId>spring-cloud-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-dependencies</artifactId>
|
||||
<version>2.7.8</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-dependencies</artifactId>
|
||||
<version>${spring-cloud-dependencies.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.azure.spring</groupId>
|
||||
<artifactId>spring-cloud-azure-starter-keyvault-secrets</artifactId>
|
||||
<version>${azure-key-vault-extension-version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<spring-cloud-dependencies.version>2021.0.3</spring-cloud-dependencies.version>
|
||||
<azure-key-vault-extension-version>4.0.0</azure-key-vault-extension-version>
|
||||
</properties>
|
||||
|
||||
</project>
|
@ -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);
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
@ -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}
|
@ -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
|
Loading…
x
Reference in New Issue
Block a user