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