Skip to content
Merged
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
33 changes: 15 additions & 18 deletions pkg/internal/cyberark/dataupload/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,17 @@ func (mds *mockDataUploadServer) Close() {
func (mds *mockDataUploadServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case apiPathSnapshotLinks:
mds.handlePresignedUpload(w, r)
mds.handleSnapshotLinks(w, r)
return
case "/presigned-upload":
mds.handleUpload(w, r, false)
return
case "/presigned-upload-invalid-json":
mds.handleUpload(w, r, false)
mds.handlePresignedUpload(w, r)
return
default:
w.WriteHeader(http.StatusNotFound)
}
}

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

// An example of a real checksum mismatch error from the AWS API when the
// request body does not match the checksum in the request header.
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>`
const amzExampleChecksumError = `<?xml version="1.0" encoding="UTF-8"?>
<Error>
<Code>BadDigest</Code>
<Message>The SHA256 you specified did not match the calculated checksum.</Message>
<RequestId>THR2V1RX700Z8SC7</RequestId>
<HostId>F0xSC0H93Xs0BlCx6RjasZgrtjNkNB7lF4+yz1AiPQHswpdEoqj3iTgEN8SUWgV2Qm/laPobVIMz9SYTNHqdoA==</HostId>
</Error>`
Copy link
Member Author

Choose a reason for hiding this comment

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

I temporarily modified the client to send mismatching body and request header digest, and this is the error recorded by mitmproxy:
image


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

if invalidJSON {
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"url":`)) // invalid JSON
return
}
Copy link
Member Author

Choose a reason for hiding this comment

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

This wasn't used. I think it's a legacy of some previous version of the mock server.
S3 does not return JSON for PUT requests; either an empty response or an XML error.


amzChecksum := r.Header.Get("X-Amz-Checksum-Sha256")
if amzChecksum == "" {
http.Error(w, "should set x-amz-checksum-sha256 header on all requests", http.StatusInternalServerError)
Expand All @@ -150,11 +146,12 @@ func (mds *mockDataUploadServer) handleUpload(w http.ResponseWriter, r *http.Req
checksum := sha256.New()
_, _ = io.Copy(checksum, r.Body)

// AWS S3 responds with a BadDigest error if the request body has a
// different checksum than the checksum supplied in the request header.
if amzChecksum != base64.StdEncoding.EncodeToString(checksum.Sum(nil)) {
w.Header().Set("Content-Type", "application/xml")
http.Error(w, amzExampleChecksumError, http.StatusBadRequest)
}

// AWS S3 responds with an empty body if the PUT succeeds
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"success":true}`))
}