Search in sources :

Example 1 with UnirestInstance

use of kong.unirest.UnirestInstance in project dependency-track by DependencyTrack.

the class KennaSecurityUploader method upload.

@Override
public void upload(final InputStream payload) {
    LOGGER.debug("Uploading payload to KennaSecurity");
    final ConfigProperty tokenProperty = qm.getConfigProperty(KENNA_TOKEN.getGroupName(), KENNA_TOKEN.getPropertyName());
    try {
        final UnirestInstance ui = UnirestFactory.getUnirestInstance();
        final String token = DataEncryption.decryptAsString(tokenProperty.getPropertyValue());
        final HttpRequestWithBody request = ui.post(String.format(CONNECTOR_UPLOAD_URL, connectorId));
        final HttpResponse<JsonNode> response = request.header("X-Risk-Token", token).header("accept", "application/json").field("file", payload, ContentType.APPLICATION_JSON, "findings.json").field("run", "true").asJson();
        if (response.getStatus() == 200 && response.getBody() != null) {
            final JSONObject root = response.getBody().getObject();
            if (root.getString("success").equals("true")) {
                LOGGER.debug("Successfully uploaded KDI");
                return;
            }
            LOGGER.warn("An unexpected response was received uploading findings to Kenna Security");
        } else {
            LOGGER.warn("Kenna uploader did not receive expected response while attempting to upload " + "Dependency-Track findings. HTTP response code: " + response.getStatus() + " - " + response.getStatusText());
            handleUnexpectedHttpResponse(LOGGER, request.getUrl(), response.getStatus(), response.getStatusText());
        }
    } catch (Exception e) {
        LOGGER.error("An error occurred attempting to upload findings to Kenna Security", e);
    }
}
Also used : UnirestInstance(kong.unirest.UnirestInstance) JSONObject(kong.unirest.json.JSONObject) HttpRequestWithBody(kong.unirest.HttpRequestWithBody) ConfigProperty(alpine.model.ConfigProperty) JsonNode(kong.unirest.JsonNode)

Example 2 with UnirestInstance

use of kong.unirest.UnirestInstance in project dependency-track by DependencyTrack.

the class AbstractWebhookPublisher method publish.

public void publish(final String publisherName, final PebbleTemplate template, final Notification notification, final JsonObject config) {
    final Logger logger = Logger.getLogger(this.getClass());
    logger.debug("Preparing to publish notification");
    if (config == null) {
        logger.warn("No configuration found. Skipping notification.");
        return;
    }
    final String destination = config.getString("destination");
    final String content = prepareTemplate(notification, template);
    if (destination == null || content == null) {
        logger.warn("A destination or template was not found. Skipping notification");
        return;
    }
    final UnirestInstance ui = UnirestFactory.getUnirestInstance();
    final HttpResponse<JsonNode> response = ui.post(destination).header("content-type", "application/json").header("accept", "application/json").body(content).asJson();
    if (response.getStatus() < 200 || response.getStatus() > 299) {
        logger.error("An error was encountered publishing notification to " + publisherName);
        logger.error("HTTP Status : " + response.getStatus() + " " + response.getStatusText());
        logger.error("Destination: " + destination);
        logger.debug(content);
    }
}
Also used : UnirestInstance(kong.unirest.UnirestInstance) JsonNode(kong.unirest.JsonNode) Logger(alpine.common.logging.Logger)

Example 3 with UnirestInstance

use of kong.unirest.UnirestInstance in project dependency-track by DependencyTrack.

the class OssIndexAnalysisTask method submit.

/**
 * Submits the payload to the Sonatype OSS Index service
 */
private List<ComponentReport> submit(final JSONObject payload) throws UnirestException {
    final UnirestInstance ui = UnirestFactory.getUnirestInstance();
    final HttpRequestWithBody request = ui.post(API_BASE_URL).header(HttpHeaders.ACCEPT, "application/json").header(HttpHeaders.CONTENT_TYPE, "application/json").header(HttpHeaders.USER_AGENT, ManagedHttpClientFactory.getUserAgent());
    if (apiUsername != null && apiToken != null) {
        request.basicAuth(apiUsername, apiToken);
    }
    final HttpResponse<JsonNode> jsonResponse = request.body(payload).asJson();
    if (jsonResponse.getStatus() == 200) {
        final OssIndexParser parser = new OssIndexParser();
        return parser.parse(jsonResponse.getBody());
    } else {
        handleUnexpectedHttpResponse(LOGGER, API_BASE_URL, jsonResponse.getStatus(), jsonResponse.getStatusText());
    }
    return new ArrayList<>();
}
Also used : UnirestInstance(kong.unirest.UnirestInstance) HttpRequestWithBody(kong.unirest.HttpRequestWithBody) ArrayList(java.util.ArrayList) JsonNode(kong.unirest.JsonNode) OssIndexParser(org.dependencytrack.parser.ossindex.OssIndexParser)

Example 4 with UnirestInstance

use of kong.unirest.UnirestInstance in project dependency-track by DependencyTrack.

