Search in sources :

Example 6 with KeyManagerDTO

use of org.wso2.carbon.apimgt.rest.api.admin.v1.dto.KeyManagerDTO in project carbon-apimgt by wso2.

the class AbstractAPIManagerTestCase method init.

@Before
public void init() {
    System.setProperty(CARBON_HOME, "");
    privilegedCarbonContext = Mockito.mock(PrivilegedCarbonContext.class);
    PowerMockito.mockStatic(PrivilegedCarbonContext.class);
    PowerMockito.when(PrivilegedCarbonContext.getThreadLocalCarbonContext()).thenReturn(privilegedCarbonContext);
    PowerMockito.mockStatic(GovernanceUtils.class);
    paginationContext = Mockito.mock(PaginationContext.class);
    PowerMockito.mockStatic(PaginationContext.class);
    PowerMockito.when(PaginationContext.getInstance()).thenReturn(paginationContext);
    apiMgtDAO = Mockito.mock(ApiMgtDAO.class);
    scopesDAO = Mockito.mock(ScopesDAO.class);
    registry = Mockito.mock(Registry.class);
    genericArtifactManager = Mockito.mock(GenericArtifactManager.class);
    registryService = Mockito.mock(RegistryService.class);
    tenantManager = Mockito.mock(TenantManager.class);
    graphQLSchemaDefinition = Mockito.mock(GraphQLSchemaDefinition.class);
    keyManager = Mockito.mock(KeyManager.class);
    apiPersistenceInstance = Mockito.mock(APIPersistence.class);
    PowerMockito.mockStatic(KeyManagerHolder.class);
    KeyManagerDto keyManagerDto = new KeyManagerDto();
    keyManagerDto.setName("default");
    keyManagerDto.setKeyManager(keyManager);
    keyManagerDto.setIssuer("https://localhost");
    Map<String, KeyManagerDto> tenantKeyManagerDtoMap = new HashMap<>();
    tenantKeyManagerDtoMap.put("default", keyManagerDto);
    PowerMockito.when(KeyManagerHolder.getTenantKeyManagers("carbon.super")).thenReturn(tenantKeyManagerDtoMap);
}
Also used : GenericArtifactManager(org.wso2.carbon.governance.api.generic.GenericArtifactManager) HashMap(java.util.HashMap) PaginationContext(org.wso2.carbon.registry.core.pagination.PaginationContext) ScopesDAO(org.wso2.carbon.apimgt.impl.dao.ScopesDAO) PrivilegedCarbonContext(org.wso2.carbon.context.PrivilegedCarbonContext) ApiMgtDAO(org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO) KeyManagerDto(org.wso2.carbon.apimgt.impl.dto.KeyManagerDto) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) Registry(org.wso2.carbon.registry.core.Registry) GraphQLSchemaDefinition(org.wso2.carbon.apimgt.impl.definitions.GraphQLSchemaDefinition) RegistryService(org.wso2.carbon.registry.core.service.RegistryService) TenantManager(org.wso2.carbon.user.core.tenant.TenantManager) KeyManager(org.wso2.carbon.apimgt.api.model.KeyManager) APIPersistence(org.wso2.carbon.apimgt.persistence.APIPersistence) Before(org.junit.Before)

Example 7 with KeyManagerDTO

use of org.wso2.carbon.apimgt.rest.api.admin.v1.dto.KeyManagerDTO in project carbon-apimgt by wso2.

the class APIProviderImpl method deleteSharedScope.

/**
 * Delete shared scope.
 *
 * @param scopeName    Shared scope name
 * @param tenantDomain tenant domain
 * @throws APIManagementException If failed to delete the scope
 */
@Override
public void deleteSharedScope(String scopeName, String tenantDomain) throws APIManagementException {
    if (log.isDebugEnabled()) {
        log.debug("Deleting shared scope " + scopeName);
    }
    Map<String, KeyManagerDto> tenantKeyManagers = KeyManagerHolder.getTenantKeyManagers(tenantDomain);
    for (Map.Entry<String, KeyManagerDto> keyManagerEntry : tenantKeyManagers.entrySet()) {
        KeyManager keyManager = keyManagerEntry.getValue().getKeyManager();
        if (keyManager != null) {
            try {
                keyManager.deleteScope(scopeName);
            } catch (APIManagementException e) {
                log.error("Error while Deleting Shared Scope " + scopeName + " from Key Manager " + keyManagerEntry.getKey(), e);
            }
        }
    }
    apiMgtDAO.deleteSharedScope(scopeName, tenantDomain);
    deleteScope(scopeName, APIUtil.getTenantIdFromTenantDomain(tenantDomain));
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) KeyManagerDto(org.wso2.carbon.apimgt.impl.dto.KeyManagerDto) Map(java.util.Map) TreeMap(java.util.TreeMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) KeyManager(org.wso2.carbon.apimgt.api.model.KeyManager)

