Skip to content

Commit 914974d

Browse files
Added custom header to create artifact end point.
1 parent 08c89cf commit 914974d

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

server/src/com/thoughtworks/go/server/controller/ArtifactsController.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.thoughtworks.go.domain.JobIdentifier;
2121
import com.thoughtworks.go.domain.exception.IllegalArtifactLocationException;
2222
import com.thoughtworks.go.server.cache.ZipArtifactCache;
23+
import com.thoughtworks.go.server.security.HeaderConstraint;
2324
import com.thoughtworks.go.server.service.ArtifactsService;
2425
import com.thoughtworks.go.server.service.ConsoleActivityMonitor;
2526
import com.thoughtworks.go.server.service.ConsoleService;
@@ -31,6 +32,7 @@
3132
import com.thoughtworks.go.server.web.FileModelAndView;
3233
import com.thoughtworks.go.server.web.ResponseCodeView;
3334
import com.thoughtworks.go.util.ArtifactLogUtil;
35+
import com.thoughtworks.go.util.SystemEnvironment;
3436
import org.apache.commons.io.IOUtils;
3537
import org.apache.log4j.Logger;
3638
import org.springframework.beans.factory.annotation.Autowired;
@@ -67,10 +69,11 @@ public class ArtifactsController {
6769
private final ArtifactFolderViewFactory folderViewFactory;
6870
private final ArtifactFolderViewFactory jsonViewFactory;
6971
private final ArtifactFolderViewFactory zipViewFactory;
72+
private HeaderConstraint headerConstraint;
7073

7174
@Autowired
7275
ArtifactsController(ArtifactsService artifactsService, RestfulService restfulService, ZipArtifactCache zipArtifactCache,
73-
ConsoleActivityMonitor consoleActivityMonitor, ConsoleService consoleService) {
76+
ConsoleActivityMonitor consoleActivityMonitor, ConsoleService consoleService, SystemEnvironment systemEnvironment) {
7477
this.artifactsService = artifactsService;
7578
this.restfulService = restfulService;
7679
this.consoleActivityMonitor = consoleActivityMonitor;
@@ -79,6 +82,7 @@ public class ArtifactsController {
7982
this.folderViewFactory = FileModelAndView.htmlViewFactory();
8083
this.jsonViewFactory = FileModelAndView.jsonViewfactory();
8184
this.zipViewFactory = zipViewFactory(zipArtifactCache);
85+
this.headerConstraint = new HeaderConstraint(systemEnvironment);
8286
}
8387

8488

@@ -135,6 +139,9 @@ public ModelAndView postArtifact(@RequestParam("pipelineName") String pipelineNa
135139
@RequestParam(value = "attempt", required = false) Integer attempt,
136140
MultipartHttpServletRequest request) throws Exception {
137141
JobIdentifier jobIdentifier;
142+
if(!headerConstraint.isSatisfied(request)) {
143+
return ResponseCodeView.create(HttpServletResponse.SC_BAD_REQUEST, "Missing required header 'Confirm'");
144+
}
138145
try {
139146
jobIdentifier = restfulService.findJob(pipelineName, counterOrLabel, stageName, stageCounter,
140147
buildName, buildId);

server/test/unit/com/thoughtworks/go/server/controller/ArtifactsControllerTest.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,13 @@
2424
import com.thoughtworks.go.server.service.RestfulService;
2525
import com.thoughtworks.go.server.web.ArtifactFolderViewFactory;
2626
import com.thoughtworks.go.server.web.ResponseCodeView;
27+
import com.thoughtworks.go.util.SystemEnvironment;
2728
import org.junit.Before;
2829
import org.junit.Test;
2930
import org.springframework.mock.web.MockHttpServletRequest;
3031
import org.springframework.mock.web.MockMultipartFile;
3132
import org.springframework.mock.web.MockMultipartHttpServletRequest;
33+
import org.springframework.web.multipart.MultipartHttpServletRequest;
3234
import org.springframework.web.servlet.ModelAndView;
3335

3436
import javax.servlet.http.HttpServletResponse;
@@ -52,6 +54,7 @@ public class ArtifactsControllerTest {
5254
private RestfulService restfulService;
5355
private ArtifactsService artifactService;
5456
private ConsoleService consoleService;
57+
private SystemEnvironment systemEnvironment;
5558

5659
@Before
5760
public void setUp() {
@@ -60,8 +63,8 @@ public void setUp() {
6063
restfulService = mock(RestfulService.class);
6164
artifactService = mock(ArtifactsService.class);
6265
consoleService = mock(ConsoleService.class);
63-
64-
artifactsController = new ArtifactsController(artifactService, restfulService, mock(ZipArtifactCache.class), consoleActivityMonitor, consoleService);
66+
systemEnvironment = mock(SystemEnvironment.class);
67+
artifactsController = new ArtifactsController(artifactService, restfulService, mock(ZipArtifactCache.class), consoleActivityMonitor, consoleService, systemEnvironment);
6568

6669
request = new MockHttpServletRequest();
6770
}
@@ -102,7 +105,7 @@ public void shouldReturnHttpErrorCodeWhenChecksumFileSaveFails() throws Exceptio
102105
@Test
103106
public void shouldFunnelAll_GET_calls() throws Exception {
104107
final ModelAndView returnVal = new ModelAndView();
105-
ArtifactsController controller = new ArtifactsController(artifactService, restfulService, mock(ZipArtifactCache.class), consoleActivityMonitor, consoleService) {
108+
ArtifactsController controller = new ArtifactsController(artifactService, restfulService, mock(ZipArtifactCache.class), consoleActivityMonitor, consoleService, systemEnvironment) {
106109
@Override ModelAndView getArtifact(String filePath, ArtifactFolderViewFactory folderViewFactory, String pipelineName, String counterOrLabel, String stageName, String stageCounter,
107110
String buildName, String sha, String serverAlias) throws Exception {
108111
return returnVal;
@@ -113,4 +116,17 @@ public void shouldFunnelAll_GET_calls() throws Exception {
113116
assertThat(controller.getArtifactAsZip("pipeline", "counter", "stage", "2", "job", "file_name", "sha1"), sameInstance(returnVal));
114117
assertThat(controller.getArtifactAsJson("pipeline", "counter", "stage", "2", "job", "file_name", "sha1"), sameInstance(returnVal));
115118
}
119+
120+
@Test
121+
public void shouldReturnBadRequestIfRequiredHeadersAreMissingOnACreateArtifactRequest() throws Exception {
122+
MultipartHttpServletRequest multipartHttpServletRequest = new MockMultipartHttpServletRequest();
123+
124+
when(systemEnvironment.isApiSafeModeEnabled()).thenReturn(true);
125+
ModelAndView modelAndView = artifactsController.postArtifact("pipeline", "invalid-label", "stage", "stage-counter", "job-name", 3L, "file-path", 3, multipartHttpServletRequest);
126+
ResponseCodeView codeView = (ResponseCodeView) modelAndView.getView();
127+
128+
assertThat(codeView.getStatusCode(), is(HttpServletResponse.SC_BAD_REQUEST));
129+
assertThat(codeView.getContent(), is("Missing required header 'Confirm'"));
130+
131+
}
116132
}

0 commit comments

Comments
 (0)