Search in sources :

Example 1 with AddOnCollection

use of org.zaproxy.zap.control.AddOnCollection in project zaproxy by zaproxy.

the class ExtensionAutoUpdate method uninstallAddOns.

/**
     * Uninstalls the specified add-ons
     * @param addons The identifiers of the add-ons to be installed
     * @return A string containing any error messages, will be empty if there were no problems
     */
public synchronized String uninstallAddOns(List<String> addons) {
    StringBuilder errorMessages = new StringBuilder();
    AddOnCollection aoc = this.getLocalVersionInfo();
    if (aoc == null) {
        String error = Constant.messages.getString("cfu.cmdline.nocfu");
        errorMessages.append(error);
        CommandLine.error(error);
    } else {
        for (String aoName : addons) {
            AddOn ao = aoc.getAddOn(aoName);
            if (ao == null) {
                String error = MessageFormat.format(Constant.messages.getString("cfu.cmdline.noaddon"), aoName);
                errorMessages.append(error);
                errorMessages.append("\n");
                CommandLine.error(error);
                continue;
            }
            AddOnDependencyChecker addOnDependencyChecker = new AddOnDependencyChecker(getLocalVersionInfo(), aoc);
            Set<AddOn> addonSet = new HashSet<AddOn>();
            addonSet.add(ao);
            UninstallationResult result = addOnDependencyChecker.calculateUninstallChanges(addonSet);
            // Check to see if other add-ons depend on it
            if (result.getUninstallations().size() > 1) {
                // Will always report this add-on as needing to be uninstalled
                // Remove the specified add-on for the error message
                result.getUninstallations().remove(ao);
                String error = MessageFormat.format(Constant.messages.getString("cfu.cmdline.addonuninst.uninstalls.required"), result.getUninstallations());
                errorMessages.append(error);
                errorMessages.append("\n");
                CommandLine.error(error);
                continue;
            }
            if (this.uninstallAddOn(null, ao, false)) {
                CommandLine.info(MessageFormat.format(Constant.messages.getString("cfu.cmdline.uninstallok"), aoName));
            } else {
                String error = MessageFormat.format(Constant.messages.getString("cfu.cmdline.uninstallfail"), aoName);
                errorMessages.append(error);
                errorMessages.append("\n");
                CommandLine.error(error);
            }
        }
    }
    return errorMessages.toString();
}
Also used : AddOn(org.zaproxy.zap.control.AddOn) AddOnCollection(org.zaproxy.zap.control.AddOnCollection) UninstallationResult(org.zaproxy.zap.extension.autoupdate.AddOnDependencyChecker.UninstallationResult) HashSet(java.util.HashSet)

Example 2 with AddOnCollection

use of org.zaproxy.zap.control.AddOnCollection in project zaproxy by zaproxy.

the class ExtensionAutoUpdate method installAddOns.

/**
     * Installs the specified add-ons
     * @param addons The identifiers of the add-ons to be installed
     * @return A string containing any error messages, will be empty if there were no problems
     */
public synchronized String installAddOns(List<String> addons) {
    StringBuilder errorMessages = new StringBuilder();
    AddOnCollection aoc = getLatestVersionInfo();
    if (aoc == null) {
        String error = Constant.messages.getString("cfu.cmdline.nocfu");
        errorMessages.append(error);
        CommandLine.error(error);
    } else {
        for (String aoName : addons) {
            AddOn ao = aoc.getAddOn(aoName);
            if (ao == null) {
                String error = MessageFormat.format(Constant.messages.getString("cfu.cmdline.noaddon"), aoName);
                errorMessages.append(error);
                errorMessages.append("\n");
                CommandLine.error(error);
                continue;
            }
            AddOnDependencyChecker addOnDependencyChecker = new AddOnDependencyChecker(getLocalVersionInfo(), aoc);
            AddOnDependencyChecker.AddOnChangesResult result;
            // Check to see if its already installed
            AddOn iao = getLocalVersionInfo().getAddOn(aoName);
            if (iao != null) {
                if (!ao.isUpdateTo(iao)) {
                    CommandLine.info(MessageFormat.format(Constant.messages.getString("cfu.cmdline.addoninst"), iao.getFile().getAbsolutePath()));
                    continue;
                }
                result = addOnDependencyChecker.calculateUpdateChanges(ao);
            } else {
                result = addOnDependencyChecker.calculateInstallChanges(ao);
            }
            if (!result.getUninstalls().isEmpty()) {
                String error = MessageFormat.format(Constant.messages.getString("cfu.cmdline.addoninst.uninstalls.required"), result.getUninstalls());
                errorMessages.append(error);
                errorMessages.append("\n");
                CommandLine.error(error);
                continue;
            }
            Set<AddOn> allAddOns = new HashSet<>();
            allAddOns.addAll(result.getInstalls());
            allAddOns.addAll(result.getNewVersions());
            for (AddOn addOn : allAddOns) {
                CommandLine.info(MessageFormat.format(Constant.messages.getString("cfu.cmdline.addonurl"), addOn.getUrl()));
            }
            processAddOnChanges(null, result);
        }
        waitAndInstallDownloads();
    }
    return errorMessages.toString();
}
Also used : AddOnChangesResult(org.zaproxy.zap.extension.autoupdate.AddOnDependencyChecker.AddOnChangesResult) AddOn(org.zaproxy.zap.control.AddOn) AddOnCollection(org.zaproxy.zap.control.AddOnCollection) HashSet(java.util.HashSet)