Example 8 with KeyManagerDTO

use of org.wso2.carbon.apimgt.rest.api.admin.v1.dto.KeyManagerDTO in project carbon-apimgt by wso2.

the class APIProviderImpl method updateAPIResources.

/**
 * Update resources of the API including local scopes and resource to scope attachments.
 *
 * @param api      API
 * @param tenantId Tenant Id
 * @throws APIManagementException If fails to update local scopes of the API.
 */
private void updateAPIResources(API api, int tenantId) throws APIManagementException {
    String tenantDomain = APIUtil.getTenantDomainFromTenantId(tenantId);
    APIIdentifier apiIdentifier = api.getId();
    // Get the new URI templates for the API
    Set<URITemplate> uriTemplates = api.getUriTemplates();
    // Get the existing local scope keys attached for the API
    Set<String> oldLocalScopeKeys = apiMgtDAO.getAllLocalScopeKeysForAPI(api.getUuid(), tenantId);
    // Get the existing URI templates for the API
    Set<URITemplate> oldURITemplates = apiMgtDAO.getURITemplatesOfAPI(api.getUuid());
    // Get the new local scope keys from URI templates
    Set<Scope> newLocalScopes = getScopesToRegisterFromURITemplates(api.getId().getApiName(), api.getOrganization(), uriTemplates);
    Set<String> newLocalScopeKeys = newLocalScopes.stream().map(Scope::getKey).collect(Collectors.toSet());
    // Get the existing versioned local scope keys attached for the API
    Set<String> oldVersionedLocalScopeKeys = apiMgtDAO.getVersionedLocalScopeKeysForAPI(api.getUuid(), tenantId);
    // Get the existing versioned local scope keys which needs to be removed (not updated) from the current updating
    // API and remove them from the oldLocalScopeKeys set before sending to KM, so that they will not be removed
    // from KM and can be still used by other versioned APIs.
    Iterator oldLocalScopesItr = oldLocalScopeKeys.iterator();
    while (oldLocalScopesItr.hasNext()) {
        String oldLocalScopeKey = (String) oldLocalScopesItr.next();
        // if the scope is used in versioned APIs and it is not in new local scope key set
        if (oldVersionedLocalScopeKeys.contains(oldLocalScopeKey) && !newLocalScopeKeys.contains(oldLocalScopeKey)) {
            // remove from old local scope key set which will be send to KM
            oldLocalScopesItr.remove();
        }
    }
    apiMgtDAO.updateURITemplates(api, tenantId);
    if (log.isDebugEnabled()) {
        log.debug("Successfully updated the URI templates of API: " + apiIdentifier + " in the database");
    }
    // Update the resource scopes of the API in KM.
    // Need to remove the old local scopes and register new local scopes and, update the resource scope mappings
    // using the updated URI templates of the API.
    deleteScopes(oldLocalScopeKeys, tenantId);
    addScopes(newLocalScopes, tenantId);
    Map<String, KeyManagerDto> tenantKeyManagers = KeyManagerHolder.getTenantKeyManagers(tenantDomain);
    for (Map.Entry<String, KeyManagerDto> keyManagerDtoEntry : tenantKeyManagers.entrySet()) {
        KeyManager keyManager = keyManagerDtoEntry.getValue().getKeyManager();
        if (keyManager != null) {
            try {
                keyManager.updateResourceScopes(api, oldLocalScopeKeys, newLocalScopes, oldURITemplates, uriTemplates);
                if (log.isDebugEnabled()) {
                    log.debug("Successfully updated the resource scopes of API: " + apiIdentifier + " in Key Manager " + keyManagerDtoEntry.getKey() + " .");
                }
            } catch (APIManagementException e) {
                log.error("Error while updating resource to scope attachment in Key Manager " + keyManagerDtoEntry.getKey(), e);
            }
        }
    }
}
Also used : URITemplate(org.wso2.carbon.apimgt.api.model.URITemplate) KeyManagerDto(org.wso2.carbon.apimgt.impl.dto.KeyManagerDto) Scope(org.wso2.carbon.apimgt.api.model.Scope) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Iterator(java.util.Iterator) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) Map(java.util.Map) TreeMap(java.util.TreeMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) KeyManager(org.wso2.carbon.apimgt.api.model.KeyManager)

Example 9 with KeyManagerDTO

use of org.wso2.carbon.apimgt.rest.api.admin.v1.dto.KeyManagerDTO in project carbon-apimgt by wso2.

