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
|
|
|
|
2016-02-11 01:40:09 +01:00
|
|
|
"github.com/minio/minio/pkg/probe"
|
|
|
|
v4 "github.com/minio/minio/pkg/signature"
|
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-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
|
|
|
|
}
|
|
|
|
|
2015-12-29 02:43:28 +01: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
|
|
|
|
}
|
|
|
|
|
2016-01-17 10:39:09 +01:00
|
|
|
// verify if region value is valid with configured minioRegion.
|
|
|
|
func isValidRegion(region string, minioRegion string) *probe.Error {
|
|
|
|
if minioRegion == "" {
|
|
|
|
minioRegion = "us-east-1"
|
|
|
|
}
|
|
|
|
if region != minioRegion && region != "US" {
|
2015-12-29 02:43:28 +01:00
|
|
|
return probe.NewError(errInvalidRegion)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// stripRegion - strip only region from auth header.
|
|
|
|
func stripRegion(authHeaderValue string) (string, *probe.Error) {
|
2015-09-18 23:48:01 +02:00
|
|
|
credentialElements, err := getCredentialsFromAuth(authHeaderValue)
|
|
|
|
if err != nil {
|
2015-12-29 02:43:28 +01:00
|
|
|
return "", err.Trace(authHeaderValue)
|
2015-09-18 23:48:01 +02:00
|
|
|
}
|
|
|
|
region := credentialElements[2]
|
2015-12-29 02:43:28 +01:00
|
|
|
return region, nil
|
2015-09-18 23:48:01 +02:00
|
|
|
}
|
|
|
|
|
2015-12-29 02:43:28 +01:00
|
|
|
// stripAccessKeyID - strip only access key id from auth header.
|
2015-09-18 23:48:01 +02:00
|
|
|
func stripAccessKeyID(authHeaderValue string) (string, *probe.Error) {
|
|
|
|
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-12-29 02:43:28 +01:00
|
|
|
// initSignatureV4 initializing signature verification.
|
2016-02-11 01:40:09 +01:00
|
|
|
func initSignatureV4(req *http.Request) (*v4.Signature, *probe.Error) {
|
2015-12-29 02:43:28 +01:00
|
|
|
// strip auth from authorization header.
|
2015-09-18 23:48:01 +02:00
|
|
|
authHeaderValue := req.Header.Get("Authorization")
|
2015-12-29 02:43:28 +01:00
|
|
|
|
2016-01-17 10:39:09 +01:00
|
|
|
config, err := loadConfigV2()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err.Trace()
|
|
|
|
}
|
|
|
|
|
2015-12-29 02:43:28 +01:00
|
|
|
region, err := stripRegion(authHeaderValue)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err.Trace(authHeaderValue)
|
|
|
|
}
|
2016-01-17 10:39:09 +01:00
|
|
|
|
|
|
|
if err = isValidRegion(region, config.Credentials.Region); err != nil {
|
|
|
|
return nil, err.Trace(authHeaderValue)
|
|
|
|
}
|
|
|
|
|
2015-09-18 23:48:01 +02:00
|
|
|
accessKeyID, err := stripAccessKeyID(authHeaderValue)
|
|
|
|
if err != nil {
|
2015-12-29 02:43:28 +01: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 23:42:04 +02:00
|
|
|
}
|
2015-10-20 09:33:53 +02:00
|
|
|
if config.Credentials.AccessKeyID == accessKeyID {
|
2016-02-11 01:40:09 +01:00
|
|
|
signature := &v4.Signature{
|
2015-10-20 09:33:53 +02:00
|
|
|
AccessKeyID: config.Credentials.AccessKeyID,
|
|
|
|
SecretAccessKey: config.Credentials.SecretAccessKey,
|
2015-12-29 02:43:28 +01:00
|
|
|
Region: region,
|
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)
|
2016-02-04 21:52:25 +01:00
|
|
|
var e error
|
|
|
|
for e == nil {
|
2015-10-02 08:51:17 +02:00
|
|
|
var part *multipart.Part
|
2016-02-04 21:52:25 +01:00
|
|
|
part, e = reader.NextPart()
|
2015-10-02 08:51:17 +02:00
|
|
|
if part != nil {
|
|
|
|
if part.FileName() == "" {
|
2016-02-04 21:52:25 +01:00
|
|
|
buffer, e := ioutil.ReadAll(part)
|
|
|
|
if e != nil {
|
|
|
|
return nil, nil, probe.NewError(e)
|
2015-10-02 08:51:17 +02:00
|
|
|
}
|
2015-10-06 19:12:06 +02:00
|
|
|
formValues[http.CanonicalHeaderKey(part.FormName())] = string(buffer)
|
2015-10-02 08:51:17 +02:00
|
|
|
} else {
|
2016-02-04 21:52:25 +01:00
|
|
|
if _, e := io.Copy(filePart, part); e != nil {
|
|
|
|
return nil, nil, probe.NewError(e)
|
2015-10-06 19:12:06 +02:00
|
|
|
}
|
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
|
2016-02-04 21:52:25 +01:00
|
|
|
policyBytes, e := base64.StdEncoding.DecodeString(formValues["Policy"])
|
|
|
|
if e != nil {
|
|
|
|
return probe.NewError(e)
|
2015-10-06 19:12:06 +02:00
|
|
|
}
|
2016-02-11 01:40:09 +01:00
|
|
|
postPolicyForm, err := v4.ParsePostPolicyForm(string(policyBytes))
|
2016-02-04 21:52:25 +01:00
|
|
|
if err != nil {
|
|
|
|
return err.Trace()
|
2015-10-02 08:51:17 +02:00
|
|
|
}
|
|
|
|
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
|
2016-02-11 01:40:09 +01:00
|
|
|
func initPostPresignedPolicyV4(formValues map[string]string) (*v4.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)
|
|
|
|
}
|
2016-02-04 21:52:25 +01:00
|
|
|
config, err := loadConfigV2()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err.Trace()
|
2015-10-02 08:51:17 +02:00
|
|
|
}
|
2015-12-29 02:43:28 +01:00
|
|
|
region := credentialElements[2]
|
2015-10-20 09:33:53 +02:00
|
|
|
if config.Credentials.AccessKeyID == accessKeyID {
|
2016-02-11 01:40:09 +01:00
|
|
|
signature := &v4.Signature{
|
2015-10-20 09:33:53 +02:00
|
|
|
AccessKeyID: config.Credentials.AccessKeyID,
|
|
|
|
SecretAccessKey: config.Credentials.SecretAccessKey,
|
2015-12-29 02:43:28 +01:00
|
|
|
Region: region,
|
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
|
2016-02-11 01:40:09 +01:00
|
|
|
func initPresignedSignatureV4(req *http.Request) (*v4.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()
|
|
|
|
}
|
2015-12-29 02:43:28 +01:00
|
|
|
region := credentialElements[2]
|
2015-10-01 07:59:52 +02: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 09:33:53 +02:00
|
|
|
if config.Credentials.AccessKeyID == accessKeyID {
|
2016-02-11 01:40:09 +01:00
|
|
|
signature := &v4.Signature{
|
2015-10-20 09:33:53 +02:00
|
|
|
AccessKeyID: config.Credentials.AccessKeyID,
|
|
|
|
SecretAccessKey: config.Credentials.SecretAccessKey,
|
2015-12-29 02:43:28 +01:00
|
|
|
Region: region,
|
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)
|
|
|
|
}
|