Skip to content

Commit

Permalink
Mapping spring legacy props (Azure#23166)
Browse files Browse the repository at this point in the history
* use file for property mapping

* add environmentpostprocessor after kv

* map multiple key vault use case
  • Loading branch information
yiliuTo authored and saragluna committed Aug 16, 2021
1 parent 40f8622 commit e8be15d
Show file tree
Hide file tree
Showing 20 changed files with 819 additions and 188 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2371,6 +2371,14 @@
<Field name="azureProperties"/>
<Bug pattern="URF_UNREAD_FIELD"/>
</Match>

<!-- Field is initialized in postProcessEnvironment function -->
<Match>
<Class name="com.azure.spring.autoconfigure.unity.AbstractLegacyPropertyEnvironmentPostProcessor"/>
<Field name="environment"/>
<Bug pattern="UWF_FIELD_NOT_INITIALIZED_IN_CONSTRUCTOR"/>
</Match>

<!-- The transient fields are not used if deserialization happens. -->
<Match>
<Class name="com.azure.identity.implementation.MsalAuthenticationAccount"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
org.springframework.boot.env.EnvironmentPostProcessor=com.azure.spring.keyvault.KeyVaultEnvironmentPostProcessor
org.springframework.boot.env.EnvironmentPostProcessor=com.azure.spring.keyvault.KeyVaultEnvironmentPostProcessor,\
com.azure.spring.keyvault.PostLegacyPropertyEnvironmentPostProcessor
12 changes: 12 additions & 0 deletions sdk/spring/azure-spring-cloud-autoconfigure/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@
<artifactId>HikariCP</artifactId>
<version>4.0.3</version> <!-- {x-version-update;com.zaxxer:HikariCP;external_dependency} -->
<scope>test</scope>
<exclusions>
<exclusion>
<artifactId>slf4j-api</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>

<!-- PostgreSQL -->
Expand Down Expand Up @@ -308,6 +314,12 @@
<artifactId>azure-security-keyvault-jca</artifactId>
<version>2.0.0-beta.1</version> <!-- {x-version-update;com.azure:azure-security-keyvault-jca;current} -->
<scope>test</scope>
<exclusions>
<exclusion>
<artifactId>slf4j-nop</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
<!-- for the samples -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.spring.autoconfigure.unity;

import com.azure.spring.keyvault.KeyVaultEnvironmentPostProcessor;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.Ordered;
import org.springframework.core.env.ConfigurableEnvironment;

import java.util.Properties;

import static com.azure.spring.utils.PropertyLoader.loadPropertiesFromClassPath;

/**
* Abstract class to convert legacy properties to the current when only legacy properties are configured,
* need to be executed before and after {@link KeyVaultEnvironmentPostProcessor}
* if {@link KeyVaultEnvironmentPostProcessor} is enabled.
*/
public abstract class AbstractLegacyPropertyEnvironmentPostProcessor implements EnvironmentPostProcessor, Ordered {

private static Properties springPropertyMap;
static {
// Load the map of each service's legacy properties and associated current properties from classpath.
springPropertyMap = loadPropertiesFromClassPath("legacy-property-mapping.properties");
}

@Override
public abstract int getOrder();

@Override
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
Properties legacyToCurrentMap = buildLegacyToCurrentPropertyMap(environment);
Properties convertedProperties = convertLegacyToCurrent(environment, legacyToCurrentMap);
setConvertedPropertyToEnvironment(environment, convertedProperties);
}

protected Properties buildLegacyToCurrentPropertyMap(ConfigurableEnvironment environment) {
Properties legacyToCurrentMap = new Properties();
legacyToCurrentMap.putAll(springPropertyMap);
return legacyToCurrentMap;
}

/**
* Convert legacy properties to the current and create new {@link Properties} to store mapped current properties
* if only legacy properties are configured.
*
* @param environment The application environment to load property from.
* @param legacyToCurrentMap A {@link Properties} contains a map of all legacy properties and associated current ones.
* @return A {@link Properties} to store mapped current properties
*/
protected abstract Properties convertLegacyToCurrent(ConfigurableEnvironment environment, Properties legacyToCurrentMap);

/**
* Add the mapped current properties to application environment,
* of which the precedence varies in different processors.
*
* @param environment The application environment to load property from.
* @param properties The converted current properties to be configured.
*/
protected abstract void setConvertedPropertyToEnvironment(ConfigurableEnvironment environment, Properties properties);
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ public class CredentialProperties {
/**
* Path of a PEM certificate file to use when performing service principal authentication with Azure.
*/
private String certificatePath;
private String clientCertificatePath;

/**
* Password of the certificate file.
*/
private String certificatePassword;
private String clientCertificatePassword;

/**
* Tenant id for the Azure resources.
Expand All @@ -50,20 +50,20 @@ public void setClientSecret(String clientSecret) {
this.clientSecret = clientSecret;
}

public String getCertificatePath() {
return certificatePath;
public String getClientCertificatePath() {
return clientCertificatePath;
}

public void setCertificatePath(String certificatePath) {
this.certificatePath = certificatePath;
public void setClientCertificatePath(String clientCertificatePath) {
this.clientCertificatePath = clientCertificatePath;
}

public String getCertificatePassword() {
return certificatePassword;
public String getClientCertificatePassword() {
return clientCertificatePassword;
}

public void setCertificatePassword(String certificatePassword) {
this.certificatePassword = certificatePassword;
public void setClientCertificatePassword(String clientCertificatePassword) {
this.clientCertificatePassword = clientCertificatePassword;
}

public String getTenantId() {
Expand Down
Loading

0 comments on commit e8be15d

Please sign in to comment.