Example #1
0
  public synchronized List<String> getAllPolicyNames() {
    if (allPolicyNames == null) {
      allPolicyNames = new ArrayList<String>();
      for (String file : Constant.getPoliciesDir().list()) {
        if (file.endsWith(POLICY_EXTENSION)) {
          logger.debug("Found policy file " + file);
          allPolicyNames.add(file.substring(0, file.lastIndexOf(POLICY_EXTENSION)));
        }
      }
      if (allPolicyNames.size() == 0) {
        // No policies :( Create a default one
        ScanPolicy defaultPolicy = new ScanPolicy();
        defaultPolicy.setName(DEFAULT_POLICY_NAME);
        // Load from the 'old' configs
        defaultPolicy.getPluginFactory().loadAllPlugin(extension.getScannerParam().getConfig());
        try {
          // Note this will add the name to allPolicyNames
          this.savePolicy(defaultPolicy);
        } catch (ConfigurationException e) {
          logger.debug(
              "Failed to create default scan policy in "
                  + Constant.getPoliciesDir().getAbsolutePath(),
              e);
        }
      }

      Collections.sort(allPolicyNames);
    }
    return allPolicyNames;
  }
Example #2
0
 public void exportPolicy(ScanPolicy policy, File file) throws ConfigurationException {
   logger.debug("Export policy to " + file.getAbsolutePath());
   ZapXmlConfiguration conf = new ZapXmlConfiguration();
   conf.setProperty("policy", policy.getName());
   conf.setProperty("scanner.level", policy.getDefaultThreshold().name());
   conf.setProperty("scanner.strength", policy.getDefaultStrength().name());
   policy.getPluginFactory().saveTo(conf);
   conf.save(file);
 }
Example #3
0
  private ScanPolicy loadPolicy(File file) throws ConfigurationException {
    ScanPolicy policy = new ScanPolicy(new ZapXmlConfiguration(file));
    if (!file.getName().equals(policy.getName() + POLICY_EXTENSION)) {
      // The file name takes precedence in case theres another policy with the same name
      policy.setName(file.getName().substring(0, file.getName().indexOf(POLICY_EXTENSION)));
    }

    return policy;
  }
Example #4
0
 public void importPolicy(File file) throws ConfigurationException, IOException {
   logger.debug("Import policy from " + file.getAbsolutePath());
   ScanPolicy policy = new ScanPolicy(new ZapXmlConfiguration(file));
   String baseName = file.getName();
   if (baseName.endsWith(POLICY_EXTENSION)) {
     // Stip off the extension for the 'friendly name' and if we need to prevent overwriting an
     // existing one
     baseName = baseName.substring(0, baseName.indexOf(POLICY_EXTENSION));
   }
   String finalName = baseName;
   File newFile = new File(Constant.getPoliciesDir(), finalName + POLICY_EXTENSION);
   int i = 2;
   while (newFile.exists()) {
     finalName = baseName + i;
     newFile = new File(Constant.getPoliciesDir(), finalName + POLICY_EXTENSION);
     i++;
   }
   policy.setName(finalName);
   this.savePolicy(policy);
 }
Example #5
0
  public void savePolicy(ScanPolicy policy, String previousName) throws ConfigurationException {
    logger.debug("Save policy " + policy.getName());

    File file = new File(Constant.getPoliciesDir(), policy.getName() + POLICY_EXTENSION);

    ZapXmlConfiguration conf = new ZapXmlConfiguration();
    conf.setProperty("policy", policy.getName());
    conf.setProperty("scanner.level", policy.getDefaultThreshold().name());
    conf.setProperty("scanner.strength", policy.getDefaultStrength().name());

    policy.getPluginFactory().saveTo(conf);

    conf.save(file);

    if (previousName != null && previousName.length() > 0) {
      allPolicyNames.remove(previousName);
    }
    if (!allPolicyNames.contains(policy.getName())) {
      allPolicyNames.add(policy.getName());
      Collections.sort(allPolicyNames);
    }
  }