use of org.wso2.carbon.apimgt.impl.dto.KeyManagerDto in project carbon-apimgt by wso2.
the class KeyManagerMappingUtil method toKeyManagerConfigurationDTO.
public static KeyManagerConfigurationDTO toKeyManagerConfigurationDTO(String tenantDomain, KeyManagerDTO keyManagerDTO) {
KeyManagerConfigurationDTO keyManagerConfigurationDTO = new KeyManagerConfigurationDTO();
Map<String, String> endpoints = new HashMap<>();
keyManagerConfigurationDTO.setName(keyManagerDTO.getName());
keyManagerConfigurationDTO.setDisplayName(keyManagerDTO.getDisplayName());
keyManagerConfigurationDTO.setDescription(keyManagerDTO.getDescription());
keyManagerConfigurationDTO.setEnabled(keyManagerDTO.isEnabled());
keyManagerConfigurationDTO.setType(keyManagerDTO.getType());
keyManagerConfigurationDTO.setOrganization(tenantDomain);
keyManagerConfigurationDTO.setTokenType(keyManagerDTO.getTokenType().toString());
keyManagerConfigurationDTO.setAlias(keyManagerDTO.getAlias());
Map<String, Object> additionalProperties = new HashMap();
if (keyManagerDTO.getAdditionalProperties() != null && keyManagerDTO.getAdditionalProperties() instanceof Map) {
additionalProperties.putAll((Map) keyManagerDTO.getAdditionalProperties());
}
if (StringUtils.isNotEmpty(keyManagerDTO.getClientRegistrationEndpoint())) {
additionalProperties.put(APIConstants.KeyManager.CLIENT_REGISTRATION_ENDPOINT, keyManagerDTO.getClientRegistrationEndpoint());
endpoints.put(APIConstants.KeyManager.CLIENT_REGISTRATION_ENDPOINT, keyManagerDTO.getClientRegistrationEndpoint());
}
if (StringUtils.isNotEmpty(keyManagerDTO.getIntrospectionEndpoint())) {
additionalProperties.put(APIConstants.KeyManager.INTROSPECTION_ENDPOINT, keyManagerDTO.getIntrospectionEndpoint());
endpoints.put(APIConstants.KeyManager.INTROSPECTION_ENDPOINT, keyManagerDTO.getIntrospectionEndpoint());
}
if (StringUtils.isNotEmpty(keyManagerDTO.getTokenEndpoint())) {
additionalProperties.put(APIConstants.KeyManager.TOKEN_ENDPOINT, keyManagerDTO.getTokenEndpoint());
endpoints.put(APIConstants.KeyManager.TOKEN_ENDPOINT, keyManagerDTO.getTokenEndpoint());
}
if (StringUtils.isNotEmpty(keyManagerDTO.getDisplayTokenEndpoint())) {
additionalProperties.put(APIConstants.KeyManager.DISPLAY_TOKEN_ENDPOINT, keyManagerDTO.getDisplayTokenEndpoint());
endpoints.put(APIConstants.KeyManager.DISPLAY_TOKEN_ENDPOINT, keyManagerDTO.getDisplayTokenEndpoint());
}
if (StringUtils.isNotEmpty(keyManagerDTO.getRevokeEndpoint())) {
additionalProperties.put(APIConstants.KeyManager.REVOKE_ENDPOINT, keyManagerDTO.getRevokeEndpoint());
endpoints.put(APIConstants.KeyManager.REVOKE_ENDPOINT, keyManagerDTO.getRevokeEndpoint());
}
if (StringUtils.isNotEmpty(keyManagerDTO.getDisplayRevokeEndpoint())) {
additionalProperties.put(APIConstants.KeyManager.DISPLAY_REVOKE_ENDPOINT, keyManagerDTO.getDisplayRevokeEndpoint());
endpoints.put(APIConstants.KeyManager.DISPLAY_REVOKE_ENDPOINT, keyManagerDTO.getDisplayRevokeEndpoint());
}
if (StringUtils.isNotEmpty(keyManagerDTO.getScopeManagementEndpoint())) {
additionalProperties.put(APIConstants.KeyManager.SCOPE_MANAGEMENT_ENDPOINT, keyManagerDTO.getScopeManagementEndpoint());
endpoints.put(APIConstants.KeyManager.SCOPE_MANAGEMENT_ENDPOINT, keyManagerDTO.getScopeManagementEndpoint());
}
if (keyManagerDTO.getAvailableGrantTypes() != null) {
additionalProperties.put(APIConstants.KeyManager.AVAILABLE_GRANT_TYPE, keyManagerDTO.getAvailableGrantTypes());
}
if (StringUtils.isNotEmpty(keyManagerDTO.getIssuer())) {
additionalProperties.put(APIConstants.KeyManager.ISSUER, keyManagerDTO.getIssuer());
}
if (keyManagerDTO.getCertificates() != null) {
additionalProperties.put(APIConstants.KeyManager.CERTIFICATE_VALUE, keyManagerDTO.getCertificates().getValue());
if (KeyManagerCertificatesDTO.TypeEnum.JWKS.equals(keyManagerDTO.getCertificates().getType())) {
additionalProperties.put(APIConstants.KeyManager.CERTIFICATE_TYPE, APIConstants.KeyManager.CERTIFICATE_TYPE_JWKS_ENDPOINT);
} else if (KeyManagerCertificatesDTO.TypeEnum.PEM.equals(keyManagerDTO.getCertificates().getType())) {
additionalProperties.put(APIConstants.KeyManager.CERTIFICATE_TYPE, APIConstants.KeyManager.CERTIFICATE_TYPE_PEM_FILE);
}
}
if (StringUtils.isNotEmpty(keyManagerDTO.getUserInfoEndpoint())) {
additionalProperties.put(APIConstants.KeyManager.USERINFO_ENDPOINT, keyManagerDTO.getUserInfoEndpoint());
endpoints.put(APIConstants.KeyManager.USERINFO_ENDPOINT, keyManagerDTO.getUserInfoEndpoint());
}
if (StringUtils.isNotEmpty(keyManagerDTO.getAuthorizeEndpoint())) {
additionalProperties.put(APIConstants.KeyManager.AUTHORIZE_ENDPOINT, keyManagerDTO.getAuthorizeEndpoint());
endpoints.put(APIConstants.KeyManager.AUTHORIZE_ENDPOINT, keyManagerDTO.getAuthorizeEndpoint());
}
if (StringUtils.isNotEmpty(keyManagerDTO.getWellKnownEndpoint())) {
additionalProperties.put(APIConstants.KeyManager.WELL_KNOWN_ENDPOINT, keyManagerDTO.getWellKnownEndpoint());
}
if (keyManagerDTO.getEndpoints() != null) {
for (KeyManagerEndpointDTO endpoint : keyManagerDTO.getEndpoints()) {
endpoints.put(endpoint.getName(), endpoint.getValue());
}
}
keyManagerConfigurationDTO.setEndpoints(endpoints);
additionalProperties.put(APIConstants.KeyManager.ENABLE_OAUTH_APP_CREATION, keyManagerDTO.isEnableOAuthAppCreation());
additionalProperties.put(APIConstants.KeyManager.ENABLE_MAP_OAUTH_CONSUMER_APPS, keyManagerDTO.isEnableMapOAuthConsumerApps());
additionalProperties.put(APIConstants.KeyManager.ENABLE_TOKEN_GENERATION, keyManagerDTO.isEnableTokenGeneration());
additionalProperties.put(APIConstants.KeyManager.ENABLE_TOKEN_HASH, keyManagerDTO.isEnableTokenHashing());
additionalProperties.put(APIConstants.KeyManager.ENABLE_TOKEN_ENCRYPTION, keyManagerDTO.isEnableTokenEncryption());
additionalProperties.put(APIConstants.KeyManager.SELF_VALIDATE_JWT, keyManagerDTO.isEnableSelfValidationJWT());
List<TokenValidationDTO> tokenValidationDTOList = keyManagerDTO.getTokenValidation();
if (tokenValidationDTOList != null && !tokenValidationDTOList.isEmpty()) {
additionalProperties.put(APIConstants.KeyManager.TOKEN_FORMAT_STRING, new Gson().toJson(tokenValidationDTOList));
}
List<ClaimMappingEntryDTO> claimMapping = keyManagerDTO.getClaimMapping();
if (claimMapping != null) {
additionalProperties.put(APIConstants.KeyManager.CLAIM_MAPPING, new Gson().toJsonTree(claimMapping));
}
if (StringUtils.isNotEmpty(keyManagerDTO.getConsumerKeyClaim())) {
additionalProperties.put(APIConstants.KeyManager.CONSUMER_KEY_CLAIM, keyManagerDTO.getConsumerKeyClaim());
}
if (StringUtils.isNotEmpty(keyManagerDTO.getScopesClaim())) {
additionalProperties.put(APIConstants.KeyManager.SCOPES_CLAIM, keyManagerDTO.getScopesClaim());
}
keyManagerConfigurationDTO.setAdditionalProperties(additionalProperties);
return keyManagerConfigurationDTO;
}
use of org.wso2.carbon.apimgt.impl.dto.KeyManagerDto in project carbon-apimgt by wso2.
the class APIProviderImpl method updateSharedScope.
/**
* Update a shared scope.
*
* @param sharedScope Shared Scope
* @param tenantDomain tenant domain
* @throws APIManagementException If failed to update
*/
@Override
public void updateSharedScope(Scope sharedScope, String tenantDomain) throws APIManagementException {
int tenantId = APIUtil.getTenantIdFromTenantDomain(tenantDomain);
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.updateScope(sharedScope);
} catch (APIManagementException e) {
log.error("Error while Updating Shared Scope " + sharedScope.getKey() + " from Key Manager " + keyManagerEntry.getKey(), e);
}
}
}
updateScope(sharedScope, tenantId);
}
use of org.wso2.carbon.apimgt.impl.dto.KeyManagerDto in project carbon-apimgt by wso2.
the class APIProviderImpl method addLocalScopes.
/**
* Add local scopes for the API if the scopes does not exist as shared scopes. The local scopes to add will be
* take from the URI templates.
*
* @param apiName API name
* @param uriTemplates URI Templates
* @param organization Organization
* @throws APIManagementException if fails to add local scopes for the API
*/
private void addLocalScopes(String apiName, Set<URITemplate> uriTemplates, String organization) throws APIManagementException {
int tenantId = APIUtil.getInternalOrganizationId(organization);
String tenantDomain = APIUtil.getTenantDomainFromTenantId(tenantId);
Map<String, KeyManagerDto> tenantKeyManagers = KeyManagerHolder.getTenantKeyManagers(tenantDomain);
// Get the local scopes set to register for the API from URI templates
Set<Scope> scopesToRegister = getScopesToRegisterFromURITemplates(apiName, organization, uriTemplates);
// Register scopes
for (Scope scope : scopesToRegister) {
for (Map.Entry<String, KeyManagerDto> keyManagerDtoEntry : tenantKeyManagers.entrySet()) {
KeyManager keyManager = keyManagerDtoEntry.getValue().getKeyManager();
if (keyManager != null) {
String scopeKey = scope.getKey();
try {
// version.
if (!keyManager.isScopeExists(scopeKey)) {
// register scope in KM
keyManager.registerScope(scope);
} else {
if (log.isDebugEnabled()) {
log.debug("Scope: " + scopeKey + " already registered in KM. Skipping registering scope.");
}
}
} catch (APIManagementException e) {
log.error("Error while registering Scope " + scopeKey + "in Key Manager " + keyManagerDtoEntry.getKey(), e);
}
}
}
}
addScopes(scopesToRegister, tenantId);
}
use of org.wso2.carbon.apimgt.impl.dto.KeyManagerDto in project carbon-apimgt by wso2.
the class APIProviderImpl method deleteAPIFromDB.
/**
* Deletes API from the database and delete local scopes and resource scope attachments from KM.
*
* @param api API to delete
* @throws APIManagementException if fails to delete the API
*/
private void deleteAPIFromDB(API api) throws APIManagementException {
APIIdentifier apiIdentifier = api.getId();
int tenantId = APIUtil.getTenantId(APIUtil.replaceEmailDomainBack(apiIdentifier.getProviderName()));
String tenantDomain = APIUtil.getTenantDomainFromTenantId(tenantId);
// Get local scopes for the given API which are not already assigned for different versions of the same API
Set<String> localScopeKeysToDelete = apiMgtDAO.getUnversionedLocalScopeKeysForAPI(api.getUuid(), tenantId);
// Get the URI Templates for the given API to detach the resources scopes from
Set<URITemplate> uriTemplates = apiMgtDAO.getURITemplatesOfAPI(api.getUuid());
// Detach all the resource scopes from the API resources in KM
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.detachResourceScopes(api, uriTemplates);
if (log.isDebugEnabled()) {
log.debug("Resource scopes are successfully detached for the API : " + apiIdentifier + " from Key Manager :" + keyManagerDtoEntry.getKey() + ".");
}
// remove the local scopes from the KM
for (String localScope : localScopeKeysToDelete) {
keyManager.deleteScope(localScope);
}
if (log.isDebugEnabled()) {
log.debug("Local scopes are successfully deleted for the API : " + apiIdentifier + " from Key Manager : " + keyManagerDtoEntry.getKey() + ".");
}
} catch (APIManagementException e) {
log.error("Error while Detach and Delete Scope from Key Manager " + keyManagerDtoEntry.getKey(), e);
}
}
}
deleteScopes(localScopeKeysToDelete, tenantId);
apiMgtDAO.deleteAPI(api.getUuid());
if (log.isDebugEnabled()) {
log.debug("API : " + apiIdentifier + " is successfully deleted from the database and Key Manager.");
}
}
use of org.wso2.carbon.apimgt.impl.dto.KeyManagerDto in project carbon-apimgt by wso2.
the class APIProviderImpl method deleteAPI.
public void deleteAPI(String apiUuid, String organization) throws APIManagementException {
boolean isError = false;
int apiId = -1;
API api = null;
// get api object by uuid
try {
api = getAPIbyUUID(apiUuid, organization);
} catch (APIManagementException e) {
log.error("Error while getting API by uuid for deleting API " + apiUuid + " on organization " + organization);
log.debug("Following steps will be skipped while deleting API " + apiUuid + "on organization " + organization + " due to api being null. " + "deleting Resource Registration from key managers, deleting on external API stores, " + "event publishing to gateways, logging audit message, extracting API details for " + "the recommendation system. ");
isError = true;
}
// get api id from db
try {
apiId = apiMgtDAO.getAPIID(apiUuid);
} catch (APIManagementException e) {
log.error("Error while getting API ID from DB for deleting API " + apiUuid + " on organization " + organization, e);
log.debug("Following steps will be skipped while deleting the API " + apiUuid + " on organization " + organization + "due to api id being null. cleanup workflow tasks of the API, " + "delete event publishing to gateways");
isError = true;
}
// DB delete operations
if (!isError && api != null) {
try {
deleteAPIRevisions(apiUuid, organization);
deleteAPIFromDB(api);
if (log.isDebugEnabled()) {
String logMessage = "API Name: " + api.getId().getApiName() + ", API Version " + api.getId().getVersion() + " successfully removed from the database.";
log.debug(logMessage);
}
} catch (APIManagementException e) {
log.error("Error while executing API delete operations on DB for API " + apiUuid + " on organization " + organization, e);
isError = true;
}
}
// Deleting Resource Registration from key managers
if (api != null && api.getId() != null && api.getId().toString() != null) {
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.deleteRegisteredResourceByAPIId(api.getId().toString());
log.debug("API " + apiUuid + " on organization " + organization + " has successfully removed from the Key Manager " + keyManagerDtoEntry.getKey());
} catch (APIManagementException e) {
log.error("Error while deleting Resource Registration for API " + apiUuid + " on organization " + organization + " in Key Manager " + keyManagerDtoEntry.getKey(), e);
}
}
}
}
try {
GatewayArtifactsMgtDAO.getInstance().deleteGatewayArtifacts(apiUuid);
log.debug("API " + apiUuid + " on organization " + organization + " has successfully removed from the gateway artifacts.");
} catch (APIManagementException e) {
log.error("Error while executing API delete operation on gateway artifacts for API " + apiUuid, e);
isError = true;
}
try {
apiPersistenceInstance.deleteAPI(new Organization(organization), apiUuid);
log.debug("API " + apiUuid + " on organization " + organization + " has successfully removed from the persistence instance.");
} catch (APIPersistenceException e) {
log.error("Error while executing API delete operation on persistence instance for API " + apiUuid + " on organization " + organization, e);
isError = true;
}
// Deleting on external API stores
if (api != null) {
// gatewayType check is required when API Management is deployed on
// other servers to avoid synapse
// Check if there are already published external APIStores.If yes,removing APIs from them.
Set<APIStore> apiStoreSet;
try {
apiStoreSet = getPublishedExternalAPIStores(apiUuid);
WSO2APIPublisher wso2APIPublisher = new WSO2APIPublisher();
if (apiStoreSet != null && !apiStoreSet.isEmpty()) {
for (APIStore store : apiStoreSet) {
wso2APIPublisher.deleteFromStore(api.getId(), APIUtil.getExternalAPIStore(store.getName(), tenantId));
}
}
} catch (APIManagementException e) {
log.error("Error while executing API delete operation on external API stores for API " + apiUuid + " on organization " + organization, e);
isError = true;
}
}
if (apiId != -1) {
try {
cleanUpPendingAPIStateChangeTask(apiId, false);
} catch (WorkflowException | APIManagementException e) {
log.error("Error while executing API delete operation on cleanup workflow tasks for API " + apiUuid + " on organization " + organization, e);
isError = true;
}
}
// Delete event publishing to gateways
if (api != null && apiId != -1) {
APIEvent apiEvent = new APIEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.API_DELETE.name(), tenantId, tenantDomain, api.getId().getApiName(), apiId, api.getUuid(), api.getId().getVersion(), api.getType(), api.getContext(), APIUtil.replaceEmailDomainBack(api.getId().getProviderName()), api.getStatus());
APIUtil.sendNotification(apiEvent, APIConstants.NotifierType.API.name());
} else {
log.debug("Event has not published to gateways due to API id has failed to retrieve from DB for API " + apiUuid + " on organization " + organization);
}
// Logging audit message for API delete
if (api != null) {
JSONObject apiLogObject = new JSONObject();
apiLogObject.put(APIConstants.AuditLogConstants.NAME, api.getId().getApiName());
apiLogObject.put(APIConstants.AuditLogConstants.VERSION, api.getId().getVersion());
apiLogObject.put(APIConstants.AuditLogConstants.PROVIDER, api.getId().getProviderName());
APIUtil.logAuditMessage(APIConstants.AuditLogConstants.API, apiLogObject.toString(), APIConstants.AuditLogConstants.DELETED, this.username);
}
// Extracting API details for the recommendation system
if (api != null && recommendationEnvironment != null) {
RecommenderEventPublisher extractor = new RecommenderDetailsExtractor(api, tenantDomain, APIConstants.DELETE_API);
Thread recommendationThread = new Thread(extractor);
recommendationThread.start();
}
// if one of the above has failed throw an error
if (isError) {
throw new APIManagementException("Error while deleting the API " + apiUuid + " on organization " + organization);
}
}
Aggregations