Example 3 with AddOnCollection

use of org.zaproxy.zap.control.AddOnCollection in project zaproxy by zaproxy.

the class ExtensionAutoUpdate method getLatestVersionInfo.

protected AddOnCollection getLatestVersionInfo(final CheckForUpdateCallback callback) {
    if (latestVersionInfo == null) {
        if (this.remoteCallThread == null || !this.remoteCallThread.isAlive()) {
            this.remoteCallThread = new Thread() {

                @Override
                public void run() {
                    // Using a thread as the first call could timeout
                    // and we dont want the ui to hang in the meantime
                    this.setName("ZAP-cfu");
                    String shortUrl;
                    String longUrl;
                    if (Constant.isDevBuild()) {
                        shortUrl = ZAP_VERSIONS_DEV_XML_SHORT;
                        longUrl = ZAP_VERSIONS_DEV_XML_FULL;
                    } else if (Constant.isDailyBuild()) {
                        shortUrl = ZAP_VERSIONS_WEEKLY_XML_SHORT;
                        longUrl = ZAP_VERSIONS_DEV_XML_FULL;
                    } else {
                        shortUrl = ZAP_VERSIONS_REL_XML_SHORT;
                        longUrl = ZAP_VERSIONS_REL_XML_FULL;
                    }
                    logger.debug("Getting latest version info from " + shortUrl);
                    try {
                        latestVersionInfo = new AddOnCollection(getRemoteConfigurationUrl(shortUrl), getPlatform(), false);
                    } catch (Exception e1) {
                        logger.debug("Failed to access " + shortUrl, e1);
                        logger.debug("Getting latest version info from " + longUrl);
                        try {
                            latestVersionInfo = new AddOnCollection(getRemoteConfigurationUrl(longUrl), getPlatform(), false);
                        } catch (SSLHandshakeException e2) {
                            if (callback != null) {
                                callback.insecureUrl(longUrl, e2);
                            }
                        } catch (InvalidCfuUrlException e2) {
                            if (callback != null) {
                                callback.insecureUrl(longUrl, e2);
                            }
                        } catch (Exception e2) {
                            logger.debug("Failed to access " + longUrl, e2);
                        }
                    }
                    if (callback != null && latestVersionInfo != null) {
                        logger.debug("Calling callback with  " + latestVersionInfo);
                        callback.gotLatestData(latestVersionInfo);
                    }
                    logger.debug("Done");
                }
            };
            this.remoteCallThread.start();
        }
        if (callback == null) {
            // Synchronous, but include a 30 sec max anyway
            int i = 0;
            while (latestVersionInfo == null && this.remoteCallThread.isAlive() && i < 30) {
                try {
                    Thread.sleep(1000);
                    i++;
                } catch (InterruptedException e) {
                // Ignore
                }
            }
        }
    }
    return latestVersionInfo;
}
Also used : AddOnCollection(org.zaproxy.zap.control.AddOnCollection) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ConfigurationException(org.apache.commons.configuration.ConfigurationException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) SSLHandshakeException(javax.net.ssl.SSLHandshakeException)

Example 4 with AddOnCollection

use of org.zaproxy.zap.control.AddOnCollection in project zaproxy by zaproxy.

the class ExtensionAutoUpdate method execute.

