Skip to content

Commit

Permalink
Add Secret Manager Environment Post Processors (#496)
Browse files Browse the repository at this point in the history
Fix secret manager compatibility for spring boot version >= 2.5.1
  • Loading branch information
dzou authored Aug 2, 2021
1 parent 8a26f8c commit d8457bf
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,13 @@
import com.google.cloud.spring.core.UserAgentHeaderProvider;
import com.google.cloud.spring.secretmanager.SecretManagerPropertySourceLocator;
import com.google.cloud.spring.secretmanager.SecretManagerTemplate;
import com.google.protobuf.ByteString;

import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.env.ConfigurableEnvironment;

/**
Expand All @@ -61,23 +59,6 @@ public GcpSecretManagerBootstrapConfiguration(
this.gcpProjectIdProvider = properties.getProjectId() != null
? properties::getProjectId
: new DefaultGcpProjectIdProvider();

// Registers {@link ByteString} type converters to convert to String and byte[].
configurableEnvironment.getConversionService().addConverter(
new Converter<ByteString, String>() {
@Override
public String convert(ByteString source) {
return source.toStringUtf8();
}
});

configurableEnvironment.getConversionService().addConverter(
new Converter<ByteString, byte[]>() {
@Override
public byte[] convert(ByteString source) {
return source.toByteArray();
}
});
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright 2017-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.cloud.spring.autoconfigure.secretmanager;

import com.google.protobuf.ByteString;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.env.ConfigurableEnvironment;

/**
* Registers converters used by Spring Cloud GCP Secret Manager.
*/
public class GcpSecretManagerEnvironmentPostProcessor implements EnvironmentPostProcessor {

@Override
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
boolean isSecretManagerEnabled =
Boolean.parseBoolean(
environment.getProperty("spring.cloud.gcp.secretmanager.enabled", "true"));

if (isSecretManagerEnabled) {
// Registers {@link ByteString} type converters to convert to String and byte[].
environment.getConversionService().addConverter(
new Converter<ByteString, String>() {
@Override
public String convert(ByteString source) {
return source.toStringUtf8();
}
});

environment.getConversionService().addConverter(
new Converter<ByteString, byte[]>() {
@Override
public byte[] convert(ByteString source) {
return source.toByteArray();
}
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ com.google.cloud.spring.autoconfigure.config.GcpConfigBootstrapConfiguration,\
com.google.cloud.spring.autoconfigure.secretmanager.GcpSecretManagerBootstrapConfiguration

org.springframework.boot.env.EnvironmentPostProcessor=\
com.google.cloud.spring.autoconfigure.sql.CloudSqlEnvironmentPostProcessor
com.google.cloud.spring.autoconfigure.sql.CloudSqlEnvironmentPostProcessor,\
com.google.cloud.spring.autoconfigure.secretmanager.GcpSecretManagerEnvironmentPostProcessor
2 changes: 1 addition & 1 deletion spring-cloud-gcp-samples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.9</version>
<version>2.5.3</version>
<relativePath/>
</parent>

Expand Down

0 comments on commit d8457bf

Please sign in to comment.