public BAMServerProfile build() { BAMServerProfile bamServerProfile = new BAMServerProfile(); Registry registry; String location; if (profileLocation.startsWith(UnifiedEndpointConstants.VIRTUAL_CONF_REG)) { registry = CarbonContext.getCurrentContext().getRegistry(RegistryType.SYSTEM_CONFIGURATION); location = profileLocation.substring( profileLocation.indexOf(UnifiedEndpointConstants.VIRTUAL_CONF_REG) + UnifiedEndpointConstants.VIRTUAL_CONF_REG.length()); loadBAMProfileFromRegistry(bamServerProfile, registry, location); } else if (profileLocation.startsWith(UnifiedEndpointConstants.VIRTUAL_GOV_REG)) { registry = CarbonContext.getCurrentContext().getRegistry(RegistryType.SYSTEM_GOVERNANCE); location = profileLocation.substring( profileLocation.indexOf(UnifiedEndpointConstants.VIRTUAL_GOV_REG) + UnifiedEndpointConstants.VIRTUAL_GOV_REG.length()); loadBAMProfileFromRegistry(bamServerProfile, registry, location); } else if (profileLocation.startsWith(UnifiedEndpointConstants.VIRTUAL_REG)) { registry = CarbonContext.getCurrentContext().getRegistry(RegistryType.LOCAL_REPOSITORY); location = profileLocation.substring( profileLocation.indexOf(UnifiedEndpointConstants.VIRTUAL_REG) + UnifiedEndpointConstants.VIRTUAL_REG.length()); loadBAMProfileFromRegistry(bamServerProfile, registry, location); } else if (profileLocation.startsWith(UnifiedEndpointConstants.VIRTUAL_FILE)) { // TODO } else { String errMsg = "Invalid bam profile location: " + profileLocation; handleError(errMsg); } return bamServerProfile; }
/** * @return * @throws IdentityException */ public String[] getClaimURIs() throws IdentityException { String tenatUser = MultitenantUtils.getTenantAwareUsername(CarbonContext.getCurrentContext().getUsername()); String domainName = MultitenantUtils.getTenantDomain(tenatUser); String[] claimUris = null; try { UserRealm realm = IdentityTenantUtil.getRealm(domainName, tenatUser); String claimDialect = IdentityUtil.getProperty(IdentityConstants.ServerConfig.SSO_ATTRIB_CLAIM_DIALECT); if (claimDialect == null || claimDialect.equals("")) { // set default claimDialect = "http://wso2.org/claims"; } ClaimMapping[] claims = realm.getClaimManager().getAllClaimMappings(claimDialect); claimUris = new String[claims.length]; for (int i = 0; i < claims.length; i++) { Claim claim = claims[i].getClaim(); claimUris[i] = claim.getClaimUri(); } } catch (IdentityException e) { log.error("Error while getting realm for " + tenatUser, e); throw new IdentityException("Error while getting realm for " + tenatUser + e); } catch (org.wso2.carbon.user.api.UserStoreException e) { log.error("Error while getting claims for " + tenatUser, e); throw new IdentityException("Error while getting claims for " + tenatUser + e); } return claimUris; }
/** * Checks whether received credentials for accessing API is authorized for exporting and importing * APIs * * @param headers HTTP headers of the received request * @return Response indicating whether authentication and authorization for accessing API got * succeeded * @throws APIExportException If an error occurs while authorizing current user */ public static Response authorizeUser(HttpHeaders headers) throws APIExportException { if (!isValidCredentials(headers)) { log.error("No username and password is provided for authentication"); return Response.status(Response.Status.UNAUTHORIZED) .entity("No username and password is provided for authentication") .type(MediaType.APPLICATION_JSON) .build(); } try { String tenantDomain = MultitenantUtils.getTenantDomain(username); PrivilegedCarbonContext.startTenantFlow(); PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenantDomain, true); UserStoreManager userstoremanager = CarbonContext.getThreadLocalCarbonContext().getUserRealm().getUserStoreManager(); String tenantAwareUsername = MultitenantUtils.getTenantAwareUsername(username); // authenticate user provided credentials if (userstoremanager.authenticate(tenantAwareUsername, password)) { log.info(username + " user authenticated successfully"); // Get admin role name of the current domain String adminRoleName = CarbonContext.getCurrentContext() .getUserRealm() .getRealmConfiguration() .getAdminRoleName(); String[] userRoles = userstoremanager.getRoleListOfUser(tenantAwareUsername); // user is only authorized for exporting and importing if he is an admin of his // domain if (Arrays.asList(userRoles).contains(adminRoleName)) { log.info(username + " is authorized to import and export APIs"); return Response.ok().build(); } else { return Response.status(Response.Status.FORBIDDEN) .entity("User Authorization " + "Failed") .type(MediaType.APPLICATION_JSON) .build(); } } else { return Response.status(Response.Status.UNAUTHORIZED) .entity("User Authentication " + "Failed") .type(MediaType.APPLICATION_JSON) .build(); } } catch (UserStoreException e) { log.error("Error while accessing user configuration" + e.getMessage()); throw new APIExportException("Error while accessing user configuration", e); } finally { PrivilegedCarbonContext.endTenantFlow(); } }
public StatusHolder(String type, String key, String version, String target, String targetAction) { this.type = type; this.key = key; this.version = version; this.target = target; this.targetAction = targetAction; this.user = CarbonContext.getCurrentContext().getUsername(); this.success = true; this.timeInstance = Long.toString(System.currentTimeMillis()); }
/** * @param keyStoreName * @return * @throws IdentityException */ private String[] getStoreEntries(String keyStoreName) throws IdentityException { KeyStoreAdmin admin; try { admin = new KeyStoreAdmin( CarbonContext.getCurrentContext().getTenantId(), getGovernanceRegistry()); return admin.getStoreEntries(keyStoreName); } catch (SecurityConfigException e) { log.error("Error reading entries from the key store : " + keyStoreName); throw new IdentityException("Error reading entries from the keystore" + e); } }
/** * Getting the service either from the Registry service or other means. * * @return UserRegistry - The registry instance. */ private static UserRegistry getRegistry(int tenantId) { UserRegistry registry = null; try { // registry = (UserRegistry) DashboardContext.getRegistry(tenantId); // int tID = PrivilegedCarbonContext.getCurrentContext().getTenantId(); registry = (UserRegistry) DashboardContext.getRegistry(tenantId); } catch (Exception e) { // During startup, we can't get a registry instance from declarative services. // But the deployer gets called. Using instance stored in CarbonContext in that case. registry = (UserRegistry) CarbonContext.getCurrentContext().getRegistry(RegistryType.SYSTEM_CONFIGURATION); } return registry; }
private static JaggeryContext createJaggeryContext( OutputStream out, String scriptPath, HttpServletRequest request, HttpServletResponse response) { WebAppContext context = new WebAppContext(); context.setTenantId(Integer.toString(CarbonContext.getCurrentContext().getTenantId())); context.setOutputStream(out); context.setServletRequest(request); context.setServletResponse(response); context.setServletConext(request.getServletContext()); context.setScriptPath(scriptPath); context.getIncludesCallstack().push(scriptPath); context.getIncludedScripts().put(scriptPath, true); return context; }
public void setUp() throws Exception { if (System.getProperty("carbon.home") == null) { File file = new File("../../distribution/kernel/carbon-home"); if (file.exists()) { System.setProperty("carbon.home", file.getAbsolutePath()); } file = new File("../../../distribution/kernel/carbon-home"); if (file.exists()) { System.setProperty("carbon.home", file.getAbsolutePath()); } } CarbonContext.getCurrentContext(); PrivilegedCarbonContext.getThreadLocalCarbonContext() .setTenantDomain(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME); PrivilegedCarbonContext.getThreadLocalCarbonContext() .setTenantId(MultitenantConstants.SUPER_TENANT_ID); }
public static String getEndpoint() { try { String[] endpoints = CarbonContext.getCurrentContext() .discover( new URI[] { new URI("http://shopping-cart.samples.stratos.wso2.org/service/admin/proxy") }); if (endpoints != null) { for (String endpoint : endpoints) { if (endpoint.startsWith("http://")) { return endpoint; } } } } catch (Exception ignored) { // If we are unable to fetch the EPR, we'll be using the properties file. } return getProperty("admin.proxy.service"); }
/** * @param topicName - topic name to publish messages * @param message - is and Object[]{Event, EventDefinition} * @param brokerConfiguration - broker configuration to be used * @throws BrokerEventProcessingException */ public void publish(String topicName, Object message, BrokerConfiguration brokerConfiguration) throws BrokerEventProcessingException { Integer tenantId = CarbonContext.getCurrentContext().getTenantId(); ConcurrentHashMap<BrokerConfiguration, AsyncDataPublisher> dataPublishers = dataPublisherMap.get(tenantId); if (dataPublishers == null) { dataPublishers = new ConcurrentHashMap<BrokerConfiguration, AsyncDataPublisher>(); dataPublisherMap.putIfAbsent(tenantId, dataPublishers); dataPublishers = dataPublisherMap.get(tenantId); } AsyncDataPublisher dataPublisher = dataPublishers.get(brokerConfiguration); if (dataPublisher == null) { synchronized (this) { dataPublisher = dataPublishers.get(brokerConfiguration); if (dataPublisher == null) { dataPublisher = createDataPublisher(brokerConfiguration); dataPublishers.putIfAbsent(brokerConfiguration, dataPublisher); } } } try { Event event = (Event) ((Object[]) message)[0]; StreamDefinition streamDefinition = (StreamDefinition) ((Object[]) message)[1]; // String streamId = outputStreamIdMap.get(topicName + tenantId); if (!dataPublisher.isStreamDefinitionAdded(streamDefinition)) { dataPublisher.addStreamDefinition(streamDefinition); // Sending the first Event publishEvent(brokerConfiguration, dataPublisher, event, streamDefinition); } else { // Sending Events publishEvent(brokerConfiguration, dataPublisher, event, streamDefinition); } } catch (Exception ex) { throw new BrokerEventProcessingException( ex.getMessage() + " Error Occurred When Publishing Events", ex); } }
public void setUp() throws RegistryException { if (System.getProperty("carbon.home") == null) { File file = new File("src/test/resources/carbon-home"); if (file.exists()) { System.setProperty("carbon.home", file.getAbsolutePath()); } } // The line below is responsible for initializing the cache. CarbonContext.getCurrentContext(); String carbonHome = System.getProperty("carbon.home"); System.out.println("carbon home " + carbonHome); String carbonXMLPath = carbonHome + File.separator + "repository" + File.separator + "conf" + File.separator + "carbon.xml"; RegistryConfiguration regConfig = new RegistryConfiguration(carbonXMLPath); RegistryCoreServiceComponent.setRegistryConfig(regConfig); RealmService realmService = new InMemoryRealmService(); InputStream is; try { is = this.getClass().getClassLoader().getResourceAsStream("registry.xml"); } catch (Exception e) { is = null; } ctx = RegistryContext.getBaseInstance(is, realmService); // RegistryConfigurationProcessor.populateRegistryConfig(is, ctx); ctx.setSetup(true); ctx.selectDBConfig("h2-db"); }