2015-07-09 17:42:04 -04:00
|
|
|
/*
|
2015-07-24 20:51:40 -04:00
|
|
|
* Minio Cloud Storage, (C) 2015 Minio, Inc.
|
2015-07-09 17:42:04 -04: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-09-19 03:52:01 -04:00
|
|
|
package main
|
2015-07-09 17:42:04 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/minio/minio/pkg/auth"
|
|
|
|
"github.com/minio/minio/pkg/donut"
|
2015-08-03 19:17:21 -04:00
|
|
|
"github.com/minio/minio/pkg/probe"
|
2015-07-09 17:42:04 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
authHeaderPrefix = "AWS4-HMAC-SHA256"
|
2015-09-19 03:52:01 -04:00
|
|
|
iso8601Format = "20060102T150405Z"
|
|
|
|
yyyymmdd = "20060102"
|
2015-07-09 17:42:04 -04:00
|
|
|
)
|
|
|
|
|
2015-09-18 17:48:01 -04:00
|
|
|
// getCredentialsFromAuth parse credentials tag from authorization value
|
|
|
|
func getCredentialsFromAuth(authValue string) ([]string, *probe.Error) {
|
|
|
|
if authValue == "" {
|
|
|
|
return nil, probe.NewError(errMissingAuthHeaderValue)
|
2015-07-09 17:42:04 -04:00
|
|
|
}
|
2015-09-18 17:48:01 -04:00
|
|
|
authFields := strings.Split(strings.TrimSpace(authValue), ",")
|
2015-07-09 17:42:04 -04:00
|
|
|
if len(authFields) != 3 {
|
2015-09-18 17:48:01 -04:00
|
|
|
return nil, probe.NewError(errInvalidAuthHeaderValue)
|
2015-07-09 17:42:04 -04:00
|
|
|
}
|
|
|
|
authPrefixFields := strings.Fields(authFields[0])
|
|
|
|
if len(authPrefixFields) != 2 {
|
2015-09-18 17:48:01 -04:00
|
|
|
return nil, probe.NewError(errMissingFieldsAuthHeader)
|
2015-07-09 17:42:04 -04:00
|
|
|
}
|
|
|
|
if authPrefixFields[0] != authHeaderPrefix {
|
2015-09-18 17:48:01 -04:00
|
|
|
return nil, probe.NewError(errInvalidAuthHeaderPrefix)
|
2015-07-09 17:42:04 -04:00
|
|
|
}
|
2015-07-10 00:44:24 -04:00
|
|
|
credentials := strings.Split(strings.TrimSpace(authPrefixFields[1]), "=")
|
2015-07-09 17:42:04 -04:00
|
|
|
if len(credentials) != 2 {
|
2015-09-18 17:48:01 -04:00
|
|
|
return nil, probe.NewError(errMissingFieldsCredentialTag)
|
2015-07-09 17:42:04 -04:00
|
|
|
}
|
2015-07-10 00:44:24 -04:00
|
|
|
if len(strings.Split(strings.TrimSpace(authFields[1]), "=")) != 2 {
|
2015-09-18 17:48:01 -04:00
|
|
|
return nil, probe.NewError(errMissingFieldsSignedHeadersTag)
|
2015-07-09 17:42:04 -04:00
|
|
|
}
|
2015-07-10 00:44:24 -04:00
|
|
|
if len(strings.Split(strings.TrimSpace(authFields[2]), "=")) != 2 {
|
2015-09-18 17:48:01 -04:00
|
|
|
return nil, probe.NewError(errMissingFieldsSignatureTag)
|
2015-07-09 17:42:04 -04:00
|
|
|
}
|
2015-09-18 17:48:01 -04:00
|
|
|
credentialElements := strings.Split(strings.TrimSpace(credentials[1]), "/")
|
|
|
|
if len(credentialElements) != 5 {
|
|
|
|
return nil, probe.NewError(errCredentialTagMalformed)
|
|
|
|
}
|
|
|
|
return credentialElements, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// verify if authHeader value has valid region
|
|
|
|
func isValidRegion(authHeaderValue string) *probe.Error {
|
|
|
|
credentialElements, err := getCredentialsFromAuth(authHeaderValue)
|
|
|
|
if err != nil {
|
|
|
|
return err.Trace()
|
|
|
|
}
|
|
|
|
region := credentialElements[2]
|
|
|
|
if region != "milkyway" {
|
|
|
|
return probe.NewError(errInvalidRegion)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// stripAccessKeyID - strip only access key id from auth header
|
|
|
|
func stripAccessKeyID(authHeaderValue string) (string, *probe.Error) {
|
|
|
|
if err := isValidRegion(authHeaderValue); err != nil {
|
|
|
|
return "", err.Trace()
|
|
|
|
}
|
|
|
|
credentialElements, err := getCredentialsFromAuth(authHeaderValue)
|
|
|
|
if err != nil {
|
|
|
|
return "", err.Trace()
|
|
|
|
}
|
|
|
|
accessKeyID := credentialElements[0]
|
2015-07-09 17:42:04 -04:00
|
|
|
if !auth.IsValidAccessKey(accessKeyID) {
|
2015-09-18 17:48:01 -04:00
|
|
|
return "", probe.NewError(errAccessKeyIDInvalid)
|
2015-07-09 17:42:04 -04:00
|
|
|
}
|
|
|
|
return accessKeyID, nil
|
|
|
|
}
|
|
|
|
|
2015-09-18 17:48:01 -04:00
|
|
|
// initSignatureV4 initializing signature verification
|
|
|
|
func initSignatureV4(req *http.Request) (*donut.Signature, *probe.Error) {
|
2015-07-09 17:42:04 -04:00
|
|
|
// strip auth from authorization header
|
2015-09-18 17:48:01 -04:00
|
|
|
authHeaderValue := req.Header.Get("Authorization")
|
|
|
|
accessKeyID, err := stripAccessKeyID(authHeaderValue)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err.Trace()
|
2015-07-09 17:42:04 -04:00
|
|
|
}
|
|
|
|
authConfig, err := auth.LoadConfig()
|
2015-08-03 19:17:21 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err.Trace()
|
|
|
|
}
|
2015-09-18 06:40:36 -04:00
|
|
|
for _, user := range authConfig.Users {
|
|
|
|
if user.AccessKeyID == accessKeyID {
|
|
|
|
signature := &donut.Signature{
|
|
|
|
AccessKeyID: user.AccessKeyID,
|
|
|
|
SecretAccessKey: user.SecretAccessKey,
|
2015-09-18 17:48:01 -04:00
|
|
|
AuthHeader: authHeaderValue,
|
2015-09-18 06:40:36 -04:00
|
|
|
Request: req,
|
|
|
|
}
|
|
|
|
return signature, nil
|
|
|
|
}
|
2015-07-09 17:42:04 -04:00
|
|
|
}
|
2015-09-18 17:48:01 -04:00
|
|
|
return nil, probe.NewError(errors.New("AccessKeyID not found"))
|
2015-07-09 17:42:04 -04:00
|
|
|
}
|