From c5249c35d3e659c2f97100d9e7d82bfad6ee9a90 Mon Sep 17 00:00:00 2001 From: Krishna Srinivas Date: Tue, 11 Apr 2017 17:44:26 -0700 Subject: [PATCH] gateway: Support for custom endpoint. (#4086) --- cmd/azure.go | 8 ++++--- cmd/gateway-main.go | 35 +++++++++++++++++++++++++--- cmd/gateway-main_test.go | 50 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 87 insertions(+), 6 deletions(-) create mode 100644 cmd/gateway-main_test.go diff --git a/cmd/azure.go b/cmd/azure.go index 1217556bd..bc34102b0 100644 --- a/cmd/azure.go +++ b/cmd/azure.go @@ -123,9 +123,11 @@ func azureToObjectError(err error, params ...string) error { } // Inits azure blob storage client and returns AzureObjects. -func newAzureLayer(account, key string) (GatewayLayer, error) { - useHTTPS := true - c, err := storage.NewClient(account, key, storage.DefaultBaseURL, globalAzureAPIVersion, useHTTPS) +func newAzureLayer(endPoint string, account, key string, secure bool) (GatewayLayer, error) { + if endPoint == "" { + endPoint = storage.DefaultBaseURL + } + c, err := storage.NewClient(account, key, endPoint, globalAzureAPIVersion, secure) if err != nil { return AzureObjects{}, err } diff --git a/cmd/gateway-main.go b/cmd/gateway-main.go index 3d21efba1..5465b35e4 100644 --- a/cmd/gateway-main.go +++ b/cmd/gateway-main.go @@ -19,7 +19,9 @@ package cmd import ( "errors" "fmt" + "net/url" "os" + "strings" "github.com/gorilla/mux" "github.com/minio/cli" @@ -83,11 +85,11 @@ func mustGetGatewayCredsFromEnv() (accessKey, secretKey string) { // // - Azure Blob Storage. // - Add your favorite backend here. -func newGatewayLayer(backendType, accessKey, secretKey string) (GatewayLayer, error) { +func newGatewayLayer(backendType, endPoint, accessKey, secretKey string, secure bool) (GatewayLayer, error) { if gatewayBackend(backendType) != azureBackend { return nil, fmt.Errorf("Unrecognized backend type %s", backendType) } - return newAzureLayer(accessKey, secretKey) + return newAzureLayer(endPoint, accessKey, secretKey, secure) } // Initialize a new gateway config. @@ -117,6 +119,28 @@ func newGatewayConfig(accessKey, secretKey, region string) error { return nil } +// Return endpoint. +func parseGatewayEndpoint(arg string) (endPoint string, secure bool, err error) { + schemeSpecified := len(strings.Split(arg, "://")) > 1 + if !schemeSpecified { + // Default connection will be "secure". + arg = "https://" + arg + } + u, err := url.Parse(arg) + if err != nil { + return "", false, err + } + + switch u.Scheme { + case "http": + return u.Host, false, nil + case "https": + return u.Host, true, nil + default: + return "", false, fmt.Errorf("Unrecognized scheme %s", u.Scheme) + } +} + // Handler for 'minio gateway'. func gatewayMain(ctx *cli.Context) { if !ctx.Args().Present() || ctx.Args().First() == "help" { @@ -142,10 +166,15 @@ func gatewayMain(ctx *cli.Context) { // First argument is selected backend type. backendType := ctx.Args().First() + // Second argument is endpoint. If no endpoint is specified then the + // gateway implementation should use a default setting. + endPoint, secure, err := parseGatewayEndpoint(ctx.Args().Get(1)) + fatalIf(err, "Unable to parse endpoint") + // Create certs path for SSL configuration. fatalIf(createConfigDir(), "Unable to create configuration directory") - newObject, err := newGatewayLayer(backendType, accessKey, secretKey) + newObject, err := newGatewayLayer(backendType, endPoint, accessKey, secretKey, secure) fatalIf(err, "Unable to initialize gateway layer") initNSLock(false) // Enable local namespace lock. diff --git a/cmd/gateway-main_test.go b/cmd/gateway-main_test.go new file mode 100644 index 000000000..dc1b986d5 --- /dev/null +++ b/cmd/gateway-main_test.go @@ -0,0 +1,50 @@ +/* + * Minio Cloud Storage, (C) 2017 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" + +// Test parseGatewayEndpoint +func TestParseGatewayEndpoint(t *testing.T) { + testCases := []struct { + arg string + endPoint string + secure bool + errReturned bool + }{ + {"http://127.0.0.1:9000", "127.0.0.1:9000", false, false}, + {"https://127.0.0.1:9000", "127.0.0.1:9000", true, false}, + {"http://play.minio.io:9000", "play.minio.io:9000", false, false}, + {"https://play.minio.io:9000", "play.minio.io:9000", true, false}, + {"ftp://127.0.0.1:9000", "", false, true}, + {"ftp://play.minio.io:9000", "", false, true}, + {"play.minio.io:9000", "play.minio.io:9000", true, false}, + } + + for i, test := range testCases { + endPoint, secure, err := parseGatewayEndpoint(test.arg) + errReturned := err != nil + + if endPoint != test.endPoint || + secure != test.secure || + errReturned != test.errReturned { + t.Errorf("Test %d: expected %s,%t,%t got %s,%t,%t", + i+1, test.endPoint, test.secure, test.errReturned, + endPoint, secure, errReturned) + } + } +}