Skip to content

Commit

Permalink
Restrict HTTP redirects to max 10 attempts
Browse files Browse the repository at this point in the history
  • Loading branch information
vknaisl committed Dec 11, 2019
1 parent 35585ea commit ffe3777
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package nl.dtls.adminpanel.entity.exception;

public class HttpRedirectException extends Exception {

public HttpRedirectException(String message) {
super(message);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@ public void dispose(Pipeline pipeline) throws IOException {
}

private void createDirectory(Pipeline pipeline) throws IOException {
log(pipeline, "1. Creating directory - started");
log(pipeline, "1. Create directory - started");
Instance instance = pipeline.getInstance();
Server server = instance.getServer();
String createDirCommand = format("mkdir %s", instance.getPath());
sshService.ssh(server, createDirCommand);
log(pipeline, "1. Creating directory - ended");
log(pipeline, "1. Create directory - ended");
}

private void copyBinaryFiles(Pipeline pipeline) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import nl.dtls.adminpanel.database.repository.instance.InstanceRepository;
import nl.dtls.adminpanel.database.repository.server.ServerRepository;
import nl.dtls.adminpanel.entity.application.Application;
import nl.dtls.adminpanel.entity.exception.HttpRedirectException;
import nl.dtls.adminpanel.entity.exception.ValidationException;
import nl.dtls.adminpanel.entity.instance.Instance;
import nl.dtls.adminpanel.entity.instance.InstanceStatus;
Expand All @@ -32,6 +33,8 @@
@Service
public class InstanceService {

private static final int MAX_HTTP_REDIRECT_ATTEMPTS = 10;

@Autowired
private InstanceRepository instanceRepository;

Expand Down Expand Up @@ -126,24 +129,32 @@ public boolean deleteInstance(String uuid) {

private InstanceStatus computeInstanceStatus(Instance instance) {
try {
doHttpCall(instance.getUrl());
doHttpCall(instance.getUrl(), 0);
return InstanceStatus.RUNNING;
} catch (ResourceAccessException e) {
} catch (ResourceAccessException | HttpClientErrorException.NotFound e) {
return InstanceStatus.NOT_RUNNING;
} catch (HttpRedirectException e) {
log.info("Instance {} ({}, status: {})", instance.getUrl(),
e.getMessage(), InstanceStatus.ERROR);
return InstanceStatus.ERROR;
} catch (HttpClientErrorException e) {
log.info("Instance {} (http: {}, status: {})", instance.getUrl(),
e.getStatusCode().toString(), InstanceStatus.ERROR);
return InstanceStatus.ERROR;
}
}

private ResponseEntity<String> doHttpCall(String url) {
private ResponseEntity<String> doHttpCall(String url, int attempts)
throws HttpRedirectException {
if (attempts >= MAX_HTTP_REDIRECT_ATTEMPTS) {
throw new HttpRedirectException("Too many HTTP redirect attempts");
}
ResponseEntity<String> response = restTemplate.getForEntity(url, String.class);
if (response.getStatusCode() == HttpStatus.FOUND
|| response.getStatusCode() == HttpStatus.MOVED_PERMANENTLY) {
List<String> locations = response.getHeaders().get(HttpHeaders.LOCATION);
if (locations != null && locations.size() == 1) {
return doHttpCall(locations.get(0));
return doHttpCall(locations.get(0), attempts + 1);
}
}
return response;
Expand Down

0 comments on commit ffe3777

Please sign in to comment.