@Override
public void execute(CommandLineArgument[] args) {
    if (arguments[ARG_CFU_UPDATE_IDX].isEnabled()) {
        AddOnCollection aoc = getLatestVersionInfo();
        // Create some temporary options with the settings we need
        OptionsParamCheckForUpdates options = new OptionsParamCheckForUpdates();
        options.load(new XMLPropertiesConfiguration());
        options.setCheckOnStart(true);
        options.setCheckAddonUpdates(true);
        options.setInstallAddonUpdates(true);
        checkForAddOnUpdates(aoc, options);
        waitAndInstallDownloads();
        CommandLine.info(Constant.messages.getString("cfu.cmdline.updated"));
    }
    if (arguments[ARG_CFU_INSTALL_ALL_IDX].isEnabled()) {
        AddOnCollection aoc = getLatestVersionInfo();
        if (aoc == null) {
            CommandLine.error(Constant.messages.getString("cfu.cmdline.nocfu"));
        } else {
            AddOnDependencyChecker addOnDependencyChecker = new AddOnDependencyChecker(getLocalVersionInfo(), aoc);
            AddOnDependencyChecker.AddOnChangesResult result;
            AddOnDependencyChecker.AddOnChangesResult allResults = null;
            Set<AddOn> allAddOns = new HashSet<>();
            for (AddOn ao : aoc.getAddOns()) {
                if (ao.getId().equals("coreLang") && (Constant.isDevBuild() || Constant.isDailyBuild())) {
                    // this may well be missing strings that are now necessary
                    continue;
                }
                // Check to see if its already installed
                AddOn iao = getLocalVersionInfo().getAddOn(ao.getId());
                if (iao != null) {
                    if (!ao.isUpdateTo(iao)) {
                        continue;
                    }
                    result = addOnDependencyChecker.calculateUpdateChanges(ao);
                } else {
                    result = addOnDependencyChecker.calculateInstallChanges(ao);
                }
                if (result.getUninstalls().isEmpty()) {
                    allAddOns.addAll(result.getInstalls());
                    allAddOns.addAll(result.getNewVersions());
                    if (allResults == null) {
                        allResults = result;
                    } else {
                        allResults.addResults(result);
                    }
                }
            }
            if (allAddOns.isEmpty()) {
                // Nothing to do
                return;
            }
            for (AddOn addOn : allAddOns) {
                CommandLine.info(MessageFormat.format(Constant.messages.getString("cfu.cmdline.addonurl"), addOn.getUrl()));
            }
            processAddOnChanges(null, allResults);
            waitAndInstallDownloads();
        }
    }
    if (arguments[ARG_CFU_INSTALL_IDX].isEnabled()) {
        Vector<String> params = arguments[ARG_CFU_INSTALL_IDX].getArguments();
        installAddOns(params);
    }
    if (arguments[ARG_CFU_UNINSTALL_IDX].isEnabled()) {
        Vector<String> params = arguments[ARG_CFU_UNINSTALL_IDX].getArguments();
        uninstallAddOns(params);
    }
    if (arguments[ARG_CFU_LIST_IDX].isEnabled()) {
        AddOnCollection aoc = this.getLocalVersionInfo();
        List<AddOn> aolist = new ArrayList<AddOn>(aoc.getAddOns());
        Collections.sort(aolist, new Comparator<AddOn>() {

            @Override
            public int compare(AddOn ao1, AddOn ao2) {
                return ao1.getName().compareTo(ao2.getName());
            }
        });
        for (AddOn addon : aolist) {
            CommandLine.info(addon.getName() + "\t" + addon.getId() + "\tv" + addon.getFileVersion() + "\t" + addon.getStatus().name() + "\t" + addon.getDescription());
        }
    }
}
Also used : AddOn(org.zaproxy.zap.control.AddOn) ArrayList(java.util.ArrayList) XMLPropertiesConfiguration(org.apache.commons.configuration.XMLPropertiesConfiguration) AddOnChangesResult(org.zaproxy.zap.extension.autoupdate.AddOnDependencyChecker.AddOnChangesResult) AddOnCollection(org.zaproxy.zap.control.AddOnCollection) HashSet(java.util.HashSet)

Aggregations

AddOnCollection (org.zaproxy.zap.control.AddOnCollection)4 HashSet (java.util.HashSet)3 AddOn (org.zaproxy.zap.control.AddOn)3 AddOnChangesResult (org.zaproxy.zap.extension.autoupdate.AddOnDependencyChecker.AddOnChangesResult)2 IOException (java.io.IOException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 MalformedURLException (java.net.MalformedURLException)1 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)1 ArrayList (java.util.ArrayList)1 SSLHandshakeException (javax.net.ssl.SSLHandshakeException)1 ConfigurationException (org.apache.commons.configuration.ConfigurationException)1 XMLPropertiesConfiguration (org.apache.commons.configuration.XMLPropertiesConfiguration)1 UninstallationResult (org.zaproxy.zap.extension.autoupdate.AddOnDependencyChecker.UninstallationResult)1