Search in sources :

Example 1 with MustExitRuntimeException

use of com.mercedesbenz.sechub.owaspzapwrapper.cli.MustExitRuntimeException in project sechub by mercedes-benz.

the class AbstractScan method resolveParentDirectoryPath.

private String resolveParentDirectoryPath(Path reportFile) {
    if (reportFile == null) {
        throw new MustExitRuntimeException("For scan: " + scanConfig.getContextName() + ". Report file not set.", MustExitCode.REPORT_FILE_ERROR);
    }
    if (Files.isDirectory(reportFile)) {
        throw new MustExitRuntimeException("For scan: " + scanConfig.getContextName() + ". Report file must not be a directory!", MustExitCode.REPORT_FILE_ERROR);
    }
    Path parent = reportFile.getParent();
    Path absolutePath = parent.toAbsolutePath();
    return absolutePath.toString();
}
Also used : Path(java.nio.file.Path) MustExitRuntimeException(com.mercedesbenz.sechub.owaspzapwrapper.cli.MustExitRuntimeException)

Example 2 with MustExitRuntimeException

use of com.mercedesbenz.sechub.owaspzapwrapper.cli.MustExitRuntimeException in project sechub by mercedes-benz.

the class AbstractScan method renameReportFileIfFileExtensionIsNotJSON.

/**
 * This method is used to rename the file back to the specified name in case the
 * file did not end with .json.
 *
 * The reason for this method is that the Owasp Zap appends ".json" to the
 * result file if we generate a report in json format. The PDS result.txt will
 * then be called result.txt.json. Because of this behaviour the file will be
 * renamed.
 */
private void renameReportFileIfFileExtensionIsNotJSON() {
    String specifiedReportFile = scanConfig.getReportFile().toAbsolutePath().toFile().getAbsolutePath();
    // If the Owasp Zap creates the file below, it will be renamed to the originally
    // specified name
    File owaspZapCreatedFile = new File(specifiedReportFile + ".json");
    if (owaspZapCreatedFile.exists()) {
        try {
            Path owaspzapReport = Paths.get(specifiedReportFile + ".json");
            Files.move(owaspzapReport, owaspzapReport.resolveSibling(scanConfig.getReportFile().toAbsolutePath()), StandardCopyOption.REPLACE_EXISTING);
        } catch (IOException e) {
            throw new MustExitRuntimeException("For scan: " + scanConfig.getContextName() + ". An error occurred renaming the report file", e, MustExitCode.REPORT_FILE_ERROR);
        }
    }
}
Also used : Path(java.nio.file.Path) MustExitRuntimeException(com.mercedesbenz.sechub.owaspzapwrapper.cli.MustExitRuntimeException) IOException(java.io.IOException) File(java.io.File)

Example 3 with MustExitRuntimeException

use of com.mercedesbenz.sechub.owaspzapwrapper.cli.MustExitRuntimeException in project sechub by mercedes-benz.

the class OwaspZapScanConfigurationFactory method create.

public OwaspZapScanConfiguration create(CommandLineSettings settings) {
    if (settings == null) {
        throw new MustExitRuntimeException("Command line settings must not be null!", MustExitCode.COMMANDLINE_CONFIGURATION_INVALID);
    }
    /* Wrapper settings */
    OwaspZapServerConfiguration serverConfig = createOwaspZapServerConfig(settings);
    ProxyInformation proxyInformation = createProxyInformation(settings);
    /* SecHub settings */
    URI targetUri = targetUriFactory.create(settings.getTargetURL());
    SecHubWebScanConfiguration sechubWebConfig = webConfigProvider.getSecHubWebConfiguration(settings.getSecHubConfigFile());
    long maxScanDurationInMillis = sechubWebConfigHelper.fetchMaxScanDurationInMillis(sechubWebConfig);
    AuthenticationType authType = sechubWebConfigHelper.determineAuthenticationType(sechubWebConfig);
    /* we always use the SecHub job UUID as OWASP Zap context name */
    String contextName = settings.getJobUUID();
    if (contextName == null) {
        contextName = UUID.randomUUID().toString();
        LOG.warn("The job UUID was not set. Using randomly generated UUID: {} as fallback.", contextName);
    }
    /* @formatter:off */
    OwaspZapScanConfiguration scanConfig = OwaspZapScanConfiguration.builder().setTargetUri(targetUri).setVerboseOutput(settings.isVerboseEnabled()).setReportFile(settings.getReportFile()).setContextName(contextName).setAjaxSpiderEnabled(settings.isAjaxSpiderEnabled()).setActiveScanEnabled(settings.isActiveScanEnabled()).setServerConfig(serverConfig).setAuthenticationType(authType).setMaxScanDurationInMillis(maxScanDurationInMillis).setSecHubWebScanConfiguration(sechubWebConfig).setProxyInformation(proxyInformation).build();
    /* @formatter:on */
    return scanConfig;
}
Also used : SecHubWebScanConfiguration(com.mercedesbenz.sechub.commons.model.SecHubWebScanConfiguration) MustExitRuntimeException(com.mercedesbenz.sechub.owaspzapwrapper.cli.MustExitRuntimeException) URI(java.net.URI) AuthenticationType(com.mercedesbenz.sechub.owaspzapwrapper.config.auth.AuthenticationType)

Example 4 with MustExitRuntimeException

