You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
258 lines
8.3 KiB
258 lines
8.3 KiB
![]()
9 years ago
|
/*
|
||
![]()
9 years ago
|
* Minio Cloud Storage, (C) 2015 Minio, Inc.
|
||
![]()
9 years ago
|
*
|
||
|
* 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.
|
||
|
*/
|
||
|
|
||
![]()
9 years ago
|
package main
|
||
![]()
9 years ago
|
|
||
|
import (
|
||
![]()
9 years ago
|
"bytes"
|
||
![]()
9 years ago
|
"encoding/base64"
|
||
|
"io"
|
||
|
"io/ioutil"
|
||
|
"mime/multipart"
|
||
![]()
9 years ago
|
"net/http"
|
||
|
"strings"
|
||
![]()
9 years ago
|
"time"
|
||
![]()
9 years ago
|
|
||
![]()
9 years ago
|
"github.com/minio/minio/pkg/fs"
|
||
|
"github.com/minio/minio-xl/pkg/probe"
|
||
![]()
9 years ago
|
)
|
||
|
|
||
|
const (
|
||
|
authHeaderPrefix = "AWS4-HMAC-SHA256"
|
||
![]()
9 years ago
|
iso8601Format = "20060102T150405Z"
|
||
![]()
9 years ago
|
)
|
||
|
|
||
![]()
9 years ago
|
// getCredentialsFromAuth parse credentials tag from authorization value
|
||
|
func getCredentialsFromAuth(authValue string) ([]string, *probe.Error) {
|
||
|
if authValue == "" {
|
||
|
return nil, probe.NewError(errMissingAuthHeaderValue)
|
||
![]()
9 years ago
|
}
|
||
![]()
9 years ago
|
authFields := strings.Split(strings.TrimSpace(authValue), ",")
|
||
![]()
9 years ago
|
if len(authFields) != 3 {
|
||
![]()
9 years ago
|
return nil, probe.NewError(errInvalidAuthHeaderValue)
|
||
![]()
9 years ago
|
}
|
||
|
authPrefixFields := strings.Fields(authFields[0])
|
||
|
if len(authPrefixFields) != 2 {
|
||
![]()
9 years ago
|
return nil, probe.NewError(errMissingFieldsAuthHeader)
|
||
![]()
9 years ago
|
}
|
||
|
if authPrefixFields[0] != authHeaderPrefix {
|
||
![]()
9 years ago
|
return nil, probe.NewError(errInvalidAuthHeaderPrefix)
|
||
![]()
9 years ago
|
}
|
||
![]()
9 years ago
|
credentials := strings.Split(strings.TrimSpace(authPrefixFields[1]), "=")
|
||
![]()
9 years ago
|
if len(credentials) != 2 {
|
||
![]()
9 years ago
|
return nil, probe.NewError(errMissingFieldsCredentialTag)
|
||
![]()
9 years ago
|
}
|
||
![]()
9 years ago
|
if len(strings.Split(strings.TrimSpace(authFields[1]), "=")) != 2 {
|
||
![]()
9 years ago
|
return nil, probe.NewError(errMissingFieldsSignedHeadersTag)
|
||
![]()
9 years ago
|
}
|
||
![]()
9 years ago
|
if len(strings.Split(strings.TrimSpace(authFields[2]), "=")) != 2 {
|
||
![]()
9 years ago
|
return nil, probe.NewError(errMissingFieldsSignatureTag)
|
||
![]()
9 years ago
|
}
|
||
![]()
9 years ago
|
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]
|
||
![]()
9 years ago
|
if !isValidAccessKey(accessKeyID) {
|
||
![]()
9 years ago
|
return "", probe.NewError(errAccessKeyIDInvalid)
|
||
![]()
9 years ago
|
}
|
||
|
return accessKeyID, nil
|
||
|
}
|
||
|
|
||
![]()
9 years ago
|
// initSignatureV4 initializing signature verification
|
||
![]()
9 years ago
|
func initSignatureV4(req *http.Request) (*fs.Signature, *probe.Error) {
|
||
![]()
9 years ago
|
// strip auth from authorization header
|
||
![]()
9 years ago
|
authHeaderValue := req.Header.Get("Authorization")
|
||
|
accessKeyID, err := stripAccessKeyID(authHeaderValue)
|
||
|
if err != nil {
|
||
|
return nil, err.Trace()
|
||
![]()
9 years ago
|
}
|
||
![]()
9 years ago
|
authConfig, err := loadAuthConfig()
|
||
![]()
9 years ago
|
if err != nil {
|
||
|
return nil, err.Trace()
|
||
|
}
|
||
![]()
9 years ago
|
authFields := strings.Split(strings.TrimSpace(authHeaderValue), ",")
|
||
|
signedHeaders := strings.Split(strings.Split(strings.TrimSpace(authFields[1]), "=")[1], ";")
|
||
|
signature := strings.Split(strings.TrimSpace(authFields[2]), "=")[1]
|
||
![]()
9 years ago
|
if authConfig.AccessKeyID == accessKeyID {
|
||
|
signature := &fs.Signature{
|
||
|
AccessKeyID: authConfig.AccessKeyID,
|
||
|
SecretAccessKey: authConfig.SecretAccessKey,
|
||
|
Signature: signature,
|
||
|
SignedHeaders: signedHeaders,
|
||
|
Request: req,
|
||
![]()
9 years ago
|
}
|
||
![]()
9 years ago
|
return signature, nil
|
||
![]()
9 years ago
|
}
|
||
![]()
9 years ago
|
return nil, probe.NewError(errAccessKeyIDInvalid)
|
||
![]()
9 years ago
|
}
|
||
![]()
9 years ago
|
|
||
![]()
9 years ago
|
func extractHTTPFormValues(reader *multipart.Reader) (io.Reader, map[string]string, *probe.Error) {
|
||
|
/// HTML Form values
|
||
|
formValues := make(map[string]string)
|
||
![]()
9 years ago
|
filePart := new(bytes.Buffer)
|
||
![]()
9 years ago
|
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)
|
||
|
}
|
||
![]()
9 years ago
|
formValues[http.CanonicalHeaderKey(part.FormName())] = string(buffer)
|
||
![]()
9 years ago
|
} else {
|
||
![]()
9 years ago
|
_, err := io.Copy(filePart, part)
|
||
|
if err != nil {
|
||
|
return nil, nil, probe.NewError(err)
|
||
|
}
|
||
![]()
9 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
return filePart, formValues, nil
|
||
|
}
|
||
|
|
||
![]()
9 years ago
|
func applyPolicy(formValues map[string]string) *probe.Error {
|
||
![]()
9 years ago
|
if formValues["X-Amz-Algorithm"] != "AWS4-HMAC-SHA256" {
|
||
|
return probe.NewError(errUnsupportedAlgorithm)
|
||
|
}
|
||
![]()
9 years ago
|
/// Decoding policy
|
||
|
policyBytes, err := base64.StdEncoding.DecodeString(formValues["Policy"])
|
||
|
if err != nil {
|
||
|
return probe.NewError(err)
|
||
|
}
|
||
![]()
9 years ago
|
postPolicyForm, perr := fs.ParsePostPolicyForm(string(policyBytes))
|
||
![]()
9 years ago
|
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" {
|
||
![]()
9 years ago
|
if !strings.HasPrefix(formValues["Key"], postPolicyForm.Conditions.Policies["$key"].Value) {
|
||
![]()
9 years ago
|
return probe.NewError(errPolicyMissingFields)
|
||
|
}
|
||
|
}
|
||
|
if postPolicyForm.Conditions.Policies["$key"].Operator == "eq" {
|
||
![]()
9 years ago
|
if formValues["Key"] != postPolicyForm.Conditions.Policies["$key"].Value {
|
||
![]()
9 years ago
|
return probe.NewError(errPolicyMissingFields)
|
||
|
}
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
// initPostPresignedPolicyV4 initializing post policy signature verification
|
||
![]()
9 years ago
|
func initPostPresignedPolicyV4(formValues map[string]string) (*fs.Signature, *probe.Error) {
|
||
![]()
9 years ago
|
credentialElements := strings.Split(strings.TrimSpace(formValues["X-Amz-Credential"]), "/")
|
||
|
if len(credentialElements) != 5 {
|
||
|
return nil, probe.NewError(errCredentialTagMalformed)
|
||
|
}
|
||
|
accessKeyID := credentialElements[0]
|
||
![]()
9 years ago
|
if !isValidAccessKey(accessKeyID) {
|
||
![]()
9 years ago
|
return nil, probe.NewError(errAccessKeyIDInvalid)
|
||
|
}
|
||
![]()
9 years ago
|
authConfig, perr := loadAuthConfig()
|
||
![]()
9 years ago
|
if perr != nil {
|
||
|
return nil, perr.Trace()
|
||
|
}
|
||
![]()
9 years ago
|
if authConfig.AccessKeyID == accessKeyID {
|
||
|
signature := &fs.Signature{
|
||
|
AccessKeyID: authConfig.AccessKeyID,
|
||
|
SecretAccessKey: authConfig.SecretAccessKey,
|
||
|
Signature: formValues["X-Amz-Signature"],
|
||
|
PresignedPolicy: formValues["Policy"],
|
||
![]()
9 years ago
|
}
|
||
![]()
9 years ago
|
return signature, nil
|
||
![]()
9 years ago
|
}
|
||
|
return nil, probe.NewError(errAccessKeyIDInvalid)
|
||
|
}
|
||
|
|
||
![]()
9 years ago
|
// initPresignedSignatureV4 initializing presigned signature verification
|
||
![]()
9 years ago
|
func initPresignedSignatureV4(req *http.Request) (*fs.Signature, *probe.Error) {
|
||
![]()
9 years ago
|
credentialElements := strings.Split(strings.TrimSpace(req.URL.Query().Get("X-Amz-Credential")), "/")
|
||
|
if len(credentialElements) != 5 {
|
||
|
return nil, probe.NewError(errCredentialTagMalformed)
|
||
|
}
|
||
|
accessKeyID := credentialElements[0]
|
||
![]()
9 years ago
|
if !isValidAccessKey(accessKeyID) {
|
||
![]()
9 years ago
|
return nil, probe.NewError(errAccessKeyIDInvalid)
|
||
|
}
|
||
![]()
9 years ago
|
authConfig, err := loadAuthConfig()
|
||
![]()
9 years ago
|
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"))
|
||
![]()
9 years ago
|
if authConfig.AccessKeyID == accessKeyID {
|
||
|
signature := &fs.Signature{
|
||
|
AccessKeyID: authConfig.AccessKeyID,
|
||
|
SecretAccessKey: authConfig.SecretAccessKey,
|
||
|
Signature: signature,
|
||
|
SignedHeaders: signedHeaders,
|
||
|
Presigned: true,
|
||
|
Request: req,
|
||
![]()
9 years ago
|
}
|
||
![]()
9 years ago
|
return signature, nil
|
||
![]()
9 years ago
|
}
|
||
|
return nil, probe.NewError(errAccessKeyIDInvalid)
|
||
|
}
|