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);
}
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));
}
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);
}
}
}
}
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);
}
}
}
}
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);
}
Aggregations