Пример #1
0
 /**
  * Creates and returns an InputStream from the file path / http location given.
  *
  * @throws DataServiceFault
  * @see InputStream
  */
 public static InputStream getInputStreamFromPath(String path)
     throws IOException, DataServiceFault {
   InputStream ins;
   if (path.startsWith("http://")) {
     /* This is a url file path */
     URL url = new URL(path);
     ins = url.openStream();
   } else if (isRegistryPath(path)) {
     try {
       RegistryService registryService = DataServicesDSComponent.getRegistryService();
       if (registryService == null) {
         throw new DataServiceFault(
             "DBUtils.getInputStreamFromPath(): Registry service is not available");
       }
       Registry registry;
       if (path.startsWith(DBConstants.CONF_REGISTRY_PATH_PREFIX)) {
         if (path.length() > DBConstants.CONF_REGISTRY_PATH_PREFIX.length()) {
           path = path.substring(DBConstants.CONF_REGISTRY_PATH_PREFIX.length());
           registry = registryService.getConfigSystemRegistry(getCurrentTenantId());
         } else {
           throw new DataServiceFault("Empty configuration registry path given");
         }
       } else {
         if (path.length() > DBConstants.GOV_REGISTRY_PATH_PREFIX.length()) {
           path = path.substring(DBConstants.GOV_REGISTRY_PATH_PREFIX.length());
           registry = registryService.getGovernanceSystemRegistry(getCurrentTenantId());
         } else {
           throw new DataServiceFault("Empty governance registry path given");
         }
       }
       if (registry.resourceExists(path)) {
         Resource serviceResource = registry.get(path);
         ins = serviceResource.getContentStream();
       } else {
         throw new DataServiceFault(
             "The given XSLT resource path at '" + path + "' does not exist");
       }
     } catch (RegistryException e) {
       String msg = "Error in retrieving the resource: " + path;
       log.error(msg, e);
       throw new DataServiceFault(e, msg);
     }
   } else {
     File csvFile = new File(path);
     if (path.startsWith("." + File.separator) || path.startsWith(".." + File.separator)) {
       /* this is a relative path */
       path = csvFile.getAbsolutePath();
     }
     /* local file */
     ins = new FileInputStream(path);
   }
   return ins;
 }
Пример #2
0
 public static boolean authenticate(String username, String password) throws DataServiceFault {
   try {
     RegistryService registryService = DataServicesDSComponent.getRegistryService();
     UserRealm realm =
         registryService.getUserRealm(
             PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId());
     username = MultitenantUtils.getTenantAwareUsername(username);
     return realm.getUserStoreManager().authenticate(username, password);
   } catch (Exception e) {
     throw new DataServiceFault(e, "Error in authenticating user '" + username + "'");
   }
 }
  protected void setRegistryService(RegistryService registryService) {
    PrivilegedCarbonContext carbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext();
    carbonContext.setTenantDomain(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);
    carbonContext.setTenantId(MultitenantConstants.SUPER_TENANT_ID);

    CommonUtil.setRegistryService(registryService);
    // Generate LCM search query if it doesn't exist.
    try {
      CommonUtil.addDefaultLifecyclesIfNotAvailable(
          registryService.getConfigSystemRegistry(),
          registryService.getRegistry(CarbonConstants.REGISTRY_SYSTEM_USERNAME));
    } catch (Exception e) {
      log.error("An error occurred while setting up Governance Life Cycle Management", e);
    }
  }
Пример #4
0
 /**
  * Retrieves the current user's roles given the username.
  *
  * @param username The username
  * @return The user roles
  * @throws DataServiceFault
  */
 public static String[] getUserRoles(String username) throws DataServiceFault {
   RealmService realmService = DataServicesDSComponent.getRealmService();
   RegistryService registryService = DataServicesDSComponent.getRegistryService();
   username = MultitenantUtils.getTenantAwareUsername(username);
   String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain();
   int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
   username = MultitenantUtils.getTenantAwareUsername(username);
   try {
     if (tenantId < MultitenantConstants.SUPER_TENANT_ID) {
       tenantId = realmService.getTenantManager().getTenantId(tenantDomain);
     }
     if (tenantId < MultitenantConstants.SUPER_TENANT_ID) {
       /* the tenant doesn't exist. */
       log.error("The tenant doesn't exist. Tenant domain:" + tenantDomain);
       throw new DataServiceFault("Access Denied. You are not authorized.");
     }
     if (!realmService.getTenantManager().isTenantActive(tenantId)) {
       /* the tenant is not active. */
       log.error("The tenant is not active. Tenant domain:" + tenantDomain);
       throw new DataServiceFault("The tenant is not active. Tenant domain:" + tenantDomain);
     }
     UserRealm realm = registryService.getUserRealm(tenantId);
     String roles[] = realm.getUserStoreManager().getRoleListOfUser(username);
     return roles;
   } catch (Exception e) {
     String msg =
         "Error in retrieving the realm for the tenant id: "
             + tenantId
             + ", username: "******". "
             + e.getMessage();
     log.error(msg);
     throw new DataServiceFault(msg);
   }
 }
