Skip to content

Commit

Permalink
New generic keystore file loader.
Browse files Browse the repository at this point in the history
  • Loading branch information
Thisara-Welmilla committed Feb 2, 2024
1 parent 18049d7 commit c0b1282
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ SAMPLES_HOME
│ ├── oidc-sso-sample
│ │ ├── oidc-jks-loader
│ │ ├── pickup-dispatch
│ │ └── pickup-manager
│ │ ├── pickup-manager
│ │ └── oidc-keystore-loader
│ └── saml2-sso-sample
│ ├── saml2-web-app-pickup-dispatch
│ └── saml2-web-app-pickup-manager
Expand Down
41 changes: 41 additions & 0 deletions sso-samples/oidc-sso-sample/oidc-keystore-loader/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.wso2.samples.is</groupId>
<artifactId>oidc-sso-sample</artifactId>
<version>4.6.0-SNAPSHOT</version>
</parent>

<artifactId>oidc-keystore-loader</artifactId>

<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
<sourceDirectory>src/main</sourceDirectory>
<finalName>oidc-jks-loader</finalName>

<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-scr-plugin</artifactId>
<executions>
<execution>
<id>generate-scr-scrdescriptor</id>
<phase>none</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>

Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.wso2.sample.identity;

import java.net.URISyntaxException;
import java.nio.file.Paths;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* A listener to get invoked at application deployment.
* This will allow us to set the carbon keystore for HTTPS communication.
*/
public class KeystoreLoader implements ServletContextListener {

private static final Logger LOGGER = Logger.getLogger(KeystoreLoader.class.getName());

@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
// First find keystore properties
final InputStream keystoreInputStream = this.getClass().getClassLoader()
.getResourceAsStream("keystore.properties");

if (keystoreInputStream == null) {
LOGGER.log(Level.SEVERE, "keystore.properties not found. Trust store properties will not be set.");
return;
}

// Load properties
final Properties keystoreProperties = new Properties();

try {
keystoreProperties.load(keystoreInputStream);
} catch (IOException e) {
LOGGER.log(Level.SEVERE, "Error while loading properties.", e);
return;
}

// Find and set keystore required for IS server communication
final URL resource = this.getClass().getClassLoader()
.getResource(keystoreProperties.getProperty("keystorename"));

if (resource != null) {
try {
String trustStorePath = Paths.get(resource.toURI()).toFile().getAbsolutePath();
LOGGER.log(Level.INFO, "Setting trust store path to : " + trustStorePath);
System.setProperty("javax.net.ssl.trustStore", trustStorePath);
} catch (URISyntaxException e) {
LOGGER.log(Level.SEVERE, "Unable to find keystore defined by properties. " +
"Trust store properties will not be set.", e);
}
System.setProperty("javax.net.ssl.trustStorePassword", keystoreProperties.getProperty("keystorepassword"));
} else {
LOGGER.log(Level.INFO, "Unable to find keystore defined by properties. " +
"Trust store properties will not be set.");
}
}

@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
// Ignored
}
}
1 change: 1 addition & 0 deletions sso-samples/oidc-sso-sample/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
<module>oidc-jks-loader</module>
<module>pickup-dispatch</module>
<module>pickup-manager</module>
<module>oidc-keystore-loader</module>
</modules>

<build>
Expand Down

0 comments on commit c0b1282

Please sign in to comment.