All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.whitesource.config.ConfigurationValidation Maven / Gradle / Ivy

The newest version!
/**
 * The project is licensed under the WhiteSource Software End User License Agreement .
 * Copyright (C) 2015 WhiteSource Ltd.
 * Licensed under the WhiteSource Software End User License Agreement;
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * https://s3.amazonaws.com/unified-agent/LICENSE.txt
 */
package org.whitesource.config;

import org.apache.commons.lang.StringUtils;
import org.whitesource.config.enums.ViaLanguage;
import org.whitesource.utils.Constants;

import java.util.ArrayList;
import java.util.List;

import static org.whitesource.config.utils.ConfigPropertyKeys.*;

/**
 * Author: eugen.horovitz
 */
public class ConfigurationValidation {

    // Check configuration errors
    public List getConfigurationErrors(boolean projectPerFolder, String configProjectToken, String configProjectName, String configApiToken, String configFilePath,
                                               int archiveDepth, String[] includes, String[] projectPerFolderIncludes, String[] pythonIncludes,
                                               String scanComment, boolean useCommandLineRequestFiles, boolean projectNameFromDependencyFile,
                                               boolean isMavenMultiModule, boolean isGradleMultiModule) {
        List errors = new ArrayList<>();
        String[] requirements = pythonIncludes[Constants.ZERO].split(Constants.WHITESPACE);

        if (!useCommandLineRequestFiles) {
            if (StringUtils.isBlank(configApiToken)) {
                String error = "Could not retrieve " + ORG_TOKEN_PROPERTY_KEY + " property from " + configFilePath;
                errors.add(error);
            }

            // boolean parameter to skip the validation of project name and token in case its gradle or maven multi module
            // in this case the project name will be taken from the package manager file
            boolean noProjectToken = StringUtils.isBlank(configProjectToken);
            boolean noProjectName = StringUtils.isBlank(configProjectName);
            if (noProjectToken && noProjectName && !projectPerFolder && !projectNameFromDependencyFile
                        && !isMavenMultiModule && !isGradleMultiModule) {
                    String error = "Could not retrieve properties " + PROJECT_NAME_PROPERTY_KEY + " and " + PROJECT_TOKEN_PROPERTY_KEY + " from " + configFilePath;
                    errors.add(error);
            } else if (!noProjectToken && !noProjectName) {
                String error = "Please choose just one of either " + PROJECT_NAME_PROPERTY_KEY + " or " + PROJECT_TOKEN_PROPERTY_KEY + " (and not both)";
                errors.add(error);
            }
        }
        if (archiveDepth < Constants.ZERO || archiveDepth > Constants.MAX_EXTRACTION_DEPTH) {
            errors.add("Error: archiveExtractionDepth value should be greater than 0 and less than " + Constants.MAX_EXTRACTION_DEPTH);
        }
        if (includes.length < Constants.ONE || StringUtils.isBlank(includes[Constants.ZERO])) {
            errors.add("Error: includes parameter must have at least one scanning pattern");
        }
        if (projectPerFolder && projectPerFolderIncludes == null) {
            errors.add("projectPerFolderIncludes parameter is empty, specify folders to include or mark as comment to scan all folders");
        }

        if (requirements.length > Constants.ZERO) {
            for (String requirement : requirements) {
                if (!requirement.endsWith(Constants.TXT_EXTENSION)) {
                    String error = "Invalid file name: " + requirement + Constants.WHITESPACE + "in property" + PYTHON_REQUIREMENTS_FILE_INCLUDES + "from " + configFilePath;
                    errors.add(error);
                }
            }
        }
        // get user comment & check max valid size
        if (!StringUtils.isBlank(scanComment) && scanComment.length() > Constants.COMMENT_MAX_LENGTH) {
            errors.add("Error: scanComment parameter value should not exceed 1000 characters");
        }
        return errors;
    }


    /**
     * @param iaLanguage language to be analyzed
     * @param enableIA ENABLE_IMPACT_ANALYSIS flag
     * @return list with all errors found
     */
    public List validateIaLanguage(String iaLanguage, boolean enableIA) {
        List errors = new ArrayList<>();

        boolean iaLanguageValid = false;
        if (iaLanguage != null) {
            for (ViaLanguage viaLanguage : ViaLanguage.values()) {
                if (iaLanguage.toLowerCase().equals(viaLanguage.toString().toLowerCase())) {
                    iaLanguageValid = true;
                    break;
                }
            }
            if (!iaLanguageValid) {
                errors.add("Error: VIA setting are not applicable parameters are not valid. exiting... ");
            }
            if (iaLanguageValid && !enableIA) {
                errors.add("Error: VIA setting are not applicable parameters are not valid. exiting... ");
            }
        }

        return errors;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy