2017-03-16 20:21:58 +01:00
|
|
|
/*
|
2020-03-18 00:37:28 +01:00
|
|
|
* MinIO Cloud Storage, (C) 2017-2020 MinIO, Inc.
|
2017-03-16 20:21:58 +01: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-12 02:44:26 +02:00
|
|
|
"net/url"
|
2017-07-13 01:33:21 +02:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
2017-04-12 02:44:26 +02:00
|
|
|
"strings"
|
2017-07-13 01:33:21 +02:00
|
|
|
"syscall"
|
2017-06-06 00:18:03 +02:00
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/minio/cli"
|
2019-10-04 19:35:33 +02:00
|
|
|
"github.com/minio/minio/cmd/config"
|
2018-04-22 04:23:54 +02:00
|
|
|
xhttp "github.com/minio/minio/cmd/http"
|
2018-04-06 00:04:40 +02:00
|
|
|
"github.com/minio/minio/cmd/logger"
|
2018-05-31 21:30:15 +02:00
|
|
|
"github.com/minio/minio/pkg/certs"
|
2019-10-04 19:35:33 +02:00
|
|
|
"github.com/minio/minio/pkg/color"
|
|
|
|
"github.com/minio/minio/pkg/env"
|
2017-03-16 20:21:58 +01:00
|
|
|
)
|
|
|
|
|
2017-06-10 04:50:51 +02:00
|
|
|
var (
|
|
|
|
gatewayCmd = cli.Command{
|
|
|
|
Name: "gateway",
|
2018-11-21 02:35:33 +01:00
|
|
|
Usage: "start object storage gateway",
|
2019-06-10 16:57:42 +02:00
|
|
|
Flags: append(ServerFlags, GlobalFlags...),
|
2017-06-10 04:50:51 +02:00
|
|
|
HideHelpCommand: true,
|
|
|
|
}
|
|
|
|
)
|
2017-06-09 08:28:45 +02:00
|
|
|
|
2017-10-28 00:07:46 +02:00
|
|
|
// RegisterGatewayCommand registers a new command for gateway.
|
|
|
|
func RegisterGatewayCommand(cmd cli.Command) error {
|
2019-07-03 23:31:19 +02:00
|
|
|
cmd.Flags = append(append(cmd.Flags, ServerFlags...), GlobalFlags...)
|
2017-10-28 00:07:46 +02:00
|
|
|
gatewayCmd.Subcommands = append(gatewayCmd.Subcommands, cmd)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-12-06 02:58:09 +01:00
|
|
|
// ParseGatewayEndpoint - Return endpoint.
|
|
|
|
func ParseGatewayEndpoint(arg string) (endPoint string, secure bool, err error) {
|
2017-04-12 02:44:26 +02:00
|
|
|
schemeSpecified := len(strings.Split(arg, "://")) > 1
|
|
|
|
if !schemeSpecified {
|
|
|
|
// Default connection will be "secure".
|
|
|
|
arg = "https://" + arg
|
|
|
|
}
|
2017-04-27 20:26:00 +02:00
|
|
|
|
2017-04-12 02:44:26 +02: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-06 02:58:09 +01:00
|
|
|
// ValidateGatewayArguments - Validate gateway arguments.
|
|
|
|
func ValidateGatewayArguments(serverAddr, endpointAddr string) error {
|
2017-06-08 20:20:56 +02:00
|
|
|
if err := CheckLocalServerAddr(serverAddr); 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 20:58:29 +01:00
|
|
|
return fmt.Errorf("endpoint points to the local gateway")
|
2017-06-08 20:20:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-12-06 02:58:09 +01:00
|
|
|
// StartGateway - handler for 'minio gateway <name>'.
|
|
|
|
func StartGateway(ctx *cli.Context, gw Gateway) {
|
|
|
|
if gw == nil {
|
2018-05-10 00:11:24 +02:00
|
|
|
logger.FatalIf(errUnexpected, "Gateway implementation not initialized")
|
2017-12-06 02:58:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Validate if we have access, secret set through environment.
|
2019-08-14 20:43:43 +02:00
|
|
|
globalGatewayName = gw.Name()
|
2017-12-06 02:58:09 +01:00
|
|
|
gatewayName := gw.Name()
|
|
|
|
if ctx.Args().First() == "help" {
|
|
|
|
cli.ShowCommandHelpAndExit(ctx, gatewayName, 1)
|
|
|
|
}
|
|
|
|
|
2017-06-10 04:50:51 +02:00
|
|
|
// Handle common command args.
|
|
|
|
handleCommonCmdArgs(ctx)
|
|
|
|
|
2019-12-05 00:32:37 +01:00
|
|
|
// Initialize all help
|
|
|
|
initHelp()
|
|
|
|
|
2018-09-07 01:42:33 +02:00
|
|
|
// Get port to listen on from gateway address
|
2018-12-19 01:08:11 +01:00
|
|
|
globalMinioHost, globalMinioPort = mustSplitHostPort(globalCLIContext.Addr)
|
2018-09-07 01:42:33 +02:00
|
|
|
|
2018-08-18 06:06:36 +02:00
|
|
|
// On macOS, if a process already listens on LOCALIPADDR:PORT, net.Listen() falls back
|
|
|
|
// to IPv6 address ie 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.
|
2019-06-25 00:02:40 +02:00
|
|
|
logger.FatalIf(checkPortAvailability(globalMinioHost, globalMinioPort), "Unable to start the gateway")
|
2018-08-18 06:06:36 +02:00
|
|
|
|
2018-10-28 02:51:00 +02:00
|
|
|
// Check and load TLS certificates.
|
2017-06-10 04:50:51 +02:00
|
|
|
var err error
|
2018-10-28 02:51:00 +02:00
|
|
|
globalPublicCerts, globalTLSCerts, globalIsSSL, err = getTLSConfig()
|
|
|
|
logger.FatalIf(err, "Invalid TLS certificate file")
|
|
|
|
|
|
|
|
// Check and load Root CAs.
|
2019-10-08 07:47:56 +02:00
|
|
|
globalRootCAs, err = config.GetRootCAs(globalCertsCADir.Get())
|
2018-10-28 02:51:00 +02:00
|
|
|
logger.FatalIf(err, "Failed to read root CAs (%v)", err)
|
2017-03-31 07:26:24 +02:00
|
|
|
|
2019-01-05 23:16:43 +01:00
|
|
|
// Handle gateway specific env
|
2019-11-01 23:53:16 +01:00
|
|
|
gatewayHandleEnvVars()
|
2019-01-05 23:16:43 +01:00
|
|
|
|
2017-12-24 15:39:30 +01:00
|
|
|
// Set system resources to maximum.
|
2020-04-09 18:30:02 +02:00
|
|
|
logger.LogIf(GlobalContext, setMaxResources())
|
2017-12-24 15:39:30 +01:00
|
|
|
|
2019-06-08 00:49:13 +02:00
|
|
|
// Set when gateway is enabled
|
|
|
|
globalIsGateway = true
|
|
|
|
|
2019-11-12 12:16:25 +01:00
|
|
|
enableConfigOps := gatewayName == "nas"
|
|
|
|
|
|
|
|
// TODO: We need to move this code with globalConfigSys.Init()
|
|
|
|
// for now keep it here such that "s3" gateway layer initializes
|
|
|
|
// itself properly when KMS is set.
|
|
|
|
|
|
|
|
// Initialize server config.
|
|
|
|
srvCfg := newServerConfig()
|
|
|
|
|
|
|
|
// Override any values from ENVs.
|
2019-12-15 02:27:57 +01:00
|
|
|
lookupConfigs(srvCfg)
|
2019-11-12 12:16:25 +01:00
|
|
|
|
|
|
|
// hold the mutex lock before a new config is assigned.
|
|
|
|
globalServerConfigMu.Lock()
|
|
|
|
globalServerConfig = srvCfg
|
|
|
|
globalServerConfigMu.Unlock()
|
|
|
|
|
2020-02-12 04:38:02 +01:00
|
|
|
// Initialize router. `SkipClean(true)` stops gorilla/mux from
|
|
|
|
// normalizing URL path minio/minio#3256
|
|
|
|
// avoid URL path encoding minio/minio#8950
|
|
|
|
router := mux.NewRouter().SkipClean(true).UseEncodedPath()
|
2017-06-01 18:43:20 +02:00
|
|
|
|
2018-10-18 02:25:16 +02:00
|
|
|
if globalEtcdClient != nil {
|
|
|
|
// Enable STS router if etcd is enabled.
|
|
|
|
registerSTSRouter(router)
|
|
|
|
}
|
|
|
|
|
2019-01-23 20:10:59 +01:00
|
|
|
enableIAMOps := globalEtcdClient != nil
|
|
|
|
|
2018-12-18 22:03:26 +01:00
|
|
|
// Enable IAM admin APIs if etcd is enabled, if not just enable basic
|
|
|
|
// operations such as profiling, server info etc.
|
2019-01-23 20:10:59 +01:00
|
|
|
registerAdminRouter(router, enableConfigOps, enableIAMOps)
|
2018-12-18 22:03:26 +01:00
|
|
|
|
2018-04-05 04:07:54 +02:00
|
|
|
// Add healthcheck router
|
|
|
|
registerHealthCheckRouter(router)
|
|
|
|
|
2018-05-30 06:43:46 +02:00
|
|
|
// Add server metrics router
|
|
|
|
registerMetricsRouter(router)
|
|
|
|
|
2017-06-01 18:43:20 +02:00
|
|
|
// Register web router when its enabled.
|
2019-10-23 07:59:13 +02:00
|
|
|
if globalBrowserEnabled {
|
2018-04-06 00:04:40 +02:00
|
|
|
logger.FatalIf(registerWebRouter(router), "Unable to configure web browser")
|
2017-06-01 18:43:20 +02:00
|
|
|
}
|
2017-03-16 20:21:58 +01:00
|
|
|
|
2019-01-05 23:16:43 +01:00
|
|
|
// Currently only NAS and S3 gateway support encryption headers.
|
|
|
|
encryptionEnabled := gatewayName == "s3" || gatewayName == "nas"
|
2019-06-20 02:37:08 +02:00
|
|
|
allowSSEKMS := gatewayName == "s3" // Only S3 can support SSE-KMS (as pass-through)
|
2019-01-05 23:16:43 +01:00
|
|
|
|
2018-04-05 04:07:54 +02:00
|
|
|
// Add API router.
|
2019-06-20 02:37:08 +02:00
|
|
|
registerAPIRouter(router, encryptionEnabled, allowSSEKMS)
|
2017-03-16 20:21:58 +01:00
|
|
|
|
2018-05-31 21:30:15 +02:00
|
|
|
var getCert certs.GetCertificateFunc
|
|
|
|
if globalTLSCerts != nil {
|
|
|
|
getCert = globalTLSCerts.GetCertificate
|
|
|
|
}
|
|
|
|
|
2019-12-03 00:54:26 +01:00
|
|
|
httpServer := xhttp.NewServer([]string{globalCLIContext.Addr},
|
2019-11-12 12:16:25 +01:00
|
|
|
criticalErrorHandler{registerHandlers(router, globalHandlers...)}, getCert)
|
2017-03-16 20:21:58 +01:00
|
|
|
go func() {
|
2019-12-03 00:54:26 +01:00
|
|
|
globalHTTPServerErrorCh <- httpServer.Start()
|
2017-03-16 20:21:58 +01:00
|
|
|
}()
|
|
|
|
|
2019-12-03 00:54:26 +01:00
|
|
|
globalObjLayerMutex.Lock()
|
|
|
|
globalHTTPServer = httpServer
|
|
|
|
globalObjLayerMutex.Unlock()
|
|
|
|
|
2017-07-13 01:33:21 +02:00
|
|
|
signal.Notify(globalOSSignalCh, os.Interrupt, syscall.SIGTERM)
|
|
|
|
|
2019-10-23 07:59:13 +02:00
|
|
|
newObject, err := gw.NewGatewayLayer(globalActiveCred)
|
2018-12-18 19:42:09 +01:00
|
|
|
if err != nil {
|
|
|
|
// Stop watching for any certificate changes.
|
|
|
|
globalTLSCerts.Stop()
|
|
|
|
|
|
|
|
globalHTTPServer.Shutdown()
|
|
|
|
logger.FatalIf(err, "Unable to initialize gateway backend")
|
|
|
|
}
|
2019-11-20 01:45:35 +01:00
|
|
|
newObject = NewGatewayLayerWithLocker(newObject)
|
|
|
|
|
2019-11-09 18:27:23 +01:00
|
|
|
// Once endpoints are finalized, initialize the new object api in safe mode.
|
|
|
|
globalObjLayerMutex.Lock()
|
|
|
|
globalSafeMode = true
|
|
|
|
globalObjectAPI = newObject
|
|
|
|
globalObjLayerMutex.Unlock()
|
|
|
|
|
2019-11-01 23:53:16 +01:00
|
|
|
// Migrate all backend configs to encrypted backend, also handles rotation as well.
|
|
|
|
// For "nas" gateway we need to specially handle the backend migration as well.
|
|
|
|
// Internally code handles migrating etcd if enabled automatically.
|
|
|
|
logger.FatalIf(handleEncryptedConfigBackend(newObject, enableConfigOps),
|
|
|
|
"Unable to handle encrypted backend for config, iam and policies")
|
|
|
|
|
2019-11-09 18:27:23 +01:00
|
|
|
// Calls all New() for all sub-systems.
|
|
|
|
newAllSubsystems()
|
|
|
|
|
2019-11-01 23:53:16 +01:00
|
|
|
// **** WARNING ****
|
|
|
|
// Migrating to encrypted backend should happen before initialization of any
|
|
|
|
// sub-systems, make sure that we do not move the above codeblock elsewhere.
|
2019-01-23 20:10:59 +01:00
|
|
|
if enableConfigOps {
|
2019-11-09 18:27:23 +01:00
|
|
|
logger.FatalIf(globalConfigSys.Init(newObject), "Unable to initialize config system")
|
2020-04-09 18:30:02 +02:00
|
|
|
buckets, err := newObject.ListBuckets(GlobalContext)
|
2020-02-02 10:52:07 +01:00
|
|
|
if err != nil {
|
|
|
|
logger.Fatal(err, "Unable to list buckets")
|
|
|
|
}
|
2019-07-26 02:41:25 +02:00
|
|
|
|
2020-02-02 10:52:07 +01:00
|
|
|
logger.FatalIf(globalNotificationSys.Init(buckets, newObject), "Unable to initialize notification system")
|
2019-07-26 02:41:25 +02:00
|
|
|
// Start watching disk for reloading config, this
|
|
|
|
// is only enabled for "NAS" gateway.
|
|
|
|
globalConfigSys.WatchConfigNASDisk(newObject)
|
2018-12-18 19:42:09 +01:00
|
|
|
}
|
2018-10-12 21:25:59 +02:00
|
|
|
// This is only to uniquely identify each gateway deployments.
|
2019-10-04 19:35:33 +02:00
|
|
|
globalDeploymentID = env.Get("MINIO_GATEWAY_DEPLOYMENT_ID", mustGetUUID())
|
2019-07-01 21:22:01 +02:00
|
|
|
logger.SetDeploymentID(globalDeploymentID)
|
2018-10-12 21:25:59 +02:00
|
|
|
|
2019-11-01 23:53:16 +01:00
|
|
|
if globalEtcdClient != nil {
|
|
|
|
// **** WARNING ****
|
|
|
|
// Migrating to encrypted backend on etcd should happen before initialization of
|
|
|
|
// IAM sub-systems, make sure that we do not move the above codeblock elsewhere.
|
2020-04-07 23:26:39 +02:00
|
|
|
logger.FatalIf(migrateIAMConfigsEtcdToEncrypted(GlobalContext, globalEtcdClient),
|
2019-11-01 23:53:16 +01:00
|
|
|
"Unable to handle encrypted backend for iam and policies")
|
|
|
|
}
|
|
|
|
|
2019-01-23 20:10:59 +01:00
|
|
|
if enableIAMOps {
|
2018-10-12 20:32:18 +02:00
|
|
|
// Initialize IAM sys.
|
2020-04-07 23:26:39 +02:00
|
|
|
logger.FatalIf(globalIAMSys.Init(GlobalContext, newObject), "Unable to initialize IAM system")
|
2018-10-12 20:32:18 +02:00
|
|
|
}
|
2018-10-09 23:00:01 +02:00
|
|
|
|
2019-11-09 18:27:23 +01:00
|
|
|
if globalCacheConfig.Enabled {
|
|
|
|
// initialize the new disk cache objects.
|
|
|
|
var cacheAPI CacheObjectLayer
|
2020-03-22 20:16:36 +01:00
|
|
|
cacheAPI, err = newServerCacheObjects(GlobalContext, globalCacheConfig)
|
2019-11-09 18:27:23 +01:00
|
|
|
logger.FatalIf(err, "Unable to initialize disk caching")
|
2019-07-19 22:20:33 +02:00
|
|
|
|
2019-11-09 18:27:23 +01:00
|
|
|
globalObjLayerMutex.Lock()
|
|
|
|
globalCacheObjectAPI = cacheAPI
|
|
|
|
globalObjLayerMutex.Unlock()
|
2019-10-31 07:39:09 +01:00
|
|
|
}
|
2018-12-14 22:35:48 +01:00
|
|
|
|
2019-12-17 22:50:07 +01:00
|
|
|
// Populate existing buckets to the etcd backend
|
|
|
|
if globalDNSConfig != nil {
|
2020-03-22 20:16:36 +01:00
|
|
|
buckets, err := newObject.ListBuckets(GlobalContext)
|
2019-12-17 22:50:07 +01:00
|
|
|
if err != nil {
|
|
|
|
logger.Fatal(err, "Unable to list buckets")
|
|
|
|
}
|
|
|
|
initFederatorBackend(buckets, newObject)
|
|
|
|
}
|
|
|
|
|
2019-07-26 11:41:16 +02:00
|
|
|
// Verify if object layer supports
|
|
|
|
// - encryption
|
|
|
|
// - compression
|
|
|
|
verifyObjectLayerFeatures("gateway "+gatewayName, newObject)
|
2018-12-14 22:35:48 +01:00
|
|
|
|
2019-11-16 22:44:28 +01:00
|
|
|
// Disable safe mode operation, after all initialization is over.
|
|
|
|
globalObjLayerMutex.Lock()
|
|
|
|
globalSafeMode = false
|
|
|
|
globalObjLayerMutex.Unlock()
|
|
|
|
|
2017-03-16 20:21:58 +01:00
|
|
|
// Prints the formatted startup message once object layer is initialized.
|
2018-12-19 01:08:11 +01:00
|
|
|
if !globalCLIContext.Quiet {
|
2017-10-28 00:07:46 +02:00
|
|
|
mode := globalMinioModeGatewayPrefix + gatewayName
|
2017-06-10 04:50:51 +02:00
|
|
|
// Check update mode.
|
2017-03-16 20:21:58 +01:00
|
|
|
checkUpdate(mode)
|
2017-06-10 04:50:51 +02:00
|
|
|
|
2017-12-06 02:58:09 +01:00
|
|
|
// Print a warning message if gateway is not ready for production before the startup banner.
|
|
|
|
if !gw.Production() {
|
2019-10-04 19:35:33 +02:00
|
|
|
logStartupMessage(color.Yellow(" *** Warning: Not Ready for Production ***"))
|
2017-12-06 02:58:09 +01:00
|
|
|
}
|
|
|
|
|
2017-06-10 04:50:51 +02:00
|
|
|
// Print gateway startup message.
|
2018-12-14 08:37:46 +01:00
|
|
|
printGatewayStartupMessage(getAPIEndpoints(), gatewayName)
|
2017-03-16 20:21:58 +01:00
|
|
|
}
|
|
|
|
|
2017-07-13 01:33:21 +02:00
|
|
|
handleSignals()
|
2017-03-16 20:21:58 +01:00
|
|
|
}
|