Пример #5
0
  private UserRegistry initRegistry() throws RegistryException {

    UserRegistry govRegistry = registryService.getGovernanceSystemRegistry();

    // check if the resource is available, else create it
    if (!govRegistry.resourceExists(STRATOS_MANAGER_REOSURCE)) {
      synchronized (RegistryManager.class) {
        try {
          if (!govRegistry.resourceExists(STRATOS_MANAGER_REOSURCE)) {
            govRegistry.put(STRATOS_MANAGER_REOSURCE, govRegistry.newCollection());
          }
        } catch (RegistryException e) {
          String errorMsg = "Failed to create the registry resource " + STRATOS_MANAGER_REOSURCE;
          log.error(errorMsg, e);
          throw e;
        }
      }
    }

    return govRegistry;
  }
  /**
   * This method is used to load custom security scenarios used inside Identity STS componsnts.
   *
   * @throws Exception
   */
  private void loadSecurityScenarios() throws Exception {

    Registry registry = registryService.getConfigSystemRegistry();

    try {
      // Scenarios are listed in resources/scenario-config.xml
      URL resource = bundleContext.getBundle().getResource("scenario-config.xml");
      XmlConfiguration xmlConfiguration =
          new XmlConfiguration(resource.openStream(), SecurityConstants.SECURITY_NAMESPACE);

      OMElement[] elements = xmlConfiguration.getElements("//ns:Scenario");

      boolean transactionStarted = Transaction.isStarted();
      if (!transactionStarted) {
        registry.beginTransaction();
      }

      for (OMElement scenarioEle : elements) {
        SecurityScenario scenario = new SecurityScenario();
        String scenarioId = scenarioEle.getAttribute(SecurityConstants.ID_QN).getAttributeValue();

        scenario.setScenarioId(scenarioId);
        scenario.setSummary(
            scenarioEle.getFirstChildWithName(SecurityConstants.SUMMARY_QN).getText());
        scenario.setDescription(
            scenarioEle.getFirstChildWithName(SecurityConstants.DESCRIPTION_QN).getText());
        scenario.setCategory(
            scenarioEle.getFirstChildWithName(SecurityConstants.CATEGORY_QN).getText());
        scenario.setWsuId(scenarioEle.getFirstChildWithName(SecurityConstants.WSUID_QN).getText());
        scenario.setType(scenarioEle.getFirstChildWithName(SecurityConstants.TYPE_QN).getText());

        OMElement genPolicyElem =
            scenarioEle.getFirstChildWithName(SecurityConstants.IS_GEN_POLICY_QN);
        if (genPolicyElem != null && genPolicyElem.getText().equals("false")) {
          scenario.setGeneralPolicy(false);
        }

        String resourceUri = SecurityConstants.SECURITY_POLICY + "/" + scenarioId;

        for (Iterator modules =
                scenarioEle.getFirstChildWithName(SecurityConstants.MODULES_QN).getChildElements();
            modules.hasNext(); ) {
          String module = ((OMElement) modules.next()).getText();
          scenario.addModule(module);
        }

        // Save it in the DB
        SecurityScenarioDatabase.put(scenarioId, scenario);

        // Store the scenario in the Registry
        if (!scenarioId.equals(SecurityConstants.SCENARIO_DISABLE_SECURITY)
            && !scenarioId.equals(SecurityConstants.POLICY_FROM_REG_SCENARIO)) {
          Resource scenarioResource = new ResourceImpl();
          scenarioResource.setContentStream(
              bundleContext.getBundle().getResource(scenarioId + "-policy.xml").openStream());
          if (!registry.resourceExists(resourceUri)) {
            registry.put(resourceUri, scenarioResource);
          }
        }
      }
      if (!transactionStarted) {
        registry.commitTransaction();
      }
    } catch (Exception e) {
      registry.rollbackTransaction();
      throw e;
    }
  }
 public static UserRegistry getConfigSystemRegistry(int tenantId) throws RegistryException {
   return registryService.getConfigSystemRegistry(tenantId);
 }
 public Registry getGovernanceRegistry() throws RegistryException {
   if (registrySvc != null) {
     return registrySvc.getGovernanceSystemRegistry();
   }
   return null;
 }
  private void setupHandlers() {
    if (Boolean.toString(Boolean.TRUE).equals(System.getProperty("disable.event.handlers"))) {
      initialized = true;
      log.debug(
          "Default Eventing Handlers have been disabled. Events will not be "
              + "generated unless a custom handler has been configured.");
      return;
    }
    RegistryService registryService = EventingDataHolder.getInstance().getRegistryService();
    if (registryService instanceof RemoteRegistryService && !initialized) {
      initialized = true;
      log.warn("Eventing is not available on Remote Registry");
      return;
    }
    if (!initialized && listenerManager != null && registryService != null) {
      initialized = true;
      try {
        // We can't get Registry from Utils, as the MessageContext is not available at
        // activation time.
        Registry systemRegistry = registryService.getConfigSystemRegistry();
        if (registry != null && registry == systemRegistry) {
          return;
        }
        registry = systemRegistry;
        if (registry == null
            || registry.getRegistryContext() == null
            || registry.getRegistryContext().getHandlerManager() == null) {
          String msg = "Error Initializing Registry Eventing Handler";
          log.error(msg);
        } else {
          URLMatcher filter = new URLMatcher();
          filter.setDeletePattern(".*");
          filter.setPutPattern(".*");
          filter.setPutChildPattern(".*");
          filter.setMovePattern(".*");
          filter.setCopyPattern(".*");
          filter.setRenamePattern(".*");
          filter.setCreateVersionPattern(".*");
          filter.setApplyTagPattern(".*");
          filter.setRemoveTagPattern(".*");
          filter.setAddCommentPattern(".*");
          filter.setAddAssociationPattern(".*");
          filter.setRemoveAssociationPattern(".*");
          filter.setRateResourcePattern(".*");
          filter.setCreateLinkPattern(".*");
          filter.setRemoveLinkPattern(".*");
          filter.setRestorePattern(".*");
          RegistryEventingHandler handler = new RegistryEventingHandler();
          registry
              .getRegistryContext()
              .getHandlerManager()
              .addHandler(
                  null, filter, handler, HandlerLifecycleManager.DEFAULT_REPORTING_HANDLER_PHASE);
          registry.setEventingServiceURL(null, endpoint);
          EventingDataHolder.getInstance().setDefaultEventingServiceURL(endpoint);
          log.debug("Successfully Initialized the Registry Eventing Handler");

          /*URLMatcher erbSubManagerMountFilter = new URLMatcher();
          erbSubManagerMountFilter.setPutPattern(
                  eventingRoot + "/.*");
          SubscriptionManagerHandler erbSubManagerMountHanlder =
                  new EmbeddedRegistryBasedSubscriptionManagerMountHandler();
          registry.getRegistryContext().getHandlerManager().addHandler(null,
                  erbSubManagerMountFilter, erbSubManagerMountHanlder);
          erbSubManagerMountHanlder.init(registry.getRegistryContext(), eventingRoot);
          log.debug("Successfully Initialized the Subscription Manager Mount Handler");*/

          URLMatcher erbSubManagerRRFilter = new URLMatcher();
          erbSubManagerRRFilter.setCopyPattern(".*");
          erbSubManagerRRFilter.setRenamePattern(".*");
          erbSubManagerRRFilter.setMovePattern(".*");
          erbSubManagerRRFilter.setDeletePattern(".*");
          SubscriptionManagerHandler erbSubManagerRRHanlder =
              new EmbeddedRegistryBasedSubscriptionManagerResourceRelocateHandler();
          registry
              .getRegistryContext()
              .getHandlerManager()
              .addHandler(
                  null,
                  erbSubManagerRRFilter,
                  erbSubManagerRRHanlder,
                  HandlerLifecycleManager.DEFAULT_REPORTING_HANDLER_PHASE);
          erbSubManagerRRHanlder.init(registry.getRegistryContext(), eventingRoot);
          log.debug("Successfully Initialized the Subscription Manager Resource Relocate Handler");
          log.info("Successfully Initialized Eventing on Registry");
        }
      } catch (Exception e) {
        String msg = "Error Initializing Eventing on Registry";
        log.error(msg, e);
        throw new RuntimeException(msg, e);
      }
    }
  }
 protected void unsetRegistryService(RegistryService registryService) {
   if (registryService != null && registryService.equals(Utils.getRegistryService())) {
     Utils.setRegistryService(null);
   }
 }