Skip to content

Commit

Permalink
Update get key manager usages API
Browse files Browse the repository at this point in the history
- Add REST APIs to fetch key manager API usages and Application usages
- Remove key manager usages API
- Add isUsed parameter when fetching keymanager list
  • Loading branch information
SavinduDimal committed Mar 24, 2024
1 parent 6e79b44 commit 08c3355
Show file tree
Hide file tree
Showing 23 changed files with 1,326 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -276,10 +276,12 @@ void updateMonetizationUsagePublishInfo(MonetizationUsagePublishInfo monetizatio
/**
* This method used to retrieve key manager configurations for tenant
* @param organization organization of the key manager
* @param checkUsages whether to check usages
* @return KeyManagerConfigurationDTO list
* @throws APIManagementException if error occurred
*/
List<KeyManagerConfigurationDTO> getKeyManagerConfigurationsByOrganization(String organization) throws APIManagementException;
List<KeyManagerConfigurationDTO> getKeyManagerConfigurationsByOrganization(String organization,
boolean checkUsages) throws APIManagementException;

/**
* This method returns all the key managers registered in all the tenants
Expand Down Expand Up @@ -515,6 +517,13 @@ Map<String, Object> searchPaginatedApis(String searchQuery, String organization,
*/
List<KeyManagerConfigurationDTO> getGlobalKeyManagerConfigurations() throws APIManagementException;

/**
* This method used to retrieve global key manager configurations with usage check
* @return KeyManagerConfigurationDTO list
* @throws APIManagementException if error occurred
*/
List<KeyManagerConfigurationDTO> getGlobalKeyManagerConfigurations(String organization) throws APIManagementException;

/**
* This method used to retrieve global key manager with Id
* @param id uuid of key manager
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public class KeyManagerConfigurationDTO implements Serializable {
private String externalReferenceId = null;
private String alias = null;
private KeyManagerPermissionConfigurationDTO permissions = new KeyManagerPermissionConfigurationDTO();
private Boolean isUsed = null;

public KeyManagerConfigurationDTO() {
}
Expand Down Expand Up @@ -196,4 +197,14 @@ public void setPermissions (KeyManagerPermissionConfigurationDTO permissions) {
}
this.permissions = permissions;
}

public Boolean getIsUsed() {

return this.isUsed;
}

public void setUsed(Boolean isUsed) {

this.isUsed = isUsed;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright (c) 2024, WSO2 LLC. (http://www.wso2.com) All Rights Reserved.
*
* 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.apimgt.api.model;

import java.util.ArrayList;
import java.util.List;

public class KeyManagerApplicationUsages {

int applicationCount;
List<ApplicationInfoKeyManager> applications = new ArrayList<>();

public int getApplicationCount() {
return applicationCount;
}

public void setApplicationCount(int applicationCount) {
this.applicationCount = applicationCount;
}

public List<ApplicationInfoKeyManager> getApplications() { return applications; }

public void setApplications(List<ApplicationInfoKeyManager> applications) {this.applications = applications; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public LinkedHashMap<String, String> purge(String organization) {
isKeyManagerOrganizationExist = organizationPurgeDAO.keyManagerOrganizationExist(organization);
break;
case APIConstants.OrganizationDeletion.KM_RETRIEVER:
keyManagerList = apiAdmin.getKeyManagerConfigurationsByOrganization(organization);
keyManagerList = apiAdmin.getKeyManagerConfigurationsByOrganization(organization, false);
break;
case APIConstants.OrganizationDeletion.IDP_DATA_REMOVER:
deleteIdpList(organization, keyManagerList);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public class IdpKmPurgeTest {
List<KeyManagerConfigurationDTO> keyManagerList = new ArrayList<>();
keyManagerList.add(kmConfig);

Mockito.doReturn(keyManagerList).when(amAdmin).getKeyManagerConfigurationsByOrganization("testOrg");
Mockito.doReturn(keyManagerList).when(amAdmin).getKeyManagerConfigurationsByOrganization("testOrg", false);
Mockito.doNothing().when(organizationPurgeDAO).deleteKeyManagerConfigurationList(keyManagerList, "testOrg");
Mockito.doReturn(true).when(organizationPurgeDAO).keyManagerOrganizationExist(Mockito.anyString());
Mockito.doNothing().when(amAdmin).deleteIdentityProvider("testOrg", kmConfig);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import org.wso2.carbon.apimgt.api.model.ApplicationInfoKeyManager;
import org.wso2.carbon.apimgt.api.model.ConfigurationDto;
import org.wso2.carbon.apimgt.api.model.Environment;
import org.wso2.carbon.apimgt.api.model.KeyManagerApplicationUsages;
import org.wso2.carbon.apimgt.api.model.KeyManagerConfiguration;
import org.wso2.carbon.apimgt.api.model.KeyManagerConnectorConfiguration;
import org.wso2.carbon.apimgt.api.model.Monetization;
Expand Down Expand Up @@ -119,6 +120,7 @@
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import static org.wso2.carbon.apimgt.impl.utils.APIUtil.getPaginatedApplicationList;

/**
* This class provides the core API admin functionality.
Expand Down Expand Up @@ -373,8 +375,8 @@ public long getTimestamp(String date) {
}

@Override
public List<KeyManagerConfigurationDTO> getKeyManagerConfigurationsByOrganization(String organization)
throws APIManagementException {
public List<KeyManagerConfigurationDTO> getKeyManagerConfigurationsByOrganization(String organization,
boolean checkUsages) throws APIManagementException {

// For Choreo scenario (Choreo organization uses the same super tenant Resident Key Manager
// Hence no need to register the default key manager per organization)
Expand Down Expand Up @@ -426,6 +428,10 @@ public List<KeyManagerConfigurationDTO> getKeyManagerConfigurationsByOrganizatio
}

setIdentityProviderRelatedInformation(keyManagerConfigurationsByTenant, organization);
if (checkUsages) {
setKeyManagerUsageRelatedInformation(keyManagerConfigurationsByTenant, organization);
}

return keyManagerConfigurationsByTenant;
}

Expand Down Expand Up @@ -455,6 +461,29 @@ private void setIdentityProviderRelatedInformation(List<KeyManagerConfigurationD

}

private void setKeyManagerUsageRelatedInformation(
List<KeyManagerConfigurationDTO> keyManagerConfigurationsByOrganization, String organization)
throws APIManagementException {

for (KeyManagerConfigurationDTO keyManagerConfigurationDTO : keyManagerConfigurationsByOrganization) {

KeyManagerApplicationUsages appUsages = getApplicationsOfKeyManager(keyManagerConfigurationDTO.getUuid(), 0,
Integer.MAX_VALUE);
if (appUsages.getApplicationCount() > 0) {
keyManagerConfigurationDTO.setUsed(true);
continue;
}

AdminContentSearchResult apiUsages = getAPIUsagesByKeyManagerNameAndOrganization(organization,
keyManagerConfigurationDTO.getName(), 0, Integer.MAX_VALUE);
if (apiUsages.getApiCount() > 0) {
keyManagerConfigurationDTO.setUsed(true);
continue;
}
keyManagerConfigurationDTO.setUsed(false);
}
}

private void setAliasForTokenExchangeKeyManagers(List<KeyManagerConfigurationDTO> keyManagerConfigurationsByTenant,
String tenantDomain) throws APIManagementException {
for (KeyManagerConfigurationDTO keyManagerConfigurationDTO : keyManagerConfigurationsByTenant) {
Expand Down Expand Up @@ -627,17 +656,29 @@ public KeyManagerConfigurationDTO addKeyManagerConfiguration(
}

public AdminContentSearchResult getAPIUsagesByKeyManagerNameAndOrganization(String org, String keyManagerName,
int start, int offset, int limit)
int offset, int limit)
throws APIManagementException {

APIPersistence apiPersistenceInstance = PersistenceFactory.getAPIPersistenceInstance();
String searchQuery = APIConstants.API_USAGE_BY_KEY_MANAGER_QUERY.replace("$1", keyManagerName);
try {
return apiPersistenceInstance.searchContentForAdmin(org, searchQuery, start, offset, limit);
return apiPersistenceInstance.searchContentForAdmin(org, searchQuery, offset, limit, limit);
} catch (APIPersistenceException e) {
throw new APIManagementException("Error while finding the key manager ", e);
}
}

public KeyManagerApplicationUsages getApplicationsOfKeyManager(String keyManagerId, int offset, int limit)
throws APIManagementException {

KeyManagerApplicationUsages keyManagerApplicationUsages = new KeyManagerApplicationUsages();
ApiMgtDAO apiMgtDAO = ApiMgtDAO.getInstance();
List<ApplicationInfoKeyManager> applications = apiMgtDAO.getAllApplicationsOfKeyManager(keyManagerId);
keyManagerApplicationUsages.setApplicationCount(applications.size());
keyManagerApplicationUsages.setApplications(getPaginatedApplicationList(applications, offset, limit));
return keyManagerApplicationUsages;
}

public List<ApplicationInfoKeyManager> getAllApplicationsOfKeyManager(String keyManagerId)
throws APIManagementException {
ApiMgtDAO apiMgtDAO = ApiMgtDAO.getInstance();
Expand Down Expand Up @@ -927,9 +968,12 @@ public void deleteIdentityProvider(String organization, KeyManagerConfigurationD
public void deleteKeyManagerConfigurationById(String organization, KeyManagerConfigurationDTO kmConfig)
throws APIManagementException {
if (kmConfig != null) {
AdminContentSearchResult usage = getAPIUsagesByKeyManagerNameAndOrganization(organization, kmConfig.getName()
, 0, 0, Integer.MAX_VALUE);
if (usage != null && usage.getApiCount() == 0 && usage.getApplicationCount() == 0) {
AdminContentSearchResult apiUsage = getAPIUsagesByKeyManagerNameAndOrganization(organization,
kmConfig.getName(), 0, Integer.MAX_VALUE);
KeyManagerApplicationUsages appUsages = getApplicationsOfKeyManager(kmConfig.getUuid(), 0,
Integer.MAX_VALUE);
if (apiUsage != null && apiUsage.getApiCount() == 0 && appUsages != null
&& appUsages.getApplicationCount() == 0) {
if (!APIConstants.KeyManager.DEFAULT_KEY_MANAGER.equals(kmConfig.getName())) {
deleteIdentityProvider(organization, kmConfig);
apiMgtDAO.deleteKeyManagerConfigurationById(kmConfig.getUuid(), organization);
Expand Down Expand Up @@ -1704,12 +1748,24 @@ private static IdentityProvider cloneIdentityProvider(IdentityProvider identityP
}

@Override
public List<KeyManagerConfigurationDTO> getGlobalKeyManagerConfigurations() throws APIManagementException {
public List<KeyManagerConfigurationDTO> getGlobalKeyManagerConfigurations()
throws APIManagementException {
List<KeyManagerConfigurationDTO> keyManagerConfigurations = apiMgtDAO.getKeyManagerConfigurationsByOrganization(
APIConstants.GLOBAL_KEY_MANAGER_TENANT_DOMAIN);
for (KeyManagerConfigurationDTO keyManagerConfigurationDTO : keyManagerConfigurations) {
decryptKeyManagerConfigurationValues(keyManagerConfigurationDTO);
}
return keyManagerConfigurations;
}

public List<KeyManagerConfigurationDTO> getGlobalKeyManagerConfigurations(String organization)
throws APIManagementException {
List<KeyManagerConfigurationDTO> keyManagerConfigurations = apiMgtDAO.getKeyManagerConfigurationsByOrganization(
APIConstants.GLOBAL_KEY_MANAGER_TENANT_DOMAIN);
for (KeyManagerConfigurationDTO keyManagerConfigurationDTO : keyManagerConfigurations) {
decryptKeyManagerConfigurationValues(keyManagerConfigurationDTO);
}
setKeyManagerUsageRelatedInformation(keyManagerConfigurations, organization);
return keyManagerConfigurations;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4498,7 +4498,7 @@ public List<KeyManagerConfigurationDTO> getKeyManagerConfigurationsByOrganizatio

APIAdmin apiAdmin = new APIAdminImpl();
List<KeyManagerConfigurationDTO> keyManagerConfigurations =
apiAdmin.getKeyManagerConfigurationsByOrganization(organization);
apiAdmin.getKeyManagerConfigurationsByOrganization(organization, false);
List<KeyManagerConfigurationDTO> permittedKeyManagerConfigurations = new ArrayList<>();
if (keyManagerConfigurations.size() > 0) {
for (KeyManagerConfigurationDTO keyManagerConfiguration : keyManagerConfigurations) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
import org.wso2.carbon.apimgt.api.model.APIStatus;
import org.wso2.carbon.apimgt.api.model.APIStore;
import org.wso2.carbon.apimgt.api.model.Application;
import org.wso2.carbon.apimgt.api.model.ApplicationInfoKeyManager;
import org.wso2.carbon.apimgt.api.model.CORSConfiguration;
import org.wso2.carbon.apimgt.api.model.Documentation;
import org.wso2.carbon.apimgt.api.model.DocumentationType;
Expand Down Expand Up @@ -10560,4 +10561,22 @@ public static void DeleteApi(String endpoint, String authToken,
throw new APIManagementException("Error encountered while connecting to service", e);
}
}

/**
* Retrieves a paginated list of applications from the provided list, based on the specified offset and limit.
*
* @param applications The list of applications to paginate.
* @param offset The starting index of the paginated sublist.
* @param limit The maximum number of applications to include in the paginated sublist.
* @return A paginated sublist of applications, or an empty list if the offset exceeds the size of the input list.
*/
public static List<ApplicationInfoKeyManager> getPaginatedApplicationList(
List<ApplicationInfoKeyManager> applications, int offset, int limit) {

int endIndex = Math.min(offset + limit, applications.size());
if (offset >= applications.size()) {
return Collections.emptyList();
}
return applications.subList(offset, endIndex);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public Response keymanagersGet(String xWSO2Tenant, MessageContext messageContext

APIAdmin apiAdmin = new APIAdminImpl();
List<KeyManagerConfigurationDTO> keyManagerConfigurations =
apiAdmin.getKeyManagerConfigurationsByOrganization(xWSO2Tenant);
apiAdmin.getKeyManagerConfigurationsByOrganization(xWSO2Tenant, false);
List<KeyManagerConfigurationDTO> globalKeyManagerConfigurations = apiAdmin
.getGlobalKeyManagerConfigurations();
keyManagerConfigurations.addAll(globalKeyManagerConfigurations);
Expand Down
Loading

0 comments on commit 08c3355

Please sign in to comment.