Skip to content

Commit

Permalink
fix: use a more generic way to convert a Secret into a property
Browse files Browse the repository at this point in the history
(cherry picked from commit 183e631)
  • Loading branch information
benoitgravitee authored and jgiovaresco committed Mar 26, 2024
1 parent cb1e055 commit 42ec62a
Showing 1 changed file with 20 additions and 1 deletion.
Expand Up @@ -27,12 +27,17 @@
import io.gravitee.rest.api.idp.core.plugin.IdentityProviderDefinition;
import io.gravitee.rest.api.idp.core.plugin.IdentityProviderManager;
import java.util.*;
import javax.annotation.Nonnull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Import;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.ConverterFactory;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.StandardEnvironment;
Expand Down Expand Up @@ -130,7 +135,21 @@ public ConfigurableEnvironment environment() {
protected void customizePropertySources(MutablePropertySources propertySources) {
propertySources.addFirst(new RelaxedPropertySource(plugin.id(), properties));
super.customizePropertySources(propertySources);
this.getConversionService().addConverter(Secret.class, byte[].class, Secret::asBytes);

// add missing converters in this newly created environment
// this syntax allows a property of any kind to be converted from a secret. eg. Secret +> String -> Double
this.getConversionService()
.addConverterFactory(
new ConverterFactory<Secret, Object>() {
final ConversionService conversionService = DefaultConversionService.getSharedInstance();

@Nonnull
public <C> Converter<Secret, C> getConverter(@Nonnull Class<C> targetType) {
return source -> conversionService.convert(source.asString(), targetType);
}
}
);
// byte[] has to be created separately
this.getConversionService().addConverter(Secret.class, String.class, Secret::asString);
}
};
Expand Down

0 comments on commit 42ec62a

Please sign in to comment.