Skip to content

Commit

Permalink
Introduce DB based Admin Banner DAO implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
UdeshAthukorala committed Jan 27, 2025
1 parent bb7a6ec commit 7cb02f7
Show file tree
Hide file tree
Showing 12 changed files with 212 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@
<groupId>org.wso2.carbon.identity.framework</groupId>
<artifactId>org.wso2.carbon.identity.application.authentication.framework</artifactId>
</dependency>
<dependency>
<groupId>org.wso2.carbon</groupId>
<artifactId>org.wso2.carbon.admin.advisory.mgt</artifactId>
</dependency>
</dependencies>

<build>
Expand All @@ -113,7 +117,12 @@
org.osgi.framework; version="${osgi.framework.imp.pkg.version.range}",
org.osgi.service.component; version="${osgi.service.component.imp.pkg.version.range}",
org.apache.commons.logging; version="${import.package.version.commons.logging}",
org.wso2.carbon.identity.core.cache; version="${carbon.identity.package.import.version.range}"
org.apache.commons.io; version="${commons.io.wso2.osgi.version.range}",
org.apache.commons.lang; version="${commons-lang.wso2.osgi.version.range}",
org.wso2.carbon.identity.core.cache; version="${carbon.identity.package.import.version.range}",
org.wso2.carbon.admin.advisory.mgt.dao; version="${carbon.kernel.package.import.version.range}",
org.wso2.carbon.admin.advisory.mgt.dto; version="${carbon.kernel.package.import.version.range}",
org.wso2.carbon.admin.advisory.mgt.exception; version="${carbon.kernel.package.import.version.range}",
</Import-Package>
<Export-Package>
!org.wso2.carbon.identity.cors.mgt.core.internal,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
/*
* Copyright (c) 2025, 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.cors.mgt.core.advisorybanner;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.admin.advisory.mgt.dao.AdminAdvisoryBannerDAO;
import org.wso2.carbon.admin.advisory.mgt.dto.AdminAdvisoryBannerDTO;
import org.wso2.carbon.admin.advisory.mgt.exception.AdminAdvisoryMgtException;
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.configuration.mgt.core.model.ResourceFile;
import org.wso2.carbon.identity.cors.mgt.core.internal.CORSManagementServiceHolder;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
* This class is used to manage storage of the Admin Advisory Banner configurations in the configuration-mgt store.
*/
public class DBBasedAdminBannerDAO implements AdminAdvisoryBannerDAO {

public static final String ADVISORY_BANNER_RESOURCE_TYPE = "ADMIN_ADVISORY_BANNER";
public static final String ADVISORY_BANNER_RESOURCE_NAME = "ADMIN_ADVISORY_BANNER_RESOURCE";
public static final String ENABLE_BANNER = "enableBanner";
public static final String BANNER_CONTENT = "bannerContent";
public static final String RESOURCE_NOT_EXISTS_ERROR_CODE = "CONFIGM_00017";

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

@Override
public void saveAdminAdvisoryConfig(AdminAdvisoryBannerDTO adminAdvisoryBannerDTO, String tenantDomain)
throws AdminAdvisoryMgtException {

Resource resource = buildResourceFromAdvisoryBannerDTO(adminAdvisoryBannerDTO);
try {
if (isAdminAdvisoryResourceExists(ADVISORY_BANNER_RESOURCE_TYPE, ADVISORY_BANNER_RESOURCE_NAME)) {
getConfigurationManager().replaceResource(ADVISORY_BANNER_RESOURCE_TYPE, resource);
} else {
getConfigurationManager().addResource(ADVISORY_BANNER_RESOURCE_TYPE, resource);
}
} catch (ConfigurationManagementException e) {
throw new AdminAdvisoryMgtException("Error occurred while saving advisory banner configuration.", e);
}
if (LOG.isDebugEnabled()) {
LOG.debug("Admin advisory banner configuration saved successfully in configuration-store for tenant: "
+ tenantDomain);
}
}

@Override
public Optional<AdminAdvisoryBannerDTO> loadAdminAdvisoryConfig(String tenantDomain) throws AdminAdvisoryMgtException {

try {
Resource resource = getConfigurationManager().getResource(ADVISORY_BANNER_RESOURCE_TYPE,
ADVISORY_BANNER_RESOURCE_NAME);
if (resource == null) {
return Optional.empty();
}

List<ResourceFile> resourceFiles = getConfigurationManager().getFiles(ADVISORY_BANNER_RESOURCE_TYPE,
ADVISORY_BANNER_RESOURCE_NAME);

if (resourceFiles.isEmpty() || StringUtils.isBlank(resourceFiles.get(0).getId())) {
return Optional.empty();
}
InputStream inputStream = getConfigurationManager().getFileById
(ADVISORY_BANNER_RESOURCE_TYPE, ADVISORY_BANNER_RESOURCE_NAME, resourceFiles.get(0).getId());
AdminAdvisoryBannerDTO adminAdvisoryBannerDTO = buildAdvisoryBannerDTOFromResource(resource, inputStream);

if (LOG.isDebugEnabled()) {
LOG.debug("Admin advisory banner configuration loaded successfully from configuration-store for" +
" tenant: " + tenantDomain);
}
return Optional.of(adminAdvisoryBannerDTO);
} catch (ConfigurationManagementException e) {
if (RESOURCE_NOT_EXISTS_ERROR_CODE.equals(e.getErrorCode())) {
if (LOG.isDebugEnabled()) {
LOG.debug("Can not find a admin advisory banner configurations for tenant: " + tenantDomain, e);
}
return Optional.empty();
}
throw new AdminAdvisoryMgtException("Error occurred while loading advisory banner configuration.", e);
}
}

/**
* Check whether the admin advisory resource exists for the current tenant.
*
* @param resourceType Admin Advisory Banner resource type.
* @param resourceName Admin Advisory Banner resource name.
* @return Return true if the resource already exists. If not return false.
*/
private boolean isAdminAdvisoryResourceExists(String resourceType, String resourceName)
throws AdminAdvisoryMgtException {

Resource resource;
try {
resource = getConfigurationManager().getResource(resourceType, resourceName);
} catch (ConfigurationManagementException e) {
if (RESOURCE_NOT_EXISTS_ERROR_CODE.equals(e.getErrorCode())) {
return false;
}
throw new AdminAdvisoryMgtException("Error occurred while checking the existence of the admin advisory " +
"resource.", e);
}
return resource != null;
}

private AdminAdvisoryBannerDTO buildAdvisoryBannerDTOFromResource(Resource resource, InputStream inputStream)
throws AdminAdvisoryMgtException {

AdminAdvisoryBannerDTO adminAdvisoryBannerDTO = new AdminAdvisoryBannerDTO();
adminAdvisoryBannerDTO.setEnableBanner(Boolean.parseBoolean(resource.getAttributes().get(0).getValue()));
String bannerContent;

try {
bannerContent = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
} catch (IOException e) {
throw new AdminAdvisoryMgtException("Error occurred while converting banner content to string.", e);
}
adminAdvisoryBannerDTO.setBannerContent(bannerContent);
return adminAdvisoryBannerDTO;
}

private Resource buildResourceFromAdvisoryBannerDTO(AdminAdvisoryBannerDTO adminAdvisoryBannerDTO)
throws AdminAdvisoryMgtException {

Resource resource = new Resource(ADVISORY_BANNER_RESOURCE_NAME, ADVISORY_BANNER_RESOURCE_TYPE);
List<Attribute> attributes = new ArrayList<>();
attributes.add(new Attribute(ENABLE_BANNER, String.valueOf(adminAdvisoryBannerDTO.getEnableBanner())));
resource.setAttributes(attributes);

ResourceFile file = new ResourceFile();
file.setName(BANNER_CONTENT);
try (InputStream inputStream = new ByteArrayInputStream(adminAdvisoryBannerDTO.getBannerContent().
getBytes(StandardCharsets.UTF_8))) {
file.setInputStream(inputStream);
} catch (IOException e) {
throw new AdminAdvisoryMgtException("Error occurred while converting banner content to input stream.", e);
}
List<ResourceFile> resourceFiles = new ArrayList<>();
resourceFiles.add(file);
resource.setFiles(resourceFiles);
return resource;
}

private ConfigurationManager getConfigurationManager() {

return CORSManagementServiceHolder.getInstance().getConfigurationManager();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@
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.admin.advisory.mgt.dao.AdminAdvisoryBannerDAO;
import org.wso2.carbon.identity.configuration.mgt.core.ConfigurationManager;
import org.wso2.carbon.identity.cors.mgt.core.CORSManagementService;
import org.wso2.carbon.identity.cors.mgt.core.advisorybanner.DBBasedAdminBannerDAO;
import org.wso2.carbon.identity.cors.mgt.core.internal.impl.CORSManagementServiceImpl;

/**
Expand All @@ -54,6 +56,8 @@ protected void activate(ComponentContext context) {
try {
BundleContext bundleContext = context.getBundleContext();
bundleContext.registerService(CORSManagementService.class, new CORSManagementServiceImpl(), null);
// Register the Database based Admin Advisory Banner DAO.
bundleContext.registerService(AdminAdvisoryBannerDAO.class, new DBBasedAdminBannerDAO(), null);

if (log.isDebugEnabled()) {
log.debug("CORSManagementServiceComponent is activated.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1524,7 +1524,8 @@ INSERT INTO IDN_CONFIG_TYPE (ID, NAME, DESCRIPTION) VALUES
('1fc809a0-dc0d-4cb2-82f3-58934d389236', 'CUSTOM_TEXT', 'A resource type to keep the tenant custom text preferences.'),
('c385a42a-5697-4604-b49a-62456621e926', 'DCR_CONFIGURATION', 'A resource type to keep the DCR configurations.'),
('3e5b1f91-72d8-4fbc-94d1-1b9a4f8c3b07', 'IMPERSONATION_CONFIGURATION', 'A resource type to keep the tenant impersonation preferences.'),
('a731af34-f96a-4069-812d-30dc3b713a28', 'response-max-limit-configurations', 'A resource type to max limit configurations for API response.')
('a731af34-f96a-4069-812d-30dc3b713a28', 'response-max-limit-configurations', 'A resource type to max limit configurations for API response.'),
('32360745-9c3e-4391-b563-66f17f0eb93a', 'ADMIN_ADVISORY_BANNER', 'A resource type to store admin advisory banner configurations.')
/

CREATE TABLE IDN_CONFIG_RESOURCE (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -985,7 +985,8 @@ INSERT INTO IDN_CONFIG_TYPE (ID, NAME, DESCRIPTION) VALUES
('1fc809a0-dc0d-4cb2-82f3-58934d389236', 'CUSTOM_TEXT', 'A resource type to keep the tenant custom text preferences.'),
('c385a42a-5697-4604-b49a-62456621e926', 'DCR_CONFIGURATION', 'A resource type to keep the DCR configurations.'),
('3e5b1f91-72d8-4fbc-94d1-1b9a4f8c3b07', 'IMPERSONATION_CONFIGURATION', 'A resource type to keep the tenant impersonation preferences.'),
('a731af34-f96a-4069-812d-30dc3b713a28', 'response-max-limit-configurations', 'A resource type to max limit configurations for API response.');
('a731af34-f96a-4069-812d-30dc3b713a28', 'response-max-limit-configurations', 'A resource type to max limit configurations for API response.'),
('32360745-9c3e-4391-b563-66f17f0eb93a', 'ADMIN_ADVISORY_BANNER', 'A resource type to store admin advisory banner configurations.');

CREATE TABLE IF NOT EXISTS IDN_CONFIG_RESOURCE (
ID VARCHAR(255) NOT NULL,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1093,7 +1093,8 @@ INSERT INTO IDN_CONFIG_TYPE (ID, NAME, DESCRIPTION) VALUES
('1fc809a0-dc0d-4cb2-82f3-58934d389236', 'CUSTOM_TEXT', 'A resource type to keep the tenant custom text preferences.'),
('c385a42a-5697-4604-b49a-62456621e926', 'DCR_CONFIGURATION', 'A resource type to keep the DCR configurations.'),
('3e5b1f91-72d8-4fbc-94d1-1b9a4f8c3b07', 'IMPERSONATION_CONFIGURATION', 'A resource type to keep the tenant impersonation preferences.'),
('a731af34-f96a-4069-812d-30dc3b713a28', 'response-max-limit-configurations', 'A resource type to max limit configurations for API response.');
('a731af34-f96a-4069-812d-30dc3b713a28', 'response-max-limit-configurations', 'A resource type to max limit configurations for API response.'),
('32360745-9c3e-4391-b563-66f17f0eb93a', 'ADMIN_ADVISORY_BANNER', 'A resource type to store admin advisory banner configurations.');

IF NOT EXISTS (SELECT * FROM SYS.OBJECTS WHERE OBJECT_ID = OBJECT_ID(N'[DBO].[IDN_CONFIG_RESOURCE]')
AND TYPE IN (N'U'))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1148,7 +1148,8 @@ INSERT INTO IDN_CONFIG_TYPE (ID, NAME, DESCRIPTION) VALUES
('1fc809a0-dc0d-4cb2-82f3-58934d389236', 'CUSTOM_TEXT', 'A resource type to keep the tenant custom text preferences.'),
('c385a42a-5697-4604-b49a-62456621e926', 'DCR_CONFIGURATION', 'A resource type to keep the DCR configurations.'),
('3e5b1f91-72d8-4fbc-94d1-1b9a4f8c3b07', 'IMPERSONATION_CONFIGURATION', 'A resource type to keep the tenant impersonation preferences.'),
('a731af34-f96a-4069-812d-30dc3b713a28', 'response-max-limit-configurations', 'A resource type to max limit configurations for API response.');
('a731af34-f96a-4069-812d-30dc3b713a28', 'response-max-limit-configurations', 'A resource type to max limit configurations for API response.'),
('32360745-9c3e-4391-b563-66f17f0eb93a', 'ADMIN_ADVISORY_BANNER', 'A resource type to store admin advisory banner configurations.');

CREATE TABLE IF NOT EXISTS IDN_CONFIG_RESOURCE (
ID VARCHAR(255) NOT NULL,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1014,7 +1014,8 @@ INSERT INTO IDN_CONFIG_TYPE (ID, NAME, DESCRIPTION) VALUES
('1fc809a0-dc0d-4cb2-82f3-58934d389236', 'CUSTOM_TEXT', 'A resource type to keep the tenant custom text preferences.'),
('c385a42a-5697-4604-b49a-62456621e926', 'DCR_CONFIGURATION', 'A resource type to keep the DCR configurations.'),
('3e5b1f91-72d8-4fbc-94d1-1b9a4f8c3b07', 'IMPERSONATION_CONFIGURATION', 'A resource type to keep the tenant impersonation preferences.'),
('a731af34-f96a-4069-812d-30dc3b713a28', 'response-max-limit-configurations', 'A resource type to max limit configurations for API response.');
('a731af34-f96a-4069-812d-30dc3b713a28', 'response-max-limit-configurations', 'A resource type to max limit configurations for API response.'),
('32360745-9c3e-4391-b563-66f17f0eb93a', 'ADMIN_ADVISORY_BANNER', 'A resource type to store admin advisory banner configurations.');

CREATE TABLE IF NOT EXISTS IDN_CONFIG_RESOURCE (
ID VARCHAR(255) NOT NULL,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1698,6 +1698,8 @@ INTO IDN_CONFIG_TYPE (ID, NAME, DESCRIPTION) VALUES
('3e5b1f91-72d8-4fbc-94d1-1b9a4f8c3b07', 'IMPERSONATION_CONFIGURATION', 'A resource type to keep the tenant impersonation preferences.')
INTO IDN_CONFIG_TYPE (ID, NAME, DESCRIPTION) VALUES
('a731af34-f96a-4069-812d-30dc3b713a28', 'response-max-limit-configurations', 'A resource type to max limit configurations for API response.')
INTO IDN_CONFIG_TYPE (ID, NAME, DESCRIPTION) VALUES
('32360745-9c3e-4391-b563-66f17f0eb93a', 'ADMIN_ADVISORY_BANNER', 'A resource type to store admin advisory banner configurations.')
SELECT 1 FROM dual
/

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1547,6 +1547,8 @@ INTO IDN_CONFIG_TYPE (ID, NAME, DESCRIPTION) VALUES
('3e5b1f91-72d8-4fbc-94d1-1b9a4f8c3b07', 'IMPERSONATION_CONFIGURATION', 'A resource type to keep the tenant impersonation preferences.')
INTO IDN_CONFIG_TYPE (ID, NAME, DESCRIPTION) VALUES
('a731af34-f96a-4069-812d-30dc3b713a28', 'response-max-limit-configurations', 'A resource type to max limit configurations for API response.')
INTO IDN_CONFIG_TYPE (ID, NAME, DESCRIPTION) VALUES
('32360745-9c3e-4391-b563-66f17f0eb93a', 'ADMIN_ADVISORY_BANNER', 'A resource type to store admin advisory banner configurations.')
SELECT 1 FROM dual
/
CREATE TABLE IDN_CONFIG_RESOURCE (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1210,7 +1210,8 @@ INSERT INTO IDN_CONFIG_TYPE (ID, NAME, DESCRIPTION) VALUES
('1fc809a0-dc0d-4cb2-82f3-58934d389236', 'CUSTOM_TEXT', 'A resource type to keep the tenant custom text preferences.'),
('c385a42a-5697-4604-b49a-62456621e926', 'DCR_CONFIGURATION', 'A resource type to keep the DCR configurations.'),
('3e5b1f91-72d8-4fbc-94d1-1b9a4f8c3b07', 'IMPERSONATION_CONFIGURATION', 'A resource type to keep the tenant impersonation preferences.'),
('a731af34-f96a-4069-812d-30dc3b713a28', 'response-max-limit-configurations', 'A resource type to max limit configurations for API response.');
('a731af34-f96a-4069-812d-30dc3b713a28', 'response-max-limit-configurations', 'A resource type to max limit configurations for API response.'),
('32360745-9c3e-4391-b563-66f17f0eb93a', 'ADMIN_ADVISORY_BANNER', 'A resource type to store admin advisory banner configurations.');

DROP TABLE IF EXISTS IDN_CONFIG_RESOURCE;
CREATE TABLE IDN_CONFIG_RESOURCE (
Expand Down
7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,11 @@
<artifactId>org.wso2.securevault</artifactId>
<version>${org.wso2.securevault.version}</version>
</dependency>
<dependency>
<groupId>org.wso2.carbon</groupId>
<artifactId>org.wso2.carbon.admin.advisory.mgt</artifactId>
<version>${carbon.kernel.version}</version>
</dependency>
<dependency>
<groupId>org.wso2.carbon</groupId>
<artifactId>org.wso2.carbon.securevault</artifactId>
Expand Down Expand Up @@ -1853,7 +1858,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<!-- Carbon kernel version -->
<carbon.kernel.version>4.10.33</carbon.kernel.version>
<carbon.kernel.version>4.10.34</carbon.kernel.version>
<carbon.kernel.feature.version>4.7.0</carbon.kernel.feature.version>
<carbon.kernel.package.import.version.range>[4.5.0, 5.0.0)</carbon.kernel.package.import.version.range>
<carbon.kernel.registry.imp.pkg.version>[1.0.1, 2.0.0)</carbon.kernel.registry.imp.pkg.version>
Expand Down

0 comments on commit 7cb02f7

Please sign in to comment.