-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
192 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
src/main/java/ee/openeid/siga/client/configuration/SiGaApiConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package ee.openeid.siga.client.configuration; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
import org.apache.hc.core5.ssl.SSLContextBuilder; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.core.io.Resource; | ||
|
||
import javax.net.ssl.SSLContext; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.security.KeyManagementException; | ||
import java.security.KeyStore; | ||
import java.security.KeyStoreException; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.security.cert.CertificateException; | ||
import java.util.Optional; | ||
|
||
@Configuration | ||
public class SiGaApiConfig { | ||
|
||
@Bean | ||
SSLContext sigaApiSslContext(SiGaDemoProperties.SigaApi sigaApiProperties) { | ||
try { | ||
return new SSLContextBuilder() | ||
.loadTrustMaterial(loadTrustStore(sigaApiProperties), null) | ||
.build(); | ||
} catch (KeyManagementException | KeyStoreException | NoSuchAlgorithmException e) { | ||
throw new IllegalStateException("Failed to create SiGa API SSL context", e); | ||
} | ||
} | ||
|
||
private static KeyStore loadTrustStore(SiGaDemoProperties.SigaApi sigaApiProperties) { | ||
String trustStoreType = Optional | ||
.ofNullable(sigaApiProperties.trustStoreType()) | ||
.filter(StringUtils::isNotBlank) | ||
.orElse(KeyStore.getDefaultType()); | ||
|
||
KeyStore trustStore; | ||
try { | ||
trustStore = KeyStore.getInstance(trustStoreType); | ||
} catch (KeyStoreException e) { | ||
throw new IllegalStateException("Failed to create keystore of type: " + trustStoreType, e); | ||
} | ||
|
||
Resource trustStoreResource = sigaApiProperties.trustStore(); | ||
try (InputStream in = trustStoreResource.getInputStream()) { | ||
trustStore.load(in, sigaApiProperties.trustStorePassword()); | ||
} catch (CertificateException | IOException | NoSuchAlgorithmException | NullPointerException e) { | ||
throw new IllegalStateException("Failed to load truststore: " + trustStoreResource, e); | ||
} | ||
|
||
return trustStore; | ||
} | ||
|
||
} |
13 changes: 7 additions & 6 deletions
13
src/main/java/ee/openeid/siga/client/configuration/SiGaDemoProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.