Skip to content

Commit 1209e4b

Browse files
authored
Merge pull request #690 from jetstack/VC-43403-inventory-api-mock-cleanup
[VC-43403] Various improvements to the mock API server
2 parents c73a626 + 4009af7 commit 1209e4b

File tree

1 file changed

+15
-18
lines changed
  • pkg/internal/cyberark/dataupload

1 file changed

+15
-18
lines changed

pkg/internal/cyberark/dataupload/mock.go

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -38,20 +38,17 @@ func (mds *mockDataUploadServer) Close() {
3838
func (mds *mockDataUploadServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
3939
switch r.URL.Path {
4040
case apiPathSnapshotLinks:
41-
mds.handlePresignedUpload(w, r)
41+
mds.handleSnapshotLinks(w, r)
4242
return
4343
case "/presigned-upload":
44-
mds.handleUpload(w, r, false)
45-
return
46-
case "/presigned-upload-invalid-json":
47-
mds.handleUpload(w, r, false)
44+
mds.handlePresignedUpload(w, r)
4845
return
4946
default:
5047
w.WriteHeader(http.StatusNotFound)
5148
}
5249
}
5350

54-
func (mds *mockDataUploadServer) handlePresignedUpload(w http.ResponseWriter, r *http.Request) {
51+
func (mds *mockDataUploadServer) handleSnapshotLinks(w http.ResponseWriter, r *http.Request) {
5552
if r.Method != http.MethodPost {
5653
w.WriteHeader(http.StatusMethodNotAllowed)
5754
_, _ = w.Write([]byte(`{"message":"method not allowed"}`))
@@ -120,9 +117,15 @@ func (mds *mockDataUploadServer) handlePresignedUpload(w http.ResponseWriter, r
120117

121118
// An example of a real checksum mismatch error from the AWS API when the
122119
// request body does not match the checksum in the request header.
123-
const amzExampleChecksumError = `<Error><Code>BadDigest</Code><Message>The SHA256 you specified did not match the calculated checksum.</Message><RequestId>GBDMP09BEZ929YBK</RequestId><HostId>sFTQb9JQpfJY/t+Ctn0anBmp4lKzEGES8ttmfAmFInuJIhvaV/U+20vYaGbdtlEnExZQRV/5xo6RQqq3xItM+px/Q2AEiv1G</HostId></Error>`
120+
const amzExampleChecksumError = `<?xml version="1.0" encoding="UTF-8"?>
121+
<Error>
122+
<Code>BadDigest</Code>
123+
<Message>The SHA256 you specified did not match the calculated checksum.</Message>
124+
<RequestId>THR2V1RX700Z8SC7</RequestId>
125+
<HostId>F0xSC0H93Xs0BlCx6RjasZgrtjNkNB7lF4+yz1AiPQHswpdEoqj3iTgEN8SUWgV2Qm/laPobVIMz9SYTNHqdoA==</HostId>
126+
</Error>`
124127

125-
func (mds *mockDataUploadServer) handleUpload(w http.ResponseWriter, r *http.Request, invalidJSON bool) {
128+
func (mds *mockDataUploadServer) handlePresignedUpload(w http.ResponseWriter, r *http.Request) {
126129
if r.Method != http.MethodPut {
127130
w.WriteHeader(http.StatusMethodNotAllowed)
128131
_, _ = w.Write([]byte(`{"message":"method not allowed"}`))
@@ -134,13 +137,6 @@ func (mds *mockDataUploadServer) handleUpload(w http.ResponseWriter, r *http.Req
134137
return
135138
}
136139

137-
if invalidJSON {
138-
w.WriteHeader(http.StatusOK)
139-
w.Header().Set("Content-Type", "application/json")
140-
_, _ = w.Write([]byte(`{"url":`)) // invalid JSON
141-
return
142-
}
143-
144140
amzChecksum := r.Header.Get("X-Amz-Checksum-Sha256")
145141
if amzChecksum == "" {
146142
http.Error(w, "should set x-amz-checksum-sha256 header on all requests", http.StatusInternalServerError)
@@ -150,11 +146,12 @@ func (mds *mockDataUploadServer) handleUpload(w http.ResponseWriter, r *http.Req
150146
checksum := sha256.New()
151147
_, _ = io.Copy(checksum, r.Body)
152148

149+
// AWS S3 responds with a BadDigest error if the request body has a
150+
// different checksum than the checksum supplied in the request header.
153151
if amzChecksum != base64.StdEncoding.EncodeToString(checksum.Sum(nil)) {
152+
w.Header().Set("Content-Type", "application/xml")
154153
http.Error(w, amzExampleChecksumError, http.StatusBadRequest)
155154
}
156-
155+
// AWS S3 responds with an empty body if the PUT succeeds
157156
w.WriteHeader(http.StatusOK)
158-
w.Header().Set("Content-Type", "application/json")
159-
_, _ = w.Write([]byte(`{"success":true}`))
160157
}

0 commit comments

Comments
 (0)