2015-02-15 03:48:15 -05:00
|
|
|
/*
|
2015-03-19 17:35:50 -04:00
|
|
|
* Minimalist Object Storage, (C) 2015 Minio, Inc.
|
2015-02-15 03:48:15 -05:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2015-03-18 20:49:33 -04:00
|
|
|
package api
|
2015-02-11 06:23:15 -05:00
|
|
|
|
|
|
|
import (
|
2015-04-27 06:54:49 -04:00
|
|
|
"errors"
|
2015-02-11 06:23:15 -05:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
2015-04-27 06:54:49 -04:00
|
|
|
"time"
|
2015-02-11 06:23:15 -05:00
|
|
|
|
2015-03-19 01:49:02 -04:00
|
|
|
"github.com/minio-io/minio/pkg/api/config"
|
2015-02-11 06:23:15 -05:00
|
|
|
)
|
|
|
|
|
2015-04-27 06:54:49 -04:00
|
|
|
type timeHandler struct {
|
|
|
|
handler http.Handler
|
|
|
|
}
|
|
|
|
|
|
|
|
type validateHandler struct {
|
2015-02-11 06:23:15 -05:00
|
|
|
conf config.Config
|
|
|
|
handler http.Handler
|
|
|
|
}
|
|
|
|
|
2015-04-27 06:54:49 -04:00
|
|
|
type resourceHandler struct {
|
2015-02-18 15:15:33 -05:00
|
|
|
handler http.Handler
|
|
|
|
}
|
|
|
|
|
2015-02-23 19:46:48 -05:00
|
|
|
// strip AccessKey from authorization header
|
2015-02-11 06:23:15 -05:00
|
|
|
func stripAccessKey(r *http.Request) string {
|
|
|
|
fields := strings.Fields(r.Header.Get("Authorization"))
|
|
|
|
if len(fields) < 2 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
splits := strings.Split(fields[1], ":")
|
|
|
|
if len(splits) < 2 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return splits[0]
|
|
|
|
}
|
|
|
|
|
2015-04-27 06:54:49 -04:00
|
|
|
func getDate(req *http.Request) (time.Time, error) {
|
|
|
|
if req.Header.Get("x-amz-date") != "" {
|
|
|
|
return time.Parse(http.TimeFormat, req.Header.Get("x-amz-date"))
|
|
|
|
}
|
|
|
|
if req.Header.Get("Date") != "" {
|
|
|
|
return time.Parse(http.TimeFormat, req.Header.Get("Date"))
|
|
|
|
}
|
|
|
|
return time.Time{}, errors.New("invalid request")
|
|
|
|
}
|
|
|
|
|
|
|
|
func timeValidityHandler(h http.Handler) http.Handler {
|
|
|
|
return timeHandler{h}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h timeHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
acceptsContentType := getContentType(r)
|
|
|
|
if acceptsContentType == unknownContentType {
|
|
|
|
writeErrorResponse(w, r, NotAcceptable, acceptsContentType, r.URL.Path)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Verify if date headers are set, if not reject the request
|
2015-04-29 21:30:17 -04:00
|
|
|
|
|
|
|
if r.Header.Get("Authorization") != "" {
|
|
|
|
if r.Header.Get("x-amz-date") == "" && r.Header.Get("Date") == "" {
|
|
|
|
// there is no way to knowing if this is a valid request, could be a attack reject such clients
|
|
|
|
writeErrorResponse(w, r, RequestTimeTooSkewed, acceptsContentType, r.URL.Path)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
date, err := getDate(r)
|
|
|
|
if err != nil {
|
|
|
|
// there is no way to knowing if this is a valid request, could be a attack reject such clients
|
|
|
|
writeErrorResponse(w, r, RequestTimeTooSkewed, acceptsContentType, r.URL.Path)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
duration := time.Since(date)
|
|
|
|
minutes := time.Duration(5) * time.Minute
|
|
|
|
if duration.Minutes() > minutes.Minutes() {
|
|
|
|
writeErrorResponse(w, r, RequestTimeTooSkewed, acceptsContentType, r.URL.Path)
|
|
|
|
return
|
|
|
|
}
|
2015-04-27 06:54:49 -04:00
|
|
|
}
|
|
|
|
h.handler.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
|
2015-02-23 19:46:48 -05:00
|
|
|
// Validate handler is wrapper handler used for API request validation with authorization header.
|
|
|
|
// Current authorization layer supports S3's standard HMAC based signature request.
|
2015-04-27 06:54:49 -04:00
|
|
|
func validateRequestHandler(conf config.Config, h http.Handler) http.Handler {
|
|
|
|
return validateHandler{
|
2015-04-22 21:19:20 -04:00
|
|
|
conf: conf,
|
|
|
|
handler: h,
|
|
|
|
}
|
2015-02-11 06:23:15 -05:00
|
|
|
}
|
|
|
|
|
2015-03-03 05:36:12 -05:00
|
|
|
// Validate handler ServeHTTP() wrapper
|
2015-04-27 06:54:49 -04:00
|
|
|
func (h validateHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
2015-02-12 01:00:45 -05:00
|
|
|
acceptsContentType := getContentType(r)
|
2015-04-22 21:19:20 -04:00
|
|
|
if acceptsContentType == unknownContentType {
|
2015-04-26 20:02:49 -04:00
|
|
|
writeErrorResponse(w, r, NotAcceptable, acceptsContentType, r.URL.Path)
|
2015-04-22 21:19:20 -04:00
|
|
|
return
|
|
|
|
}
|
2015-04-23 04:20:03 -04:00
|
|
|
// success
|
|
|
|
h.handler.ServeHTTP(w, r)
|
|
|
|
|
|
|
|
// Enable below only when webcli is ready
|
|
|
|
|
|
|
|
/*
|
|
|
|
switch true {
|
|
|
|
case accessKey != "":
|
|
|
|
if err := h.conf.ReadConfig(); err != nil {
|
|
|
|
writeErrorResponse(w, r, InternalError, acceptsContentType, r.URL.Path)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
user, ok := h.conf.Users[accessKey]
|
|
|
|
if !ok {
|
|
|
|
writeErrorResponse(w, r, AccessDenied, acceptsContentType, r.URL.Path)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ok, _ = ValidateRequest(user, r)
|
|
|
|
if !ok {
|
|
|
|
writeErrorResponse(w, r, AccessDenied, acceptsContentType, r.URL.Path)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Success
|
|
|
|
h.handler.ServeHTTP(w, r)
|
|
|
|
default:
|
|
|
|
// Control reaches when no access key is found, ideally we would
|
|
|
|
// like to throw back `403`. But for now with our tests lacking
|
|
|
|
// this functionality it is better for us to be serving anonymous
|
|
|
|
// requests as well.
|
|
|
|
// We should remove this after adding tests to support signature request
|
|
|
|
h.handler.ServeHTTP(w, r)
|
|
|
|
// ## Uncommented below links of code after disabling anonymous requests
|
|
|
|
// error := errorCodeError(AccessDenied)
|
|
|
|
// errorResponse := getErrorResponse(error, "")
|
|
|
|
// w.WriteHeader(error.HTTPStatusCode)
|
|
|
|
// w.Write(writeErrorResponse(w, errorResponse, acceptsContentType))
|
2015-04-22 21:19:20 -04:00
|
|
|
}
|
2015-04-23 04:20:03 -04:00
|
|
|
*/
|
2015-02-11 06:23:15 -05:00
|
|
|
}
|
|
|
|
|
2015-02-23 19:46:48 -05:00
|
|
|
// Ignore resources handler is wrapper handler used for API request resource validation
|
|
|
|
// Since we do not support all the S3 queries, it is necessary for us to throw back a
|
|
|
|
// valid error message indicating such a feature to have been not implemented.
|
2015-02-18 15:15:33 -05:00
|
|
|
func ignoreResourcesHandler(h http.Handler) http.Handler {
|
2015-04-27 06:54:49 -04:00
|
|
|
return resourceHandler{h}
|
2015-02-18 15:15:33 -05:00
|
|
|
}
|
|
|
|
|
2015-03-03 05:36:12 -05:00
|
|
|
// Resource handler ServeHTTP() wrapper
|
2015-04-27 06:54:49 -04:00
|
|
|
func (h resourceHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
2015-02-18 15:15:33 -05:00
|
|
|
acceptsContentType := getContentType(r)
|
2015-04-27 06:54:49 -04:00
|
|
|
if acceptsContentType == unknownContentType {
|
|
|
|
writeErrorResponse(w, r, NotAcceptable, acceptsContentType, r.URL.Path)
|
|
|
|
return
|
|
|
|
}
|
2015-04-29 18:28:04 -04:00
|
|
|
if ignoreNotImplementedObjectResources(r) || ignoreNotImplementedBucketResources(r) {
|
2015-03-28 21:00:55 -04:00
|
|
|
error := getErrorCode(NotImplemented)
|
2015-02-18 15:15:33 -05:00
|
|
|
errorResponse := getErrorResponse(error, "")
|
2015-03-28 21:00:55 -04:00
|
|
|
setCommonHeaders(w, getContentTypeString(acceptsContentType))
|
2015-03-06 00:07:19 -05:00
|
|
|
w.WriteHeader(error.HTTPStatusCode)
|
2015-03-28 21:00:55 -04:00
|
|
|
w.Write(encodeErrorResponse(errorResponse, acceptsContentType))
|
2015-04-27 06:54:49 -04:00
|
|
|
return
|
2015-02-18 15:15:33 -05:00
|
|
|
}
|
2015-04-27 06:54:49 -04:00
|
|
|
h.handler.ServeHTTP(w, r)
|
2015-02-11 06:23:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
//// helpers
|
|
|
|
|
2015-04-29 18:28:04 -04:00
|
|
|
// Checks requests for not implemented Bucket resources
|
|
|
|
func ignoreNotImplementedBucketResources(req *http.Request) bool {
|
2015-02-11 06:23:15 -05:00
|
|
|
q := req.URL.Query()
|
|
|
|
for name := range q {
|
2015-04-29 18:28:04 -04:00
|
|
|
if notimplementedBucketResourceNames[name] {
|
2015-02-11 06:23:15 -05:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-04-29 18:28:04 -04:00
|
|
|
// Checks requests for not implemented Object resources
|
|
|
|
func ignoreNotImplementedObjectResources(req *http.Request) bool {
|
2015-02-11 06:23:15 -05:00
|
|
|
q := req.URL.Query()
|
|
|
|
for name := range q {
|
2015-04-29 18:28:04 -04:00
|
|
|
if notimplementedObjectResourceNames[name] {
|
2015-02-11 06:23:15 -05:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|