2015-07-09 23:42:04 +02:00
|
|
|
/*
|
2015-07-25 02:51:40 +02:00
|
|
|
* Minio Cloud Storage, (C) 2015 Minio, Inc.
|
2015-07-09 23:42:04 +02: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 09:52:01 +02:00
|
|
|
package main
|
2015-07-09 23:42:04 +02:00
|
|
|
|
|
|
|
import (
|
2015-10-06 19:12:06 +02:00
|
|
|
"bytes"
|
2015-10-02 08:51:17 +02:00
|
|
|
"encoding/base64"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"mime/multipart"
|
2015-07-09 23:42:04 +02:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
2015-10-02 08:51:17 +02:00
|
|
|
"time"
|
2015-07-09 23:42:04 +02:00
|
|
|
|
2015-10-16 20:26:01 +02:00
|
|
|
"github.com/minio/minio-xl/pkg/probe"
|
2015-10-17 04:09:35 +02:00
|
|
|
"github.com/minio/minio/pkg/fs"
|
2015-07-09 23:42:04 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
authHeaderPrefix = "AWS4-HMAC-SHA256"
|
2015-09-19 09:52:01 +02:00
|
|
|
iso8601Format = "20060102T150405Z"
|
2015-07-09 23:42:04 +02:00
|
|
|
)
|
|
|
|
|
2015-09-18 23:48:01 +02: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 23:42:04 +02:00
|
|
|
}
|
2015-11-11 03:10:27 +01:00
|
|
|
// replace all spaced strings
|
|
|
|
authValue = strings.Replace(authValue, " ", "", -1)
|
|
|
|
if !strings.HasPrefix(authValue, authHeaderPrefix) {
|
2015-09-18 23:48:01 +02:00
|
|
|
return nil, probe.NewError(errMissingFieldsAuthHeader)
|
2015-07-09 23:42:04 +02:00
|
|
|
}
|
2015-11-11 03:10:27 +01:00
|
|
|
if !strings.HasPrefix(strings.TrimPrefix(authValue, authHeaderPrefix), "Credential") {
|
2015-09-18 23:48:01 +02:00
|
|
|
return nil, probe.NewError(errInvalidAuthHeaderPrefix)
|
2015-07-09 23:42:04 +02:00
|
|
|
}
|
2015-11-11 03:10:27 +01: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 23:42:04 +02:00
|
|
|
if len(credentials) != 2 {
|
2015-09-18 23:48:01 +02:00
|
|
|
return nil, probe.NewError(errMissingFieldsCredentialTag)
|
2015-07-09 23:42:04 +02:00
|
|
|
}
|
2015-07-10 06:44:24 +02:00
|
|
|
if len(strings.Split(strings.TrimSpace(authFields[1]), "=")) != 2 {
|
2015-09-18 23:48:01 +02:00
|
|
|
return nil, probe.NewError(errMissingFieldsSignedHeadersTag)
|
2015-07-09 23:42:04 +02:00
|
|
|
}
|
2015-07-10 06:44:24 +02:00
|
|
|
if len(strings.Split(strings.TrimSpace(authFields[2]), "=")) != 2 {
|
2015-09-18 23:48:01 +02:00
|
|
|
return nil, probe.NewError(errMissingFieldsSignatureTag)
|
2015-07-09 23:42:04 +02:00
|
|
|
}
|
2015-09-18 23:48:01 +02: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-10-16 20:26:01 +02:00
|
|
|
if !isValidAccessKey(accessKeyID) {
|
2015-09-18 23:48:01 +02:00
|
|
|
return "", probe.NewError(errAccessKeyIDInvalid)
|
2015-07-09 23:42:04 +02:00
|
|
|
}
|
|
|
|
return accessKeyID, nil
|
|
|
|
}
|
|
|
|
|
2015-09-18 23:48:01 +02:00
|
|
|
// initSignatureV4 initializing signature verification
|
2015-10-16 20:26:01 +02:00
|
|
|
func initSignatureV4(req *http.Request) (*fs.Signature, *probe.Error) {
|
2015-07-09 23:42:04 +02:00
|
|
|
// strip auth from authorization header
|
2015-09-18 23:48:01 +02:00
|
|
|
authHeaderValue := req.Header.Get("Authorization")
|
|
|
|
accessKeyID, err := stripAccessKeyID(authHeaderValue)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err.Trace()
|
2015-07-09 23:42:04 +02:00
|
|
|
}
|
2015-10-20 09:33:53 +02:00
|
|
|
config, err := loadConfigV2()
|
2015-08-04 01:17:21 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err.Trace()
|
|
|
|
}
|
2015-10-01 07:59:52 +02:00
|
|
|
authFields := strings.Split(strings.TrimSpace(authHeaderValue), ",")
|
|
|
|
signedHeaders := strings.Split(strings.Split(strings.TrimSpace(authFields[1]), "=")[1], ";")
|
|
|
|
signature := strings.Split(strings.TrimSpace(authFields[2]), "=")[1]
|
2015-10-20 09:33:53 +02:00
|
|
|
if config.Credentials.AccessKeyID == accessKeyID {
|
2015-10-16 20:26:01 +02:00
|
|
|
signature := &fs.Signature{
|
2015-10-20 09:33:53 +02:00
|
|
|
AccessKeyID: config.Credentials.AccessKeyID,
|
|
|
|
SecretAccessKey: config.Credentials.SecretAccessKey,
|
2015-10-16 20:26:01 +02:00
|
|
|
Signature: signature,
|
|
|
|
SignedHeaders: signedHeaders,
|
|
|
|
Request: req,
|
2015-09-18 12:40:36 +02:00
|
|
|
}
|
2015-10-16 20:26:01 +02:00
|
|
|
return signature, nil
|
2015-07-09 23:42:04 +02:00
|
|
|
}
|
2015-10-04 21:06:14 +02:00
|
|
|
return nil, probe.NewError(errAccessKeyIDInvalid)
|
2015-07-09 23:42:04 +02:00
|
|
|
}
|
2015-10-01 07:59:52 +02:00
|
|
|
|
2015-10-02 08:51:17 +02:00
|
|
|
func extractHTTPFormValues(reader *multipart.Reader) (io.Reader, map[string]string, *probe.Error) {
|
|
|
|
/// HTML Form values
|
|
|
|
formValues := make(map[string]string)
|
2015-10-06 19:12:06 +02:00
|
|
|
filePart := new(bytes.Buffer)
|
2015-10-02 08:51:17 +02: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 19:12:06 +02:00
|
|
|
formValues[http.CanonicalHeaderKey(part.FormName())] = string(buffer)
|
2015-10-02 08:51:17 +02:00
|
|
|
} else {
|
2015-10-06 19:12:06 +02:00
|
|
|
_, err := io.Copy(filePart, part)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, probe.NewError(err)
|
|
|
|
}
|
2015-10-02 08:51:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return filePart, formValues, nil
|
|
|
|
}
|
|
|
|
|
2015-10-06 19:12:06 +02:00
|
|
|
func applyPolicy(formValues map[string]string) *probe.Error {
|
2015-10-02 08:51:17 +02:00
|
|
|
if formValues["X-Amz-Algorithm"] != "AWS4-HMAC-SHA256" {
|
|
|
|
return probe.NewError(errUnsupportedAlgorithm)
|
|
|
|
}
|
2015-10-06 19:12:06 +02:00
|
|
|
/// Decoding policy
|
|
|
|
policyBytes, err := base64.StdEncoding.DecodeString(formValues["Policy"])
|
|
|
|
if err != nil {
|
|
|
|
return probe.NewError(err)
|
|
|
|
}
|
2015-10-16 20:26:01 +02:00
|
|
|
postPolicyForm, perr := fs.ParsePostPolicyForm(string(policyBytes))
|
2015-10-02 08:51:17 +02: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 19:12:06 +02:00
|
|
|
if !strings.HasPrefix(formValues["Key"], postPolicyForm.Conditions.Policies["$key"].Value) {
|
2015-10-02 08:51:17 +02:00
|
|
|
return probe.NewError(errPolicyMissingFields)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if postPolicyForm.Conditions.Policies["$key"].Operator == "eq" {
|
2015-10-06 19:12:06 +02:00
|
|
|
if formValues["Key"] != postPolicyForm.Conditions.Policies["$key"].Value {
|
2015-10-02 08:51:17 +02:00
|
|
|
return probe.NewError(errPolicyMissingFields)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// initPostPresignedPolicyV4 initializing post policy signature verification
|
2015-10-16 20:26:01 +02:00
|
|
|
func initPostPresignedPolicyV4(formValues map[string]string) (*fs.Signature, *probe.Error) {
|
2015-10-02 08:51:17 +02: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 20:26:01 +02:00
|
|
|
if !isValidAccessKey(accessKeyID) {
|
2015-10-02 08:51:17 +02:00
|
|
|
return nil, probe.NewError(errAccessKeyIDInvalid)
|
|
|
|
}
|
2015-10-20 09:33:53 +02:00
|
|
|
config, perr := loadConfigV2()
|
2015-10-02 08:51:17 +02:00
|
|
|
if perr != nil {
|
|
|
|
return nil, perr.Trace()
|
|
|
|
}
|
2015-10-20 09:33:53 +02:00
|
|
|
if config.Credentials.AccessKeyID == accessKeyID {
|
2015-10-16 20:26:01 +02:00
|
|
|
signature := &fs.Signature{
|
2015-10-20 09:33:53 +02:00
|
|
|
AccessKeyID: config.Credentials.AccessKeyID,
|
|
|
|
SecretAccessKey: config.Credentials.SecretAccessKey,
|
2015-10-16 20:26:01 +02:00
|
|
|
Signature: formValues["X-Amz-Signature"],
|
|
|
|
PresignedPolicy: formValues["Policy"],
|
2015-10-02 08:51:17 +02:00
|
|
|
}
|
2015-10-16 20:26:01 +02:00
|
|
|
return signature, nil
|
2015-10-02 08:51:17 +02:00
|
|
|
}
|
|
|
|
return nil, probe.NewError(errAccessKeyIDInvalid)
|
|
|
|
}
|
|
|
|
|
2015-10-01 07:59:52 +02:00
|
|
|
// initPresignedSignatureV4 initializing presigned signature verification
|
2015-10-16 20:26:01 +02:00
|
|
|
func initPresignedSignatureV4(req *http.Request) (*fs.Signature, *probe.Error) {
|
2015-10-01 07:59:52 +02: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 20:26:01 +02:00
|
|
|
if !isValidAccessKey(accessKeyID) {
|
2015-10-01 07:59:52 +02:00
|
|
|
return nil, probe.NewError(errAccessKeyIDInvalid)
|
|
|
|
}
|
2015-10-20 09:33:53 +02:00
|
|
|
config, err := loadConfigV2()
|
2015-10-01 07:59:52 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err.Trace()
|
|
|
|
}
|
|
|
|
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 09:33:53 +02:00
|
|
|
if config.Credentials.AccessKeyID == accessKeyID {
|
2015-10-16 20:26:01 +02:00
|
|
|
signature := &fs.Signature{
|
2015-10-20 09:33:53 +02:00
|
|
|
AccessKeyID: config.Credentials.AccessKeyID,
|
|
|
|
SecretAccessKey: config.Credentials.SecretAccessKey,
|
2015-10-16 20:26:01 +02:00
|
|
|
Signature: signature,
|
|
|
|
SignedHeaders: signedHeaders,
|
|
|
|
Presigned: true,
|
|
|
|
Request: req,
|
2015-10-01 07:59:52 +02:00
|
|
|
}
|
2015-10-16 20:26:01 +02:00
|
|
|
return signature, nil
|
2015-10-01 07:59:52 +02:00
|
|
|
}
|
|
|
|
return nil, probe.NewError(errAccessKeyIDInvalid)
|
|
|
|
}
|