From 2575f4198a860e06abded4aa59747730690aa43b Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Mon, 18 Jun 2018 15:27:14 -0700 Subject: [PATCH] Auto-probe backend signature support (#6044) --- cmd/gateway/s3/gateway-s3.go | 60 +++++++++++++++++++++++------------- 1 file changed, 38 insertions(+), 22 deletions(-) diff --git a/cmd/gateway/s3/gateway-s3.go b/cmd/gateway/s3/gateway-s3.go index 76f6d9802..4a438a509 100644 --- a/cmd/gateway/s3/gateway-s3.go +++ b/cmd/gateway/s3/gateway-s3.go @@ -1,5 +1,5 @@ /* - * Minio Cloud Storage, (C) 2017 Minio, Inc. + * Minio Cloud Storage, (C) 2017, 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. @@ -97,12 +97,16 @@ EXAMPLES: // Handler for 'minio gateway s3' command line. func s3GatewayMain(ctx *cli.Context) { + args := ctx.Args() + if !ctx.Args().Present() { + args = cli.Args{"https://s3.amazonaws.com"} + } + // Validate gateway arguments. - host := ctx.Args().First() - // Validate gateway arguments. - logger.FatalIf(minio.ValidateGatewayArguments(ctx.GlobalString("address"), host), "Invalid argument") + logger.FatalIf(minio.ValidateGatewayArguments(ctx.GlobalString("address"), args.First()), "Invalid argument") - minio.StartGateway(ctx, &S3{host}) + // Start the gateway.. + minio.StartGateway(ctx, &S3{args.First()}) } // S3 implements Gateway. @@ -115,34 +119,46 @@ func (g *S3) Name() string { return s3Backend } -// NewGatewayLayer returns s3 ObjectLayer. -func (g *S3) NewGatewayLayer(creds auth.Credentials) (minio.ObjectLayer, error) { - var err error - var endpoint string - var secure = true - - // Validate host parameters. - if g.host != "" { - // Override default params if the host is provided - endpoint, secure, err = minio.ParseGatewayEndpoint(g.host) +// newS3 - Initializes a new client by auto probing S3 server signature. +func newS3(url, accessKey, secretKey string) (*miniogo.Core, error) { + if url == "" { + url = "https://s3.amazonaws.com" + } + + // Override default params if the host is provided + endpoint, secure, err := minio.ParseGatewayEndpoint(url) + if err != nil { + return nil, err + } + + clnt, err := miniogo.NewV4(endpoint, accessKey, secretKey, secure) + if err != nil { + return nil, err + } + + if _, err = clnt.BucketExists("probe-bucket-sign"); err != nil { + clnt, err = miniogo.NewV2(endpoint, accessKey, secretKey, secure) if err != nil { return nil, err } + if _, err = clnt.BucketExists("probe-bucket-sign"); err != nil { + return nil, err + } } - // Default endpoint parameters - if endpoint == "" { - endpoint = "s3.amazonaws.com" - } + return &miniogo.Core{Client: clnt}, nil +} - // Initialize minio client object. - client, err := miniogo.NewCore(endpoint, creds.AccessKey, creds.SecretKey, secure) +// NewGatewayLayer returns s3 ObjectLayer. +func (g *S3) NewGatewayLayer(creds auth.Credentials) (minio.ObjectLayer, error) { + // Probe S3 signature with input credentials. + clnt, err := newS3(g.host, creds.AccessKey, creds.SecretKey) if err != nil { return nil, err } return &s3Objects{ - Client: client, + Client: clnt, }, nil }