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);
}
}
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);
}
}
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<>();
}
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;
}
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());
}
}
}
Aggregations