use of com.mercedesbenz.sechub.owaspzapwrapper.cli.MustExitRuntimeException in project sechub by mercedes-benz.

the class TargetConnectionChecker method isTargetReachable.

/**
 * Tests if site is reachable - no matter if certificate is self signed or not
 * trusted!
 *
 * @param targetUri
 * @param proxyInformation
 * @return <code>true</code> when reachable otherwise <code>false</code>
 */
public boolean isTargetReachable(URI targetUri, ProxyInformation proxyInformation) {
    URL urlToCheckConnection;
    try {
        urlToCheckConnection = targetUri.toURL();
    } catch (MalformedURLException e) {
        throw new MustExitRuntimeException("Target URI " + targetUri + " could not be converted to URL!", null);
    }
    TrustManager pseudoTrustManager = createTrustManagerWhichTrustsEveryBody();
    SSLContext sslContext = createSSLContextForTrustManager(pseudoTrustManager);
    try {
        LOG.info("Trying to reach target URL: {}", urlToCheckConnection.toExternalForm());
        HttpURLConnection connection;
        if (proxyInformation == null) {
            connection = (HttpURLConnection) urlToCheckConnection.openConnection();
        } else {
            Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyInformation.getHost(), proxyInformation.getPort()));
            connection = (HttpURLConnection) urlToCheckConnection.openConnection(proxy);
        }
        if (connection instanceof HttpsURLConnection) {
            HttpsURLConnection httpsUrlConnection = (HttpsURLConnection) connection;
            httpsUrlConnection.setSSLSocketFactory(sslContext.getSocketFactory());
            httpsUrlConnection.setHostnameVerifier(new AllowAllHostnameVerifier());
        }
        int responseCode = connection.getResponseCode();
        if (isReponseCodeValid(responseCode)) {
            LOG.info("Target is reachable.");
            return true;
        } else {
            LOG.error("Target is NOT reachable. Aborting Scan...");
        }
    } catch (IOException e) {
        LOG.error("An exception occurred while checking if target URL is reachable: {} because: {}", urlToCheckConnection.toExternalForm(), e.getMessage());
    }
    return false;
}
Also used : MalformedURLException(java.net.MalformedURLException) Proxy(java.net.Proxy) HttpURLConnection(java.net.HttpURLConnection) InetSocketAddress(java.net.InetSocketAddress) MustExitRuntimeException(com.mercedesbenz.sechub.owaspzapwrapper.cli.MustExitRuntimeException) SSLContext(javax.net.ssl.SSLContext) IOException(java.io.IOException) URL(java.net.URL) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) TrustManager(javax.net.ssl.TrustManager) X509TrustManager(javax.net.ssl.X509TrustManager)

Example 5 with MustExitRuntimeException

use of com.mercedesbenz.sechub.owaspzapwrapper.cli.MustExitRuntimeException in project sechub by mercedes-benz.

the class SechubWebConfigProvider method getSecHubWebConfiguration.

public SecHubWebScanConfiguration getSecHubWebConfiguration(File secHubConfigFile) {
    TextFileReader fileReader = new TextFileReader();
    if (secHubConfigFile == null) {
        // can happen when an unauthenticated scan is started with only the target URL
        return new SecHubWebScanConfiguration();
    }
    String sechubConfigJson;
    SecHubScanConfiguration sechubConfig;
    try {
        sechubConfigJson = fileReader.loadTextFile(secHubConfigFile);
        sechubConfig = SecHubScanConfiguration.createFromJSON(sechubConfigJson);
    } catch (IOException e) {
        throw new MustExitRuntimeException("Was not able to read sechub config file: " + secHubConfigFile, e, MustExitCode.SECHUB_CONFIGURATION_INVALID);
    }
    return getSecHubWebConfiguration(sechubConfig);
}
Also used : SecHubWebScanConfiguration(com.mercedesbenz.sechub.commons.model.SecHubWebScanConfiguration) MustExitRuntimeException(com.mercedesbenz.sechub.owaspzapwrapper.cli.MustExitRuntimeException) IOException(java.io.IOException) SecHubScanConfiguration(com.mercedesbenz.sechub.commons.model.SecHubScanConfiguration) TextFileReader(com.mercedesbenz.sechub.commons.TextFileReader)

Aggregations

MustExitRuntimeException (com.mercedesbenz.sechub.owaspzapwrapper.cli.MustExitRuntimeException)6 IOException (java.io.IOException)3 SecHubWebScanConfiguration (com.mercedesbenz.sechub.commons.model.SecHubWebScanConfiguration)2 URI (java.net.URI)2 Path (java.nio.file.Path)2 TextFileReader (com.mercedesbenz.sechub.commons.TextFileReader)1 SecHubScanConfiguration (com.mercedesbenz.sechub.commons.model.SecHubScanConfiguration)1 AuthenticationType (com.mercedesbenz.sechub.owaspzapwrapper.config.auth.AuthenticationType)1 File (java.io.File)1 HttpURLConnection (java.net.HttpURLConnection)1 InetSocketAddress (java.net.InetSocketAddress)1 MalformedURLException (java.net.MalformedURLException)1 Proxy (java.net.Proxy)1 URISyntaxException (java.net.URISyntaxException)1 URL (java.net.URL)1 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)1 SSLContext (javax.net.ssl.SSLContext)1 TrustManager (javax.net.ssl.TrustManager)1 X509TrustManager (javax.net.ssl.X509TrustManager)1