Skip to content

Commit 3da0358

Browse files
jzheauxAyush Kohli
authored and
Ayush Kohli
committed
Inline ResourceKeyConverterAdapter
Closes spring-projectsgh-9689 Closes spring-projectsgh-9626
1 parent f0af156 commit 3da0358

File tree

3 files changed

+70
-220
lines changed

3 files changed

+70
-220
lines changed

config/src/main/java/org/springframework/security/config/crypto/RsaKeyConversionServicePostProcessor.java

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@
1818

1919
import java.beans.PropertyEditor;
2020
import java.beans.PropertyEditorSupport;
21+
import java.io.ByteArrayInputStream;
22+
import java.io.IOException;
23+
import java.io.InputStream;
24+
import java.io.UncheckedIOException;
25+
import java.nio.charset.StandardCharsets;
26+
import java.security.Key;
2127
import java.security.interfaces.RSAPrivateKey;
2228
import java.security.interfaces.RSAPublicKey;
2329

@@ -27,8 +33,9 @@
2733
import org.springframework.core.convert.ConversionService;
2834
import org.springframework.core.convert.converter.Converter;
2935
import org.springframework.core.convert.converter.ConverterRegistry;
36+
import org.springframework.core.io.DefaultResourceLoader;
37+
import org.springframework.core.io.Resource;
3038
import org.springframework.core.io.ResourceLoader;
31-
import org.springframework.security.converter.ResourceKeyConverterAdapter;
3239
import org.springframework.security.converter.RsaKeyConverters;
3340
import org.springframework.util.Assert;
3441
import org.springframework.util.StringUtils;
@@ -104,4 +111,66 @@ public void setAsText(String text) throws IllegalArgumentException {
104111

105112
}
106113

114+
static class ResourceKeyConverterAdapter<T extends Key> implements Converter<String, T> {
115+
116+
private ResourceLoader resourceLoader = new DefaultResourceLoader();
117+
118+
private final Converter<String, T> delegate;
119+
120+
/**
121+
* Construct a {@link ResourceKeyConverterAdapter} with the provided parameters
122+
* @param delegate converts a stream of key material into a {@link Key}
123+
*/
124+
ResourceKeyConverterAdapter(Converter<InputStream, T> delegate) {
125+
this.delegate = pemInputStreamConverter().andThen(autoclose(delegate));
126+
}
127+
128+
/**
129+
* {@inheritDoc}
130+
*/
131+
@Override
132+
public T convert(String source) {
133+
return this.delegate.convert(source);
134+
}
135+
136+
/**
137+
* Use this {@link ResourceLoader} to read the key material
138+
* @param resourceLoader the {@link ResourceLoader} to use
139+
*/
140+
void setResourceLoader(ResourceLoader resourceLoader) {
141+
Assert.notNull(resourceLoader, "resourceLoader cannot be null");
142+
this.resourceLoader = resourceLoader;
143+
}
144+
145+
private Converter<String, InputStream> pemInputStreamConverter() {
146+
return (source) -> source.startsWith("-----") ? toInputStream(source)
147+
: toInputStream(this.resourceLoader.getResource(source));
148+
}
149+
150+
private InputStream toInputStream(String raw) {
151+
return new ByteArrayInputStream(raw.getBytes(StandardCharsets.UTF_8));
152+
}
153+
154+
private InputStream toInputStream(Resource resource) {
155+
try {
156+
return resource.getInputStream();
157+
}
158+
catch (IOException ex) {
159+
throw new UncheckedIOException(ex);
160+
}
161+
}
162+
163+
private <T> Converter<InputStream, T> autoclose(Converter<InputStream, T> inputStreamKeyConverter) {
164+
return (inputStream) -> {
165+
try (InputStream is = inputStream) {
166+
return inputStreamKeyConverter.convert(is);
167+
}
168+
catch (IOException ex) {
169+
throw new UncheckedIOException(ex);
170+
}
171+
};
172+
}
173+
174+
}
175+
107176
}

core/src/main/java/org/springframework/security/converter/ResourceKeyConverterAdapter.java

Lines changed: 0 additions & 102 deletions
This file was deleted.

core/src/test/java/org/springframework/security/converter/ResourceKeyConverterAdapterTests.java

Lines changed: 0 additions & 117 deletions
This file was deleted.

0 commit comments

Comments
 (0)