Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#127 Improve Error Handling for Failed DefectDojo API Requests #128

Merged
merged 3 commits into from
Jul 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -124,7 +124,7 @@ public String getFilename() {

final var payload = new HttpEntity<MultiValueMap<String, Object>>(body, headers);
return exchangeRequest(endpoint, payload);
} catch (HttpClientErrorException e) {
} catch (RestClientException e) {
log.error("Exception while attaching findings to engagement: {}", e.getMessage());
throw new PersistenceException("Failed to attach findings to engagement.", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.fasterxml.jackson.databind.cfg.CoercionInputShape;
import io.securecodebox.persistence.defectdojo.config.Config;
import io.securecodebox.persistence.defectdojo.exception.LoopException;
import io.securecodebox.persistence.defectdojo.exception.PersistenceException;
import io.securecodebox.persistence.defectdojo.http.Foo;
import io.securecodebox.persistence.defectdojo.http.ProxyConfigFactory;
import io.securecodebox.persistence.defectdojo.model.Engagement;
Expand All @@ -29,6 +30,7 @@
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

Expand Down Expand Up @@ -73,14 +75,20 @@ public final T get(long id) {

final var url = this.config.getUrl() + API_PREFIX + this.getUrlPath() + "/" + id;
log.debug("Requesting URL: {}", url);
ResponseEntity<T> response = restTemplate.exchange(
url,
HttpMethod.GET,
payload,
getModelClass()
);

return response.getBody();

try {
ResponseEntity<T> response = restTemplate.exchange(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you intentionally only add this for the GET requests?
The error handling for the other http methods seems to be the same

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔No. This means that any error handling is missing on non-GET bc I simply grepped HttpClientErrorException 😬

url,
HttpMethod.GET,
payload,
getModelClass()
);

return response.getBody();
} catch (RestClientException e) {
log.error("Exception while doing a GET request to DefectDojo API: {}", e.getMessage());
throw new PersistenceException("Failed to do a GET to DefectDojo API!", e);
}
}

@Override
Expand Down Expand Up @@ -133,27 +141,53 @@ public final T create(@NonNull T object) {
var restTemplate = this.getRestTemplate();
HttpEntity<T> payload = new HttpEntity<>(object, getDefectDojoAuthorizationHeaders());

ResponseEntity<T> response = restTemplate.exchange(this.config.getUrl() + API_PREFIX + getUrlPath() + "/", HttpMethod.POST, payload, getModelClass());
return response.getBody();
try {
ResponseEntity<T> response = restTemplate.exchange(
this.config.getUrl() + API_PREFIX + getUrlPath() + "/",
HttpMethod.POST,
payload,
getModelClass());
return response.getBody();
} catch (RestClientException e) {
log.error("Exception while doing a POST request to DefectDojo API: {}", e.getMessage());
throw new PersistenceException("Failed to do a POST to DefectDojo API!", e);
}
}

@Override
public final void delete(long id) {
var restTemplate = this.getRestTemplate();
HttpEntity<String> payload = new HttpEntity<>(getDefectDojoAuthorizationHeaders());

restTemplate.exchange(this.config.getUrl() + API_PREFIX + getUrlPath() + "/" + id + "/", HttpMethod.DELETE, payload, String.class);
try {
restTemplate.exchange(
this.config.getUrl() + API_PREFIX + getUrlPath() + "/" + id + "/",
HttpMethod.DELETE,
payload,
String.class);
} catch (RestClientException e) {
log.error("Exception while doing a DELETE request to DefectDojo API: {}", e.getMessage());
throw new PersistenceException("Failed to do a DELETE to DefectDojo API!", e);
}
}

@Override
public final T update(@NonNull T object, long id) {
var restTemplate = this.getRestTemplate();
HttpEntity<T> payload = new HttpEntity<>(object, getDefectDojoAuthorizationHeaders());

ResponseEntity<T> response = restTemplate.exchange(this.config.getUrl() + API_PREFIX + getUrlPath() + "/" + id + "/", HttpMethod.PUT, payload, getModelClass());
return response.getBody();
try {
ResponseEntity<T> response = restTemplate.exchange(this.config.getUrl() + API_PREFIX + getUrlPath() + "/" + id + "/",
HttpMethod.PUT,
payload,
getModelClass());
return response.getBody();
} catch (RestClientException e) {
log.error("Exception while doing a PUT request to DefectDojo API: {}", e.getMessage());
throw new PersistenceException("Failed to do a PUT to DefectDojo API!", e);
}
}

/**
* Get the URL path for the REST endpoint relative to {@link #API_PREFIX}
*
Expand Down Expand Up @@ -212,16 +246,21 @@ protected PaginatedResult<T> internalSearch(Map<String, Object> queryParams, lon
}

var url = new URI(this.config.getUrl() + API_PREFIX + this.getUrlPath() + "/");
log.debug("Requesting URL: " + url);
log.debug("Requesting URL: {}", url);
var uriBuilder = UriComponentsBuilder.fromUri(url).queryParams(multiValueMap);

ResponseEntity<String> responseString = restTemplate.exchange(
uriBuilder.build(mutableQueryParams),
HttpMethod.GET,
payload,
String.class
);

return deserializeList(responseString.getBody());
try {
ResponseEntity<String> responseString = restTemplate.exchange(
uriBuilder.build(mutableQueryParams),
HttpMethod.GET,
payload,
String.class
);

return deserializeList(responseString.getBody());
} catch (RestClientException e) {
log.error("Exception while doing a GET request to DefectDojo API: {}", e.getMessage());
throw new PersistenceException("Failed to do a GET to DefectDojo API!", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -94,8 +94,8 @@ public String getFilename() {
var payload = new HttpEntity<>(mvn, headers);

return restTemplate.exchange(config.getUrl() + "/api/v2/" + endpoint + "/", HttpMethod.POST, payload, ImportScanResponse.class).getBody();
} catch (HttpClientErrorException e) {
throw new PersistenceException("Failed to attach findings to engagement.");
} catch (RestClientException e) {
throw new PersistenceException("Failed to attach findings to engagement.", e);
}
}

Expand Down