Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce SAMLSSOPersistenceManagerFactory to switch between different persistence implementation #6240

Merged
merged 1 commit into from
Dec 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
package org.wso2.carbon.identity.core;

import org.wso2.carbon.identity.base.IdentityException;
import org.wso2.carbon.identity.core.dao.SAMLServiceProviderPersistenceManagerFactory;
import org.wso2.carbon.identity.core.dao.SAMLSSOServiceProviderDAO;
import org.wso2.carbon.identity.core.dao.SAMLSSOServiceProviderRegistryDAOImpl;
import org.wso2.carbon.identity.core.model.SAMLSSOServiceProviderDO;

/**
Expand All @@ -29,7 +29,9 @@
*/
public class SAMLSSOServiceProviderManager {
darshanasbg marked this conversation as resolved.
Show resolved Hide resolved

private static SAMLSSOServiceProviderDAO serviceProviderDAO = new SAMLSSOServiceProviderRegistryDAOImpl();
SAMLServiceProviderPersistenceManagerFactory
samlSSOPersistenceManagerFactory = new SAMLServiceProviderPersistenceManagerFactory();
SAMLSSOServiceProviderDAO serviceProviderDAO = samlSSOPersistenceManagerFactory.getSAMLServiceProviderPersistenceManager();

/**
* Add a saml service provider.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
/**
* This class is used for managing SAML SSO service providers in the Registry.
*/
public class SAMLSSOServiceProviderRegistryDAOImpl extends AbstractDAO<SAMLSSOServiceProviderDO>
public class RegistrySAMLSSOServiceProviderDAOImpl extends AbstractDAO<SAMLSSOServiceProviderDO>
implements SAMLSSOServiceProviderDAO {

private static final String CERTIFICATE_PROPERTY_NAME = "CERTIFICATE";
Expand All @@ -67,9 +67,9 @@ public class SAMLSSOServiceProviderRegistryDAOImpl extends AbstractDAO<SAMLSSOSe
"META.`VALUE` FROM SP_INBOUND_AUTH INBOUND, SP_APP SP, SP_METADATA META WHERE SP.ID = INBOUND.APP_ID AND " +
"SP.ID = META.SP_ID AND META.NAME = ? AND INBOUND.INBOUND_AUTH_KEY = ? AND META.TENANT_ID = ?";

private static Log log = LogFactory.getLog(SAMLSSOServiceProviderRegistryDAOImpl.class);
private static Log log = LogFactory.getLog(RegistrySAMLSSOServiceProviderDAOImpl.class);

public SAMLSSOServiceProviderRegistryDAOImpl () {
public RegistrySAMLSSOServiceProviderDAOImpl() {
}

protected SAMLSSOServiceProviderDO resourceToObject(Resource resource) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright (c) 2024, WSO2 LLC. (http://www.wso2.com).
*
* WSO2 LLC. 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.carbon.identity.core.dao;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.identity.core.util.IdentityUtil;

/**
* Factory class to create instances of SAMLSSOServiceProviderDAO based on the configured storage type.
*/
public class SAMLServiceProviderPersistenceManagerFactory {

private static final Log LOG = LogFactory.getLog(SAMLServiceProviderPersistenceManagerFactory.class);
private static String SAML_STORAGE_TYPE = IdentityUtil.getProperty("DataStorageType.SAML");
private static final String HYBRID = "hybrid";
private static final String DATABASE = "database";

public SAMLSSOServiceProviderDAO getSAMLServiceProviderPersistenceManager() {

SAMLSSOServiceProviderDAO samlSSOServiceProviderDAO = new RegistrySAMLSSOServiceProviderDAOImpl();
if (StringUtils.isNotBlank(SAML_STORAGE_TYPE)) {
switch (SAML_STORAGE_TYPE) {
case HYBRID:
// Initialize hybrid SAML storage.
LOG.info("Hybrid SAML storage initialized.");
break;
case DATABASE:
// Initialize JDBC SAML storage.
LOG.info("JDBC based SAML storage initialized.");
break;
}
}

if (LOG.isDebugEnabled()) {
LOG.debug(
"SAML SSO Service Provider DAO initialized with the type: " + samlSSOServiceProviderDAO.getClass());
}
return samlSSOServiceProviderDAO;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
import org.wso2.carbon.identity.core.dao.OpenIDAdminDAO;
import org.wso2.carbon.identity.core.dao.OpenIDUserDAO;
import org.wso2.carbon.identity.core.dao.ParameterDAO;
import org.wso2.carbon.identity.core.dao.SAMLServiceProviderPersistenceManagerFactory;
import org.wso2.carbon.identity.core.dao.SAMLSSOServiceProviderDAO;
import org.wso2.carbon.identity.core.dao.SAMLSSOServiceProviderRegistryDAOImpl;
import org.wso2.carbon.identity.core.dao.XMPPSettingsDAO;
import org.wso2.carbon.identity.core.model.OpenIDAdminDO;
import org.wso2.carbon.identity.core.model.OpenIDUserDO;
Expand All @@ -36,7 +36,9 @@
public class IdentityPersistenceManager {

private static IdentityPersistenceManager manager = new IdentityPersistenceManager();
private static SAMLSSOServiceProviderDAO serviceProviderDAO = new SAMLSSOServiceProviderRegistryDAOImpl();
SAMLServiceProviderPersistenceManagerFactory
samlSSOPersistenceManagerFactory = new SAMLServiceProviderPersistenceManagerFactory();
SAMLSSOServiceProviderDAO serviceProviderDAO = samlSSOPersistenceManagerFactory.getSAMLServiceProviderPersistenceManager();

private IdentityPersistenceManager() {
}
Expand Down Expand Up @@ -266,8 +268,7 @@ public SAMLSSOServiceProviderDO[] getServiceProviders(Registry registry)
throws IdentityException {

int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
SAMLSSOServiceProviderDAO serviceProviderDOA = new SAMLSSOServiceProviderRegistryDAOImpl();
return serviceProviderDOA.getServiceProviders(tenantId);
return serviceProviderDAO.getServiceProviders(tenantId);
}

public boolean removeServiceProvider(Registry registry, String issuer) throws IdentityException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
*/
public class SAMLSSOServiceProviderDAOTest {

private SAMLSSOServiceProviderRegistryDAOImpl objUnderTest;
private RegistrySAMLSSOServiceProviderDAOImpl objUnderTest;
private boolean transactionStarted = false;

private Registry mockRegistry;
Expand Down Expand Up @@ -106,7 +106,7 @@ public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
}
}).when(mockRegistry).beginTransaction();

objUnderTest = new SAMLSSOServiceProviderRegistryDAOImpl();
objUnderTest = new RegistrySAMLSSOServiceProviderDAOImpl();
identityTenantUtil = mockStatic(IdentityTenantUtil.class);
when(IdentityTenantUtil.getRegistryService()).thenReturn(mockRegistryService);
when(mockRegistryService.getConfigSystemRegistry(TENANT_ID)).thenReturn((UserRegistry) mockRegistry);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright (c) 2024, WSO2 LLC. (http://www.wso2.com).
*
* WSO2 LLC. 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.carbon.identity.core.dao;

import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.lang.reflect.Field;

import static org.testng.Assert.assertTrue;

public class SAMLServiceProviderPersistenceManagerFactoryTest {

private SAMLServiceProviderPersistenceManagerFactory factory;

@BeforeMethod
public void setUp() {

factory = new SAMLServiceProviderPersistenceManagerFactory();

}

@AfterMethod
public void tearDown() throws Exception {

setPrivateStaticField(SAMLServiceProviderPersistenceManagerFactory.class, "SAML_STORAGE_TYPE", "");
factory = null;
}

@Test
public void testGetSAMLServiceProviderPersistenceManagerWithDefaultStorage() throws Exception {

setPrivateStaticField(SAMLServiceProviderPersistenceManagerFactory.class, "SAML_STORAGE_TYPE", "database");
SAMLSSOServiceProviderDAO samlSSOServiceProviderDAO = factory.getSAMLServiceProviderPersistenceManager();
// assertTrue(samlSSOServiceProviderDAO instanceof JDBCSAMLSSOServiceProviderDAOImpl);
}

@Test
public void testGetSAMLServiceProviderPersistenceManagerWithRegistryStorage() throws Exception {

setPrivateStaticField(SAMLServiceProviderPersistenceManagerFactory.class, "SAML_STORAGE_TYPE", "registry");
SAMLSSOServiceProviderDAO samlSSOServiceProviderDAO = factory.getSAMLServiceProviderPersistenceManager();
assertTrue(samlSSOServiceProviderDAO instanceof RegistrySAMLSSOServiceProviderDAOImpl);
}

@Test
public void testGetSAMLServiceProviderPersistenceManagerWithHybridStorage() throws Exception {

setPrivateStaticField(SAMLServiceProviderPersistenceManagerFactory.class, "SAML_STORAGE_TYPE", "hybrid");
SAMLSSOServiceProviderDAO samlSSOServiceProviderDAO = factory.getSAMLServiceProviderPersistenceManager();
// assertTrue(samlSSOServiceProviderDAO instanceof JDBCSAMLSSOServiceProviderDAOImpl);
}

private void setPrivateStaticField(Class<?> clazz, String fieldName, Object newValue)
throws NoSuchFieldException, IllegalAccessException {

Field field = clazz.getDeclaredField(fieldName);
field.setAccessible(true);
field.set(null, newValue);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<class name="org.wso2.carbon.identity.core.util.IdentityUtilTest"/>
<class name="org.wso2.carbon.identity.core.util.IdentityConfigParserTest"/>
<class name="org.wso2.carbon.identity.core.dao.SAMLSSOServiceProviderDAOTest"/>
<class name="org.wso2.carbon.identity.core.dao.SAMLServiceProviderPersistenceManagerFactoryTest"/>
<class name="org.wso2.carbon.identity.core.internal.DefaultServiceURLBuilderTest"/>
<class name="org.wso2.carbon.identity.core.cache.BaseCacheTest"/>
<class name="org.wso2.carbon.identity.core.ThreadLocalAwareThreadPoolExecutorTest"/>
Expand Down
Loading