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 (
|
2015-10-06 13:12:06 -04:00
|
|
|
"bytes"
|
2015-10-02 02:51:17 -04:00
|
|
|
"encoding/base64"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"mime/multipart"
|
2015-07-09 17:42:04 -04:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
2015-10-02 02:51:17 -04:00
|
|
|
"time"
|
2015-07-09 17:42:04 -04:00
|
|
|
|
2015-10-16 14:26:01 -04:00
|
|
|
"github.com/minio/minio-xl/pkg/probe"
|
2015-10-16 22:09:35 -04:00
|
|
|
"github.com/minio/minio/pkg/fs"
|
2015-07-09 17:42:04 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
authHeaderPrefix = "AWS4-HMAC-SHA256"
|
2015-09-19 03:52:01 -04:00
|
|
|
iso8601Format = "20060102T150405Z"
|
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-11-10 21:10:27 -05:00
|
|
|
// replace all spaced strings
|
|
|
|
authValue = strings.Replace(authValue, " ", "", -1)
|
|
|
|
if !strings.HasPrefix(authValue, authHeaderPrefix) {
|
2015-09-18 17:48:01 -04:00
|
|
|
return nil, probe.NewError(errMissingFieldsAuthHeader)
|
2015-07-09 17:42:04 -04:00
|
|
|
}
|
2015-11-10 21:10:27 -05:00
|
|
|
if !strings.HasPrefix(strings.TrimPrefix(authValue, authHeaderPrefix), "Credential") {
|
2015-09-18 17:48:01 -04:00
|
|
|
return nil, probe.NewError(errInvalidAuthHeaderPrefix)
|
2015-07-09 17:42:04 -04:00
|
|
|
}
|
2015-11-10 21:10:27 -05:00
|
|
|
authValue = strings.TrimPrefix(authValue, authHeaderPrefix)
|
|
|
|
authFields := strings.Split(strings.TrimSpace(authValue), ",")
|
|
|
|
if len(authFields) != 3 {
|
|
|
|
return nil, probe.NewError(errInvalidAuthHeaderValue)
|
|
|
|
}
|
|
|
|
credentials := strings.Split(strings.TrimSpace(authFields[0]), "=")
|
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-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
|
|
|
|
}
|
|
|
|
|
2015-12-28 20:43:28 -05:00
|
|
|
func getSignatureFromAuth(authHeaderValue string) (string, *probe.Error) {
|
|
|
|
authValue := strings.TrimPrefix(authHeaderValue, authHeaderPrefix)
|
|
|
|
authFields := strings.Split(strings.TrimSpace(authValue), ",")
|
|
|
|
if len(authFields) != 3 {
|
|
|
|
return "", probe.NewError(errInvalidAuthHeaderValue)
|
|
|
|
}
|
|
|
|
if len(strings.Split(strings.TrimSpace(authFields[2]), "=")) != 2 {
|
|
|
|
return "", probe.NewError(errMissingFieldsSignatureTag)
|
|
|
|
}
|
|
|
|
signature := strings.Split(strings.TrimSpace(authFields[2]), "=")[1]
|
|
|
|
return signature, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getSignedHeadersFromAuth(authHeaderValue string) ([]string, *probe.Error) {
|
|
|
|
authValue := strings.TrimPrefix(authHeaderValue, authHeaderPrefix)
|
|
|
|
authFields := strings.Split(strings.TrimSpace(authValue), ",")
|
|
|
|
if len(authFields) != 3 {
|
|
|
|
return nil, probe.NewError(errInvalidAuthHeaderValue)
|
|
|
|
}
|
|
|
|
if len(strings.Split(strings.TrimSpace(authFields[1]), "=")) != 2 {
|
|
|
|
return nil, probe.NewError(errMissingFieldsSignedHeadersTag)
|
|
|
|
}
|
|
|
|
signedHeaders := strings.Split(strings.Split(strings.TrimSpace(authFields[1]), "=")[1], ";")
|
|
|
|
return signedHeaders, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// verify if region value is valid.
|
|
|
|
func isValidRegion(region string) *probe.Error {
|
|
|
|
if region != "us-east-1" && region != "US" {
|
|
|
|
return probe.NewError(errInvalidRegion)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// stripRegion - strip only region from auth header.
|
|
|
|
func stripRegion(authHeaderValue string) (string, *probe.Error) {
|
2015-09-18 17:48:01 -04:00
|
|
|
credentialElements, err := getCredentialsFromAuth(authHeaderValue)
|
|
|
|
if err != nil {
|
2015-12-28 20:43:28 -05:00
|
|
|
return "", err.Trace(authHeaderValue)
|
2015-09-18 17:48:01 -04:00
|
|
|
}
|
|
|
|
region := credentialElements[2]
|
2015-12-28 20:43:28 -05:00
|
|
|
if err = isValidRegion(region); err != nil {
|
|
|
|
return "", err.Trace(authHeaderValue)
|
2015-09-18 17:48:01 -04:00
|
|
|
}
|
2015-12-28 20:43:28 -05:00
|
|
|
return region, nil
|
2015-09-18 17:48:01 -04:00
|
|
|
}
|
|
|
|
|
2015-12-28 20:43:28 -05:00
|
|
|
// stripAccessKeyID - strip only access key id from auth header.
|
2015-09-18 17:48:01 -04:00
|
|
|
func stripAccessKeyID(authHeaderValue string) (string, *probe.Error) {
|
|
|
|
credentialElements, err := getCredentialsFromAuth(authHeaderValue)
|
|
|
|
if err != nil {
|
|
|
|
return "", err.Trace()
|
|
|
|
}
|
|
|
|
accessKeyID := credentialElements[0]
|
2015-10-16 14:26:01 -04:00
|
|
|
if !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-12-28 20:43:28 -05:00
|
|
|
// initSignatureV4 initializing signature verification.
|
2015-10-16 14:26:01 -04:00
|
|
|
func initSignatureV4(req *http.Request) (*fs.Signature, *probe.Error) {
|
2015-12-28 20:43:28 -05:00
|
|
|
// strip auth from authorization header.
|
2015-09-18 17:48:01 -04:00
|
|
|
authHeaderValue := req.Header.Get("Authorization")
|
2015-12-28 20:43:28 -05:00
|
|
|
|
|
|
|
region, err := stripRegion(authHeaderValue)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err.Trace(authHeaderValue)
|
|
|
|
}
|
2015-09-18 17:48:01 -04:00
|
|
|
accessKeyID, err := stripAccessKeyID(authHeaderValue)
|
|
|
|
if err != nil {
|
2015-12-28 20:43:28 -05:00
|
|
|
return nil, err.Trace(authHeaderValue)
|
|
|
|
}
|
|
|
|
signature, err := getSignatureFromAuth(authHeaderValue)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err.Trace(authHeaderValue)
|
|
|
|
}
|
|
|
|
signedHeaders, err := getSignedHeadersFromAuth(authHeaderValue)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err.Trace(authHeaderValue)
|
2015-07-09 17:42:04 -04:00
|
|
|
}
|
2015-10-20 03:33:53 -04:00
|
|
|
config, err := loadConfigV2()
|
2015-08-03 19:17:21 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err.Trace()
|
|
|
|
}
|
2015-10-20 03:33:53 -04:00
|
|
|
if config.Credentials.AccessKeyID == accessKeyID {
|
2015-10-16 14:26:01 -04:00
|
|
|
signature := &fs.Signature{
|
2015-10-20 03:33:53 -04:00
|
|
|
AccessKeyID: config.Credentials.AccessKeyID,
|
|
|
|
SecretAccessKey: config.Credentials.SecretAccessKey,
|
2015-12-28 20:43:28 -05:00
|
|
|
Region: region,
|
2015-10-16 14:26:01 -04:00
|
|
|
Signature: signature,
|
|
|
|
SignedHeaders: signedHeaders,
|
|
|
|
Request: req,
|
2015-09-18 06:40:36 -04:00
|
|
|
}
|
2015-10-16 14:26:01 -04:00
|
|
|
return signature, nil
|
2015-07-09 17:42:04 -04:00
|
|
|
}
|
2015-10-04 15:06:14 -04:00
|
|
|
return nil, probe.NewError(errAccessKeyIDInvalid)
|
2015-07-09 17:42:04 -04:00
|
|
|
}
|
2015-10-01 01:59:52 -04:00
|
|
|
|
2015-10-02 02:51:17 -04:00
|
|
|
func extractHTTPFormValues(reader *multipart.Reader) (io.Reader, map[string]string, *probe.Error) {
|
|
|
|
/// HTML Form values
|
|
|
|
formValues := make(map[string]string)
|
2015-10-06 13:12:06 -04:00
|
|
|
filePart := new(bytes.Buffer)
|
2015-10-02 02:51:17 -04:00
|
|
|
var err error
|
|
|
|
for err == nil {
|
|
|
|
var part *multipart.Part
|
|
|
|
part, err = reader.NextPart()
|
|
|
|
if part != nil {
|
|
|
|
if part.FileName() == "" {
|
|
|
|
buffer, err := ioutil.ReadAll(part)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, probe.NewError(err)
|
|
|
|
}
|
2015-10-06 13:12:06 -04:00
|
|
|
formValues[http.CanonicalHeaderKey(part.FormName())] = string(buffer)
|
2015-10-02 02:51:17 -04:00
|
|
|
} else {
|
2015-10-06 13:12:06 -04:00
|
|
|
_, err := io.Copy(filePart, part)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, probe.NewError(err)
|
|
|
|
}
|
2015-10-02 02:51:17 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return filePart, formValues, nil
|
|
|
|
}
|
|
|
|
|
2015-10-06 13:12:06 -04:00
|
|
|
func applyPolicy(formValues map[string]string) *probe.Error {
|
2015-10-02 02:51:17 -04:00
|
|
|
if formValues["X-Amz-Algorithm"] != "AWS4-HMAC-SHA256" {
|
|
|
|
return probe.NewError(errUnsupportedAlgorithm)
|
|
|
|
}
|
2015-10-06 13:12:06 -04:00
|
|
|
/// Decoding policy
|
|
|
|
policyBytes, err := base64.StdEncoding.DecodeString(formValues["Policy"])
|
|
|
|
if err != nil {
|
|
|
|
return probe.NewError(err)
|
|
|
|
}
|
2015-10-16 14:26:01 -04:00
|
|
|
postPolicyForm, perr := fs.ParsePostPolicyForm(string(policyBytes))
|
2015-10-02 02:51:17 -04:00
|
|
|
if perr != nil {
|
|
|
|
return perr.Trace()
|
|
|
|
}
|
|
|
|
if !postPolicyForm.Expiration.After(time.Now().UTC()) {
|
|
|
|
return probe.NewError(errPolicyAlreadyExpired)
|
|
|
|
}
|
|
|
|
if postPolicyForm.Conditions.Policies["$bucket"].Operator == "eq" {
|
|
|
|
if formValues["Bucket"] != postPolicyForm.Conditions.Policies["$bucket"].Value {
|
|
|
|
return probe.NewError(errPolicyMissingFields)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if postPolicyForm.Conditions.Policies["$x-amz-date"].Operator == "eq" {
|
|
|
|
if formValues["X-Amz-Date"] != postPolicyForm.Conditions.Policies["$x-amz-date"].Value {
|
|
|
|
return probe.NewError(errPolicyMissingFields)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if postPolicyForm.Conditions.Policies["$Content-Type"].Operator == "starts-with" {
|
|
|
|
if !strings.HasPrefix(formValues["Content-Type"], postPolicyForm.Conditions.Policies["$Content-Type"].Value) {
|
|
|
|
return probe.NewError(errPolicyMissingFields)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if postPolicyForm.Conditions.Policies["$Content-Type"].Operator == "eq" {
|
|
|
|
if formValues["Content-Type"] != postPolicyForm.Conditions.Policies["$Content-Type"].Value {
|
|
|
|
return probe.NewError(errPolicyMissingFields)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if postPolicyForm.Conditions.Policies["$key"].Operator == "starts-with" {
|
2015-10-06 13:12:06 -04:00
|
|
|
if !strings.HasPrefix(formValues["Key"], postPolicyForm.Conditions.Policies["$key"].Value) {
|
2015-10-02 02:51:17 -04:00
|
|
|
return probe.NewError(errPolicyMissingFields)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if postPolicyForm.Conditions.Policies["$key"].Operator == "eq" {
|
2015-10-06 13:12:06 -04:00
|
|
|
if formValues["Key"] != postPolicyForm.Conditions.Policies["$key"].Value {
|
2015-10-02 02:51:17 -04:00
|
|
|
return probe.NewError(errPolicyMissingFields)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// initPostPresignedPolicyV4 initializing post policy signature verification
|
2015-10-16 14:26:01 -04:00
|
|
|
func initPostPresignedPolicyV4(formValues map[string]string) (*fs.Signature, *probe.Error) {
|
2015-10-02 02:51:17 -04:00
|
|
|
credentialElements := strings.Split(strings.TrimSpace(formValues["X-Amz-Credential"]), "/")
|
|
|
|
if len(credentialElements) != 5 {
|
|
|
|
return nil, probe.NewError(errCredentialTagMalformed)
|
|
|
|
}
|
|
|
|
accessKeyID := credentialElements[0]
|
2015-10-16 14:26:01 -04:00
|
|
|
if !isValidAccessKey(accessKeyID) {
|
2015-10-02 02:51:17 -04:00
|
|
|
return nil, probe.NewError(errAccessKeyIDInvalid)
|
|
|
|
}
|
2015-10-20 03:33:53 -04:00
|
|
|
config, perr := loadConfigV2()
|
2015-10-02 02:51:17 -04:00
|
|
|
if perr != nil {
|
|
|
|
return nil, perr.Trace()
|
|
|
|
}
|
2015-12-28 20:43:28 -05:00
|
|
|
region := credentialElements[2]
|
2015-10-20 03:33:53 -04:00
|
|
|
if config.Credentials.AccessKeyID == accessKeyID {
|
2015-10-16 14:26:01 -04:00
|
|
|
signature := &fs.Signature{
|
2015-10-20 03:33:53 -04:00
|
|
|
AccessKeyID: config.Credentials.AccessKeyID,
|
|
|
|
SecretAccessKey: config.Credentials.SecretAccessKey,
|
2015-12-28 20:43:28 -05:00
|
|
|
Region: region,
|
2015-10-16 14:26:01 -04:00
|
|
|
Signature: formValues["X-Amz-Signature"],
|
|
|
|
PresignedPolicy: formValues["Policy"],
|
2015-10-02 02:51:17 -04:00
|
|
|
}
|
2015-10-16 14:26:01 -04:00
|
|
|
return signature, nil
|
2015-10-02 02:51:17 -04:00
|
|
|
}
|
|
|
|
return nil, probe.NewError(errAccessKeyIDInvalid)
|
|
|
|
}
|
|
|
|
|
2015-10-01 01:59:52 -04:00
|
|
|
// initPresignedSignatureV4 initializing presigned signature verification
|
2015-10-16 14:26:01 -04:00
|
|
|
func initPresignedSignatureV4(req *http.Request) (*fs.Signature, *probe.Error) {
|
2015-10-01 01:59:52 -04:00
|
|
|
credentialElements := strings.Split(strings.TrimSpace(req.URL.Query().Get("X-Amz-Credential")), "/")
|
|
|
|
if len(credentialElements) != 5 {
|
|
|
|
return nil, probe.NewError(errCredentialTagMalformed)
|
|
|
|
}
|
|
|
|
accessKeyID := credentialElements[0]
|
2015-10-16 14:26:01 -04:00
|
|
|
if !isValidAccessKey(accessKeyID) {
|
2015-10-01 01:59:52 -04:00
|
|
|
return nil, probe.NewError(errAccessKeyIDInvalid)
|
|
|
|
}
|
2015-10-20 03:33:53 -04:00
|
|
|
config, err := loadConfigV2()
|
2015-10-01 01:59:52 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err.Trace()
|
|
|
|
}
|
2015-12-28 20:43:28 -05:00
|
|
|
region := credentialElements[2]
|
2015-10-01 01:59:52 -04:00
|
|
|
signedHeaders := strings.Split(strings.TrimSpace(req.URL.Query().Get("X-Amz-SignedHeaders")), ";")
|
|
|
|
signature := strings.TrimSpace(req.URL.Query().Get("X-Amz-Signature"))
|
2015-10-20 03:33:53 -04:00
|
|
|
if config.Credentials.AccessKeyID == accessKeyID {
|
2015-10-16 14:26:01 -04:00
|
|
|
signature := &fs.Signature{
|
2015-10-20 03:33:53 -04:00
|
|
|
AccessKeyID: config.Credentials.AccessKeyID,
|
|
|
|
SecretAccessKey: config.Credentials.SecretAccessKey,
|
2015-12-28 20:43:28 -05:00
|
|
|
Region: region,
|
2015-10-16 14:26:01 -04:00
|
|
|
Signature: signature,
|
|
|
|
SignedHeaders: signedHeaders,
|
|
|
|
Presigned: true,
|
|
|
|
Request: req,
|
2015-10-01 01:59:52 -04:00
|
|
|
}
|
2015-10-16 14:26:01 -04:00
|
|
|
return signature, nil
|
2015-10-01 01:59:52 -04:00
|
|
|
}
|
|
|
|
return nil, probe.NewError(errAccessKeyIDInvalid)
|
|
|
|
}
|