Skip to content

Commit

Permalink
Merge pull request #192 from hwupathum/pkcs12
Browse files Browse the repository at this point in the history
Use getKeystoreInstance from KeystoreUtils
  • Loading branch information
hwupathum authored Oct 3, 2024
2 parents 18f44b3 + 0c558f7 commit 3dc4190
Show file tree
Hide file tree
Showing 18 changed files with 187 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.wso2.carbon.identity.governance.IdentityGovernanceService;
import org.wso2.carbon.identity.governance.common.IdentityConnectorConfig;
import org.wso2.carbon.utils.Axis2ConfigurationContextObserver;
import org.wso2.carbon.utils.security.KeystoreUtils;

import java.io.FileInputStream;
import java.io.IOException;
Expand Down Expand Up @@ -91,7 +92,7 @@ protected void activate(ComponentContext context) {
String keyStoreType = config.getFirstProperty("Security.TrustStore.Type");
String password = config.getFirstProperty("Security.TrustStore.Password");
try (InputStream keyStoreStream = new FileInputStream(filePath)) {
KeyStore keyStore = KeyStore.getInstance(keyStoreType); // or "PKCS12"
KeyStore keyStore = KeystoreUtils.getKeystoreInstance(keyStoreType); // or "PKCS12"
keyStore.load(keyStoreStream, password.toCharArray());
AnalyticsFunctionsServiceHolder.getInstance().setTrustStore(keyStore);
} catch (IOException | CertificateException | KeyStoreException | NoSuchAlgorithmException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import com.google.gson.JsonObject;
import org.graalvm.polyglot.HostAccess;
import org.mockito.Mockito;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
Expand All @@ -37,6 +39,7 @@
import org.wso2.carbon.identity.application.common.model.Property;
import org.wso2.carbon.identity.application.common.model.ServiceProvider;
import org.wso2.carbon.identity.application.common.model.script.AuthenticationScriptConfig;
import org.wso2.carbon.identity.central.log.mgt.internal.CentralLogMgtServiceComponentHolder;
import org.wso2.carbon.identity.common.testng.InjectMicroservicePort;
import org.wso2.carbon.identity.common.testng.WithCarbonHome;
import org.wso2.carbon.identity.common.testng.WithH2Database;
Expand All @@ -47,6 +50,7 @@
import org.wso2.carbon.identity.conditional.auth.functions.test.utils.sequence.JsTestException;
import org.wso2.carbon.identity.conditional.auth.functions.test.utils.sequence.ResponseValidator;
import org.wso2.carbon.identity.core.util.IdentityTenantUtil;
import org.wso2.carbon.identity.event.services.IdentityEventService;
import org.wso2.carbon.identity.governance.IdentityGovernanceException;
import org.wso2.carbon.identity.governance.IdentityGovernanceService;
import org.wso2.carbon.user.api.UserRealm;
Expand All @@ -65,6 +69,7 @@
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import static org.mockito.Mockito.mock;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;

Expand All @@ -86,6 +91,19 @@ public class CallAnalyticsFunctionImplTest extends JsSequenceHandlerAbstractTest
@InjectMicroservicePort
private int microServicePort;

@BeforeClass
protected void setUpMocks() {

IdentityEventService identityEventService = mock(IdentityEventService.class);
CentralLogMgtServiceComponentHolder.getInstance().setIdentityEventService(identityEventService);
}

@AfterClass
protected void tearDown() {

CentralLogMgtServiceComponentHolder.getInstance().setIdentityEventService(null);
}

@BeforeMethod
protected void setUp() throws Exception {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,15 @@
import org.wso2.carbon.identity.secret.mgt.core.SecretResolveManager;
import org.wso2.carbon.identity.secret.mgt.core.SecretResolveManagerImpl;
import org.wso2.carbon.utils.Axis2ConfigurationContextObserver;
import org.wso2.carbon.utils.security.KeystoreUtils;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.cert.CertificateException;

/**
Expand Down Expand Up @@ -87,10 +89,11 @@ protected void activate(ComponentContext context) throws FrameworkException {
String keyStoreType = config.getFirstProperty("Security.TrustStore.Type");
String password = config.getFirstProperty("Security.TrustStore.Password");
try (InputStream keyStoreStream = new FileInputStream(filePath)) {
KeyStore keyStore = KeyStore.getInstance(keyStoreType); // or "PKCS12"
KeyStore keyStore = KeystoreUtils.getKeystoreInstance(keyStoreType); // or "PKCS12"
keyStore.load(keyStoreStream, password.toCharArray());
ChoreoFunctionServiceHolder.getInstance().setTrustStore(keyStore);
} catch (IOException | CertificateException | KeyStoreException | NoSuchAlgorithmException e) {
} catch (IOException | CertificateException | KeyStoreException | NoSuchAlgorithmException |
NoSuchProviderException e) {
LOG.error("Error while loading truststore.", e);
throw new FrameworkException("Error while trying to load Key Store.", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@
import org.apache.commons.logging.LogFactory;
import org.graalvm.polyglot.HostAccess;
import org.mockito.Mockito;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
Expand All @@ -49,6 +51,7 @@
import org.wso2.carbon.identity.application.common.model.LocalAndOutboundAuthenticationConfig;
import org.wso2.carbon.identity.application.common.model.ServiceProvider;
import org.wso2.carbon.identity.application.common.model.script.AuthenticationScriptConfig;
import org.wso2.carbon.identity.central.log.mgt.internal.CentralLogMgtServiceComponentHolder;
import org.wso2.carbon.identity.common.testng.InjectMicroservicePort;
import org.wso2.carbon.identity.common.testng.WithCarbonHome;
import org.wso2.carbon.identity.common.testng.WithH2Database;
Expand All @@ -62,6 +65,7 @@
import org.wso2.carbon.identity.conditional.auth.functions.test.utils.sequence.JsTestException;
import org.wso2.carbon.identity.conditional.auth.functions.test.utils.sequence.ResponseValidator;
import org.wso2.carbon.identity.core.util.IdentityTenantUtil;
import org.wso2.carbon.identity.event.services.IdentityEventService;
import org.wso2.carbon.user.api.UserRealm;
import org.wso2.carbon.user.core.service.RealmService;

Expand All @@ -81,6 +85,7 @@
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import static org.mockito.Mockito.mock;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;

Expand Down Expand Up @@ -115,6 +120,19 @@ public class CallChoreoFunctionImplTest extends JsSequenceHandlerAbstractTest {
@InjectMicroservicePort
private int microServicePort;

@BeforeClass
protected void setUpMocks() {

IdentityEventService identityEventService = mock(IdentityEventService.class);
CentralLogMgtServiceComponentHolder.getInstance().setIdentityEventService(identityEventService);
}

@AfterClass
protected void tearDown() {

CentralLogMgtServiceComponentHolder.getInstance().setIdentityEventService(null);
}

@BeforeMethod
protected void setUp() throws Exception {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@
org.wso2.carbon.identity.event; version="${carbon.identity.package.import.version.range}",
org.wso2.carbon.identity.governance; version="${identity.governance.import.version.range}",
org.wso2.carbon.identity.governance.common; version="${identity.governance.import.version.range}",
org.wso2.carbon.utils.*;version="${carbon.kernel.package.import.version.range}",
</Import-Package>
</instructions>
</configuration>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.wso2.carbon.identity.core.util.IdentityCoreInitializedEvent;
import org.wso2.carbon.identity.governance.IdentityGovernanceService;
import org.wso2.carbon.identity.governance.common.IdentityConnectorConfig;
import org.wso2.carbon.utils.security.KeystoreUtils;

import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -83,7 +84,7 @@ protected void activate(ComponentContext context) {
String keyStoreType = config.getFirstProperty(SECURITY_TRUSTSTORE_TYPE);
String password = config.getFirstProperty(SECURITY_TRUSTSTORE_PASSWORD);
try (InputStream keyStoreStream = Files.newInputStream(Paths.get(filePath))) {
KeyStore keyStore = KeyStore.getInstance(keyStoreType); // or "PKCS12"
KeyStore keyStore = KeystoreUtils.getKeystoreInstance(keyStoreType); // or "PKCS12"
keyStore.load(keyStoreStream, password.toCharArray());
ElasticFunctionsServiceHolder.getInstance().setTrustStore(keyStore);
} catch (IOException | CertificateException | KeyStoreException | NoSuchAlgorithmException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.google.gson.Gson;
import org.json.JSONObject;
import org.mockito.Mockito;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
Expand All @@ -33,6 +34,7 @@
import org.wso2.carbon.identity.application.authentication.framework.store.LongWaitStatusStoreService;
import org.wso2.carbon.identity.application.common.model.Property;
import org.wso2.carbon.identity.application.common.model.ServiceProvider;
import org.wso2.carbon.identity.central.log.mgt.internal.CentralLogMgtServiceComponentHolder;
import org.wso2.carbon.identity.common.testng.WithCarbonHome;
import org.wso2.carbon.identity.common.testng.WithH2Database;
import org.wso2.carbon.identity.common.testng.WithMicroService;
Expand All @@ -42,6 +44,7 @@
import org.wso2.carbon.identity.conditional.auth.functions.test.utils.sequence.JsSequenceHandlerAbstractTest;
import org.wso2.carbon.identity.conditional.auth.functions.test.utils.sequence.JsTestException;
import org.wso2.carbon.identity.core.util.IdentityTenantUtil;
import org.wso2.carbon.identity.event.services.IdentityEventService;
import org.wso2.carbon.identity.governance.IdentityGovernanceException;
import org.wso2.carbon.identity.governance.IdentityGovernanceService;
import org.wso2.msf4j.Response;
Expand All @@ -56,6 +59,7 @@
import java.util.HashMap;
import java.util.Map;

import static org.mockito.Mockito.mock;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;

Expand All @@ -74,13 +78,21 @@ public class CallElasticFunctionImplTest extends JsSequenceHandlerAbstractTest {
@InjectMicroservicePort
private int microServicePort;

@AfterClass
protected void tearDown() {

CentralLogMgtServiceComponentHolder.getInstance().setIdentityEventService(null);
}

@BeforeClass
@Parameters({"scriptEngine"})
protected void initClass(String scriptEngine) throws Exception {

super.setUp(scriptEngine);
CarbonConstants.ENABLE_LEGACY_AUTHZ_RUNTIME = true;
sequenceHandlerRunner.registerJsFunction("callElastic", new CallElasticFunctionImpl());
IdentityEventService identityEventService = mock(IdentityEventService.class);
CentralLogMgtServiceComponentHolder.getInstance().setIdentityEventService(identityEventService);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
Expand All @@ -40,13 +42,15 @@
import org.wso2.carbon.identity.application.authentication.framework.internal.FrameworkServiceDataHolder;
import org.wso2.carbon.identity.application.authentication.framework.util.FrameworkConstants;
import org.wso2.carbon.identity.application.common.model.ServiceProvider;
import org.wso2.carbon.identity.central.log.mgt.internal.CentralLogMgtServiceComponentHolder;
import org.wso2.carbon.identity.common.testng.WithCarbonHome;
import org.wso2.carbon.identity.common.testng.WithH2Database;
import org.wso2.carbon.identity.common.testng.WithRealmService;
import org.wso2.carbon.identity.conditional.auth.functions.http.util.HTTPConstants;
import org.wso2.carbon.identity.conditional.auth.functions.test.utils.sequence.JsSequenceHandlerAbstractTest;
import org.wso2.carbon.identity.conditional.auth.functions.test.utils.sequence.JsSequenceHandlerRunner;
import org.wso2.carbon.identity.conditional.auth.functions.test.utils.sequence.JsTestException;
import org.wso2.carbon.identity.event.services.IdentityEventService;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
Expand All @@ -55,6 +59,7 @@
import java.util.HashMap;
import java.util.Map;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

/**
Expand All @@ -65,6 +70,19 @@
@WithRealmService(injectToSingletons = FrameworkServiceDataHolder.class)
public class CookieFunctionImplTest extends JsSequenceHandlerAbstractTest {

@BeforeClass
protected void setUpMocks() {

IdentityEventService identityEventService = mock(IdentityEventService.class);
CentralLogMgtServiceComponentHolder.getInstance().setIdentityEventService(identityEventService);
}

@AfterClass
protected void tearDown() {

CentralLogMgtServiceComponentHolder.getInstance().setIdentityEventService(null);
}

@BeforeMethod
protected void setUp() throws Exception {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import org.wso2.carbon.identity.application.common.model.LocalAndOutboundAuthenticationConfig;
import org.wso2.carbon.identity.application.common.model.ServiceProvider;
import org.wso2.carbon.identity.application.common.model.script.AuthenticationScriptConfig;
import org.wso2.carbon.identity.central.log.mgt.internal.CentralLogMgtServiceComponentHolder;
import org.wso2.carbon.identity.common.testng.InjectMicroservicePort;
import org.wso2.carbon.identity.common.testng.WithCarbonHome;
import org.wso2.carbon.identity.common.testng.WithH2Database;
Expand All @@ -57,6 +58,7 @@
import org.wso2.carbon.identity.conditional.auth.functions.test.utils.sequence.JsTestException;
import org.wso2.carbon.identity.conditional.auth.functions.test.utils.sequence.ResponseValidator;
import org.wso2.carbon.identity.core.util.IdentityTenantUtil;
import org.wso2.carbon.identity.event.services.IdentityEventService;

import java.util.Date;
import java.time.Instant;
Expand All @@ -74,6 +76,7 @@
import javax.ws.rs.Produces;

import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.doNothing;
Expand Down Expand Up @@ -127,12 +130,15 @@ protected void initClass(String scriptEngine) throws Exception {
// Mocking the executeHttpMethod method to avoid actual http calls.
httpGetFunction = spy(new HTTPGetFunctionImpl());
doNothing().when(httpGetFunction).executeHttpMethod(any(), any(), any());
IdentityEventService identityEventService = mock(IdentityEventService.class);
CentralLogMgtServiceComponentHolder.getInstance().setIdentityEventService(identityEventService);
}

@AfterClass
protected void tearDown() {

unsetAllowedDomains();
CentralLogMgtServiceComponentHolder.getInstance().setIdentityEventService(null);
}

@AfterMethod
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import org.wso2.carbon.identity.application.common.model.LocalAndOutboundAuthenticationConfig;
import org.wso2.carbon.identity.application.common.model.ServiceProvider;
import org.wso2.carbon.identity.application.common.model.script.AuthenticationScriptConfig;
import org.wso2.carbon.identity.central.log.mgt.internal.CentralLogMgtServiceComponentHolder;
import org.wso2.carbon.identity.common.testng.InjectMicroservicePort;
import org.wso2.carbon.identity.common.testng.WithCarbonHome;
import org.wso2.carbon.identity.common.testng.WithH2Database;
Expand All @@ -57,6 +58,7 @@
import org.wso2.carbon.identity.conditional.auth.functions.test.utils.sequence.JsTestException;
import org.wso2.carbon.identity.conditional.auth.functions.test.utils.sequence.ResponseValidator;
import org.wso2.carbon.identity.core.util.IdentityTenantUtil;
import org.wso2.carbon.identity.event.services.IdentityEventService;

import java.util.Date;
import java.time.Instant;
Expand All @@ -73,6 +75,7 @@
import javax.ws.rs.Produces;

import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.doNothing;
Expand Down Expand Up @@ -123,12 +126,15 @@ protected void initClass(String scriptEngine) throws Exception {
// Mocking the executeHttpMethod method to avoid actual http calls.
httpPostFunction = spy(new HTTPPostFunctionImpl());
doNothing().when(httpPostFunction).executeHttpMethod(any(), any(), any());
IdentityEventService identityEventService = mock(IdentityEventService.class);
CentralLogMgtServiceComponentHolder.getInstance().setIdentityEventService(identityEventService);
}

@AfterClass
protected void tearDown() {

unsetAllowedDomains();
CentralLogMgtServiceComponentHolder.getInstance().setIdentityEventService(null);
}

@AfterMethod
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@
org.wso2.carbon.identity.event.event; version="${carbon.identity.package.import.version.range}",
org.wso2.carbon.identity.event.services; version="${carbon.identity.package.import.version.range}",
org.wso2.carbon.identity.event.handler.notification.exception;
version="${identity.event.handler.notification.version}",
version="${identity.event.handler.notification.version.range}",
org.wso2.carbon.identity.central.log.mgt.utils; version="${carbon.identity.package.import.version.range}",
org.wso2.carbon.user.core.service; version="${carbon.kernel.package.import.version.range}"
</Import-Package>
Expand Down
Loading

0 comments on commit 3dc4190

Please sign in to comment.