use of org.wso2.carbon.identity.application.authentication.framework.util.FrameworkConstants.RequestParams.TENANT_DOMAIN in project carbon-apimgt by wso2.
the class RegistryPersistenceImplTestCase method testRegistrySelectionForTenantUserCrossTenatAccess.
@Test
public void testRegistrySelectionForTenantUserCrossTenatAccess() throws Exception {
RegistryService registryService = Mockito.mock(RegistryService.class);
TenantManager tenantManager = Mockito.mock(TenantManager.class);
Mockito.when(tenantManager.getTenantId(TENANT_DOMAIN)).thenReturn(TENANT_ID);
Mockito.when(tenantManager.getTenantId(SUPER_TENANT_DOMAIN)).thenReturn(SUPER_TENANT_ID);
PowerMockito.mockStatic(CarbonContext.class);
CarbonContext context = Mockito.mock(CarbonContext.class);
PowerMockito.when(CarbonContext.getThreadLocalCarbonContext()).thenReturn(context);
PowerMockito.mockStatic(PrivilegedCarbonContext.class);
PrivilegedCarbonContext privilegedContext = Mockito.mock(PrivilegedCarbonContext.class);
PowerMockito.when(PrivilegedCarbonContext.getThreadLocalCarbonContext()).thenReturn(privilegedContext);
PowerMockito.mockStatic(GovernanceUtils.class);
GovernanceArtifact artifact = Mockito.mock(GovernanceArtifact.class);
List<GovernanceArtifact> artifacts = new ArrayList<GovernanceArtifact>();
artifacts.add(artifact);
PowerMockito.when(GovernanceUtils.findGovernanceArtifacts(Mockito.any(String.class), Mockito.any(Registry.class), Mockito.any(String.class), Mockito.any(Boolean.class))).thenReturn(artifacts);
PowerMockito.mockStatic(RegistryPersistenceUtil.class);
ServiceReferenceHolder serviceRefHolder = Mockito.mock(ServiceReferenceHolder.class);
PowerMockito.mockStatic(ServiceReferenceHolder.class);
PowerMockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceRefHolder);
RealmService realmService = Mockito.mock(RealmService.class);
PowerMockito.when(serviceRefHolder.getRealmService()).thenReturn(realmService);
UserRealm realm = Mockito.mock(UserRealm.class);
PowerMockito.when(realmService.getTenantUserRealm(TENANT_ID)).thenReturn(realm);
PowerMockito.doNothing().when(RegistryPersistenceUtil.class, "loadloadTenantAPIRXT", Mockito.any(String.class), Mockito.any(Integer.class));
Mockito.when(context.getTenantDomain()).thenReturn(TENANT_DOMAIN);
Mockito.when(context.getTenantId()).thenReturn(TENANT_ID);
APIPersistence apiPersistenceInstance = new RegistryPersistenceImplWrapper(tenantManager, registryService);
// return null artifact because we are not testing artifact related params. this is only to get the registry obj
GenericArtifactManager artifactManager = Mockito.mock(GenericArtifactManager.class);
PowerMockito.when(RegistryPersistenceUtil.getArtifactManager(Mockito.any(Registry.class), Mockito.any(String.class))).thenReturn(artifactManager);
Mockito.when(artifactManager.getGenericArtifact(Mockito.any(String.class))).thenReturn(null);
// trigger registry object creation. access super tenant api
UserContext ctx = new UserContext("user", new Organization(TENANT_DOMAIN), null, null);
apiPersistenceInstance.searchAPIsForDevPortal(new Organization(SUPER_TENANT_DOMAIN), "", 0, 10, ctx);
// check whether super tenant's system registy is accessed
Mockito.verify(registryService, times(1)).getGovernanceSystemRegistry((SUPER_TENANT_ID));
}
use of org.wso2.carbon.identity.application.authentication.framework.util.FrameworkConstants.RequestParams.TENANT_DOMAIN in project carbon-apimgt by wso2.
the class RegistryPersistenceUtilTestCase method testTenantDomain.
@Test
public void testTenantDomain() {
PowerMockito.mockStatic(ServerConfiguration.class);
ServerConfiguration config = Mockito.mock(ServerConfiguration.class);
PowerMockito.when(ServerConfiguration.getInstance()).thenReturn(config);
PowerMockito.when(config.getFirstProperty(anyString())).thenReturn(null);
String domain = RegistryPersistenceUtil.getTenantDomain(new APIIdentifier("test@" + TENANT_DOMAIN, "test", "1.0"));
domain = RegistryPersistenceUtil.getTenantDomain(new APIIdentifier("test", "test", "1.0"));
}
use of org.wso2.carbon.identity.application.authentication.framework.util.FrameworkConstants.RequestParams.TENANT_DOMAIN in project carbon-apimgt by wso2.
the class ApiMgtDAO method retrieveWorkflowFromInternalReference.
/**
* Returns a workflow object for a given internal workflow reference and the workflow type.
*
* @param workflowReference
* @param workflowType
* @return
* @throws APIManagementException
*/
public WorkflowDTO retrieveWorkflowFromInternalReference(String workflowReference, String workflowType) throws APIManagementException {
Connection connection = null;
PreparedStatement prepStmt = null;
ResultSet rs = null;
WorkflowDTO workflowDTO = null;
String query = SQLConstants.GET_ALL_WORKFLOW_ENTRY_FROM_INTERNAL_REF_SQL;
try {
connection = APIMgtDBUtil.getConnection();
prepStmt = connection.prepareStatement(query);
prepStmt.setString(1, workflowReference);
prepStmt.setString(2, workflowType);
rs = prepStmt.executeQuery();
while (rs.next()) {
workflowDTO = WorkflowExecutorFactory.getInstance().createWorkflowDTO(rs.getString("WF_TYPE"));
workflowDTO.setStatus(WorkflowStatus.valueOf(rs.getString("WF_STATUS")));
workflowDTO.setExternalWorkflowReference(rs.getString("WF_EXTERNAL_REFERENCE"));
workflowDTO.setCreatedTime(rs.getTimestamp("WF_CREATED_TIME").getTime());
workflowDTO.setWorkflowReference(rs.getString("WF_REFERENCE"));
workflowDTO.setTenantDomain(rs.getString("TENANT_DOMAIN"));
workflowDTO.setTenantId(rs.getInt("TENANT_ID"));
workflowDTO.setWorkflowDescription(rs.getString("WF_STATUS_DESC"));
}
} catch (SQLException e) {
handleException("Error while retrieving workflow details for " + workflowReference, e);
} finally {
APIMgtDBUtil.closeAllConnections(prepStmt, connection, rs);
}
return workflowDTO;
}
use of org.wso2.carbon.identity.application.authentication.framework.util.FrameworkConstants.RequestParams.TENANT_DOMAIN in project carbon-apimgt by wso2.
the class ApiMgtDAO method getworkflowReferenceByExternalWorkflowReferenceID.
/**
* Get the Pending workflow Request using ExternalWorkflowReference for a particular tenant
*
* @param externelWorkflowRef of pending workflow request
* @param status workflow status of workflow pending process
* @param tenantDomain tenant domain of user
* @return workflow pending request
*/
public Workflow getworkflowReferenceByExternalWorkflowReferenceID(String externelWorkflowRef, String status, String tenantDomain) throws APIManagementException {
ResultSet rs = null;
Workflow workflow = new Workflow();
String sqlQuery = SQLConstants.GET_ALL_WORKFLOW_DETAILS_BY_EXTERNAL_WORKFLOW_REFERENCE;
try (Connection connection = APIMgtDBUtil.getConnection();
PreparedStatement prepStmt = connection.prepareStatement(sqlQuery)) {
try {
prepStmt.setString(1, externelWorkflowRef);
prepStmt.setString(2, status);
prepStmt.setString(3, tenantDomain);
rs = prepStmt.executeQuery();
while (rs.next()) {
workflow.setWorkflowId(rs.getInt("WF_ID"));
workflow.setWorkflowReference(rs.getString("WF_REFERENCE"));
workflow.setWorkflowType(rs.getString("WF_TYPE"));
String workflowstatus = rs.getString("WF_STATUS");
workflow.setStatus(org.wso2.carbon.apimgt.api.WorkflowStatus.valueOf(workflowstatus));
workflow.setCreatedTime(rs.getTimestamp("WF_CREATED_TIME").toString());
workflow.setUpdatedTime(rs.getTimestamp("WF_UPDATED_TIME").toString());
workflow.setWorkflowDescription(rs.getString("WF_STATUS_DESC"));
workflow.setTenantId(rs.getInt("TENANT_ID"));
workflow.setTenantDomain(rs.getString("TENANT_DOMAIN"));
workflow.setExternalWorkflowReference(rs.getString("WF_EXTERNAL_REFERENCE"));
InputStream targetStream = rs.getBinaryStream("WF_METADATA");
InputStream propertiesTargetStream = rs.getBinaryStream("WF_PROPERTIES");
if (targetStream != null) {
String metadata = APIMgtDBUtil.getStringFromInputStream(targetStream);
Gson metadataGson = new Gson();
JSONObject metadataJson = metadataGson.fromJson(metadata, JSONObject.class);
workflow.setMetadata(metadataJson);
} else {
JSONObject metadataJson = new JSONObject();
workflow.setMetadata(metadataJson);
}
if (propertiesTargetStream != null) {
String properties = APIMgtDBUtil.getStringFromInputStream(propertiesTargetStream);
Gson propertiesGson = new Gson();
JSONObject propertiesJson = propertiesGson.fromJson(properties, JSONObject.class);
workflow.setProperties(propertiesJson);
} else {
JSONObject propertiesJson = new JSONObject();
workflow.setProperties(propertiesJson);
}
}
} catch (SQLException e) {
handleException("Error when retriving the workflow details. ", e);
} finally {
APIMgtDBUtil.closeAllConnections(prepStmt, connection, rs);
}
} catch (SQLException e) {
handleException("Error when retriving the workflow details. ", e);
}
return workflow;
}
use of org.wso2.carbon.identity.application.authentication.framework.util.FrameworkConstants.RequestParams.TENANT_DOMAIN in project carbon-apimgt by wso2.
the class GatewayArtifactsMgtDAO method retrieveGatewayArtifacts.
public List<APIRuntimeArtifactDto> retrieveGatewayArtifacts(String tenantDomain) throws APIManagementException {
String query = SQLConstants.RETRIEVE_ARTIFACTS;
List<APIRuntimeArtifactDto> apiRuntimeArtifactDtoList = new ArrayList<>();
try (Connection connection = GatewayArtifactsMgtDBUtil.getArtifactSynchronizerConnection();
PreparedStatement preparedStatement = connection.prepareStatement(query)) {
preparedStatement.setString(1, tenantDomain);
try (ResultSet resultSet = preparedStatement.executeQuery()) {
while (resultSet.next()) {
String apiId = resultSet.getString("API_ID");
String label = resultSet.getString("LABEL");
try {
APIRuntimeArtifactDto apiRuntimeArtifactDto = new APIRuntimeArtifactDto();
apiRuntimeArtifactDto.setTenantDomain(resultSet.getString("TENANT_DOMAIN"));
apiRuntimeArtifactDto.setApiId(apiId);
String resolvedVhost = VHostUtils.resolveIfNullToDefaultVhost(label, resultSet.getString("VHOST"));
apiRuntimeArtifactDto.setLabel(label);
apiRuntimeArtifactDto.setVhost(resolvedVhost);
apiRuntimeArtifactDto.setName(resultSet.getString("API_NAME"));
apiRuntimeArtifactDto.setVersion(resultSet.getString("API_VERSION"));
apiRuntimeArtifactDto.setProvider(resultSet.getString("API_PROVIDER"));
apiRuntimeArtifactDto.setRevision(resultSet.getString("REVISION_ID"));
apiRuntimeArtifactDto.setType(resultSet.getString("API_TYPE"));
apiRuntimeArtifactDto.setContext(resultSet.getString("CONTEXT"));
InputStream artifact = resultSet.getBinaryStream("ARTIFACT");
if (artifact != null) {
byte[] artifactByte = APIMgtDBUtil.getBytesFromInputStream(artifact);
try (InputStream newArtifact = new ByteArrayInputStream(artifactByte)) {
apiRuntimeArtifactDto.setArtifact(newArtifact);
}
}
apiRuntimeArtifactDto.setFile(true);
apiRuntimeArtifactDtoList.add(apiRuntimeArtifactDto);
} catch (APIManagementException e) {
// handle exception inside the loop and continue with other API artifacts
log.error(String.format("Error resolving vhost while retrieving runtime artifact for API %s, " + "gateway environment \"%s\", tenant: \"%s\"." + "Skipping runtime artifact for the API.", apiId, label, tenantDomain), e);
} catch (IOException e) {
// handle exception inside the loop and continue with other API artifacts
log.error(String.format("Error occurred retrieving input stream from byte array of " + "API: %s, gateway environment \"%s\", tenant: \"%s\".", apiId, label, tenantDomain), e);
} catch (SQLException e) {
// handle exception inside the loop and continue with other API artifacts
log.error(String.format("Failed to retrieve Gateway Artifact of API: %s, " + "gateway environment \"%s\", tenant: \"%s\".", apiId, label, tenantDomain), e);
}
}
}
} catch (SQLException e) {
handleException("Failed to retrieve Gateway Artifacts.", e);
}
return apiRuntimeArtifactDtoList;
}
Aggregations