Skip to content

Commit 284d0dc

Browse files
author
enguer
committed
add tests s3cmd comments
refactor indentations
1 parent 45546ce commit 284d0dc

File tree

3 files changed

+20
-21
lines changed

3 files changed

+20
-21
lines changed

handler.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
v4 "github.com/aws/aws-sdk-go/aws/signer/v4"
1717
log "github.com/sirupsen/logrus"
1818
)
19+
1920
// - new less strict regexp in order to allow different region naming (compatibility with other providers)
2021
// - east-eu-1 => pass (aws style)
2122
// - gra => pass (ceph style)
@@ -227,12 +228,12 @@ func (h *Handler) buildUpstreamRequest(req *http.Request) (*http.Request, error)
227228
return nil, err
228229
}
229230

230-
// WORKAROUND S3CMD which dont use white space before the some commas in the authorization header
231-
fakeAuthorizationStr := fakeReq.Header.Get("Authorization")
232-
// Sanitize fakeReq to remove white spaces before the comma signature
233-
authorizationStr := strings.Replace(req.Header["Authorization"][0],",Signature",", Signature",1)
234-
// Sanitize fakeReq to remove white spaces before the comma signheaders
235-
authorizationStr = strings.Replace(authorizationStr,",SignedHeaders",", SignedHeaders",1)
231+
// WORKAROUND S3CMD which dont use white space before the some commas in the authorization header
232+
fakeAuthorizationStr := fakeReq.Header.Get("Authorization")
233+
// Sanitize fakeReq to remove white spaces before the comma signature
234+
authorizationStr := strings.Replace(req.Header["Authorization"][0], ",Signature", ", Signature", 1)
235+
// Sanitize fakeReq to remove white spaces before the comma signheaders
236+
authorizationStr = strings.Replace(authorizationStr, ",SignedHeaders", ", SignedHeaders", 1)
236237

237238
// Verify that the fake request and the incoming request have the same signature
238239
// This ensures it was sent and signed by a client with correct AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY

handler_test.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,11 @@ func verifySignature(w http.ResponseWriter, r *http.Request) {
7979
signer.Sign(r, body, "s3", "eu-test-1", signTime)
8080
expectedAuthorization := r.Header["Authorization"][0]
8181

82-
// WORKAROUND S3CMD who dont use white space before the comma in the authorization header
83-
// Sanitize fakeReq to remove white spaces before the comma signature
84-
receivedAuthorization = strings.Replace(receivedAuthorization,",Signature",", Signature",1)
85-
// Sanitize fakeReq to remove white spaces before the comma signheaders
86-
receivedAuthorization = strings.Replace(receivedAuthorization,",SignedHeaders",", SignedHeaders",1)
82+
// WORKAROUND S3CMD who dont use white space before the comma in the authorization header
83+
// Sanitize fakeReq to remove white spaces before the comma signature
84+
receivedAuthorization = strings.Replace(receivedAuthorization, ",Signature", ", Signature", 1)
85+
// Sanitize fakeReq to remove white spaces before the comma signheaders
86+
receivedAuthorization = strings.Replace(receivedAuthorization, ",SignedHeaders", ", SignedHeaders", 1)
8787

8888
// verify signature
8989
fmt.Fprintln(w, receivedAuthorization, expectedAuthorization)
@@ -154,10 +154,14 @@ func TestHandlerValidSignatureS3cmd(t *testing.T) {
154154

155155
req := httptest.NewRequest(http.MethodGet, "http://foobar.example.com", nil)
156156
signRequest(req)
157-
authorizationReq := req.Header.Get("Authorization");
158-
authorizationReq = strings.Replace(authorizationReq,", Signature",",Signature",1)
159-
authorizationReq = strings.Replace(authorizationReq,", SignedHeaders",",SignedHeaders",1)
160-
req.Header.Set("Authorization",authorizationReq);
157+
// get the generated signed authorization header in order to simulate the s3cmd syntax
158+
authorizationReq := req.Header.Get("Authorization")
159+
// simulating s3cmd syntax and remove the whites space after the comma of the Signature part
160+
authorizationReq = strings.Replace(authorizationReq, ", Signature", ",Signature", 1)
161+
// simulating s3cmd syntax and remove the whites space before the comma of the SignedHeaders part
162+
authorizationReq = strings.Replace(authorizationReq, ", SignedHeaders", ",SignedHeaders", 1)
163+
// push the edited authorization header
164+
req.Header.Set("Authorization", authorizationReq)
161165
resp := httptest.NewRecorder()
162166
h.ServeHTTP(resp, req)
163167
assert.Equal(t, 200, resp.Code)

main.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,6 @@ func NewAwsS3ReverseProxy(opts Options) (*Handler, error) {
100100
}
101101
return handler, nil
102102
}
103-
//handle /health path
104-
func health(w http.ResponseWriter, req *http.Request){
105-
fmt.Fprintf(w,"ok")
106-
}
107103
func main() {
108104
opts := NewOptions()
109105
handler, err := NewAwsS3ReverseProxy(opts)
@@ -139,8 +135,6 @@ func main() {
139135
var wrappedHandler http.Handler = handler
140136
if len(opts.MetricsListenAddr) > 0 && len(strings.Split(opts.MetricsListenAddr, ":")) == 2 {
141137
metricsHandler := http.NewServeMux()
142-
//add health on metrics http to serve k8s liveness
143-
metricsHandler.HandleFunc("/health",health)
144138
metricsHandler.Handle("/metrics", promhttp.Handler())
145139

146140
log.Infof("Listening for secure Prometheus metrics on %s", opts.MetricsListenAddr)

0 commit comments

Comments
 (0)