2017-03-16 15:21:58 -04:00
|
|
|
/*
|
2018-02-28 23:13:33 -05:00
|
|
|
* Minio Cloud Storage, (C) 2017, 2018 Minio, Inc.
|
2017-03-16 15:21:58 -04:00
|
|
|
*
|
|
|
|
* 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 (
|
|
|
|
"fmt"
|
2017-04-11 20:44:26 -04:00
|
|
|
"net/url"
|
2017-07-12 19:33:21 -04:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
2017-06-08 14:20:56 -04:00
|
|
|
"runtime"
|
2017-04-11 20:44:26 -04:00
|
|
|
"strings"
|
2017-07-12 19:33:21 -04:00
|
|
|
"syscall"
|
2017-06-05 18:18:03 -04:00
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/minio/cli"
|
2017-11-25 14:58:29 -05:00
|
|
|
"github.com/minio/minio/pkg/errors"
|
2017-07-12 19:33:21 -04:00
|
|
|
miniohttp "github.com/minio/minio/pkg/http"
|
2017-03-16 15:21:58 -04:00
|
|
|
)
|
|
|
|
|
2017-06-09 22:50:51 -04:00
|
|
|
var (
|
|
|
|
gatewayCmd = cli.Command{
|
|
|
|
Name: "gateway",
|
|
|
|
Usage: "Start object storage gateway.",
|
|
|
|
Flags: append(serverFlags, globalFlags...),
|
|
|
|
HideHelpCommand: true,
|
|
|
|
}
|
|
|
|
)
|
2017-06-09 02:28:45 -04:00
|
|
|
|
2017-10-27 18:07:46 -04:00
|
|
|
// RegisterGatewayCommand registers a new command for gateway.
|
|
|
|
func RegisterGatewayCommand(cmd cli.Command) error {
|
2017-12-05 20:58:09 -05:00
|
|
|
cmd.Flags = append(append(cmd.Flags, append(cmd.Flags, serverFlags...)...), globalFlags...)
|
2017-10-27 18:07:46 -04:00
|
|
|
gatewayCmd.Subcommands = append(gatewayCmd.Subcommands, cmd)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-12-05 20:58:09 -05:00
|
|
|
// ParseGatewayEndpoint - Return endpoint.
|
|
|
|
func ParseGatewayEndpoint(arg string) (endPoint string, secure bool, err error) {
|
2017-04-11 20:44:26 -04:00
|
|
|
schemeSpecified := len(strings.Split(arg, "://")) > 1
|
|
|
|
if !schemeSpecified {
|
|
|
|
// Default connection will be "secure".
|
|
|
|
arg = "https://" + arg
|
|
|
|
}
|
2017-04-27 14:26:00 -04:00
|
|
|
|
2017-04-11 20:44:26 -04:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-05 20:58:09 -05:00
|
|
|
// ValidateGatewayArguments - Validate gateway arguments.
|
|
|
|
func ValidateGatewayArguments(serverAddr, endpointAddr string) error {
|
2017-06-08 14:20:56 -04:00
|
|
|
if err := CheckLocalServerAddr(serverAddr); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if runtime.GOOS == "darwin" {
|
|
|
|
_, port := mustSplitHostPort(serverAddr)
|
|
|
|
// On macOS, if a process already listens on LOCALIPADDR:PORT, net.Listen() falls back
|
|
|
|
// to IPv6 address i.e minio will start listening on IPv6 address whereas another
|
|
|
|
// (non-)minio process is listening on IPv4 of given port.
|
|
|
|
// To avoid this error situation we check for port availability only for macOS.
|
|
|
|
if err := checkPortAvailability(port); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if endpointAddr != "" {
|
|
|
|
// Reject the endpoint if it points to the gateway handler itself.
|
|
|
|
sameTarget, err := sameLocalAddrs(endpointAddr, serverAddr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if sameTarget {
|
2017-11-25 14:58:29 -05:00
|
|
|
return fmt.Errorf("endpoint points to the local gateway")
|
2017-06-08 14:20:56 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-12-05 20:58:09 -05:00
|
|
|
// StartGateway - handler for 'minio gateway <name>'.
|
|
|
|
func StartGateway(ctx *cli.Context, gw Gateway) {
|
|
|
|
if gw == nil {
|
|
|
|
fatalIf(errUnexpected, "Gateway implementation not initialized, exiting.")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate if we have access, secret set through environment.
|
|
|
|
gatewayName := gw.Name()
|
|
|
|
if ctx.Args().First() == "help" {
|
|
|
|
cli.ShowCommandHelpAndExit(ctx, gatewayName, 1)
|
|
|
|
}
|
|
|
|
|
2018-01-17 10:24:46 -05:00
|
|
|
// Get "json" flag from command line argument and
|
|
|
|
// enable json and quite modes if jason flag is turned on.
|
2018-02-28 23:13:33 -05:00
|
|
|
jsonFlag := ctx.IsSet("json") || ctx.GlobalIsSet("json")
|
2018-01-17 10:24:46 -05:00
|
|
|
if jsonFlag {
|
|
|
|
log.EnableJSON()
|
|
|
|
}
|
|
|
|
|
2017-03-16 15:21:58 -04:00
|
|
|
// Get quiet flag from command line argument.
|
2018-02-28 23:13:33 -05:00
|
|
|
quietFlag := ctx.IsSet("quiet") || ctx.GlobalIsSet("quiet")
|
2017-03-30 14:21:19 -04:00
|
|
|
if quietFlag {
|
|
|
|
log.EnableQuiet()
|
|
|
|
}
|
2017-03-16 15:21:58 -04:00
|
|
|
|
2017-07-20 19:39:11 -04:00
|
|
|
// Fetch address option
|
|
|
|
gatewayAddr := ctx.GlobalString("address")
|
|
|
|
if gatewayAddr == ":"+globalMinioPort {
|
|
|
|
gatewayAddr = ctx.String("address")
|
|
|
|
}
|
|
|
|
|
2017-06-09 22:50:51 -04:00
|
|
|
// Handle common command args.
|
|
|
|
handleCommonCmdArgs(ctx)
|
|
|
|
|
|
|
|
// Handle common env vars.
|
|
|
|
handleCommonEnvVars()
|
2017-03-16 15:21:58 -04:00
|
|
|
|
2017-06-19 22:45:13 -04:00
|
|
|
// Validate if we have access, secret set through environment.
|
|
|
|
if !globalIsEnvCreds {
|
2017-11-25 14:58:29 -05:00
|
|
|
errorIf(fmt.Errorf("Access and secret keys not set"), "Access and Secret keys should be set through ENVs for backend [%s]", gatewayName)
|
2017-11-01 14:45:27 -04:00
|
|
|
cli.ShowCommandHelpAndExit(ctx, gatewayName, 1)
|
2017-06-19 22:45:13 -04:00
|
|
|
}
|
|
|
|
|
2017-06-09 22:50:51 -04:00
|
|
|
// Create certs path.
|
|
|
|
fatalIf(createConfigDir(), "Unable to create configuration directories.")
|
|
|
|
|
|
|
|
// Initialize gateway config.
|
|
|
|
initConfig()
|
|
|
|
|
|
|
|
// Init the error tracing module.
|
2017-11-25 14:58:29 -05:00
|
|
|
errors.Init(GOPATH, "github.com/minio/minio")
|
2017-06-09 22:50:51 -04:00
|
|
|
|
|
|
|
// Check and load SSL certificates.
|
|
|
|
var err error
|
2017-07-12 19:33:21 -04:00
|
|
|
globalPublicCerts, globalRootCAs, globalTLSCertificate, globalIsSSL, err = getSSLConfig()
|
|
|
|
fatalIf(err, "Invalid SSL certificate file")
|
2017-03-31 01:26:24 -04:00
|
|
|
|
2017-12-24 09:39:30 -05:00
|
|
|
// Set system resources to maximum.
|
|
|
|
errorIf(setMaxResources(), "Unable to change resource limit")
|
|
|
|
|
2017-05-22 23:02:58 -04:00
|
|
|
initNSLock(false) // Enable local namespace lock.
|
|
|
|
|
2018-03-15 16:03:41 -04:00
|
|
|
// Initialize notification system.
|
|
|
|
globalNotificationSys, err = NewNotificationSys(globalServerConfig, EndpointList{})
|
|
|
|
fatalIf(err, "Unable to initialize notification system.")
|
|
|
|
|
2017-12-05 20:58:09 -05:00
|
|
|
newObject, err := gw.NewGatewayLayer(globalServerConfig.GetCredential())
|
2017-03-23 19:36:00 -04:00
|
|
|
fatalIf(err, "Unable to initialize gateway layer")
|
2017-03-16 15:21:58 -04:00
|
|
|
|
|
|
|
router := mux.NewRouter().SkipClean(true)
|
2017-06-01 12:43:20 -04:00
|
|
|
|
|
|
|
// Register web router when its enabled.
|
|
|
|
if globalIsBrowserEnabled {
|
2017-06-09 22:50:51 -04:00
|
|
|
fatalIf(registerWebRouter(router), "Unable to configure web browser")
|
2017-06-01 12:43:20 -04:00
|
|
|
}
|
2018-02-09 18:19:30 -05:00
|
|
|
registerAPIRouter(router)
|
2017-03-16 15:21:58 -04:00
|
|
|
|
|
|
|
var handlerFns = []HandlerFunc{
|
2017-04-28 20:17:18 -04:00
|
|
|
// Validate all the incoming paths.
|
|
|
|
setPathValidityHandler,
|
2017-03-16 15:21:58 -04:00
|
|
|
// Limits all requests size to a maximum fixed limit
|
|
|
|
setRequestSizeLimitHandler,
|
|
|
|
// Adds 'crossdomain.xml' policy handler to serve legacy flash clients.
|
|
|
|
setCrossDomainPolicy,
|
|
|
|
// Validates all incoming requests to have a valid date header.
|
2017-06-01 12:43:20 -04:00
|
|
|
// Redirect some pre-defined browser request paths to a static location prefix.
|
|
|
|
setBrowserRedirectHandler,
|
|
|
|
// Validates if incoming request is for restricted buckets.
|
2017-09-01 15:16:54 -04:00
|
|
|
setReservedBucketHandler,
|
2017-06-01 12:43:20 -04:00
|
|
|
// Adds cache control for all browser requests.
|
|
|
|
setBrowserCacheControlHandler,
|
|
|
|
// Validates all incoming requests to have a valid date header.
|
2017-03-16 15:21:58 -04:00
|
|
|
setTimeValidityHandler,
|
|
|
|
// CORS setting for all browser API requests.
|
|
|
|
setCorsHandler,
|
|
|
|
// Validates all incoming URL resources, for invalid/unsupported
|
|
|
|
// resources client receives a HTTP error.
|
|
|
|
setIgnoreResourcesHandler,
|
|
|
|
// Auth handler verifies incoming authorization headers and
|
|
|
|
// routes them accordingly. Client receives a HTTP error for
|
|
|
|
// invalid/unsupported signatures.
|
|
|
|
setAuthHandler,
|
2017-06-01 12:43:20 -04:00
|
|
|
// Add new handlers here.
|
2017-03-16 15:21:58 -04:00
|
|
|
}
|
|
|
|
|
2017-07-20 19:39:11 -04:00
|
|
|
globalHTTPServer = miniohttp.NewServer([]string{gatewayAddr}, registerHandlers(router, handlerFns...), globalTLSCertificate)
|
2017-03-16 15:21:58 -04:00
|
|
|
|
|
|
|
// Start server, automatically configures TLS if certs are available.
|
|
|
|
go func() {
|
2017-07-12 19:33:21 -04:00
|
|
|
globalHTTPServerErrorCh <- globalHTTPServer.Start()
|
2017-03-16 15:21:58 -04:00
|
|
|
}()
|
|
|
|
|
2017-07-12 19:33:21 -04:00
|
|
|
signal.Notify(globalOSSignalCh, os.Interrupt, syscall.SIGTERM)
|
|
|
|
|
2017-03-16 15:21:58 -04:00
|
|
|
// Once endpoints are finalized, initialize the new object api.
|
|
|
|
globalObjLayerMutex.Lock()
|
|
|
|
globalObjectAPI = newObject
|
|
|
|
globalObjLayerMutex.Unlock()
|
|
|
|
|
|
|
|
// Prints the formatted startup message once object layer is initialized.
|
|
|
|
if !quietFlag {
|
2017-10-27 18:07:46 -04:00
|
|
|
mode := globalMinioModeGatewayPrefix + gatewayName
|
2017-06-09 22:50:51 -04:00
|
|
|
// Check update mode.
|
2017-03-16 15:21:58 -04:00
|
|
|
checkUpdate(mode)
|
2017-06-09 22:50:51 -04:00
|
|
|
|
2017-12-05 20:58:09 -05:00
|
|
|
// Print a warning message if gateway is not ready for production before the startup banner.
|
|
|
|
if !gw.Production() {
|
|
|
|
log.Println(colorYellow("\n *** Warning: Not Ready for Production ***"))
|
|
|
|
}
|
|
|
|
|
2017-06-09 22:50:51 -04:00
|
|
|
// Print gateway startup message.
|
2017-10-27 18:07:46 -04:00
|
|
|
printGatewayStartupMessage(getAPIEndpoints(gatewayAddr), gatewayName)
|
2017-03-16 15:21:58 -04:00
|
|
|
}
|
|
|
|
|
2017-07-12 19:33:21 -04:00
|
|
|
handleSignals()
|
2017-03-16 15:21:58 -04:00
|
|
|
}
|