Fix url parsing of MC_HOSTS_ env variable (#2523)

This commit is contained in:
poornas
2018-08-30 22:22:53 +05:30
committed by Nitish Tiwari
parent ae88af8b1b
commit 1d5870b2a2
2 changed files with 112 additions and 1 deletions
+42 -1
View File
@@ -17,6 +17,7 @@
package cmd
import (
"fmt"
"net/url"
"os"
"path/filepath"
@@ -205,10 +206,50 @@ func parseEnvURL(envURL string) (*url.URL, string, string, *probe.Error) {
return u, accessKey, secretKey, nil
}
// parse url usually obtained from env.
func parseEnvURLStr(envURL string) (*url.URL, string, string, *probe.Error) {
var envURLStr string
u, accessKey, secretKey, err := parseEnvURL(envURL)
if err != nil {
// url parsing can fail when accessKey/secretKey contains non url encoded values
// such as #. Strip accessKey/secretKey from envURL and parse again.
re := regexp.MustCompile("^(https?://)(.*?):(.*?)@(.*?)$")
res := re.FindAllStringSubmatch(envURL, -1)
// regex will return full match, scheme, accessKey, secretKey and endpoint:port as
// captured groups.
if len(res[0]) != 5 {
return nil, "", "", err
}
for k, v := range res[0] {
if k == 2 {
accessKey = fmt.Sprintf("%s", v)
}
if k == 3 {
secretKey = fmt.Sprintf("%s", v)
}
if k == 1 || k == 4 {
envURLStr = fmt.Sprintf("%s%s", envURLStr, v)
}
}
u, _, _, err = parseEnvURL(envURLStr)
if err != nil {
return nil, "", "", err
}
}
// Check if username:password is provided in URL, with no
// access keys or secret we proceed and perform anonymous
// requests.
if u.User != nil {
accessKey = u.User.Username()
secretKey, _ = u.User.Password()
}
return u, accessKey, secretKey, nil
}
const mcEnvHostsPrefix = "MC_HOSTS_"
func expandAliasFromEnv(envURL string) (*hostConfigV9, *probe.Error) {
u, accessKey, secretKey, err := parseEnvURL(envURL)
u, accessKey, secretKey, err := parseEnvURLStr(envURL)
if err != nil {
return nil, err.Trace(envURL)
}
+70
View File
@@ -0,0 +1,70 @@
/*
* Minio Client (C) 2018 Minio, Inc.
*
* 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.
*/
package cmd
import "testing"
// Tests valid host URL functionality.
func TestparseEnvURLStr(t *testing.T) {
testCases := []struct {
hostURL string
accessKey string
secretKey string
url string
}{
{
hostURL: "https://minio:minio1#23@localhost:9000",
accessKey: "minio",
secretKey: "minio#123",
url: "https://localhost:9000",
},
{
hostURL: "https://minio:minio123@localhost:9000",
accessKey: "minio",
secretKey: "minio123",
url: "https://localhost:9000",
},
{
hostURL: "http://minio:minio1#23@localhost:9000",
accessKey: "minio",
secretKey: "minio#123",
url: "http://localhost:9000",
},
{
hostURL: "https://localhost:9000",
accessKey: "",
secretKey: "",
url: "https://localhost:9000",
},
}
for _, testCase := range testCases {
url, ak, sk, err := parseEnvURLStr(testCase.hostURL)
if testCase.accessKey != sk {
t.Fatalf("Expected %s, got %s", testCase.accessKey, ak)
}
if testCase.secretKey != sk {
t.Fatalf("Expected %s, got %s", testCase.secretKey, sk)
}
if testCase.url != url.Hostname() {
t.Fatalf("Expected %s, got %s", testCase.url, url.Hostname())
}
if err != nil {
t.Fatalf("Expected test to pass. Failed with err %s", err)
}
}
}