From 1d5870b2a2bf563e9efd8959570d99fb706dc4d5 Mon Sep 17 00:00:00 2001 From: poornas Date: Thu, 30 Aug 2018 09:52:53 -0700 Subject: [PATCH] Fix url parsing of MC_HOSTS_ env variable (#2523) --- cmd/config.go | 43 +++++++++++++++++++++++++++- cmd/config_test.go | 70 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 112 insertions(+), 1 deletion(-) create mode 100644 cmd/config_test.go diff --git a/cmd/config.go b/cmd/config.go index 77390e66..a06f7408 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -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) } diff --git a/cmd/config_test.go b/cmd/config_test.go new file mode 100644 index 00000000..9b705d66 --- /dev/null +++ b/cmd/config_test.go @@ -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) + } + } +}