| 
									
										
										
										
											2019-11-23 04:45:13 +08:00
										 |  |  | /* | 
					
						
							|  |  |  |  * MinIO Cloud Storage, (C) 2019 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 ( | 
					
						
							|  |  |  | 	"context" | 
					
						
							|  |  |  | 	"crypto/tls" | 
					
						
							|  |  |  | 	"encoding/json" | 
					
						
							| 
									
										
										
										
											2020-07-11 00:26:21 +08:00
										 |  |  | 	"errors" | 
					
						
							| 
									
										
										
										
											2019-11-23 04:45:13 +08:00
										 |  |  | 	"fmt" | 
					
						
							|  |  |  | 	"io" | 
					
						
							|  |  |  | 	"net/http" | 
					
						
							|  |  |  | 	"net/url" | 
					
						
							|  |  |  | 	"runtime" | 
					
						
							|  |  |  | 	"time" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/gorilla/mux" | 
					
						
							| 
									
										
										
										
											2020-07-15 00:38:05 +08:00
										 |  |  | 	"github.com/minio/minio-go/v7/pkg/set" | 
					
						
							| 
									
										
										
										
											2019-11-23 04:45:13 +08:00
										 |  |  | 	xhttp "github.com/minio/minio/cmd/http" | 
					
						
							|  |  |  | 	"github.com/minio/minio/cmd/logger" | 
					
						
							|  |  |  | 	"github.com/minio/minio/cmd/rest" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const ( | 
					
						
							|  |  |  | 	bootstrapRESTVersion       = "v1" | 
					
						
							|  |  |  | 	bootstrapRESTVersionPrefix = SlashSeparator + bootstrapRESTVersion | 
					
						
							|  |  |  | 	bootstrapRESTPrefix        = minioReservedBucketPath + "/bootstrap" | 
					
						
							|  |  |  | 	bootstrapRESTPath          = bootstrapRESTPrefix + bootstrapRESTVersionPrefix | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const ( | 
					
						
							| 
									
										
										
										
											2020-07-11 00:26:21 +08:00
										 |  |  | 	bootstrapRESTMethodHealth = "/health" | 
					
						
							| 
									
										
										
										
											2019-11-23 04:45:13 +08:00
										 |  |  | 	bootstrapRESTMethodVerify = "/verify" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // To abstract a node over network.
 | 
					
						
							|  |  |  | type bootstrapRESTServer struct{} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // ServerSystemConfig - captures information about server configuration.
 | 
					
						
							|  |  |  | type ServerSystemConfig struct { | 
					
						
							|  |  |  | 	MinioPlatform  string | 
					
						
							|  |  |  | 	MinioRuntime   string | 
					
						
							|  |  |  | 	MinioEndpoints EndpointZones | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Diff - returns error on first difference found in two configs.
 | 
					
						
							|  |  |  | func (s1 ServerSystemConfig) Diff(s2 ServerSystemConfig) error { | 
					
						
							|  |  |  | 	if s1.MinioPlatform != s2.MinioPlatform { | 
					
						
							|  |  |  | 		return fmt.Errorf("Expected platform '%s', found to be running '%s'", | 
					
						
							|  |  |  | 			s1.MinioPlatform, s2.MinioPlatform) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-03-25 03:43:40 +08:00
										 |  |  | 	if s1.MinioEndpoints.NEndpoints() != s2.MinioEndpoints.NEndpoints() { | 
					
						
							|  |  |  | 		return fmt.Errorf("Expected number of endpoints %d, seen %d", s1.MinioEndpoints.NEndpoints(), | 
					
						
							|  |  |  | 			s2.MinioEndpoints.NEndpoints()) | 
					
						
							| 
									
										
										
										
											2019-11-23 04:45:13 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for i, ep := range s1.MinioEndpoints { | 
					
						
							|  |  |  | 		if ep.SetCount != s2.MinioEndpoints[i].SetCount { | 
					
						
							|  |  |  | 			return fmt.Errorf("Expected set count %d, seen %d", ep.SetCount, | 
					
						
							|  |  |  | 				s2.MinioEndpoints[i].SetCount) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if ep.DrivesPerSet != s2.MinioEndpoints[i].DrivesPerSet { | 
					
						
							|  |  |  | 			return fmt.Errorf("Expected drives pet set %d, seen %d", ep.DrivesPerSet, | 
					
						
							|  |  |  | 				s2.MinioEndpoints[i].DrivesPerSet) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		for j, endpoint := range ep.Endpoints { | 
					
						
							|  |  |  | 			if endpoint.String() != s2.MinioEndpoints[i].Endpoints[j].String() { | 
					
						
							|  |  |  | 				return fmt.Errorf("Expected endpoint %s, seen %s", endpoint, | 
					
						
							|  |  |  | 					s2.MinioEndpoints[i].Endpoints[j]) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func getServerSystemCfg() ServerSystemConfig { | 
					
						
							|  |  |  | 	return ServerSystemConfig{ | 
					
						
							|  |  |  | 		MinioPlatform:  fmt.Sprintf("OS: %s | Arch: %s", runtime.GOOS, runtime.GOARCH), | 
					
						
							|  |  |  | 		MinioEndpoints: globalEndpoints, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-11 00:26:21 +08:00
										 |  |  | // HealthHandler returns success if request is valid
 | 
					
						
							|  |  |  | func (b *bootstrapRESTServer) HealthHandler(w http.ResponseWriter, r *http.Request) {} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-23 04:45:13 +08:00
										 |  |  | func (b *bootstrapRESTServer) VerifyHandler(w http.ResponseWriter, r *http.Request) { | 
					
						
							|  |  |  | 	ctx := newContext(r, w, "VerifyHandler") | 
					
						
							|  |  |  | 	cfg := getServerSystemCfg() | 
					
						
							|  |  |  | 	logger.LogIf(ctx, json.NewEncoder(w).Encode(&cfg)) | 
					
						
							|  |  |  | 	w.(http.Flusher).Flush() | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // registerBootstrapRESTHandlers - register bootstrap rest router.
 | 
					
						
							|  |  |  | func registerBootstrapRESTHandlers(router *mux.Router) { | 
					
						
							|  |  |  | 	server := &bootstrapRESTServer{} | 
					
						
							|  |  |  | 	subrouter := router.PathPrefix(bootstrapRESTPrefix).Subrouter() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-11 00:26:21 +08:00
										 |  |  | 	subrouter.Methods(http.MethodPost).Path(bootstrapRESTVersionPrefix + bootstrapRESTMethodHealth).HandlerFunc( | 
					
						
							|  |  |  | 		httpTraceHdrs(server.HealthHandler)) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-23 04:45:13 +08:00
										 |  |  | 	subrouter.Methods(http.MethodPost).Path(bootstrapRESTVersionPrefix + bootstrapRESTMethodVerify).HandlerFunc( | 
					
						
							|  |  |  | 		httpTraceHdrs(server.VerifyHandler)) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-25 03:43:40 +08:00
										 |  |  | // client to talk to bootstrap NEndpoints.
 | 
					
						
							| 
									
										
										
										
											2019-11-23 04:45:13 +08:00
										 |  |  | type bootstrapRESTClient struct { | 
					
						
							|  |  |  | 	endpoint   Endpoint | 
					
						
							|  |  |  | 	restClient *rest.Client | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Wrapper to restClient.Call to handle network errors, in case of network error the connection is marked disconnected
 | 
					
						
							|  |  |  | // permanently. The only way to restore the connection is at the xl-sets layer by xlsets.monitorAndConnectEndpoints()
 | 
					
						
							|  |  |  | // after verifying format.json
 | 
					
						
							|  |  |  | func (client *bootstrapRESTClient) callWithContext(ctx context.Context, method string, values url.Values, body io.Reader, length int64) (respBody io.ReadCloser, err error) { | 
					
						
							|  |  |  | 	if values == nil { | 
					
						
							|  |  |  | 		values = make(url.Values) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	respBody, err = client.restClient.CallWithContext(ctx, method, values, body, length) | 
					
						
							|  |  |  | 	if err == nil { | 
					
						
							|  |  |  | 		return respBody, nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return nil, err | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Stringer provides a canonicalized representation of node.
 | 
					
						
							|  |  |  | func (client *bootstrapRESTClient) String() string { | 
					
						
							|  |  |  | 	return client.endpoint.String() | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Verify - fetches system server config.
 | 
					
						
							| 
									
										
										
										
											2020-07-11 00:26:21 +08:00
										 |  |  | func (client *bootstrapRESTClient) Verify(ctx context.Context, srcCfg ServerSystemConfig) (err error) { | 
					
						
							| 
									
										
										
										
											2019-11-23 04:45:13 +08:00
										 |  |  | 	if newObjectLayerFn() != nil { | 
					
						
							|  |  |  | 		return nil | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-07-11 00:26:21 +08:00
										 |  |  | 	respBody, err := client.callWithContext(ctx, bootstrapRESTMethodVerify, nil, nil, -1) | 
					
						
							| 
									
										
										
										
											2019-11-23 04:45:13 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	defer xhttp.DrainBody(respBody) | 
					
						
							|  |  |  | 	recvCfg := ServerSystemConfig{} | 
					
						
							|  |  |  | 	if err = json.NewDecoder(respBody).Decode(&recvCfg); err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return srcCfg.Diff(recvCfg) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-11 00:26:21 +08:00
										 |  |  | func verifyServerSystemConfig(ctx context.Context, endpointZones EndpointZones) error { | 
					
						
							| 
									
										
										
										
											2019-11-23 04:45:13 +08:00
										 |  |  | 	srcCfg := getServerSystemCfg() | 
					
						
							|  |  |  | 	clnts := newBootstrapRESTClients(endpointZones) | 
					
						
							|  |  |  | 	var onlineServers int | 
					
						
							| 
									
										
										
										
											2020-07-11 00:26:21 +08:00
										 |  |  | 	var offlineEndpoints []string | 
					
						
							|  |  |  | 	var retries int | 
					
						
							| 
									
										
										
										
											2019-11-23 04:45:13 +08:00
										 |  |  | 	for onlineServers < len(clnts)/2 { | 
					
						
							|  |  |  | 		for _, clnt := range clnts { | 
					
						
							| 
									
										
										
										
											2020-07-11 00:26:21 +08:00
										 |  |  | 			if err := clnt.Verify(ctx, srcCfg); err != nil { | 
					
						
							| 
									
										
										
										
											2019-11-23 04:45:13 +08:00
										 |  |  | 				if isNetworkError(err) { | 
					
						
							| 
									
										
										
										
											2020-07-11 00:26:21 +08:00
										 |  |  | 					offlineEndpoints = append(offlineEndpoints, clnt.String()) | 
					
						
							| 
									
										
										
										
											2019-11-23 04:45:13 +08:00
										 |  |  | 					continue | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				return fmt.Errorf("%s as has incorrect configuration: %w", clnt.String(), err) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			onlineServers++ | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		// Sleep for a while - so that we don't go into
 | 
					
						
							|  |  |  | 		// 100% CPU when half the endpoints are offline.
 | 
					
						
							|  |  |  | 		time.Sleep(500 * time.Millisecond) | 
					
						
							| 
									
										
										
										
											2020-07-11 00:26:21 +08:00
										 |  |  | 		retries++ | 
					
						
							|  |  |  | 		// after 5 retries start logging that servers are not reachable yet
 | 
					
						
							|  |  |  | 		if retries >= 5 { | 
					
						
							|  |  |  | 			logger.Info(fmt.Sprintf("Waiting for atleast %d servers to be online for bootstrap check", len(clnts)/2)) | 
					
						
							|  |  |  | 			logger.Info(fmt.Sprintf("Following servers are currently offline or unreachable %s", offlineEndpoints)) | 
					
						
							|  |  |  | 			retries = 0 // reset to log again after 5 retries.
 | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		offlineEndpoints = nil | 
					
						
							| 
									
										
										
										
											2019-11-23 04:45:13 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func newBootstrapRESTClients(endpointZones EndpointZones) []*bootstrapRESTClient { | 
					
						
							|  |  |  | 	seenHosts := set.NewStringSet() | 
					
						
							|  |  |  | 	var clnts []*bootstrapRESTClient | 
					
						
							|  |  |  | 	for _, ep := range endpointZones { | 
					
						
							|  |  |  | 		for _, endpoint := range ep.Endpoints { | 
					
						
							|  |  |  | 			if seenHosts.Contains(endpoint.Host) { | 
					
						
							|  |  |  | 				continue | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			seenHosts.Add(endpoint.Host) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			// Only proceed for remote endpoints.
 | 
					
						
							|  |  |  | 			if !endpoint.IsLocal { | 
					
						
							| 
									
										
										
										
											2020-07-12 13:19:38 +08:00
										 |  |  | 				clnts = append(clnts, newBootstrapRESTClient(endpoint)) | 
					
						
							| 
									
										
										
										
											2019-11-23 04:45:13 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return clnts | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Returns a new bootstrap client.
 | 
					
						
							| 
									
										
										
										
											2020-07-12 13:19:38 +08:00
										 |  |  | func newBootstrapRESTClient(endpoint Endpoint) *bootstrapRESTClient { | 
					
						
							| 
									
										
										
										
											2019-11-23 04:45:13 +08:00
										 |  |  | 	serverURL := &url.URL{ | 
					
						
							|  |  |  | 		Scheme: endpoint.Scheme, | 
					
						
							|  |  |  | 		Host:   endpoint.Host, | 
					
						
							|  |  |  | 		Path:   bootstrapRESTPath, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	var tlsConfig *tls.Config | 
					
						
							|  |  |  | 	if globalIsSSL { | 
					
						
							|  |  |  | 		tlsConfig = &tls.Config{ | 
					
						
							|  |  |  | 			ServerName: endpoint.Hostname(), | 
					
						
							|  |  |  | 			RootCAs:    globalRootCAs, | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-18 02:20:56 +08:00
										 |  |  | 	trFn := newCustomHTTPTransport(tlsConfig, rest.DefaultRESTTimeout) | 
					
						
							| 
									
										
										
										
											2020-07-12 13:19:38 +08:00
										 |  |  | 	restClient := rest.NewClient(serverURL, trFn, newAuthToken) | 
					
						
							| 
									
										
										
										
											2020-07-11 00:26:21 +08:00
										 |  |  | 	restClient.HealthCheckFn = func() bool { | 
					
						
							|  |  |  | 		ctx, cancel := context.WithTimeout(GlobalContext, restClient.HealthCheckTimeout) | 
					
						
							| 
									
										
										
										
											2020-07-12 13:19:38 +08:00
										 |  |  | 		// Instantiate a new rest client for healthcheck
 | 
					
						
							|  |  |  | 		// to avoid recursive healthCheckFn()
 | 
					
						
							|  |  |  | 		respBody, err := rest.NewClient(serverURL, trFn, newAuthToken).CallWithContext(ctx, bootstrapRESTMethodHealth, nil, nil, -1) | 
					
						
							| 
									
										
										
										
											2020-07-11 00:26:21 +08:00
										 |  |  | 		xhttp.DrainBody(respBody) | 
					
						
							|  |  |  | 		cancel() | 
					
						
							|  |  |  | 		var ne *rest.NetworkError | 
					
						
							|  |  |  | 		return !errors.Is(err, context.DeadlineExceeded) && !errors.As(err, &ne) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-12 13:19:38 +08:00
										 |  |  | 	return &bootstrapRESTClient{endpoint: endpoint, restClient: restClient} | 
					
						
							| 
									
										
										
										
											2019-11-23 04:45:13 +08:00
										 |  |  | } |