use of org.wso2.carbon.user.core.UserRealm in project carbon-apimgt by wso2.
the class RegistryPersistenceImpl method getRegistry.
protected RegistryHolder getRegistry(String requestedTenantDomain) throws APIPersistenceException {
String userTenantDomain = CarbonContext.getThreadLocalCarbonContext().getTenantDomain();
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
log.debug("Accessing system registry in tenant domain " + userTenantDomain + ". Requested tenant domain: " + requestedTenantDomain);
boolean tenantFlowStarted = false;
Registry registry;
RegistryHolder holder = new RegistryHolder();
try {
if (requestedTenantDomain != null) {
int id = getTenantManager().getTenantId(requestedTenantDomain);
RegistryPersistenceUtil.startTenantFlow(requestedTenantDomain);
tenantFlowStarted = true;
if (userTenantDomain != null && !userTenantDomain.equals(requestedTenantDomain)) {
// cross tenant
log.debug("Cross tenant user from tenant " + userTenantDomain + " accessing " + requestedTenantDomain + " registry");
loadTenantRegistry(id);
registry = getRegistryService().getGovernanceSystemRegistry(id);
holder.setTenantId(id);
ServiceReferenceHolder.setUserRealm((ServiceReferenceHolder.getInstance().getRealmService().getBootstrapRealm()));
} else {
log.debug("Same tenant accessing registry of tenant " + userTenantDomain + ":" + tenantId);
loadTenantRegistry(tenantId);
registry = getRegistryService().getGovernanceSystemRegistry(tenantId);
RegistryPersistenceUtil.loadloadTenantAPIRXT(null, tenantId);
holder.setTenantId(tenantId);
ServiceReferenceHolder.setUserRealm((UserRealm) (ServiceReferenceHolder.getInstance().getRealmService().getTenantUserRealm(tenantId)));
}
} else {
log.debug("Same tenant user accessing registry of tenant " + userTenantDomain + ":" + tenantId);
loadTenantRegistry(tenantId);
registry = getRegistryService().getGovernanceSystemRegistry(tenantId);
RegistryPersistenceUtil.loadloadTenantAPIRXT(null, tenantId);
ServiceReferenceHolder.setUserRealm((UserRealm) (ServiceReferenceHolder.getInstance().getRealmService().getTenantUserRealm(tenantId)));
holder.setTenantId(tenantId);
}
} catch (RegistryException | UserStoreException | APIManagementException e) {
String msg = "Failed to get API";
throw new APIPersistenceException(msg, e);
}
holder.setRegistry(registry);
holder.setTenantFlowStarted(tenantFlowStarted);
return holder;
}
use of org.wso2.carbon.user.core.UserRealm 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.user.core.UserRealm in project carbon-apimgt by wso2.
the class UserSignUpWorkflowExecutor method updateRolesOfUser.
/**
* Method updates Roles users with list of roles
*
* @param userName
* @param tenantDomain
* @param roleList
* @throws Exception
*/
protected static void updateRolesOfUser(String userName, List<String> roleList, String tenantDomain) throws Exception {
if (log.isDebugEnabled()) {
log.debug("Adding roles to " + userName + "in " + tenantDomain + " Domain");
}
RealmService realmService = ServiceReferenceHolder.getInstance().getRealmService();
int tenantId = ServiceReferenceHolder.getInstance().getRealmService().getTenantManager().getTenantId(tenantDomain);
UserRealm realm = (UserRealm) realmService.getTenantUserRealm(tenantId);
UserStoreManager manager = realm.getUserStoreManager();
if (manager.isExistingUser(userName)) {
// check whether given roles exist
for (String role : roleList) {
if (!manager.isExistingRole(role)) {
log.error("Could not find role " + role + " in the user store");
throw new Exception("Could not find role " + role + " in the user store");
}
}
manager.updateRoleListOfUser(userName, null, roleList.toArray(new String[0]));
} else {
log.error("User does not exist. Unable to approve user " + userName);
}
}
use of org.wso2.carbon.user.core.UserRealm in project carbon-apimgt by wso2.
the class APIProviderImplTest method testGetSubscriberClaims.
@Test
public void testGetSubscriberClaims() throws APIManagementException, UserStoreException, XMLStreamException {
String configuredClaims = "http://wso2.org/claim1,http://wso2.org/claim2";
APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apimgtDAO, scopesDAO);
// Mock retrieving the tenant domain
PowerMockito.mockStatic(MultitenantUtils.class);
PowerMockito.mockStatic(APIUtil.class);
Mockito.when(MultitenantUtils.getTenantDomain("admin")).thenReturn("carbon.super");
ServiceReferenceHolder serviceReferenceHolder = TestUtils.getServiceReferenceHolder();
RealmService realmService = Mockito.mock(RealmService.class);
TenantManager tenantManager = Mockito.mock(TenantManager.class);
PowerMockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
Mockito.when(serviceReferenceHolder.getRealmService()).thenReturn(realmService);
Mockito.when(realmService.getTenantManager()).thenReturn(tenantManager);
PowerMockito.when(tenantManager.getTenantId(Matchers.anyString())).thenReturn(-1234);
UserStoreManager userStoreManager = Mockito.mock(UserStoreManager.class);
UserRealm userRealm = Mockito.mock(UserRealm.class);
PowerMockito.when(realmService.getTenantUserRealm(-1234)).thenReturn(userRealm);
PowerMockito.when(userRealm.getUserStoreManager()).thenReturn(userStoreManager);
PowerMockito.when(userStoreManager.isExistingUser("admin")).thenReturn(true);
SortedMap<String, String> claimValues = new TreeMap<String, String>();
claimValues.put("claim1", "http://wso2.org/claim1");
claimValues.put("claim2", "http://wso2.org/claim2");
claimValues.put("claim3", "http://wso2.org/claim3");
PowerMockito.when(APIUtil.getClaims("admin", -1234, DEFAULT_DIALECT_URI)).thenReturn(claimValues);
APIManagerConfiguration configuration = Mockito.mock(APIManagerConfiguration.class);
APIManagerConfigurationService configurationService = Mockito.mock(APIManagerConfigurationService.class);
PowerMockito.when(serviceReferenceHolder.getAPIManagerConfigurationService()).thenReturn(configurationService);
PowerMockito.when(configurationService.getAPIManagerConfiguration()).thenReturn(configuration);
Mockito.when(configuration.getFirstProperty(APIConstants.API_PUBLISHER_SUBSCRIBER_CLAIMS)).thenReturn(configuredClaims);
Map subscriberClaims = apiProvider.getSubscriberClaims("admin");
assertNotNull(subscriberClaims);
assertEquals(configuredClaims.split(",").length, subscriberClaims.size());
}
use of org.wso2.carbon.user.core.UserRealm in project carbon-apimgt by wso2.
the class APIProviderImplTest method testCreateNewAPIVersion.
@Test
public void testCreateNewAPIVersion() throws Exception {
// Create Original API
APIIdentifier apiId = new APIIdentifier("admin", "API1", "1.0.0");
API api = new API(apiId);
api.setContext("/test");
api.setVisibility("Public");
api.setStatus(APIConstants.CREATED);
api.setWsdlUrl("https://localhost:9443/services/echo?wsdl");
api.setOrganization("carbon.super");
long time = System.currentTimeMillis();
String newVersion = "1.0.1";
// Create new API object
APIIdentifier newApiId = new APIIdentifier("admin", "API1", "1.0.1");
final API newApi = new API(newApiId);
newApi.setStatus(APIConstants.CREATED);
newApi.setContext("/test");
newApi.setWsdlUrl("/registry/resource/_system/governance/apimgt/applicationdata/wsdls/admin--API11.0.0.wsdl");
// Create Documentation List
List<Documentation> documentationList = getDocumentationList();
final APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apiPersistenceInstance, apimgtDAO, scopesDAO, documentationList, null);
RegistryService registryService = Mockito.mock(RegistryService.class);
UserRegistry userRegistry = Mockito.mock(UserRegistry.class);
ServiceReferenceHolder serviceReferenceHolder = TestUtils.getServiceReferenceHolder();
RealmService realmService = Mockito.mock(RealmService.class);
TenantManager tenantManager = Mockito.mock(TenantManager.class);
Mockito.when(artifactManager.newGovernanceArtifact(any(QName.class))).thenReturn(artifact);
Mockito.when(APIUtil.createAPIArtifactContent(artifact, api)).thenReturn(artifact);
PowerMockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
Mockito.when(serviceReferenceHolder.getRegistryService()).thenReturn(registryService);
Mockito.when(registryService.getConfigSystemRegistry(Mockito.anyInt())).thenReturn(userRegistry);
Mockito.when(serviceReferenceHolder.getRealmService()).thenReturn(realmService);
Mockito.when(realmService.getTenantManager()).thenReturn(tenantManager);
GenericArtifact artifactNew = Mockito.mock(GenericArtifact.class);
Mockito.when(APIUtil.createAPIArtifactContent(artifact, newApi)).thenReturn(artifactNew);
PublisherAPI publisherAPI = Mockito.mock(PublisherAPI.class);
Mockito.when(publisherAPI.getVersionTimestamp()).thenReturn(String.valueOf(time));
PowerMockito.when(apiPersistenceInstance.addAPI(any(Organization.class), any(PublisherAPI.class))).thenReturn(publisherAPI);
API returnedAPI = apiProvider.addAPI(api);
Assert.assertTrue(StringUtils.isNotEmpty(returnedAPI.getVersionTimestamp()));
String targetPath = APIConstants.API_LOCATION + RegistryConstants.PATH_SEPARATOR + api.getId().getProviderName() + RegistryConstants.PATH_SEPARATOR + api.getId().getApiName() + RegistryConstants.PATH_SEPARATOR + newVersion + APIConstants.API_RESOURCE_NAME;
String apiSourcePath = APIConstants.API_LOCATION + RegistryConstants.PATH_SEPARATOR + apiId.getProviderName() + RegistryConstants.PATH_SEPARATOR + apiId.getApiName() + RegistryConstants.PATH_SEPARATOR + apiId.getVersion() + APIConstants.API_RESOURCE_NAME;
PowerMockito.when(APIUtil.getAPIPath(apiId)).thenReturn(apiSourcePath);
String apiSourceUUID = "87ty543-899hyt";
Mockito.when(apiProvider.registry.resourceExists(targetPath)).thenReturn(false);
Mockito.doNothing().when(apiProvider.registry).beginTransaction();
Mockito.doNothing().when(apiProvider.registry).commitTransaction();
Resource apiSourceArtifact = Mockito.mock(Resource.class);
Mockito.when(apiProvider.registry.get(apiSourcePath)).thenReturn(apiSourceArtifact);
// Mocking Old API retrieval
Mockito.when(apiSourceArtifact.getUUID()).thenReturn(apiSourceUUID);
Mockito.when(artifact.getAttribute(APIConstants.API_OVERVIEW_STATUS)).thenReturn("PUBLISHED");
Mockito.when(artifact.getAttribute(APIConstants.API_OVERVIEW_CONTEXT)).thenReturn("test");
Mockito.when(artifact.getAttribute(APIConstants.API_OVERVIEW_CONTEXT_TEMPLATE)).thenReturn("test/{version}");
Mockito.when(artifact.getAttribute(APIConstants.API_OVERVIEW_WEBSOCKET)).thenReturn("false");
Mockito.when(artifact.getAttribute(APIConstants.API_OVERVIEW_VISIBLE_ROLES)).thenReturn("admin, subscriber");
Mockito.when(artifactManager.getGenericArtifact(apiSourceUUID)).thenReturn(artifact);
// Mocking thumbnail
String thumbUrl = APIConstants.API_IMAGE_LOCATION + RegistryConstants.PATH_SEPARATOR + api.getId().getProviderName() + RegistryConstants.PATH_SEPARATOR + api.getId().getApiName() + RegistryConstants.PATH_SEPARATOR + api.getId().getVersion() + RegistryConstants.PATH_SEPARATOR + APIConstants.API_ICON_IMAGE;
Resource image = Mockito.mock(Resource.class);
Mockito.when(apiProvider.registry.get(thumbUrl)).thenReturn(image);
Mockito.when(apiProvider.registry.resourceExists(thumbUrl)).thenReturn(true);
// Mocking In sequence retrieval
String inSeqFilePath = "API1/1.0.0/in";
PowerMockito.when(APIUtil.getSequencePath(api.getId(), "in")).thenReturn(inSeqFilePath);
Mockito.when(apiProvider.registry.resourceExists(inSeqFilePath)).thenReturn(true);
Collection inSeqCollection = Mockito.mock(Collection.class);
Mockito.when(apiProvider.registry.get(inSeqFilePath)).thenReturn(inSeqCollection);
String[] inSeqChildPaths = { "path1" };
Mockito.when(inSeqCollection.getChildren()).thenReturn(inSeqChildPaths);
Mockito.when(apiProvider.registry.get(inSeqChildPaths[0])).thenReturn(apiSourceArtifact);
InputStream responseStream = IOUtils.toInputStream("<sequence name=\"in-seq\"></sequence>", "UTF-8");
OMElement seqElment = buildOMElement(responseStream);
PowerMockito.when(APIUtil.buildOMElement(responseStream)).thenReturn(seqElment);
Mockito.when(apiSourceArtifact.getContentStream()).thenReturn(responseStream);
// Mocking Out sequence retrieval
Resource apiSourceArtifact1 = Mockito.mock(Resource.class);
String outSeqFilePath = "API1/1.0.0/out";
PowerMockito.when(APIUtil.getSequencePath(api.getId(), "out")).thenReturn(outSeqFilePath);
Mockito.when(apiProvider.registry.resourceExists(outSeqFilePath)).thenReturn(true);
Collection outSeqCollection = Mockito.mock(Collection.class);
Mockito.when(apiProvider.registry.get(outSeqFilePath)).thenReturn(outSeqCollection);
String[] outSeqChildPaths = { "path2" };
Mockito.when(outSeqCollection.getChildren()).thenReturn(outSeqChildPaths);
Mockito.when(apiProvider.registry.get(outSeqChildPaths[0])).thenReturn(apiSourceArtifact1);
InputStream responseStream2 = IOUtils.toInputStream("<sequence name=\"in-seq\"></sequence>", "UTF-8");
OMElement seqElment2 = buildOMElement(responseStream2);
PowerMockito.when(APIUtil.buildOMElement(responseStream2)).thenReturn(seqElment2);
Mockito.when(apiSourceArtifact1.getContentStream()).thenReturn(responseStream2);
// Mock Adding new API artifact with new version
Mockito.doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
apiProvider.createAPI(newApi);
return null;
}
}).when(artifactManager).addGenericArtifact(artifact);
Mockito.doNothing().when(artifact).attachLifecycle(APIConstants.API_LIFE_CYCLE);
PowerMockito.when(APIUtil.getAPIProviderPath(api.getId())).thenReturn("/dummy/provider/path");
Mockito.doNothing().when(apiProvider.registry).addAssociation("/dummy/provider/path", targetPath, APIConstants.PROVIDER_ASSOCIATION);
PowerMockito.when(GovernanceUtils.getArtifactPath(apiProvider.registry, artifact.getId())).thenReturn(artifactPath);
PowerMockito.doNothing().when(APIUtil.class);
String[] roles = { "admin", "subscriber" };
APIUtil.setResourcePermissions("admin", "Public", roles, artifactPath);
// Mock no tags case
Mockito.when(apiProvider.registry.getTags(apiSourcePath)).thenReturn(null);
// Mock WSDL retrieval
String wsdlUrl = APIUtil.getWSDLDefinitionFilePath(api.getId().getApiName(), api.getId().getVersion(), api.getId().getProviderName());
PowerMockito.when(apiProvider.registry.resourceExists(wsdlUrl)).thenReturn(true);
// Mock new API retrieval
String newApiPath = "API1/1.0.1/";
PowerMockito.when(APIUtil.getAPIPath(newApi.getId())).thenReturn(newApiPath);
String newApiUUID = "87ty543-899hy23";
GenericArtifact newArtifact = Mockito.mock(GenericArtifact.class);
Resource newApiResource = Mockito.mock(Resource.class);
Mockito.when(newApiResource.getUUID()).thenReturn(newApiUUID);
Mockito.when(apiProvider.registry.get(newApiPath)).thenReturn(newApiResource);
Mockito.when(artifactManager.getGenericArtifact(newApiUUID)).thenReturn(newArtifact);
PowerMockito.when(APIUtil.getAPI(newArtifact, apiProvider.registry, api.getId(), "test")).thenReturn(newApi);
// Swagger resource
String resourcePath = APIUtil.getOpenAPIDefinitionFilePath(api.getId().getApiName(), api.getId().getVersion(), api.getId().getProviderName());
Mockito.when(apiProvider.registry.resourceExists(resourcePath + APIConstants.API_OAS_DEFINITION_RESOURCE_NAME)).thenReturn(true);
PowerMockito.mockStatic(OASParserUtil.class);
Mockito.when(OASParserUtil.getAPIDefinition(apiId, apiProvider.registry)).thenReturn("{\"info\": {\"swagger\":\"data\"}}");
Mockito.doNothing().when(artifactManager).updateGenericArtifact(artifact);
// WSDL
String newWsdlResourcePath = APIUtil.getWSDLDefinitionFilePath(newApi.getId().getApiName(), newApi.getId().getVersion(), newApi.getId().getProviderName());
PowerMockito.when(apiProvider.registry.copy(resourcePath, newWsdlResourcePath)).thenReturn(newWsdlResourcePath);
// Mock Config system registry
PowerMockito.when(tenantManager.getTenantId(Matchers.anyString())).thenReturn(-1234);
AuthorizationManager authManager = Mockito.mock(AuthorizationManager.class);
UserRealm userRealm = Mockito.mock(UserRealm.class);
PowerMockito.when(realmService.getTenantUserRealm(-1234)).thenReturn(userRealm);
PowerMockito.when(userRealm.getAuthorizationManager()).thenReturn(authManager);
}
Aggregations