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

Replace IdentityPersistenceManager with SAMLSSOServiceProviderManager #395

Merged
merged 4 commits into from
Jun 22, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -27,6 +27,7 @@
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.component.annotations.ReferenceCardinality;
import org.osgi.service.component.annotations.ReferencePolicy;
import org.wso2.carbon.identity.core.SAMLSSOServiceProviderManager;
import org.wso2.carbon.user.core.service.RealmService;

/**
Expand All @@ -41,6 +42,8 @@ public class SAMLQueryServiceComponent {

private static RealmService realmservice = null;

private static SAMLSSOServiceProviderManager samlSSOServiceProviderManager = null;

/**
* This method is used to get created realm service
*
Expand Down Expand Up @@ -106,4 +109,46 @@ protected void unsetRealmService(RealmService realmService) {
log.debug("DefaultUserRealm unset in to bundle");
}
}

/**
* This method is used to set SAMLSSOServiceProviderManager instance
*
* @param samlSSOServiceProviderManager SAMLSSOServiceProviderManager instance
*/
@Reference(
name = "saml.sso.service.provider.manager",
service = SAMLSSOServiceProviderManager.class,
cardinality = ReferenceCardinality.MANDATORY,
policy = ReferencePolicy.DYNAMIC,
unbind = "unsetSAMLSSOServiceProviderManager")
protected void setSAMLSSOServiceProviderManager(SAMLSSOServiceProviderManager samlSSOServiceProviderManager) {

this.samlSSOServiceProviderManager = samlSSOServiceProviderManager;
if (log.isDebugEnabled()) {
log.debug("SAMLSSOServiceProviderManager set in to bundle");
}
}

/**
* This method is used to un-set SAMLSSOServiceProviderManager instance
*
* @param samlSSOServiceProviderManager SAMLSSOServiceProviderManager instance
*/
protected void unsetSAMLSSOServiceProviderManager(SAMLSSOServiceProviderManager samlSSOServiceProviderManager) {

this.samlSSOServiceProviderManager = null;
if (log.isDebugEnabled()) {
log.debug("SAMLSSOServiceProviderManager unset in to bundle");
}
}

