Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[master] fix: add secret converter for all IdP plugin #7001

Merged
merged 2 commits into from Mar 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -16,6 +16,7 @@
package io.gravitee.rest.api.idp.core.plugin.impl;

import io.gravitee.common.util.RelaxedPropertySource;
import io.gravitee.node.api.secrets.model.Secret;
import io.gravitee.plugin.core.api.Plugin;
import io.gravitee.plugin.core.api.PluginContextFactory;
import io.gravitee.plugin.core.internal.AnnotationBasedPluginContextConfigurer;
Expand All @@ -26,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 @@ -129,6 +135,22 @@ public ConfigurableEnvironment environment() {
protected void customizePropertySources(MutablePropertySources propertySources) {
propertySources.addFirst(new RelaxedPropertySource(plugin.id(), properties));
super.customizePropertySources(propertySources);

// 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