Skip to content

Commit

Permalink
Add organization configuration service
Browse files Browse the repository at this point in the history
  • Loading branch information
dewniMW committed Sep 20, 2023
1 parent 7e57a92 commit 2c9b001
Show file tree
Hide file tree
Showing 14 changed files with 852 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ 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.
-->

<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">

<parent>
<groupId>org.wso2.carbon.identity.organization.management</groupId>
<artifactId>identity-organization-management</artifactId>
<version>1.3.70-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>

<modelVersion>4.0.0</modelVersion>
<artifactId>org.wso2.carbon.identity.organization.config.service</artifactId>
<name>WSO2 - Organization Configuration Service</name>
<packaging>bundle</packaging>

<dependencies>
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.scr.ds-annotations</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.wso2.carbon.identity.framework</groupId>
<artifactId>org.wso2.carbon.identity.configuration.mgt.core</artifactId>
</dependency>
<dependency>
<groupId>org.wso2.carbon.identity.organization.management.core</groupId>
<artifactId>org.wso2.carbon.identity.organization.management.service</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<instructions>
<Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
<Bundle-Name>${project.artifactId}</Bundle-Name>
<Bundle-Description>Organization Config Service Bundle</Bundle-Description>
<Private-Package>org.wso2.carbon.identity.organization.config.service.internal
</Private-Package>
<Export-Package>
!org.wso2.carbon.identity.organization.config.service.internal,
org.wso2.carbon.identity.organization.config.service.*;version="${project.version}"
</Export-Package>
<Import-Package>
org.apache.commons.lang;version="${org.apache.commons.lang.imp.pkg.version.range}",
org.apache.commons.logging;version="${org.apache.commons.logging.imp.pkg.version.range}",
org.osgi.framework;version="${osgi.framework.imp.pkg.version.range}",
org.osgi.service.component;version="${osgi.service.component.imp.pkg.version.range}",
org.wso2.carbon.identity.configuration.mgt.core;version="${carbon.identity.package.import.version.range}",
org.wso2.carbon.identity.configuration.mgt.core.constant;version="${carbon.identity.package.import.version.range}",
org.wso2.carbon.identity.configuration.mgt.core.exception;version="${carbon.identity.package.import.version.range}",
org.wso2.carbon.identity.configuration.mgt.core.model;version="${carbon.identity.package.import.version.range}",
org.wso2.carbon.identity.organization.config.service.constant;version="${org.wso2.identity.organization.mgt.imp.pkg.version.range}",
org.wso2.carbon.identity.organization.config.service.exception;version="${org.wso2.identity.organization.mgt.imp.pkg.version.range}",
org.wso2.carbon.identity.organization.config.service.model;version="${org.wso2.identity.organization.mgt.imp.pkg.version.range}",
org.wso2.carbon.identity.organization.config.service.util;version="${org.wso2.identity.organization.mgt.imp.pkg.version.range}",
org.wso2.carbon.identity.organization.management.service;version="${org.wso2.identity.organization.mgt.core.imp.pkg.version.range}",
org.wso2.carbon.identity.organization.management.service.exception;version="${org.wso2.identity.organization.mgt.core.imp.pkg.version.range}",
org.wso2.carbon.identity.organization.management.service.util;version="${org.wso2.identity.organization.mgt.core.imp.pkg.version.range}",
</Import-Package>
</instructions>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* 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.organization.config.service;

import org.wso2.carbon.identity.organization.config.service.exception.OrganizationConfigException;
import org.wso2.carbon.identity.organization.config.service.model.DiscoveryConfig;