/**
* This method is used to get SAMLSSOServiceProviderManager instance
*
* @return SAMLSSOServiceProviderManager instance
*/
public static SAMLSSOServiceProviderManager getSAMLSSOServiceProviderManager() {

return samlSSOServiceProviderManager;
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
/*
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
* Copyright (c) (2016-2023), WSO2 LLC. (http://www.wso2.com).
*
* 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
* 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
* 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.
* 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.query.saml.util;
Expand Down Expand Up @@ -73,15 +73,13 @@
import org.wso2.carbon.context.CarbonContext;
import org.wso2.carbon.identity.base.IdentityException;
import org.wso2.carbon.identity.core.model.SAMLSSOServiceProviderDO;
import org.wso2.carbon.identity.core.persistence.IdentityPersistenceManager;
import org.wso2.carbon.identity.query.saml.SignKeyDataHolder;
import org.wso2.carbon.identity.query.saml.exception.IdentitySAML2QueryException;
import org.wso2.carbon.identity.query.saml.internal.SAMLQueryServiceComponent;
import org.wso2.carbon.identity.saml.common.util.SAMLInitializer;
import org.wso2.carbon.identity.sso.saml.SAMLSSOConstants;
import org.wso2.carbon.identity.sso.saml.SSOServiceProviderConfigManager;
import org.wso2.carbon.identity.sso.saml.util.SAMLSSOUtil;
import org.wso2.carbon.registry.core.exceptions.RegistryException;
import org.wso2.carbon.registry.core.session.UserRegistry;
import org.xml.sax.SAXException;

import javax.xml.XMLConstants;
Expand Down Expand Up @@ -212,19 +210,11 @@ public static SAMLSSOServiceProviderDO getServiceProviderConfig(String issuer)
SSOServiceProviderConfigManager.getInstance();
SAMLSSOServiceProviderDO ssoIdpConfigs = idPConfigManager.getServiceProvider(issuer);
if (ssoIdpConfigs == null) {
IdentityPersistenceManager persistenceManager =
IdentityPersistenceManager.getPersistanceManager();
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
UserRegistry registry =
SAMLSSOUtil.getRegistryService()
.getConfigSystemRegistry(tenantId);
ssoIdpConfigs = persistenceManager.getServiceProvider(registry, issuer);
ssoIdpConfigs = SAMLQueryServiceComponent.getSAMLSSOServiceProviderManager()
.getServiceProvider(issuer, tenantId);
}
return ssoIdpConfigs;
} catch (RegistryException e) {
log.error("Unable to load registry service", e);
throw new IdentitySAML2QueryException("Unable to load registry service");

} catch (IdentityException e) {
log.error("Unable to load Identity persistence service manager", e);
throw new IdentitySAML2QueryException("Unable to load Identity persistence service manager");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/*
* Copyright (c) 2007, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
* Copyright (c) (2007-2023), WSO2 LLC. (http://www.wso2.com).
*
* WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* 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
*
Expand All @@ -27,7 +27,6 @@
import org.wso2.carbon.core.util.KeyStoreManager;
import org.wso2.carbon.identity.base.IdentityException;
import org.wso2.carbon.identity.core.model.SAMLSSOServiceProviderDO;
import org.wso2.carbon.identity.core.persistence.IdentityPersistenceManager;
import org.wso2.carbon.identity.core.util.IdentityTenantUtil;
import org.wso2.carbon.identity.core.util.IdentityUtil;
import org.wso2.carbon.identity.sp.metadata.saml2.exception.InvalidMetadataException;
Expand All @@ -38,6 +37,7 @@
import org.wso2.carbon.identity.sso.saml.dto.SAMLSSOServiceProviderInfoDTO;
import org.wso2.carbon.identity.sso.saml.exception.IdentitySAML2ClientException;
import org.wso2.carbon.identity.sso.saml.internal.IdentitySAMLSSOServiceComponent;
import org.wso2.carbon.identity.sso.saml.internal.IdentitySAMLSSOServiceComponentHolder;
import org.wso2.carbon.identity.sso.saml.util.SAMLSSOUtil;
import org.wso2.carbon.registry.core.Registry;
import org.wso2.carbon.registry.core.session.UserRegistry;
Expand All @@ -58,9 +58,11 @@ public class SAMLSSOConfigAdmin {

private static final Log log = LogFactory.getLog(SAMLSSOConfigAdmin.class);
private UserRegistry registry;
private final int tenantId;

public SAMLSSOConfigAdmin(Registry userRegistry) {
registry = (UserRegistry) userRegistry;
tenantId = ((UserRegistry) userRegistry).getTenantId();
}

/**
Expand All @@ -73,8 +75,6 @@ public SAMLSSOConfigAdmin(Registry userRegistry) {
public boolean addRelyingPartyServiceProvider(SAMLSSOServiceProviderDTO serviceProviderDTO) throws IdentityException {

SAMLSSOServiceProviderDO serviceProviderDO = createSAMLSSOServiceProviderDO(serviceProviderDTO);
IdentityPersistenceManager persistenceManager = IdentityPersistenceManager
.getPersistanceManager();
try {
String issuer = getIssuerWithQualifier(serviceProviderDO);
SAMLSSOServiceProviderDO samlssoServiceProviderDO = SSOServiceProviderConfigManager.getInstance().
Expand All @@ -86,7 +86,7 @@ public boolean addRelyingPartyServiceProvider(SAMLSSOServiceProviderDTO serviceP
log.error(message);
return false;
}
return persistenceManager.addServiceProvider(registry, serviceProviderDO);
return IdentitySAMLSSOServiceComponentHolder.getInstance().getSAMLSSOServiceProviderManager().addServiceProvider(serviceProviderDO, tenantId);
} catch (IdentityException e) {
String message = "Error obtaining a registry for adding a new service provider";
throw new IdentityException(message, e);
Expand Down Expand Up @@ -130,8 +130,8 @@ private String getIssuerWithQualifier(SAMLSSOServiceProviderDO serviceProviderDO
private SAMLSSOServiceProviderDTO persistSAMLServiceProvider(SAMLSSOServiceProviderDO samlssoServiceProviderDO)
throws IdentityException {

IdentityPersistenceManager persistenceManager = IdentityPersistenceManager.getPersistanceManager();
boolean response = persistenceManager.addServiceProvider(registry, samlssoServiceProviderDO);
boolean response = IdentitySAMLSSOServiceComponentHolder.getInstance().getSAMLSSOServiceProviderManager()
.addServiceProvider(samlssoServiceProviderDO, tenantId);
if (response) {
return createSAMLSSOServiceProviderDTO(samlssoServiceProviderDO);
} else {
Expand All @@ -150,10 +150,10 @@ private SAMLSSOServiceProviderDTO persistSAMLServiceProvider(SAMLSSOServiceProvi
*/
private void saveCertificateToKeyStore(SAMLSSOServiceProviderDO serviceProviderDO) throws Exception {

KeyStoreManager manager = KeyStoreManager.getInstance(registry.getTenantId(), IdentitySAMLSSOServiceComponent
KeyStoreManager manager = KeyStoreManager.getInstance(tenantId, IdentitySAMLSSOServiceComponent
.getServerConfigurationService(), IdentityTenantUtil.getRegistryService());

if (MultitenantConstants.SUPER_TENANT_ID == registry.getTenantId()) {
if (MultitenantConstants.SUPER_TENANT_ID == tenantId) {

KeyStore keyStore = manager.getPrimaryKeyStore();

Expand All @@ -167,7 +167,7 @@ private void saveCertificateToKeyStore(SAMLSSOServiceProviderDO serviceProviderD
}
} else {

String keyStoreName = getKeyStoreName(registry.getTenantId());
String keyStoreName = getKeyStoreName(tenantId);
KeyStore keyStore = manager.getKeyStore(keyStoreName);

// Add new certificate
Expand Down Expand Up @@ -196,7 +196,6 @@ private String getKeyStoreName(int tenantId) {
*/
public SAMLSSOServiceProviderDTO uploadRelyingPartyServiceProvider(String metadata) throws IdentityException {

IdentityPersistenceManager persistenceManager = IdentityPersistenceManager.getPersistanceManager();
Parser parser = new Parser(registry);
SAMLSSOServiceProviderDO samlssoServiceProviderDO = new SAMLSSOServiceProviderDO();

Expand Down Expand Up @@ -411,9 +410,8 @@ private SAMLSSOServiceProviderDTO createSAMLSSOServiceProviderDTO(SAMLSSOService
public SAMLSSOServiceProviderInfoDTO getServiceProviders() throws IdentityException {
SAMLSSOServiceProviderDTO[] serviceProviders = null;
try {
IdentityPersistenceManager persistenceManager = IdentityPersistenceManager
.getPersistanceManager();
SAMLSSOServiceProviderDO[] providersSet = persistenceManager.getServiceProviders(registry);
SAMLSSOServiceProviderDO[] providersSet = IdentitySAMLSSOServiceComponentHolder.getInstance().getSAMLSSOServiceProviderManager()
.getServiceProviders(tenantId);
serviceProviders = new SAMLSSOServiceProviderDTO[providersSet.length];

for (int i = 0; i < providersSet.length; i++) {
Expand Down Expand Up @@ -486,7 +484,7 @@ public SAMLSSOServiceProviderInfoDTO getServiceProviders() throws IdentityExcept
serviceProviderInfoDTO.setServiceProviders(serviceProviders);

//if it is tenant zero
if (registry.getTenantId() == 0) {
if (tenantId == 0) {
serviceProviderInfoDTO.setTenantZero(true);
}
return serviceProviderInfoDTO;
Expand All @@ -501,8 +499,7 @@ public SAMLSSOServiceProviderInfoDTO getServiceProviders() throws IdentityExcept
*/
public boolean removeServiceProvider(String issuer) throws IdentityException {
try {
IdentityPersistenceManager persistenceManager = IdentityPersistenceManager.getPersistanceManager();
return persistenceManager.removeServiceProvider(registry, issuer);
return IdentitySAMLSSOServiceComponentHolder.getInstance().getSAMLSSOServiceProviderManager().removeServiceProvider(issuer, tenantId);
} catch (IdentityException e) {
throw new IdentityException("Error removing a Service Provider with issuer: " + issuer, e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.wso2.carbon.identity.application.mgt.ApplicationManagementService;
import org.wso2.carbon.identity.application.mgt.listener.ApplicationMgtListener;
import org.wso2.carbon.identity.base.IdentityConstants;
import org.wso2.carbon.identity.core.SAMLSSOServiceProviderManager;
import org.wso2.carbon.identity.core.util.IdentityCoreInitializedEvent;
import org.wso2.carbon.identity.core.util.IdentityUtil;
import org.wso2.carbon.identity.event.handler.AbstractEventHandler;
Expand Down Expand Up @@ -458,4 +459,27 @@ protected void unsetApplicationManagementService(ApplicationManagementService ap
log.debug("Unset the ApplicationManagementService");
}
}


@Reference(
name = "saml.sso.service.provider.manager",
service = org.wso2.carbon.identity.core.SAMLSSOServiceProviderManager.class,
cardinality = ReferenceCardinality.MANDATORY,
policy = ReferencePolicy.DYNAMIC,
unbind = "unsetSAMLSSOServiceProviderManager")
protected void setSAMLSSOServiceProviderManager(SAMLSSOServiceProviderManager samlSSOServiceProviderManager) {

IdentitySAMLSSOServiceComponentHolder.getInstance().setSAMLSSOServiceProviderManager(samlSSOServiceProviderManager);
if (log.isDebugEnabled()) {
log.debug("SAMLSSOServiceProviderManager set in to bundle");
}
}

protected void unsetSAMLSSOServiceProviderManager(SAMLSSOServiceProviderManager samlSSOServiceProviderManager) {

IdentitySAMLSSOServiceComponentHolder.getInstance().setSAMLSSOServiceProviderManager(null);
if (log.isDebugEnabled()) {
log.debug("SAMLSSOServiceProviderManager unset in to bundle");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright (c) 2023, 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.sso.saml.internal;

import org.wso2.carbon.identity.core.SAMLSSOServiceProviderManager;

/**
* Identity SAML SSO Service Component Holder.
*/
public class IdentitySAMLSSOServiceComponentHolder {

private SAMLSSOServiceProviderManager samlSSOServiceProviderManager;

private static final IdentitySAMLSSOServiceComponentHolder instance = new IdentitySAMLSSOServiceComponentHolder();

private IdentitySAMLSSOServiceComponentHolder() {

}

public static IdentitySAMLSSOServiceComponentHolder getInstance() {

return instance;
}

/**
* Set SAMLSSOServiceProviderManager.
*
* @param samlSSOServiceProviderManager SAMLSSOServiceProviderManager.
*/
public void setSAMLSSOServiceProviderManager(SAMLSSOServiceProviderManager samlSSOServiceProviderManager) {
this.samlSSOServiceProviderManager = samlSSOServiceProviderManager;
}

/**
* Get SAMLSSOServiceProviderManager.
*
* @return SAMLSSOServiceProviderManager.
*/
public SAMLSSOServiceProviderManager getSAMLSSOServiceProviderManager() {
return samlSSOServiceProviderManager;
}


}
Loading