From a5d8fc114cfab44169c8d6d9c290b315b0d8697e Mon Sep 17 00:00:00 2001 From: "Anand Babu (AB) Periasamy" Date: Thu, 12 Mar 2015 18:18:04 -0700 Subject: [PATCH] s3 generic validator and some improvements to httptrace --- common.go | 14 ++++----- pkg/s3/client.go | 3 +- pkg/s3/httptrace.go | 26 +++++++++++++---- pkg/s3/s3-verify.go | 70 +++++++++++++++++++++++++++++++++++++++++++++ s3-trace.go | 7 +++-- 5 files changed, 102 insertions(+), 18 deletions(-) create mode 100644 pkg/s3/s3-verify.go diff --git a/common.go b/common.go index f1d1924e..b7965400 100644 --- a/common.go +++ b/common.go @@ -66,7 +66,7 @@ func getBashCompletion() { // NewClient - get new client func getNewClient(c *cli.Context) (*s3.Client, error) { - var s3client *s3.Client + var client *s3.Client config, err := getMcConfig() if err != nil { @@ -75,16 +75,14 @@ func getNewClient(c *cli.Context) (*s3.Client, error) { switch c.GlobalBool("debug") { case true: - traceTransport := s3.RoundTripTrace{ - Trace: s3Trace{BodyTraceFlag: false, RequestTransportFlag: true, Writer: nil}, - Transport: http.DefaultTransport, - } - s3client = s3.GetNewClient(&config.S3.Auth, traceTransport) + trace := s3Trace{BodyTraceFlag: false, RequestTransportFlag: true, Writer: nil} + traceTransport := s3.GetNewTraceTransport(trace, http.DefaultTransport) + client = s3.GetNewClient(&config.S3.Auth, traceTransport) case false: - s3client = s3.GetNewClient(&config.S3.Auth, nil) + client = s3.GetNewClient(&config.S3.Auth, nil) } - return s3client, nil + return client, nil } // Parse global options diff --git a/pkg/s3/client.go b/pkg/s3/client.go index 16cf0d8d..11d95de8 100644 --- a/pkg/s3/client.go +++ b/pkg/s3/client.go @@ -94,7 +94,8 @@ type Client struct { func GetNewClient(auth *Auth, transport http.RoundTripper) *Client { return &Client{ Auth: auth, - Transport: transport, + Transport: GetNewTraceTransport(s3Verify{}, transport), + // Transport: transport, } } diff --git a/pkg/s3/httptrace.go b/pkg/s3/httptrace.go index e1a008ac..fb26508e 100644 --- a/pkg/s3/httptrace.go +++ b/pkg/s3/httptrace.go @@ -23,8 +23,8 @@ import ( // HTTPTracer provides callback hook mechanism for HTTP transport. type HTTPTracer interface { - Request(req *http.Request) - Response(res *http.Response) + Request(req *http.Request) error + Response(res *http.Response) error } // RoundTripTrace interposes HTTP transport requests and respsonses using HTTPTracer hooks @@ -36,16 +36,30 @@ type RoundTripTrace struct { // RoundTrip executes user provided request and response hooks for each HTTP call. func (t RoundTripTrace) RoundTrip(req *http.Request) (res *http.Response, err error) { if t.Trace != nil { - t.Trace.Request(req) + err = t.Trace.Request(req) + if err != nil { + return nil, err + } } - if t.Transport != nil { - res, err = t.Transport.RoundTrip(req) - } else { + + if t.Transport == nil { return nil, errors.New("TraceRoundTrip.Transport is nil") } + res, err = t.Transport.RoundTrip(req) + if err != nil { + return res, err + } + if t.Trace != nil { t.Trace.Response(res) } + return res, err } + +// GetNewTraceTransport returns a traceable transport +func GetNewTraceTransport(trace HTTPTracer, transport http.RoundTripper) RoundTripTrace { + return RoundTripTrace{Trace: trace, + Transport: transport} +} diff --git a/pkg/s3/s3-verify.go b/pkg/s3/s3-verify.go new file mode 100644 index 00000000..f5262786 --- /dev/null +++ b/pkg/s3/s3-verify.go @@ -0,0 +1,70 @@ +/* + * Mini Object Storage, (C) 2015 Minio, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package s3 + +import ( + "errors" + "net/http" + "strings" +) + +// Only generic HTTP S3 fields are validated using this mechanism. S3 +// command specific checks should be added to their own appropriate +// files. +type s3Verify struct { +} + +/* +Example AWS S3 Request / Response +================================= + $ mc --debug ls s3://zek + GET /?max-keys=1000 HTTP/1.1 + Host: zek.s3.amazonaws.com + User-Agent: Minio Client + Authorization: AWS **PASSWORD**STRIPPED** + Date: Thu, 12 Mar 2015 23:04:23 GMT + Accept-Encoding: gzip + + HTTP/1.1 200 OK + Transfer-Encoding: chunked + Content-Type: application/xml + Date: Thu, 12 Mar 2015 23:04:24 GMT + Server: AmazonS3 + X-Amz-Id-2: NFc3FTxx4dD+z348qOv532aIaq4VufRnZEnq2kGOloTKhHYhBBZ6qe8mcmZcR+L3Us4N5j0WNtk= + X-Amz-Request-Id: 5E9A737908F886D0 + + 2015-03-11 01:28:03 -0700 PDT 1.10 KB color.go + 2015-03-07 15:53:44 -0800 PST 2.58 KB donut.go + 2015-03-08 03:31:50 -0700 PDT 11.09 KB apache/license.txt +*/ + +// HTTP S3 request validator. +func (t s3Verify) Request(req *http.Request) error { + + if req.Header.Get("Authorization") == "" { + return errors.New("Client request header has authorization key") + } + if !strings.HasPrefix(req.Header.Get("Authorization"), "AWS") { + return errors.New("Client request header has malformed authorization key") + } + return nil +} + +// HTTP S3 response validator. +func (t s3Verify) Response(res *http.Response) error { + return nil +} diff --git a/s3-trace.go b/s3-trace.go index 0ae394cd..395ff628 100644 --- a/s3-trace.go +++ b/s3-trace.go @@ -30,7 +30,7 @@ type s3Trace struct { } // Trace HTTP Request -func (t s3Trace) Request(req *http.Request) { +func (t s3Trace) Request(req *http.Request) (err error) { origAuthKey := req.Header.Get("Authorization") req.Header.Set("Authorization", "AWS **PASSWORD**STRIPPED**") @@ -47,15 +47,16 @@ func (t s3Trace) Request(req *http.Request) { } req.Header.Set("Authorization", origAuthKey) + return err } // Trace HTTP Response -func (t s3Trace) Response(res *http.Response) { - +func (t s3Trace) Response(res *http.Response) (err error) { resTrace, err := httputil.DumpResponse(res, t.BodyTraceFlag) if err == nil { t.print(resTrace) } + return err } // Trace HTTP Response