2015-02-15 03:48:15 -05:00
|
|
|
/*
|
2015-07-24 20:51:40 -04:00
|
|
|
* Minio Cloud 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"
|
2015-04-27 06:54:49 -04:00
|
|
|
"time"
|
2015-02-11 06:23:15 -05:00
|
|
|
|
2015-07-08 17:33:54 -04:00
|
|
|
"github.com/minio/minio/pkg/auth"
|
2015-08-31 17:40:12 -04:00
|
|
|
"github.com/rs/cors"
|
2015-02-11 06:23:15 -05:00
|
|
|
)
|
|
|
|
|
2015-09-17 21:53:40 -04:00
|
|
|
// MiddlewareHandler - useful to chain different middleware http.Handler
|
2015-09-15 19:59:43 -04:00
|
|
|
type MiddlewareHandler func(http.Handler) http.Handler
|
|
|
|
|
2015-05-13 15:19:41 -04:00
|
|
|
type contentTypeHandler struct {
|
|
|
|
handler http.Handler
|
|
|
|
}
|
|
|
|
|
2015-04-27 06:54:49 -04:00
|
|
|
type timeHandler struct {
|
|
|
|
handler http.Handler
|
|
|
|
}
|
|
|
|
|
2015-04-30 19:29:03 -04:00
|
|
|
type validateAuthHandler struct {
|
2015-02-11 06:23:15 -05:00
|
|
|
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-05-13 15:19:41 -04:00
|
|
|
const (
|
2015-07-08 19:54:32 -04:00
|
|
|
iso8601Format = "20060102T150405Z"
|
2015-05-13 15:19:41 -04:00
|
|
|
)
|
|
|
|
|
2015-06-30 17:42:29 -04:00
|
|
|
func parseDate(req *http.Request) (time.Time, error) {
|
2015-07-10 20:21:53 -04:00
|
|
|
amzDate := req.Header.Get(http.CanonicalHeaderKey("x-amz-date"))
|
2015-05-08 19:01:49 -04:00
|
|
|
switch {
|
|
|
|
case amzDate != "":
|
|
|
|
if _, err := time.Parse(time.RFC1123, amzDate); err == nil {
|
|
|
|
return time.Parse(time.RFC1123, amzDate)
|
|
|
|
}
|
|
|
|
if _, err := time.Parse(time.RFC1123Z, amzDate); err == nil {
|
|
|
|
return time.Parse(time.RFC1123Z, amzDate)
|
|
|
|
}
|
2015-07-08 19:54:32 -04:00
|
|
|
if _, err := time.Parse(iso8601Format, amzDate); err == nil {
|
|
|
|
return time.Parse(iso8601Format, amzDate)
|
2015-04-30 19:29:03 -04:00
|
|
|
}
|
2015-04-27 06:54:49 -04:00
|
|
|
}
|
2015-05-08 19:01:49 -04:00
|
|
|
date := req.Header.Get("Date")
|
|
|
|
switch {
|
|
|
|
case date != "":
|
|
|
|
if _, err := time.Parse(time.RFC1123, date); err == nil {
|
|
|
|
return time.Parse(time.RFC1123, date)
|
|
|
|
}
|
|
|
|
if _, err := time.Parse(time.RFC1123Z, date); err == nil {
|
|
|
|
return time.Parse(time.RFC1123Z, date)
|
|
|
|
}
|
2015-07-08 19:54:32 -04:00
|
|
|
if _, err := time.Parse(iso8601Format, amzDate); err == nil {
|
|
|
|
return time.Parse(iso8601Format, amzDate)
|
2015-04-30 19:29:03 -04:00
|
|
|
}
|
2015-04-27 06:54:49 -04:00
|
|
|
}
|
|
|
|
return time.Time{}, errors.New("invalid request")
|
|
|
|
}
|
|
|
|
|
2015-07-14 17:29:56 -04:00
|
|
|
// ValidContentTypeHandler to validate Accept type
|
2015-06-30 23:15:48 -04:00
|
|
|
func ValidContentTypeHandler(h http.Handler) http.Handler {
|
2015-05-13 15:19:41 -04:00
|
|
|
return contentTypeHandler{h}
|
2015-04-27 06:54:49 -04:00
|
|
|
}
|
|
|
|
|
2015-05-13 15:19:41 -04:00
|
|
|
func (h contentTypeHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
2015-04-27 06:54:49 -04:00
|
|
|
acceptsContentType := getContentType(r)
|
|
|
|
if acceptsContentType == unknownContentType {
|
|
|
|
writeErrorResponse(w, r, NotAcceptable, acceptsContentType, r.URL.Path)
|
|
|
|
return
|
|
|
|
}
|
2015-05-13 15:19:41 -04:00
|
|
|
h.handler.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
|
2015-07-14 17:29:56 -04:00
|
|
|
// TimeValidityHandler to validate parsable time over http header
|
2015-06-30 23:15:48 -04:00
|
|
|
func TimeValidityHandler(h http.Handler) http.Handler {
|
2015-05-13 15:19:41 -04:00
|
|
|
return timeHandler{h}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h timeHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
acceptsContentType := getContentType(r)
|
2015-04-27 06:54:49 -04:00
|
|
|
// Verify if date headers are set, if not reject the request
|
2015-04-29 21:30:17 -04:00
|
|
|
if r.Header.Get("Authorization") != "" {
|
2015-07-10 20:21:53 -04:00
|
|
|
if r.Header.Get(http.CanonicalHeaderKey("x-amz-date")) == "" && r.Header.Get("Date") == "" {
|
2015-04-29 21:30:17 -04:00
|
|
|
// 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
|
|
|
|
}
|
2015-06-30 17:42:29 -04:00
|
|
|
date, err := parseDate(r)
|
2015-04-29 21:30:17 -04:00
|
|
|
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-06-30 23:15:48 -04:00
|
|
|
// ValidateAuthHeaderHandler -
|
2015-07-14 17:29:56 -04:00
|
|
|
// validate auth header handler is wrapper handler used for request validation with authorization header.
|
2015-02-23 19:46:48 -05:00
|
|
|
// Current authorization layer supports S3's standard HMAC based signature request.
|
2015-06-30 23:15:48 -04:00
|
|
|
func ValidateAuthHeaderHandler(h http.Handler) http.Handler {
|
2015-05-13 15:19:41 -04:00
|
|
|
return validateAuthHandler{h}
|
2015-02-11 06:23:15 -05:00
|
|
|
}
|
|
|
|
|
2015-04-30 19:29:03 -04:00
|
|
|
// validate auth header handler ServeHTTP() wrapper
|
|
|
|
func (h validateAuthHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
2015-02-12 01:00:45 -05:00
|
|
|
acceptsContentType := getContentType(r)
|
2015-07-09 17:42:04 -04:00
|
|
|
accessKeyID, err := StripAccessKeyID(r.Header.Get("Authorization"))
|
2015-04-30 19:29:03 -04:00
|
|
|
switch err.(type) {
|
|
|
|
case nil:
|
2015-07-09 17:42:04 -04:00
|
|
|
// load auth config
|
2015-07-09 00:10:12 -04:00
|
|
|
authConfig, err := auth.LoadConfig()
|
2015-07-01 14:07:46 -04:00
|
|
|
if err != nil {
|
|
|
|
writeErrorResponse(w, r, InternalError, acceptsContentType, r.URL.Path)
|
|
|
|
return
|
|
|
|
}
|
2015-07-09 17:42:04 -04:00
|
|
|
// Access key not found
|
2015-09-18 06:40:36 -04:00
|
|
|
for _, user := range authConfig.Users {
|
|
|
|
if user.AccessKeyID == accessKeyID {
|
|
|
|
h.handler.ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
2015-04-22 21:19:20 -04:00
|
|
|
}
|
2015-09-18 06:40:36 -04:00
|
|
|
writeErrorResponse(w, r, InvalidAccessKeyID, acceptsContentType, r.URL.Path)
|
|
|
|
return
|
2015-04-30 19:29:03 -04:00
|
|
|
default:
|
|
|
|
// control reaches here, we should just send the request up the stack - internally
|
|
|
|
// individual calls will validate themselves against un-authenticated requests
|
|
|
|
h.handler.ServeHTTP(w, r)
|
|
|
|
}
|
2015-02-11 06:23:15 -05:00
|
|
|
}
|
|
|
|
|
2015-09-17 21:53:40 -04:00
|
|
|
// CorsHandler handler for CORS (Cross Origin Resource Sharing)
|
2015-08-31 17:40:12 -04:00
|
|
|
func CorsHandler(h http.Handler) http.Handler {
|
|
|
|
return cors.Default().Handler(h)
|
|
|
|
}
|
|
|
|
|
2015-06-30 23:15:48 -04:00
|
|
|
// IgnoreResourcesHandler -
|
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
|
2015-06-03 21:27:13 -04:00
|
|
|
// valid error message indicating such a feature is not implemented.
|
2015-06-30 23:15:48 -04: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-29 18:28:04 -04:00
|
|
|
if ignoreNotImplementedObjectResources(r) || ignoreNotImplementedBucketResources(r) {
|
2015-07-11 00:06:43 -04:00
|
|
|
writeErrorResponse(w, r, NotImplemented, acceptsContentType, r.URL.Path)
|
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
|
|
|
|
}
|