/**
* Interface for organization configuration management.
*/
public interface OrganizationConfigManager {

/**
* Add the discovery configuration of the primary organization.
*
* @param discoveryConfig The discovery configuration.
* @throws OrganizationConfigException The exception thrown when an error occurs while adding the discovery
* configuration.
*/
void addDiscoveryConfiguration(DiscoveryConfig discoveryConfig) throws OrganizationConfigException;

/**
* Fetch the discovery configuration of the primary organization.
*
* @return the discovery configuration.
* @throws OrganizationConfigException The exception thrown when an error occurs while fetching the discovery
* configuration.
*/
DiscoveryConfig getDiscoveryConfiguration() throws OrganizationConfigException;

/**
* Delete the discovery configuration of the primary organization.
*
* @throws OrganizationConfigException The exception thrown when an error occurs while deleting the discovery
* configuration.
*/
void deleteDiscoveryConfiguration() throws OrganizationConfigException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
/*
* 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.organization.config.service;

import org.wso2.carbon.identity.configuration.mgt.core.ConfigurationManager;
import org.wso2.carbon.identity.configuration.mgt.core.exception.ConfigurationManagementException;
import org.wso2.carbon.identity.configuration.mgt.core.model.Attribute;
import org.wso2.carbon.identity.configuration.mgt.core.model.Resource;
import org.wso2.carbon.identity.organization.config.service.exception.OrganizationConfigException;
import org.wso2.carbon.identity.organization.config.service.internal.OrganizationConfigServiceHolder;
import org.wso2.carbon.identity.organization.config.service.model.ConfigProperty;
import org.wso2.carbon.identity.organization.config.service.model.DiscoveryConfig;
import org.wso2.carbon.identity.organization.management.service.OrganizationManager;
import org.wso2.carbon.identity.organization.management.service.exception.OrganizationManagementServerException;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

import static org.wso2.carbon.identity.configuration.mgt.core.constant.ConfigurationConstants.ErrorMessages.ERROR_CODE_RESOURCE_DOES_NOT_EXISTS;
import static org.wso2.carbon.identity.organization.config.service.constant.OrganizationConfigConstants.ErrorMessages.ERROR_CODE_DISCOVERY_CONFIG_CONFLICT;
import static org.wso2.carbon.identity.organization.config.service.constant.OrganizationConfigConstants.ErrorMessages.ERROR_CODE_DISCOVERY_CONFIG_NOT_EXIST;
import static org.wso2.carbon.identity.organization.config.service.constant.OrganizationConfigConstants.ErrorMessages.ERROR_CODE_DISCOVERY_CONFIG_UPDATE_NOT_ALLOWED;
import static org.wso2.carbon.identity.organization.config.service.constant.OrganizationConfigConstants.ErrorMessages.ERROR_CODE_ERROR_ADDING_DISCOVERY_CONFIG;
import static org.wso2.carbon.identity.organization.config.service.constant.OrganizationConfigConstants.ErrorMessages.ERROR_CODE_ERROR_DELETING_DISCOVERY_CONFIG;
import static org.wso2.carbon.identity.organization.config.service.constant.OrganizationConfigConstants.ErrorMessages.ERROR_CODE_ERROR_RETRIEVING_DISCOVERY_CONFIG;
import static org.wso2.carbon.identity.organization.config.service.constant.OrganizationConfigConstants.RESOURCE_NAME;
import static org.wso2.carbon.identity.organization.config.service.constant.OrganizationConfigConstants.RESOURCE_TYPE_NAME;
import static org.wso2.carbon.identity.organization.config.service.util.Utils.handleClientException;
import static org.wso2.carbon.identity.organization.config.service.util.Utils.handleServerException;
import static org.wso2.carbon.identity.organization.management.service.util.Utils.getOrganizationId;

/**
* Implementation of Organization Configuration Manager Interface.
*/
public class OrganizationConfigManagerImpl implements OrganizationConfigManager {

@Override
public void addDiscoveryConfiguration(DiscoveryConfig discoveryConfig) throws OrganizationConfigException {

try {
if (!isDiscoveryConfigChangeAllowed()) {
throw handleClientException(ERROR_CODE_DISCOVERY_CONFIG_UPDATE_NOT_ALLOWED);
}
Optional<Resource> resourceOptional = getDiscoveryResource();
if (resourceOptional.isPresent()) {
throw handleClientException(ERROR_CODE_DISCOVERY_CONFIG_CONFLICT, getOrganizationId());
}
Resource resource = buildResourceFromValidationConfig(discoveryConfig);
getConfigurationManager().addResource(RESOURCE_TYPE_NAME, resource);
} catch (ConfigurationManagementException | OrganizationManagementServerException e) {
throw handleServerException(ERROR_CODE_ERROR_ADDING_DISCOVERY_CONFIG, e, getOrganizationId());
}
}

@Override
public DiscoveryConfig getDiscoveryConfiguration() throws OrganizationConfigException {

Optional<Resource> resourceOptional = getDiscoveryResource();
if (!resourceOptional.isPresent()) {
throw handleClientException(ERROR_CODE_DISCOVERY_CONFIG_NOT_EXIST, getOrganizationId());
}
List<ConfigProperty> configProperties = resourceOptional.map(resource -> resource.getAttributes().stream()
.map(attribute -> new ConfigProperty(attribute.getKey(), attribute.getValue()))
.collect(Collectors.toList())).orElse(Collections.emptyList());
return new DiscoveryConfig(configProperties);
}

@Override
public void deleteDiscoveryConfiguration() throws OrganizationConfigException {

try {
if (!isDiscoveryConfigChangeAllowed()) {
throw handleClientException(ERROR_CODE_DISCOVERY_CONFIG_UPDATE_NOT_ALLOWED);
}
Optional<Resource> resourceOptional = getDiscoveryResource();
if (resourceOptional.isPresent()) {
getConfigurationManager().deleteResource(RESOURCE_TYPE_NAME, RESOURCE_NAME);
}
} catch (ConfigurationManagementException | OrganizationManagementServerException e) {
throw handleServerException(ERROR_CODE_ERROR_DELETING_DISCOVERY_CONFIG, e, getOrganizationId());
}
}

private Optional<Resource> getDiscoveryResource() throws OrganizationConfigException {

try {
return Optional.ofNullable(getConfigurationManager().getResource(RESOURCE_TYPE_NAME, RESOURCE_NAME));
} catch (ConfigurationManagementException e) {
if (!ERROR_CODE_RESOURCE_DOES_NOT_EXISTS.getCode().equals(e.getErrorCode())) {
throw handleServerException(ERROR_CODE_ERROR_RETRIEVING_DISCOVERY_CONFIG, e, getOrganizationId());
}
}
return Optional.empty();
}

private boolean isDiscoveryConfigChangeAllowed() throws OrganizationManagementServerException {

return getOrganizationManager().isPrimaryOrganization(getOrganizationId());
}

private Resource buildResourceFromValidationConfig(DiscoveryConfig discoveryConfig) {

Map<String, String> configAttributes = new HashMap<>();
for (ConfigProperty property : discoveryConfig.getConfigProperties()) {
configAttributes.put(property.getKey(), property.getValue());
}
List<Attribute> resourceAttributes = configAttributes.entrySet().stream()
.filter(attribute -> attribute.getValue() != null && !"null".equals(attribute.getValue()))
.map(attribute -> new Attribute(attribute.getKey(), attribute.getValue()))
.collect(Collectors.toList());
Resource resource = new Resource();
resource.setResourceName(RESOURCE_NAME);
resource.setAttributes(resourceAttributes);
return resource;
}

private ConfigurationManager getConfigurationManager() {

return OrganizationConfigServiceHolder.getInstance().getConfigurationManager();
}

private OrganizationManager getOrganizationManager() {

return OrganizationConfigServiceHolder.getInstance().getOrganizationManager();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* 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.organization.config.service.constant;

/**
* Contains constants related to organization configuration management.
*/
public class OrganizationConfigConstants {

public static final String RESOURCE_TYPE_NAME = "ORGANIZATION_CONFIGURATION";
public static final String RESOURCE_NAME = "OrganizationDiscovery";
private static final String ORGANIZATION_CONFIGURATION_ERROR_CODE_PREFIX = "OCM-";

/**
* Enum for error messages related to organization management.
*/
public enum ErrorMessages {

// Client errors.
ERROR_CODE_DISCOVERY_CONFIG_UPDATE_NOT_ALLOWED("60001", "Can't modify the organization discovery " +
"configuration.", "Only the primary organization has the authority to modify the organization " +
"discovery configuration."),
ERROR_CODE_DISCOVERY_CONFIG_NOT_EXIST("60002", "No organization discovery configuration found.",
"There is no organization discovery configuration for organization with ID: %s."),
ERROR_CODE_DISCOVERY_CONFIG_CONFLICT("60003", "The organization discovery configuration already exists.",
"The organization discovery configuration is already for available for the organization with id: %s."),

// Server errors.
ERROR_CODE_ERROR_ADDING_DISCOVERY_CONFIG("65001", "Unable to add the organization discovery " +
"configuration.", "Server encountered an error while adding the organization discovery " +
"configuration for organization with ID: %s."),
ERROR_CODE_ERROR_RETRIEVING_DISCOVERY_CONFIG("65002", "Unable to retrieve the organization " +
"discovery configuration.", "Server encountered an error while retrieving the organization discovery " +
"configuration for organization with ID: %s."),
ERROR_CODE_ERROR_DELETING_DISCOVERY_CONFIG("65003", "Unable to delete the organization discovery " +
"configuration.", "Server encountered an error while deleting the organization discovery " +
"configuration for the organization with id: %s");

private final String code;
private final String message;
private final String description;

ErrorMessages(String code, String message, String description) {

this.code = code;
this.message = message;
this.description = description;
}

public String getCode() {

return ORGANIZATION_CONFIGURATION_ERROR_CODE_PREFIX + code;
}

public String getMessage() {

return message;
}

public String getDescription() {

return description;
}
}
}
Loading

0 comments on commit 2c9b001

Please sign in to comment.