use of org.wso2.carbon.apimgt.impl.certificatemgt.CertificateManager in project carbon-apimgt by wso2.
the class APIProviderImplTest method init.
@Before
public void init() throws Exception {
System.setProperty("carbon.home", APIProviderImplTest.class.getResource("/").getFile());
PowerMockito.mockStatic(ApiMgtDAO.class);
PowerMockito.mockStatic(GatewayArtifactsMgtDAO.class);
PowerMockito.mockStatic(ScopesDAO.class);
PowerMockito.mockStatic(PrivilegedCarbonContext.class);
PowerMockito.mockStatic(RegistryUtils.class);
PowerMockito.mockStatic(GovernanceUtils.class);
PowerMockito.mockStatic(WorkflowExecutorFactory.class);
PowerMockito.mockStatic(LifecycleBeanPopulator.class);
PowerMockito.mockStatic(KeyManagerHolder.class);
PowerMockito.mockStatic(Caching.class);
PowerMockito.mockStatic(PaginationContext.class);
PowerMockito.mockStatic(APIUtil.class);
PowerMockito.mockStatic(APIGatewayManager.class);
PowerMockito.mockStatic(CertificateManagerImpl.class);
PowerMockito.mockStatic(RegistryPersistenceUtil.class);
apimgtDAO = Mockito.mock(ApiMgtDAO.class);
gatewayArtifactsMgtDAO = Mockito.mock(GatewayArtifactsMgtDAO.class);
scopesDAO = Mockito.mock(ScopesDAO.class);
keyManager = Mockito.mock(KeyManager.class);
apiPersistenceInstance = Mockito.mock(APIPersistence.class);
certificateManager = Mockito.mock(CertificateManagerImpl.class);
Mockito.when(keyManager.getResourceByApiId(Mockito.anyString())).thenReturn(null);
Mockito.when(keyManager.registerNewResource(Mockito.any(API.class), Mockito.any(Map.class))).thenReturn(true);
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);
PowerMockito.when(CertificateManagerImpl.getInstance()).thenReturn(certificateManager);
PowerMockito.when(APIUtil.isAPIManagementEnabled()).thenReturn(false);
PowerMockito.when(APIUtil.replaceEmailDomainBack(Mockito.anyString())).thenReturn("admin");
Mockito.when(APIUtil.replaceEmailDomain(Mockito.anyString())).thenReturn("admin");
PrivilegedCarbonContext prcontext = Mockito.mock(PrivilegedCarbonContext.class);
PowerMockito.when(PrivilegedCarbonContext.getThreadLocalCarbonContext()).thenReturn(prcontext);
PowerMockito.doNothing().when(prcontext).setUsername(Mockito.anyString());
PowerMockito.doNothing().when(prcontext).setTenantDomain(Mockito.anyString(), Mockito.anyBoolean());
artifactManager = Mockito.mock(GenericArtifactManager.class);
registry = Mockito.mock(Registry.class);
PowerMockito.when(APIUtil.getArtifactManager(any(Registry.class), Mockito.anyString())).thenReturn(artifactManager);
artifact = Mockito.mock(GenericArtifact.class);
gatewayManager = Mockito.mock(APIGatewayManager.class);
Mockito.when(APIGatewayManager.getInstance()).thenReturn(gatewayManager);
TestUtils.mockRegistryAndUserRealm(-1234);
TestUtils.mockAPICacheClearence();
TestUtils.mockAPIMConfiguration();
mockDocumentationCreation();
config = Mockito.mock(APIManagerConfiguration.class);
APIManagerConfigurationService apiManagerConfigurationService = new APIManagerConfigurationServiceImpl(config);
ServiceReferenceHolder.getInstance().setAPIManagerConfigurationService(apiManagerConfigurationService);
APIManagerConfiguration config = ServiceReferenceHolder.getInstance().getAPIManagerConfigurationService().getAPIManagerConfiguration();
GatewayArtifactSynchronizerProperties synchronizerProperties = new GatewayArtifactSynchronizerProperties();
Mockito.when(config.getGatewayArtifactSynchronizerProperties()).thenReturn(synchronizerProperties);
Mockito.when(config.getApiRecommendationEnvironment()).thenReturn(null);
PowerMockito.when(APIUtil.replaceSystemProperty(Mockito.anyString())).thenAnswer((Answer<String>) invocation -> {
Object[] args = invocation.getArguments();
return (String) args[0];
});
TestUtils.initConfigurationContextService(true);
superTenantDomain = "carbon.super";
}
use of org.wso2.carbon.apimgt.impl.certificatemgt.CertificateManager in project carbon-apimgt by wso2.
the class APIGatewayAdmin method unDeployAPI.
private void unDeployAPI(CertificateManager certificateManager, SequenceAdminServiceProxy sequenceAdminServiceProxy, RESTAPIAdminServiceProxy restapiAdminServiceProxy, LocalEntryServiceProxy localEntryServiceProxy, EndpointAdminServiceProxy endpointAdminServiceProxy, GatewayAPIDTO gatewayAPIDTO, MediationSecurityAdminServiceProxy mediationSecurityAdminServiceProxy) throws AxisFault {
if (log.isDebugEnabled()) {
log.debug("Start to undeploy default api " + gatewayAPIDTO.getName() + ":" + gatewayAPIDTO.getVersion());
}
// Delete Default API
String qualifiedDefaultApiName = GatewayUtils.getQualifiedDefaultApiName(gatewayAPIDTO.getName());
if (restapiAdminServiceProxy.getApi(qualifiedDefaultApiName) != null) {
restapiAdminServiceProxy.deleteApi(qualifiedDefaultApiName);
}
if (log.isDebugEnabled()) {
log.debug(gatewayAPIDTO.getName() + ":" + gatewayAPIDTO.getVersion() + " Default API Definition " + "undeployed successfully");
log.debug("Start to undeploy API Definition" + gatewayAPIDTO.getName() + ":" + gatewayAPIDTO.getVersion());
}
// Delete API
String qualifiedName = GatewayUtils.getQualifiedApiName(gatewayAPIDTO.getName(), gatewayAPIDTO.getVersion());
if (restapiAdminServiceProxy.getApi(qualifiedName) != null) {
restapiAdminServiceProxy.deleteApi(qualifiedName);
}
if (log.isDebugEnabled()) {
log.debug(gatewayAPIDTO.getName() + ":" + gatewayAPIDTO.getVersion() + " API Definition undeployed " + "successfully");
log.debug("Start to undeploy custom sequences" + gatewayAPIDTO.getName() + ":" + gatewayAPIDTO.getVersion());
}
// Remove Sequences to be remove.
if (gatewayAPIDTO.getSequencesToBeRemove() != null) {
for (String sequenceName : gatewayAPIDTO.getSequencesToBeRemove()) {
if (sequenceAdminServiceProxy.isExistingSequence(sequenceName)) {
sequenceAdminServiceProxy.deleteSequence(sequenceName);
}
}
}
if (log.isDebugEnabled()) {
log.debug(gatewayAPIDTO.getName() + ":" + gatewayAPIDTO.getVersion() + " custom sequences undeployed " + "successfully");
log.debug("Start to undeploy endpoints" + gatewayAPIDTO.getName() + ":" + gatewayAPIDTO.getVersion());
}
// Remove endpoints
if (gatewayAPIDTO.getEndpointEntriesToBeRemove() != null) {
for (String endpoint : gatewayAPIDTO.getEndpointEntriesToBeRemove()) {
if (endpointAdminServiceProxy.isEndpointExist(endpoint)) {
endpointAdminServiceProxy.deleteEndpoint(endpoint);
}
}
}
if (log.isDebugEnabled()) {
log.debug(gatewayAPIDTO.getName() + ":" + gatewayAPIDTO.getVersion() + " endpoints undeployed " + "successfully");
log.debug("Start to undeploy client certificates" + gatewayAPIDTO.getName() + ":" + gatewayAPIDTO.getVersion());
}
// Remove clientCertificates
if (gatewayAPIDTO.getClientCertificatesToBeRemove() != null) {
for (String alias : gatewayAPIDTO.getClientCertificatesToBeRemove()) {
certificateManager.deleteClientCertificateFromGateway(alias);
}
}
if (log.isDebugEnabled()) {
log.debug(gatewayAPIDTO.getName() + ":" + gatewayAPIDTO.getVersion() + " client certificates undeployed " + "successfully");
log.debug("Start to undeploy local entries" + gatewayAPIDTO.getName() + ":" + gatewayAPIDTO.getVersion());
}
// Remove Local Entries if Exist
if (gatewayAPIDTO.getLocalEntriesToBeRemove() != null) {
for (String localEntryKey : gatewayAPIDTO.getLocalEntriesToBeRemove()) {
if (localEntryServiceProxy.isEntryExists(localEntryKey)) {
localEntryServiceProxy.deleteEntry(localEntryKey);
}
}
}
if (log.isDebugEnabled()) {
log.debug(gatewayAPIDTO.getName() + ":" + gatewayAPIDTO.getVersion() + " Local entries undeployed " + "successfully");
log.debug("Start to remove vault entries" + gatewayAPIDTO.getName() + ":" + gatewayAPIDTO.getVersion());
}
if (gatewayAPIDTO.getCredentialsToBeRemove() != null) {
for (String alias : gatewayAPIDTO.getCredentialsToBeRemove()) {
try {
if (mediationSecurityAdminServiceProxy.isAliasExist(alias)) {
GatewayUtils.deleteRegistryProperty(alias, APIConstants.API_SYSTEM_CONFIG_SECURE_VAULT_LOCATION, gatewayAPIDTO.getTenantDomain());
}
} catch (APIManagementException e) {
String msg = "Error while checking existence of vault entry";
log.error(msg, e);
throw new AxisFault(msg, e);
}
}
}
if (log.isDebugEnabled()) {
log.debug(gatewayAPIDTO.getName() + ":" + gatewayAPIDTO.getVersion() + " Vault entries removed " + "successfully");
log.debug(gatewayAPIDTO.getName() + ":" + gatewayAPIDTO.getVersion() + "undeployed successfully");
}
}
use of org.wso2.carbon.apimgt.impl.certificatemgt.CertificateManager in project carbon-apimgt by wso2.
the class APIGatewayAdmin method deployAPI.
public boolean deployAPI(GatewayAPIDTO gatewayAPIDTO) throws AxisFault {
CertificateManager certificateManager = CertificateManagerImpl.getInstance();
SequenceAdminServiceProxy sequenceAdminServiceProxy = getSequenceAdminServiceClient(gatewayAPIDTO.getTenantDomain());
RESTAPIAdminServiceProxy restapiAdminServiceProxy = getRestapiAdminClient(gatewayAPIDTO.getTenantDomain());
LocalEntryServiceProxy localEntryServiceProxy = new LocalEntryServiceProxy(gatewayAPIDTO.getTenantDomain());
EndpointAdminServiceProxy endpointAdminServiceProxy = new EndpointAdminServiceProxy(gatewayAPIDTO.getTenantDomain());
MediationSecurityAdminServiceProxy mediationSecurityAdminServiceProxy = new MediationSecurityAdminServiceProxy(gatewayAPIDTO.getTenantDomain());
if (log.isDebugEnabled()) {
log.debug("Start to undeploy API" + gatewayAPIDTO.getName() + ":" + gatewayAPIDTO.getVersion());
}
unDeployAPI(certificateManager, sequenceAdminServiceProxy, restapiAdminServiceProxy, localEntryServiceProxy, endpointAdminServiceProxy, gatewayAPIDTO, mediationSecurityAdminServiceProxy);
if (log.isDebugEnabled()) {
log.debug(gatewayAPIDTO.getName() + ":" + gatewayAPIDTO.getVersion() + " undeployed");
log.debug("Start to deploy Local entries" + gatewayAPIDTO.getName() + ":" + gatewayAPIDTO.getVersion());
}
// Add Local Entries
if (gatewayAPIDTO.getLocalEntriesToBeAdd() != null) {
for (GatewayContentDTO localEntry : gatewayAPIDTO.getLocalEntriesToBeAdd()) {
if (localEntryServiceProxy.isEntryExists(localEntry.getName())) {
localEntryServiceProxy.deleteEntry(localEntry.getName());
localEntryServiceProxy.addLocalEntry(localEntry.getContent());
} else {
localEntryServiceProxy.addLocalEntry(localEntry.getContent());
}
}
}
if (log.isDebugEnabled()) {
log.debug(gatewayAPIDTO.getName() + ":" + gatewayAPIDTO.getVersion() + " Local Entries deployed");
log.debug("Start to deploy Endpoint entries" + gatewayAPIDTO.getName() + ":" + gatewayAPIDTO.getVersion());
}
// Add Endpoints
if (gatewayAPIDTO.getEndpointEntriesToBeAdd() != null) {
for (GatewayContentDTO endpointEntry : gatewayAPIDTO.getEndpointEntriesToBeAdd()) {
if (endpointAdminServiceProxy.isEndpointExist(endpointEntry.getName())) {
endpointAdminServiceProxy.deleteEndpoint(endpointEntry.getName());
endpointAdminServiceProxy.addEndpoint(endpointEntry.getContent());
} else {
endpointAdminServiceProxy.addEndpoint(endpointEntry.getContent());
}
}
}
if (log.isDebugEnabled()) {
log.debug(gatewayAPIDTO.getName() + ":" + gatewayAPIDTO.getVersion() + " Endpoints deployed");
log.debug("Start to deploy Client certificates" + gatewayAPIDTO.getName() + ":" + gatewayAPIDTO.getVersion());
}
// Add Client Certificates
if (gatewayAPIDTO.getClientCertificatesToBeAdd() != null) {
for (GatewayContentDTO certificate : gatewayAPIDTO.getClientCertificatesToBeAdd()) {
certificateManager.addClientCertificateToGateway(certificate.getContent(), certificate.getName());
}
}
if (log.isDebugEnabled()) {
log.debug(gatewayAPIDTO.getName() + ":" + gatewayAPIDTO.getVersion() + " client certificates deployed");
log.debug("Start to add vault entries " + gatewayAPIDTO.getName() + ":" + gatewayAPIDTO.getVersion());
}
// Add vault entries
if (gatewayAPIDTO.getCredentialsToBeAdd() != null) {
for (CredentialDto certificate : gatewayAPIDTO.getCredentialsToBeAdd()) {
try {
String encryptedValue = mediationSecurityAdminServiceProxy.doEncryption(certificate.getPassword());
if (mediationSecurityAdminServiceProxy.isAliasExist(certificate.getAlias())) {
setRegistryProperty(gatewayAPIDTO.getTenantDomain(), certificate.getAlias(), encryptedValue);
} else {
setRegistryProperty(gatewayAPIDTO.getTenantDomain(), certificate.getAlias(), encryptedValue);
}
} catch (APIManagementException e) {
log.error("Exception occurred while encrypting password.", e);
throw new AxisFault(e.getMessage());
}
}
}
if (log.isDebugEnabled()) {
log.debug(gatewayAPIDTO.getName() + ":" + gatewayAPIDTO.getVersion() + " Vault Entries Added successfully");
log.debug("Start to deploy custom sequences" + gatewayAPIDTO.getName() + ":" + gatewayAPIDTO.getVersion());
}
// Add Sequences
if (gatewayAPIDTO.getSequenceToBeAdd() != null) {
for (GatewayContentDTO sequence : gatewayAPIDTO.getSequenceToBeAdd()) {
OMElement element;
try {
element = AXIOMUtil.stringToOM(sequence.getContent());
} catch (XMLStreamException e) {
log.error("Exception occurred while converting String to an OM.", e);
throw new AxisFault(e.getMessage());
}
if (sequenceAdminServiceProxy.isExistingSequence(sequence.getName())) {
sequenceAdminServiceProxy.deleteSequence(sequence.getName());
sequenceAdminServiceProxy.addSequence(element);
} else {
sequenceAdminServiceProxy.addSequence(element);
}
}
}
if (log.isDebugEnabled()) {
log.debug(gatewayAPIDTO.getName() + ":" + gatewayAPIDTO.getVersion() + " custom sequences deployed");
log.debug("Start to deploy API Definition" + gatewayAPIDTO.getName() + ":" + gatewayAPIDTO.getVersion());
}
// Add API
if (StringUtils.isNotEmpty(gatewayAPIDTO.getApiDefinition())) {
restapiAdminServiceProxy.addApi(gatewayAPIDTO.getApiDefinition());
}
if (log.isDebugEnabled()) {
log.debug(gatewayAPIDTO.getName() + ":" + gatewayAPIDTO.getVersion() + " API Definition deployed");
log.debug("Start to deploy Default API Definition" + gatewayAPIDTO.getName() + ":" + gatewayAPIDTO.getVersion());
}
if (log.isDebugEnabled()) {
log.debug(gatewayAPIDTO.getName() + ":" + gatewayAPIDTO.getVersion() + " Default API Definition deployed");
log.debug(gatewayAPIDTO.getName() + ":" + gatewayAPIDTO.getVersion() + "Deployed successfully");
}
return true;
}
use of org.wso2.carbon.apimgt.impl.certificatemgt.CertificateManager in project carbon-apimgt by wso2.
the class APIGatewayAdmin method unDeployAPI.
public boolean unDeployAPI(GatewayAPIDTO gatewayAPIDTO) throws AxisFault {
CertificateManager certificateManager = CertificateManagerImpl.getInstance();
SequenceAdminServiceProxy sequenceAdminServiceProxy = getSequenceAdminServiceClient(gatewayAPIDTO.getTenantDomain());
RESTAPIAdminServiceProxy restapiAdminServiceProxy = getRestapiAdminClient(gatewayAPIDTO.getTenantDomain());
LocalEntryServiceProxy localEntryServiceProxy = new LocalEntryServiceProxy(gatewayAPIDTO.getTenantDomain());
EndpointAdminServiceProxy endpointAdminServiceProxy = new EndpointAdminServiceProxy(gatewayAPIDTO.getTenantDomain());
MediationSecurityAdminServiceProxy mediationSecurityAdminServiceProxy = new MediationSecurityAdminServiceProxy(gatewayAPIDTO.getTenantDomain());
unDeployAPI(certificateManager, sequenceAdminServiceProxy, restapiAdminServiceProxy, localEntryServiceProxy, endpointAdminServiceProxy, gatewayAPIDTO, mediationSecurityAdminServiceProxy);
return true;
}
use of org.wso2.carbon.apimgt.impl.certificatemgt.CertificateManager in project carbon-apimgt by wso2.
the class CertificateManagerImplTest method init.
@BeforeClass
public static void init() throws XMLStreamException {
PowerMockito.mockStatic(CertificateMgtDAO.class);
certificateMgtDAO = Mockito.mock(CertificateMgtDAO.class);
PowerMockito.when(CertificateMgtDAO.getInstance()).thenReturn(certificateMgtDAO);
System.setProperty("javax.net.ssl.trustStore", CertificateManagerImplTest.class.getClassLoader().getResource("security/client-truststore.jks").getPath());
System.setProperty(JAVAX_NET_SSL_TRUST_STORE_PASSWORD_PROPERTY, JAVAX_SSL_TRUST_STORE_PASSWORD);
TestUtils.initConfigurationContextService(true);
certificateManager = CertificateManagerImpl.getInstance();
System.setProperty(CARBON_HOME, "");
PowerMockito.mockStatic(CarbonContext.class);
CarbonContext carbonContext = Mockito.mock(CarbonContext.class);
PowerMockito.when(CarbonContext.getThreadLocalCarbonContext()).thenReturn(carbonContext);
Mockito.when(carbonContext.getTenantDomain()).thenReturn(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);
Mockito.when(carbonContext.getTenantId()).thenReturn(MultitenantConstants.SUPER_TENANT_ID);
}
Aggregations