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

Refactor SAML SSO configs #4727

Merged
merged 3 commits into from
Jun 21, 2023
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 @@ -20,13 +20,11 @@

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.context.RegistryType;
import org.wso2.carbon.context.CarbonContext;
import org.wso2.carbon.identity.application.common.IdentityApplicationManagementException;
import org.wso2.carbon.identity.application.mgt.dao.SAMLApplicationDAO;
import org.wso2.carbon.identity.application.mgt.internal.ApplicationManagementServiceComponentHolder;
import org.wso2.carbon.identity.base.IdentityException;
import org.wso2.carbon.identity.core.persistence.IdentityPersistenceManager;
import org.wso2.carbon.registry.core.Registry;

/**
* Implementation of {@link SAMLApplicationDAO}.
Expand All @@ -38,10 +36,9 @@ public class SAMLApplicationDAOImpl implements SAMLApplicationDAO {
@Override
public void removeServiceProviderConfiguration(String issuer) throws IdentityApplicationManagementException {
try {
IdentityPersistenceManager persistenceManager = IdentityPersistenceManager.getPersistanceManager();
Registry configSystemRegistry = (Registry) PrivilegedCarbonContext.getThreadLocalCarbonContext().
getRegistry(RegistryType.SYSTEM_CONFIGURATION);
persistenceManager.removeServiceProvider(configSystemRegistry, issuer);
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
ApplicationManagementServiceComponentHolder.getInstance().getSAMLSSOServiceProviderManager()
.removeServiceProvider(issuer, tenantId);
} catch (IdentityException e) {
log.error("Erro while deleting the issuer", e);
throw new IdentityApplicationManagementException("Error while deleting SAML issuer " + e.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
import org.wso2.carbon.identity.application.mgt.validator.DefaultApplicationValidator;
import org.wso2.carbon.identity.claim.metadata.mgt.ClaimMetadataManagementService;
import org.wso2.carbon.identity.claim.metadata.mgt.listener.ClaimMetadataMgtListener;
import org.wso2.carbon.identity.core.SAMLSSOServiceProviderManager;
import org.wso2.carbon.identity.organization.management.service.OrganizationManagementInitialize;
import org.wso2.carbon.identity.organization.management.service.OrganizationUserResidentResolverService;
import org.wso2.carbon.idp.mgt.listener.IdentityProviderMgtListener;
Expand Down Expand Up @@ -455,4 +456,28 @@ protected void unsetApplicationPermissionProvider(ApplicationPermissionProvider
log.debug("Removed application permission provider.");
}
}

@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) {

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

protected void unsetSAMLSSOServiceProviderManager(SAMLSSOServiceProviderManager samlSSOServiceProviderManager) {

ApplicationManagementServiceComponentHolder.getInstance()
.setSAMLSSOServiceProviderManager(null);
if (log.isDebugEnabled()) {
log.debug("SAMLSSOServiceProviderManager unset in to bundle");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.wso2.carbon.identity.application.mgt.AbstractInboundAuthenticatorConfig;
import org.wso2.carbon.identity.application.mgt.provider.ApplicationPermissionProvider;
import org.wso2.carbon.identity.claim.metadata.mgt.ClaimMetadataManagementService;
import org.wso2.carbon.identity.core.SAMLSSOServiceProviderManager;
import org.wso2.carbon.identity.organization.management.service.OrganizationManagementInitialize;
import org.wso2.carbon.identity.organization.management.service.OrganizationUserResidentResolverService;
import org.wso2.carbon.registry.api.RegistryService;
Expand All @@ -46,6 +47,8 @@ public class ApplicationManagementServiceComponentHolder {

private RealmService realmService;

private SAMLSSOServiceProviderManager samlSSOServiceProviderManager;

private ConfigurationContextService configContextService;

private boolean databaseBackedCertificateStoringSupportAvailable;
Expand Down Expand Up @@ -131,6 +134,16 @@ public void setRealmService(RealmService realmService) {
this.realmService = realmService;
}

public void setSAMLSSOServiceProviderManager(SAMLSSOServiceProviderManager samlSSOServiceProviderManager) {

this.samlSSOServiceProviderManager = samlSSOServiceProviderManager;
}

public SAMLSSOServiceProviderManager getSAMLSSOServiceProviderManager() {

return samlSSOServiceProviderManager;
}

public ConfigurationContextService getConfigContextService() {

return configContextService;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
/*
* 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.core;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.identity.base.IdentityException;
import org.wso2.carbon.identity.core.dao.SAMLSSOServiceProviderDAO;
import org.wso2.carbon.identity.core.model.SAMLSSOServiceProviderDO;
import org.wso2.carbon.identity.core.util.IdentityTenantUtil;
import org.wso2.carbon.registry.api.RegistryException;
import org.wso2.carbon.registry.core.Registry;

/**
* This class is used for managing SAML SSO providers. Adding, retrieving and removing service
* providers are supported here.
*/
public class SAMLSSOServiceProviderManager {

private static final Log LOG = LogFactory.getLog(SAMLSSOServiceProviderManager.class);

/**
* Build the SAML service provider.
*
* @param tenantId Tenant ID.
* @return SAML service provider.
*/
private SAMLSSOServiceProviderDAO buildSAMLSSOProvider(int tenantId) throws RegistryException {

Registry registry = IdentityTenantUtil.getRegistryService().getConfigSystemRegistry(tenantId);
return new SAMLSSOServiceProviderDAO(registry);
}


/**
* Add a saml service provider.
*
* @param serviceProviderDO Service provider information object.
* @param tenantId Tenant ID.
* @return True if success.
* @throws IdentityException Error when adding the SAML service provider.
*/
public boolean addServiceProvider(SAMLSSOServiceProviderDO serviceProviderDO, int tenantId)
throws IdentityException {

try {
SAMLSSOServiceProviderDAO serviceProviderDAO = buildSAMLSSOProvider(tenantId);
return serviceProviderDAO.addServiceProvider(serviceProviderDO);
} catch (RegistryException e) {
LOG.error("Error while adding service provider", e);
throw new IdentityException("Error while retrieving registry", e);
}
}

/**
* Get all the saml service providers.
*
* @param tenantId Tenant ID.
* @return Array of SAMLSSOServiceProviderDO.
* @throws IdentityException Error when getting the SAML service providers.
*/
public SAMLSSOServiceProviderDO[] getServiceProviders(int tenantId)
throws IdentityException {

try {
SAMLSSOServiceProviderDAO serviceProviderDOA = buildSAMLSSOProvider(tenantId);
return serviceProviderDOA.getServiceProviders();
} catch (RegistryException e) {
LOG.error("Error while getting service providers", e);
throw new IdentityException("Error while retrieving registry", e);
}
}

/**
* Get SAML issuer properties from service provider by saml issuer name.
*
* @param issuer SAML issuer name.
* @param tenantId Tenant ID.
* @return SAMLSSOServiceProviderDO
* @throws IdentityException Error when getting the SAML service provider.
*/
public SAMLSSOServiceProviderDO getServiceProvider(String issuer, int tenantId)
throws IdentityException {

try {
SAMLSSOServiceProviderDAO serviceProviderDAO = buildSAMLSSOProvider(tenantId);
return serviceProviderDAO.getServiceProvider(issuer);
} catch (RegistryException e) {
LOG.error("Error while getting service provider", e);
throw new IdentityException("Error while retrieving SAML issuer " + e.getMessage());
}

}

/**
* Check whether SAML issuer exists by saml issuer name.
*
* @param issuer SAML issuer name.
* @param tenantId Tenant ID.
* @return True if exists
* @throws IdentityException Error when checking the SAML service provider.
*/
public boolean isServiceProviderExists(String issuer, int tenantId)
throws IdentityException {

try {
SAMLSSOServiceProviderDAO serviceProviderDAO = buildSAMLSSOProvider(tenantId);
return serviceProviderDAO.isServiceProviderExists(issuer);
} catch (RegistryException e) {
LOG.error("Error while getting service provider", e);
throw new IdentityException("Error while retrieving SAML issuer " + e.getMessage());
}
}

/**
* Removes the SAML configuration related to the application, idenfied by the issuer.
*
* @param issuer Issuer of the SAML application.
* @param tenantId Tenant ID.
* @throws IdentityException Error when removing the SAML configuration.
*/
public boolean removeServiceProvider(String issuer, int tenantId)
throws IdentityException {

try {
SAMLSSOServiceProviderDAO serviceProviderDAO = buildSAMLSSOProvider(tenantId);
return serviceProviderDAO.removeServiceProvider(issuer);
} catch (RegistryException e) {
LOG.error("Error while removing service provider", e);
throw new IdentityException("Error while deleting SAML issuer " + e.getMessage());
}
}

/**
* Upload the SAML configuration related to the application, using metadata.
*
* @param samlssoServiceProviderDO SAML service provider information object.
* @param tenantId Tenant ID.
* @return SAML service provider information object.
* @throws IdentityException Error when uploading the SAML configuration.
*/
public SAMLSSOServiceProviderDO uploadServiceProvider(SAMLSSOServiceProviderDO samlssoServiceProviderDO, int tenantId)
throws IdentityException {

try {
SAMLSSOServiceProviderDAO serviceProviderDAO = buildSAMLSSOProvider(tenantId);
return serviceProviderDAO.uploadServiceProvider(samlssoServiceProviderDO);
} catch (RegistryException e) {
LOG.error("Error while uploading service provider", e);
throw new IdentityException("Error while uploading SAML issuer " + e.getMessage());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.wso2.carbon.identity.base.IdentityConstants;
import org.wso2.carbon.identity.core.KeyProviderService;
import org.wso2.carbon.identity.core.KeyStoreManagerExtension;
import org.wso2.carbon.identity.core.SAMLSSOServiceProviderManager;
import org.wso2.carbon.identity.core.ServiceURLBuilderFactory;
import org.wso2.carbon.identity.core.migrate.MigrationClient;
import org.wso2.carbon.identity.core.migrate.MigrationClientException;
Expand Down Expand Up @@ -172,6 +173,10 @@ protected void activate(ComponentContext ctxt) throws MigrationClientException {
}
}

// Registering the SAML SSO Service Provider configuration manager.
ctxt.getBundleContext().registerService(SAMLSSOServiceProviderManager.class.getName(),
new SAMLSSOServiceProviderManager(), null);

defaultKeystoreManagerServiceRef = ctxt.getBundleContext().registerService(KeyProviderService.class,
defaultKeyProviderService, null);

Expand Down