Skip to content

Commit 768a727

Browse files
committed
The organization thumbprint now gets computed from DSF_DEV_FHIR_CLIENT_CERTIFICATE if the DSF_DEV_FHIR_SERVER_ORGANIZATION_THUMBPRINT is omitted in the configuration
1 parent 4054b18 commit 768a727

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

dsf-fhir/dsf-fhir-server/src/main/java/dev/dsf/fhir/spring/config/PropertiesConfig.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
package dev.dsf.fhir.spring.config;
22

3+
import java.io.IOException;
34
import java.net.MalformedURLException;
45
import java.net.URI;
56
import java.net.URISyntaxException;
67
import java.net.URL;
8+
import java.nio.file.Path;
79
import java.nio.file.Paths;
810
import java.security.KeyStore;
11+
import java.security.MessageDigest;
12+
import java.security.NoSuchAlgorithmException;
13+
import java.security.cert.CertificateEncodingException;
914
import java.security.cert.CertificateException;
1015
import java.security.cert.X509Certificate;
1116
import java.time.Duration;
17+
import java.util.HexFormat;
1218
import java.util.List;
1319
import java.util.Properties;
1420

@@ -169,6 +175,7 @@ public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderCon
169175
new DockerSecretsPropertySourceFactory(environment).readDockerSecretsAndAddPropertiesToEnvironment();
170176

171177
injectEndpointProperties(environment);
178+
computeOrganizationThumbprintPropertyIfPossible(environment);
172179

173180
return new PropertySourcesPlaceholderConfigurer();
174181
}
@@ -193,6 +200,31 @@ private static void injectEndpointProperties(ConfigurableEnvironment environment
193200
}
194201
}
195202

203+
private static void computeOrganizationThumbprintPropertyIfPossible(ConfigurableEnvironment environment)
204+
{
205+
try
206+
{
207+
String organizationThumbprint = environment.getProperty("dev.dsf.fhir.server.organization.thumbprint");
208+
209+
if (organizationThumbprint == null)
210+
{
211+
Path clientCertPath = Paths.get(environment.getRequiredProperty("dev.dsf.fhir.client.certificate"));
212+
X509Certificate clientCert = PemReader.readCertificate(clientCertPath);
213+
MessageDigest md = MessageDigest.getInstance("SHA-512");
214+
HexFormat hexFormat = HexFormat.of();
215+
String thumbprint = hexFormat.formatHex(md.digest(clientCert.getEncoded())).toLowerCase();
216+
217+
Properties properties = new Properties();
218+
properties.put("dev.dsf.fhir.server.organization.thumbprint", thumbprint);
219+
220+
environment.getPropertySources().addFirst(new PropertiesPropertySource("organization-thumbprint-properties", properties));
221+
}
222+
} catch (IOException | NoSuchAlgorithmException | CertificateEncodingException e)
223+
{
224+
throw new RuntimeException(e);
225+
}
226+
}
227+
196228
@Override
197229
public void afterPropertiesSet() throws Exception
198230
{

0 commit comments

Comments
 (0)