the class APIProviderImpl method addURITemplates.

/**
 * Add URI templates for the API.
 *
 * @param apiId    API Id
 * @param api      API
 * @param tenantId Tenant Id
 * @throws APIManagementException if fails to add URI templates for the API
 */
private void addURITemplates(int apiId, API api, int tenantId) throws APIManagementException {
    String tenantDomain = APIUtil.getTenantDomainFromTenantId(tenantId);
    apiMgtDAO.addURITemplates(apiId, api, tenantId);
    Map<String, KeyManagerDto> tenantKeyManagers = KeyManagerHolder.getTenantKeyManagers(tenantDomain);
    for (Map.Entry<String, KeyManagerDto> keyManagerDtoEntry : tenantKeyManagers.entrySet()) {
        KeyManager keyManager = keyManagerDtoEntry.getValue().getKeyManager();
        if (keyManager != null) {
            try {
                keyManager.attachResourceScopes(api, api.getUriTemplates());
            } catch (APIManagementException e) {
                log.error("Error while Attaching Resource to scope in Key Manager " + keyManagerDtoEntry.getKey(), e);
            }
        }
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) KeyManagerDto(org.wso2.carbon.apimgt.impl.dto.KeyManagerDto) Map(java.util.Map) TreeMap(java.util.TreeMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) KeyManager(org.wso2.carbon.apimgt.api.model.KeyManager)

Example 10 with KeyManagerDTO

use of org.wso2.carbon.apimgt.rest.api.admin.v1.dto.KeyManagerDTO in project carbon-apimgt by wso2.

the class APIProviderImpl method addSharedScope.

/**
 * Add Shared Scope by registering it in the KM and adding the scope as a Shared Scope in AM DB.
 *
 * @param scope        Shared Scope
 * @param tenantDomain Tenant domain
 * @return UUId of the added Shared Scope object
 * @throws APIManagementException if failed to add a scope
 */
@Override
public String addSharedScope(Scope scope, String tenantDomain) throws APIManagementException {
    Set<Scope> scopeSet = new HashSet<>();
    scopeSet.add(scope);
    int tenantId = APIUtil.getTenantIdFromTenantDomain(tenantDomain);
    addScopes(scopeSet, tenantId);
    Map<String, KeyManagerDto> tenantKeyManagers = KeyManagerHolder.getTenantKeyManagers(tenantDomain);
    for (Map.Entry<String, KeyManagerDto> keyManagerDtoEntry : tenantKeyManagers.entrySet()) {
        KeyManager keyManager = keyManagerDtoEntry.getValue().getKeyManager();
        if (keyManager != null) {
            try {
                keyManager.registerScope(scope);
            } catch (APIManagementException e) {
                log.error("Error occurred while registering Scope in Key Manager " + keyManagerDtoEntry.getKey(), e);
            }
        }
        if (log.isDebugEnabled()) {
            log.debug("Adding shared scope mapping: " + scope.getKey() + " to  Key Manager : " + keyManagerDtoEntry.getKey());
        }
    }
    return ApiMgtDAO.getInstance().addSharedScope(scope, tenantDomain);
}
Also used : Scope(org.wso2.carbon.apimgt.api.model.Scope) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) KeyManagerDto(org.wso2.carbon.apimgt.impl.dto.KeyManagerDto) Map(java.util.Map) TreeMap(java.util.TreeMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) KeyManager(org.wso2.carbon.apimgt.api.model.KeyManager) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Aggregations

KeyManagerDto (org.wso2.carbon.apimgt.impl.dto.KeyManagerDto)16 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)15 HashMap (java.util.HashMap)13 Map (java.util.Map)13 KeyManager (org.wso2.carbon.apimgt.api.model.KeyManager)13 TreeMap (java.util.TreeMap)10 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)9 KeyManagerConfigurationDTO (org.wso2.carbon.apimgt.api.dto.KeyManagerConfigurationDTO)5 Gson (com.google.gson.Gson)4 APIAdmin (org.wso2.carbon.apimgt.api.APIAdmin)4 APIAdminImpl (org.wso2.carbon.apimgt.impl.APIAdminImpl)4 JsonObject (com.google.gson.JsonObject)3 ArrayList (java.util.ArrayList)3 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)3 Scope (org.wso2.carbon.apimgt.api.model.Scope)3 URITemplate (org.wso2.carbon.apimgt.api.model.URITemplate)3 KeyManagerDTO (org.wso2.carbon.apimgt.rest.api.admin.v1.dto.KeyManagerDTO)3 JsonArray (com.google.gson.JsonArray)2 HashSet (java.util.HashSet)2 List (java.util.List)2