From 879fa3429e0b5a5b4d7e39da07ebb70f70c58c81 Mon Sep 17 00:00:00 2001 From: pavanaiyar <48223799+pavanaiyar@users.noreply.github.com> Date: Fri, 7 Feb 2020 18:11:46 -0800 Subject: [PATCH] Allow blank port for minio client to work with S3 (#2996) Background --- As of current, supplying a minioServiceHost of `s3.amazonaws.com` with any port incurs an error. Per the results of: ``` testBucket := func(hostPort string) error { client, _ := minio.New(hostPort, accessKey, secretKey, false) _, err := client.BucketExists(bucket) return err } for _, endpoint := range []string{ "s3.amazonaws.com:443", "s3.amazonaws.com:80", "s3.amazonaws.com:", "s3.amazonaws.com", } { fmt.Printf("Endpoint: %s, Error: %v\n", endpoint, testBucket(endpoint)) } ``` ``` Endpoint: s3.amazonaws.com:443, Error: Get http://s3.amazonaws.com:443/kflow-test/?location=: net/http: HTTP/1.x transport connection broken: malformed HTTP response "\x15\x00\x00\x00\x02\x01\x00" Endpoint: s3.amazonaws.com:80, Error: Head http://s3.amazonaws.com/kflow-test/: 301 response missing Location header Endpoint: s3.amazonaws.com:, Error: Head http://s3.amazonaws.com/kflow-test/: 301 response missing Location header Endpoint: s3.amazonaws.com, Error: ``` Only the connection without a port specified works. This change allows passing a blank port to support this. --- backend/src/apiserver/client/minio.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/backend/src/apiserver/client/minio.go b/backend/src/apiserver/client/minio.go index 488bd522adba..a3ad4e8aa5a0 100644 --- a/backend/src/apiserver/client/minio.go +++ b/backend/src/apiserver/client/minio.go @@ -26,7 +26,7 @@ import ( func CreateMinioClient(minioServiceHost string, minioServicePort string, accessKey string, secretKey string) (*minio.Client, error) { - minioClient, err := minio.New(fmt.Sprintf("%s:%s", minioServiceHost, minioServicePort), + minioClient, err := minio.New(joinHostPort(minioServiceHost, minioServicePort), accessKey, secretKey, false /* Secure connection */) if err != nil { return nil, errors.Wrapf(err, "Error while creating minio client: %+v", err) @@ -54,3 +54,13 @@ func CreateMinioClientOrFatal(minioServiceHost string, minioServicePort string, } return minioClient } + +// joinHostPort combines host and port into a network address of the form "host:port". +// +// An empty port value results in "host" instead of "host:" (which net.JoinHostPort would return) +func joinHostPort(host, port string) string { + if port == "" { + return host + } + return fmt.Sprintf("%s:%s", host, port) +}