the class CargoMetaAnalyzer method analyze.

/**
 * {@inheritDoc}
 */
public MetaModel analyze(final Component component) {
    final UnirestInstance ui = UnirestFactory.getUnirestInstance();
    final MetaModel meta = new MetaModel(component);
    if (component.getPurl() != null) {
        final String url = String.format(baseUrl + API_URL, component.getPurl().getName());
        try {
            final HttpResponse<JsonNode> response = ui.get(url).header("accept", "application/json").asJson();
            if (response.getStatus() == 200) {
                if (response.getBody() != null && response.getBody().getObject() != null) {
                    final JSONObject crate = response.getBody().getObject().optJSONObject("crate");
                    if (crate != null) {
                        final String latest = crate.getString("newest_version");
                        meta.setLatestVersion(latest);
                    }
                    final JSONArray versions = response.getBody().getObject().optJSONArray("versions");
                    if (versions != null) {
                        for (int i = 0; i < versions.length(); i++) {
                            final JSONObject version = versions.getJSONObject(i);
                            final String versionString = version.optString("num");
                            if (meta.getLatestVersion() != null && meta.getLatestVersion().equals(versionString)) {
                                final String publishedTimestamp = version.optString("created_at");
                                try {
                                    meta.setPublishedTimestamp(DateUtil.fromISO8601(publishedTimestamp));
                                } catch (IllegalArgumentException e) {
                                    LOGGER.warn("An error occurred while parsing published time", e);
                                }
                            }
                        }
                    }
                }
            } else {
                handleUnexpectedHttpResponse(LOGGER, url, response.getStatus(), response.getStatusText(), component);
            }
        } catch (UnirestException e) {
            handleRequestException(LOGGER, e);
        }
    }
    return meta;
}
Also used : UnirestInstance(kong.unirest.UnirestInstance) JSONObject(kong.unirest.json.JSONObject) JSONArray(kong.unirest.json.JSONArray) UnirestException(kong.unirest.UnirestException) JsonNode(kong.unirest.JsonNode)

Example 5 with UnirestInstance

use of kong.unirest.UnirestInstance in project dependency-track by DependencyTrack.

the class GitHubAdvisoryMirrorTask method retrieveAdvisories.

private void retrieveAdvisories(final String advisoriesEndCursor) {
    final String queryTemplate = generateQueryTemplate(advisoriesEndCursor);
    final UnirestInstance ui = UnirestFactory.getUnirestInstance();
    final HttpResponse<JsonNode> response = ui.post(GITHUB_GRAPHQL_URL).header("Authorization", "bearer " + accessToken).header("content-type", "application/json").header("accept", "application/json").body(new JSONObject().put("query", queryTemplate)).asJson();
    if (response.getStatus() < 200 || response.getStatus() > 299) {
        LOGGER.error("An error was encountered retrieving advisories");
        LOGGER.error("HTTP Status : " + response.getStatus() + " " + response.getStatusText());
        LOGGER.debug(queryTemplate);
    } else {
        GitHubSecurityAdvisoryParser parser = new GitHubSecurityAdvisoryParser();
        final PageableList pageableList = parser.parse(response.getBody().getObject());
        updateDatasource(pageableList.getAdvisories());
        if (pageableList.isHasNextPage()) {
            retrieveAdvisories(pageableList.getEndCursor());
        }
    }
}
Also used : UnirestInstance(kong.unirest.UnirestInstance) JSONObject(kong.unirest.json.JSONObject) JsonNode(kong.unirest.JsonNode) PageableList(org.dependencytrack.parser.github.graphql.model.PageableList) GitHubSecurityAdvisoryParser(org.dependencytrack.parser.github.graphql.GitHubSecurityAdvisoryParser)

Aggregations

UnirestInstance (kong.unirest.UnirestInstance)45 JsonNode (kong.unirest.JsonNode)18 Test (org.junit.jupiter.api.Test)18 UnirestException (kong.unirest.UnirestException)13 JSONObject (kong.unirest.json.JSONObject)12 HttpRequestWithBody (kong.unirest.HttpRequestWithBody)6 ClientConfig (com.adobe.target.edge.client.ClientConfig)5 ParseException (java.text.ParseException)5 SimpleDateFormat (java.text.SimpleDateFormat)5 DateFormat (java.text.DateFormat)4 Date (java.util.Date)4 JSONArray (kong.unirest.json.JSONArray)4 ConfigurationException (com.seleniumtests.customexception.ConfigurationException)3 HashMap (java.util.HashMap)3 HttpResponse (kong.unirest.HttpResponse)3 Proxy (kong.unirest.Proxy)3 DeliveryRequest (com.adobe.target.delivery.v1.model.DeliveryRequest)2 ClientProxyConfig (com.adobe.target.edge.client.ClientProxyConfig)2 SeleniumTestsContextManager (com.seleniumtests.core.SeleniumTestsContextManager)2 DriverConfig (com.seleniumtests.driver.DriverConfig)2