2016-12-16 01:26:15 -05:00
|
|
|
/*
|
2018-09-06 11:03:18 -04:00
|
|
|
* Minio Cloud Storage, (C) 2016, 2017, 2018 Minio, Inc.
|
2016-12-16 01:26:15 -05: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 (
|
2018-09-18 19:46:35 -04:00
|
|
|
"archive/zip"
|
2017-02-27 14:40:27 -05:00
|
|
|
"bytes"
|
2018-04-05 18:04:40 -04:00
|
|
|
"context"
|
2018-09-06 11:03:18 -04:00
|
|
|
"encoding/base64"
|
2016-12-16 01:26:15 -05:00
|
|
|
"encoding/json"
|
2018-04-05 18:04:40 -04:00
|
|
|
"errors"
|
2018-09-18 19:46:35 -04:00
|
|
|
"fmt"
|
2018-01-11 02:06:36 -05:00
|
|
|
"io"
|
2016-12-16 01:26:15 -05:00
|
|
|
"net/http"
|
2018-09-18 19:46:35 -04:00
|
|
|
"os"
|
2018-09-06 11:03:18 -04:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2017-04-21 10:15:53 -04:00
|
|
|
"sync"
|
2017-01-04 02:39:22 -05:00
|
|
|
"time"
|
2017-10-31 14:54:32 -04:00
|
|
|
|
2018-01-22 17:54:55 -05:00
|
|
|
"github.com/gorilla/mux"
|
2018-04-05 18:04:40 -04:00
|
|
|
"github.com/minio/minio/cmd/logger"
|
2017-10-31 14:54:32 -04:00
|
|
|
"github.com/minio/minio/pkg/auth"
|
2018-03-02 18:23:04 -05:00
|
|
|
"github.com/minio/minio/pkg/handlers"
|
2018-01-22 17:54:55 -05:00
|
|
|
"github.com/minio/minio/pkg/madmin"
|
2018-04-13 18:14:19 -04:00
|
|
|
"github.com/minio/minio/pkg/quick"
|
2018-09-06 11:03:18 -04:00
|
|
|
"github.com/tidwall/gjson"
|
|
|
|
"github.com/tidwall/sjson"
|
2016-12-16 01:26:15 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2018-01-11 02:06:36 -05:00
|
|
|
maxConfigJSONSize = 256 * 1024 // 256KiB
|
2016-12-16 01:26:15 -05:00
|
|
|
)
|
|
|
|
|
2017-01-17 13:02:58 -05:00
|
|
|
// Type-safe query params.
|
|
|
|
type mgmtQueryKey string
|
|
|
|
|
2017-03-16 03:15:06 -04:00
|
|
|
// Only valid query params for mgmt admin APIs.
|
2017-01-17 13:02:58 -05:00
|
|
|
const (
|
2018-08-17 21:51:34 -04:00
|
|
|
mgmtBucket mgmtQueryKey = "bucket"
|
|
|
|
mgmtPrefix mgmtQueryKey = "prefix"
|
|
|
|
mgmtClientToken mgmtQueryKey = "clientToken"
|
|
|
|
mgmtForceStart mgmtQueryKey = "forceStart"
|
2017-01-17 13:02:58 -05:00
|
|
|
)
|
|
|
|
|
2018-01-22 17:54:55 -05:00
|
|
|
var (
|
|
|
|
// This struct literal represents the Admin API version that
|
|
|
|
// the server uses.
|
2018-02-15 20:45:57 -05:00
|
|
|
adminAPIVersionInfo = madmin.AdminAPIVersionInfo{
|
|
|
|
Version: "1",
|
|
|
|
}
|
2018-01-22 17:54:55 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// VersionHandler - GET /minio/admin/version
|
|
|
|
// -----------
|
|
|
|
// Returns Administration API version
|
|
|
|
func (a adminAPIHandlers) VersionHandler(w http.ResponseWriter, r *http.Request) {
|
2018-06-07 13:37:31 -04:00
|
|
|
adminAPIErr := checkAdminRequestAuthType(r, "")
|
2018-01-22 17:54:55 -05:00
|
|
|
if adminAPIErr != ErrNone {
|
2018-06-06 21:10:51 -04:00
|
|
|
writeErrorResponseJSON(w, adminAPIErr, r.URL)
|
2018-01-22 17:54:55 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
jsonBytes, err := json.Marshal(adminAPIVersionInfo)
|
|
|
|
if err != nil {
|
|
|
|
writeErrorResponseJSON(w, ErrInternalError, r.URL)
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(context.Background(), err)
|
2018-01-22 17:54:55 -05:00
|
|
|
return
|
|
|
|
}
|
2017-01-23 11:56:06 -05:00
|
|
|
|
2018-01-22 17:54:55 -05:00
|
|
|
writeSuccessResponseJSON(w, jsonBytes)
|
2017-01-23 11:56:06 -05:00
|
|
|
}
|
|
|
|
|
2018-01-22 17:54:55 -05:00
|
|
|
// ServiceStatusHandler - GET /minio/admin/v1/service
|
2016-12-20 21:49:48 -05:00
|
|
|
// ----------
|
2018-01-22 17:54:55 -05:00
|
|
|
// Returns server version and uptime.
|
|
|
|
func (a adminAPIHandlers) ServiceStatusHandler(w http.ResponseWriter, r *http.Request) {
|
2018-06-07 13:37:31 -04:00
|
|
|
adminAPIErr := checkAdminRequestAuthType(r, "")
|
2016-12-16 01:26:15 -05:00
|
|
|
if adminAPIErr != ErrNone {
|
2018-01-22 17:54:55 -05:00
|
|
|
writeErrorResponseJSON(w, adminAPIErr, r.URL)
|
2016-12-16 01:26:15 -05:00
|
|
|
return
|
|
|
|
}
|
2017-01-23 11:56:06 -05:00
|
|
|
|
|
|
|
// Fetch server version
|
2018-01-22 17:54:55 -05:00
|
|
|
serverVersion := madmin.ServerVersion{
|
|
|
|
Version: Version,
|
|
|
|
CommitID: CommitID,
|
|
|
|
}
|
2017-01-23 11:56:06 -05:00
|
|
|
|
2017-02-08 03:13:02 -05:00
|
|
|
// Fetch uptimes from all peers. This may fail to due to lack
|
|
|
|
// of read-quorum availability.
|
|
|
|
uptime, err := getPeerUptimes(globalAdminPeers)
|
|
|
|
if err != nil {
|
2018-01-22 17:54:55 -05:00
|
|
|
writeErrorResponseJSON(w, toAPIErrorCode(err), r.URL)
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(context.Background(), err)
|
2017-02-08 03:13:02 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-01-23 11:56:06 -05:00
|
|
|
// Create API response
|
2018-01-22 17:54:55 -05:00
|
|
|
serverStatus := madmin.ServiceStatus{
|
2017-01-23 11:56:06 -05:00
|
|
|
ServerVersion: serverVersion,
|
2017-02-08 03:13:02 -05:00
|
|
|
Uptime: uptime,
|
2017-01-23 11:56:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Marshal API response
|
|
|
|
jsonBytes, err := json.Marshal(serverStatus)
|
2016-12-16 01:26:15 -05:00
|
|
|
if err != nil {
|
2018-01-22 17:54:55 -05:00
|
|
|
writeErrorResponseJSON(w, ErrInternalError, r.URL)
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(context.Background(), err)
|
2016-12-20 21:05:25 -05:00
|
|
|
return
|
2016-12-16 01:26:15 -05:00
|
|
|
}
|
2017-01-04 02:39:22 -05:00
|
|
|
// Reply with storage information (across nodes in a
|
|
|
|
// distributed setup) as json.
|
2017-01-06 03:37:00 -05:00
|
|
|
writeSuccessResponseJSON(w, jsonBytes)
|
2016-12-16 01:26:15 -05:00
|
|
|
}
|
|
|
|
|
2018-01-22 17:54:55 -05:00
|
|
|
// ServiceStopNRestartHandler - POST /minio/admin/v1/service
|
|
|
|
// Body: {"action": <restart-action>}
|
2016-12-20 21:49:48 -05:00
|
|
|
// ----------
|
2018-01-22 17:54:55 -05:00
|
|
|
// Restarts/Stops minio server gracefully. In a distributed setup,
|
|
|
|
// restarts all the servers in the cluster.
|
|
|
|
func (a adminAPIHandlers) ServiceStopNRestartHandler(w http.ResponseWriter, r *http.Request) {
|
2018-06-07 13:37:31 -04:00
|
|
|
adminAPIErr := checkAdminRequestAuthType(r, "")
|
2016-12-16 01:26:15 -05:00
|
|
|
if adminAPIErr != ErrNone {
|
2018-01-22 17:54:55 -05:00
|
|
|
writeErrorResponseJSON(w, adminAPIErr, r.URL)
|
2017-01-17 17:25:59 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-01-22 17:54:55 -05:00
|
|
|
var sa madmin.ServiceAction
|
|
|
|
err := json.NewDecoder(r.Body).Decode(&sa)
|
2017-01-17 17:25:59 -05:00
|
|
|
if err != nil {
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(context.Background(), err)
|
2018-01-22 17:54:55 -05:00
|
|
|
writeErrorResponseJSON(w, ErrRequestBodyParse, r.URL)
|
2017-01-17 17:25:59 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-01-22 17:54:55 -05:00
|
|
|
var serviceSig serviceSignal
|
|
|
|
switch sa.Action {
|
|
|
|
case madmin.ServiceActionValueRestart:
|
|
|
|
serviceSig = serviceRestart
|
|
|
|
case madmin.ServiceActionValueStop:
|
|
|
|
serviceSig = serviceStop
|
|
|
|
default:
|
|
|
|
writeErrorResponseJSON(w, ErrMalformedPOSTRequest, r.URL)
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(context.Background(), errors.New("Invalid service action received"))
|
2017-01-17 17:25:59 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-01-22 17:54:55 -05:00
|
|
|
// Reply to the client before restarting minio server.
|
|
|
|
writeSuccessResponseHeadersOnly(w)
|
2017-01-17 17:25:59 -05:00
|
|
|
|
2018-01-22 17:54:55 -05:00
|
|
|
sendServiceCmd(globalAdminPeers, serviceSig)
|
2017-01-17 17:25:59 -05:00
|
|
|
}
|
|
|
|
|
2017-02-15 13:45:45 -05:00
|
|
|
// ServerProperties holds some server information such as, version, region
|
|
|
|
// uptime, etc..
|
|
|
|
type ServerProperties struct {
|
|
|
|
Uptime time.Duration `json:"uptime"`
|
|
|
|
Version string `json:"version"`
|
|
|
|
CommitID string `json:"commitID"`
|
|
|
|
Region string `json:"region"`
|
|
|
|
SQSARN []string `json:"sqsARN"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// ServerConnStats holds transferred bytes from/to the server
|
|
|
|
type ServerConnStats struct {
|
|
|
|
TotalInputBytes uint64 `json:"transferred"`
|
|
|
|
TotalOutputBytes uint64 `json:"received"`
|
|
|
|
Throughput uint64 `json:"throughput,omitempty"`
|
|
|
|
}
|
|
|
|
|
2017-04-07 02:08:33 -04:00
|
|
|
// ServerHTTPMethodStats holds total number of HTTP operations from/to the server,
|
|
|
|
// including the average duration the call was spent.
|
|
|
|
type ServerHTTPMethodStats struct {
|
|
|
|
Count uint64 `json:"count"`
|
|
|
|
AvgDuration string `json:"avgDuration"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// ServerHTTPStats holds all type of http operations performed to/from the server
|
|
|
|
// including their average execution time.
|
|
|
|
type ServerHTTPStats struct {
|
|
|
|
TotalHEADStats ServerHTTPMethodStats `json:"totalHEADs"`
|
|
|
|
SuccessHEADStats ServerHTTPMethodStats `json:"successHEADs"`
|
|
|
|
TotalGETStats ServerHTTPMethodStats `json:"totalGETs"`
|
|
|
|
SuccessGETStats ServerHTTPMethodStats `json:"successGETs"`
|
|
|
|
TotalPUTStats ServerHTTPMethodStats `json:"totalPUTs"`
|
|
|
|
SuccessPUTStats ServerHTTPMethodStats `json:"successPUTs"`
|
|
|
|
TotalPOSTStats ServerHTTPMethodStats `json:"totalPOSTs"`
|
|
|
|
SuccessPOSTStats ServerHTTPMethodStats `json:"successPOSTs"`
|
|
|
|
TotalDELETEStats ServerHTTPMethodStats `json:"totalDELETEs"`
|
|
|
|
SuccessDELETEStats ServerHTTPMethodStats `json:"successDELETEs"`
|
|
|
|
}
|
|
|
|
|
2017-04-21 10:15:53 -04:00
|
|
|
// ServerInfoData holds storage, connections and other
|
|
|
|
// information of a given server.
|
|
|
|
type ServerInfoData struct {
|
2017-02-15 13:45:45 -05:00
|
|
|
StorageInfo StorageInfo `json:"storage"`
|
|
|
|
ConnStats ServerConnStats `json:"network"`
|
2017-04-07 02:08:33 -04:00
|
|
|
HTTPStats ServerHTTPStats `json:"http"`
|
2017-02-15 13:45:45 -05:00
|
|
|
Properties ServerProperties `json:"server"`
|
|
|
|
}
|
|
|
|
|
2017-04-21 10:15:53 -04:00
|
|
|
// ServerInfo holds server information result of one node
|
|
|
|
type ServerInfo struct {
|
2017-05-15 10:28:47 -04:00
|
|
|
Error string `json:"error"`
|
|
|
|
Addr string `json:"addr"`
|
|
|
|
Data *ServerInfoData `json:"data"`
|
2017-04-21 10:15:53 -04:00
|
|
|
}
|
|
|
|
|
2018-01-22 17:54:55 -05:00
|
|
|
// ServerInfoHandler - GET /minio/admin/v1/info
|
2017-02-15 13:45:45 -05:00
|
|
|
// ----------
|
|
|
|
// Get server information
|
2018-01-22 17:54:55 -05:00
|
|
|
func (a adminAPIHandlers) ServerInfoHandler(w http.ResponseWriter, r *http.Request) {
|
2017-02-15 13:45:45 -05:00
|
|
|
// Authenticate request
|
2018-06-07 13:37:31 -04:00
|
|
|
|
|
|
|
// Setting the region as empty so as the mc server info command is irrespective to the region.
|
|
|
|
adminAPIErr := checkAdminRequestAuthType(r, "")
|
2017-02-15 13:45:45 -05:00
|
|
|
if adminAPIErr != ErrNone {
|
2018-01-22 17:54:55 -05:00
|
|
|
writeErrorResponseJSON(w, adminAPIErr, r.URL)
|
2017-02-15 13:45:45 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-04-21 10:15:53 -04:00
|
|
|
// Web service response
|
|
|
|
reply := make([]ServerInfo, len(globalAdminPeers))
|
2017-02-15 13:45:45 -05:00
|
|
|
|
2017-04-21 10:15:53 -04:00
|
|
|
var wg sync.WaitGroup
|
2017-02-15 13:45:45 -05:00
|
|
|
|
2017-04-21 10:15:53 -04:00
|
|
|
// Gather server information for all nodes
|
|
|
|
for i, p := range globalAdminPeers {
|
|
|
|
wg.Add(1)
|
2017-02-15 13:45:45 -05:00
|
|
|
|
2017-04-21 10:15:53 -04:00
|
|
|
// Gather information from a peer in a goroutine
|
|
|
|
go func(idx int, peer adminPeer) {
|
|
|
|
defer wg.Done()
|
2017-02-15 13:45:45 -05:00
|
|
|
|
2017-04-21 10:15:53 -04:00
|
|
|
// Initialize server info at index
|
|
|
|
reply[idx] = ServerInfo{Addr: peer.addr}
|
|
|
|
|
2018-06-06 04:51:56 -04:00
|
|
|
serverInfoData, err := peer.cmdRunner.ServerInfo()
|
2017-04-21 10:15:53 -04:00
|
|
|
if err != nil {
|
2018-04-05 18:04:40 -04:00
|
|
|
reqInfo := (&logger.ReqInfo{}).AppendTags("peerAddress", peer.addr)
|
|
|
|
ctx := logger.SetReqInfo(context.Background(), reqInfo)
|
|
|
|
logger.LogIf(ctx, err)
|
2017-05-15 10:28:47 -04:00
|
|
|
reply[idx].Error = err.Error()
|
2017-04-21 10:15:53 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
reply[idx].Data = &serverInfoData
|
|
|
|
}(i, p)
|
2017-02-15 13:45:45 -05:00
|
|
|
}
|
|
|
|
|
2017-04-21 10:15:53 -04:00
|
|
|
wg.Wait()
|
|
|
|
|
2017-02-15 13:45:45 -05:00
|
|
|
// Marshal API response
|
2017-04-21 10:15:53 -04:00
|
|
|
jsonBytes, err := json.Marshal(reply)
|
2017-02-15 13:45:45 -05:00
|
|
|
if err != nil {
|
2018-01-22 17:54:55 -05:00
|
|
|
writeErrorResponseJSON(w, ErrInternalError, r.URL)
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(context.Background(), err)
|
2017-02-15 13:45:45 -05:00
|
|
|
return
|
|
|
|
}
|
2017-04-07 02:08:33 -04:00
|
|
|
|
2017-02-15 13:45:45 -05:00
|
|
|
// Reply with storage information (across nodes in a
|
|
|
|
// distributed setup) as json.
|
|
|
|
writeSuccessResponseJSON(w, jsonBytes)
|
|
|
|
}
|
|
|
|
|
2018-09-18 19:46:35 -04:00
|
|
|
// StartProfilingResult contains the status of the starting
|
|
|
|
// profiling action in a given server
|
|
|
|
type StartProfilingResult struct {
|
|
|
|
NodeName string `json:"nodeName"`
|
|
|
|
Success bool `json:"success"`
|
|
|
|
Error string `json:"error"`
|
|
|
|
}
|
|
|
|
|
2018-09-27 00:02:05 -04:00
|
|
|
// StartProfilingHandler - POST /minio/admin/v1/profiling/start?profilerType={profilerType}
|
2018-09-18 19:46:35 -04:00
|
|
|
// ----------
|
2018-09-27 00:02:05 -04:00
|
|
|
// Enable server profiling
|
2018-09-18 19:46:35 -04:00
|
|
|
func (a adminAPIHandlers) StartProfilingHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
adminAPIErr := checkAdminRequestAuthType(r, "")
|
|
|
|
if adminAPIErr != ErrNone {
|
|
|
|
writeErrorResponseJSON(w, adminAPIErr, r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
vars := mux.Vars(r)
|
2018-09-27 00:02:05 -04:00
|
|
|
profiler := vars["profilerType"]
|
2018-09-18 19:46:35 -04:00
|
|
|
|
|
|
|
startProfilingResult := make([]StartProfilingResult, len(globalAdminPeers))
|
|
|
|
|
|
|
|
// Call StartProfiling function on all nodes and save results
|
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
for i, peer := range globalAdminPeers {
|
|
|
|
wg.Add(1)
|
|
|
|
go func(idx int, peer adminPeer) {
|
|
|
|
defer wg.Done()
|
|
|
|
result := StartProfilingResult{NodeName: peer.addr}
|
|
|
|
if err := peer.cmdRunner.StartProfiling(profiler); err != nil {
|
|
|
|
result.Error = err.Error()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
result.Success = true
|
|
|
|
startProfilingResult[idx] = result
|
|
|
|
}(i, peer)
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
// Create JSON result and send it to the client
|
|
|
|
startProfilingResultInBytes, err := json.Marshal(startProfilingResult)
|
|
|
|
if err != nil {
|
|
|
|
writeCustomErrorResponseJSON(w, http.StatusInternalServerError, err.Error(), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
writeSuccessResponseJSON(w, []byte(startProfilingResultInBytes))
|
|
|
|
}
|
|
|
|
|
|
|
|
// dummyFileInfo represents a dummy representation of a profile data file
|
|
|
|
// present only in memory, it helps to generate the zip stream.
|
|
|
|
type dummyFileInfo struct {
|
|
|
|
name string
|
|
|
|
size int64
|
|
|
|
mode os.FileMode
|
|
|
|
modTime time.Time
|
|
|
|
isDir bool
|
|
|
|
sys interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f dummyFileInfo) Name() string { return f.name }
|
|
|
|
func (f dummyFileInfo) Size() int64 { return f.size }
|
|
|
|
func (f dummyFileInfo) Mode() os.FileMode { return f.mode }
|
|
|
|
func (f dummyFileInfo) ModTime() time.Time { return f.modTime }
|
|
|
|
func (f dummyFileInfo) IsDir() bool { return f.isDir }
|
|
|
|
func (f dummyFileInfo) Sys() interface{} { return f.sys }
|
|
|
|
|
|
|
|
// DownloadProfilingHandler - POST /minio/admin/v1/profiling/download
|
|
|
|
// ----------
|
|
|
|
// Download profiling information of all nodes in a zip format
|
|
|
|
func (a adminAPIHandlers) DownloadProfilingHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
adminAPIErr := checkAdminRequestAuthType(r, "")
|
|
|
|
if adminAPIErr != ErrNone {
|
|
|
|
writeErrorResponseJSON(w, adminAPIErr, r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-09-27 13:34:37 -04:00
|
|
|
profilingDataFound := false
|
2018-09-18 19:46:35 -04:00
|
|
|
|
|
|
|
// Initialize a zip writer which will provide a zipped content
|
|
|
|
// of profiling data of all nodes
|
|
|
|
zipWriter := zip.NewWriter(w)
|
|
|
|
defer zipWriter.Close()
|
|
|
|
|
|
|
|
for i, peer := range globalAdminPeers {
|
|
|
|
// Get profiling data from a node
|
|
|
|
data, err := peer.cmdRunner.DownloadProfilingData()
|
|
|
|
if err != nil {
|
|
|
|
logger.LogIf(context.Background(), fmt.Errorf("Unable to download profiling data from node `%s`, reason: %s", peer.addr, err.Error()))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-09-27 13:34:37 -04:00
|
|
|
profilingDataFound = true
|
|
|
|
|
2018-09-18 19:46:35 -04:00
|
|
|
// Send profiling data to zip as file
|
|
|
|
header, err := zip.FileInfoHeader(dummyFileInfo{
|
|
|
|
name: fmt.Sprintf("profiling-%d", i),
|
|
|
|
size: int64(len(data)),
|
|
|
|
mode: 0600,
|
|
|
|
modTime: time.Now().UTC(),
|
|
|
|
isDir: false,
|
|
|
|
sys: nil,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
writer, err := zipWriter.CreateHeader(header)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if _, err = io.Copy(writer, bytes.NewBuffer(data)); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2018-09-27 13:34:37 -04:00
|
|
|
|
|
|
|
if !profilingDataFound {
|
|
|
|
writeErrorResponseJSON(w, ErrAdminProfilerNotEnabled, r.URL)
|
|
|
|
return
|
|
|
|
}
|
2018-09-18 19:46:35 -04:00
|
|
|
}
|
|
|
|
|
2018-01-22 17:54:55 -05:00
|
|
|
// extractHealInitParams - Validates params for heal init API.
|
|
|
|
func extractHealInitParams(r *http.Request) (bucket, objPrefix string,
|
|
|
|
hs madmin.HealOpts, clientToken string, forceStart bool,
|
|
|
|
err APIErrorCode) {
|
2017-01-17 13:02:58 -05:00
|
|
|
|
2018-01-22 17:54:55 -05:00
|
|
|
vars := mux.Vars(r)
|
|
|
|
bucket = vars[string(mgmtBucket)]
|
|
|
|
objPrefix = vars[string(mgmtPrefix)]
|
2017-01-17 13:02:58 -05:00
|
|
|
|
2018-01-22 17:54:55 -05:00
|
|
|
if bucket == "" {
|
|
|
|
if objPrefix != "" {
|
|
|
|
// Bucket is required if object-prefix is given
|
|
|
|
err = ErrHealMissingBucket
|
|
|
|
return
|
2017-03-16 03:15:06 -04:00
|
|
|
}
|
2018-01-22 17:54:55 -05:00
|
|
|
} else if !IsValidBucketName(bucket) {
|
|
|
|
err = ErrInvalidBucketName
|
2017-01-17 13:02:58 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-01-22 17:54:55 -05:00
|
|
|
// empty prefix is valid.
|
|
|
|
if !IsValidObjectPrefix(objPrefix) {
|
|
|
|
err = ErrInvalidObjectName
|
2018-01-11 13:21:41 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-01-22 17:54:55 -05:00
|
|
|
qParms := r.URL.Query()
|
|
|
|
if len(qParms[string(mgmtClientToken)]) > 0 {
|
|
|
|
clientToken = qParms[string(mgmtClientToken)][0]
|
2017-01-17 13:02:58 -05:00
|
|
|
}
|
2018-01-22 17:54:55 -05:00
|
|
|
if _, ok := qParms[string(mgmtForceStart)]; ok {
|
|
|
|
forceStart = true
|
2017-01-17 13:02:58 -05:00
|
|
|
}
|
|
|
|
|
2018-01-22 17:54:55 -05:00
|
|
|
// ignore body if clientToken is provided
|
|
|
|
if clientToken == "" {
|
|
|
|
jerr := json.NewDecoder(r.Body).Decode(&hs)
|
|
|
|
if jerr != nil {
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(context.Background(), jerr)
|
2018-01-22 17:54:55 -05:00
|
|
|
err = ErrRequestBodyParse
|
|
|
|
return
|
|
|
|
}
|
2017-04-14 13:28:35 -04:00
|
|
|
}
|
|
|
|
|
2018-01-22 17:54:55 -05:00
|
|
|
err = ErrNone
|
|
|
|
return
|
2017-03-31 20:55:15 -04:00
|
|
|
}
|
|
|
|
|
2018-01-22 17:54:55 -05:00
|
|
|
// HealHandler - POST /minio/admin/v1/heal/
|
|
|
|
// -----------
|
|
|
|
// Start heal processing and return heal status items.
|
|
|
|
//
|
|
|
|
// On a successful heal sequence start, a unique client token is
|
|
|
|
// returned. Subsequent requests to this endpoint providing the client
|
|
|
|
// token will receive heal status records from the running heal
|
|
|
|
// sequence.
|
|
|
|
//
|
|
|
|
// If no client token is provided, and a heal sequence is in progress
|
|
|
|
// an error is returned with information about the running heal
|
|
|
|
// sequence. However, if the force-start flag is provided, the server
|
|
|
|
// aborts the running heal sequence and starts a new one.
|
|
|
|
func (a adminAPIHandlers) HealHandler(w http.ResponseWriter, r *http.Request) {
|
2018-07-20 21:46:32 -04:00
|
|
|
ctx := newContext(r, w, "Heal")
|
2018-03-14 15:01:47 -04:00
|
|
|
|
2017-01-17 13:02:58 -05:00
|
|
|
// Get object layer instance.
|
|
|
|
objLayer := newObjectLayerFn()
|
|
|
|
if objLayer == nil {
|
2018-01-22 17:54:55 -05:00
|
|
|
writeErrorResponseJSON(w, ErrServerNotInitialized, r.URL)
|
2017-01-17 13:02:58 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate request signature.
|
2018-06-08 20:54:57 -04:00
|
|
|
adminAPIErr := checkAdminRequestAuthType(r, "")
|
2017-01-17 13:02:58 -05:00
|
|
|
if adminAPIErr != ErrNone {
|
2018-01-22 17:54:55 -05:00
|
|
|
writeErrorResponseJSON(w, adminAPIErr, r.URL)
|
2017-01-17 13:02:58 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-01-11 13:21:41 -05:00
|
|
|
// Check if this setup has an erasure coded backend.
|
|
|
|
if !globalIsXL {
|
2018-01-22 17:54:55 -05:00
|
|
|
writeErrorResponseJSON(w, ErrHealNotImplemented, r.URL)
|
2017-01-24 11:11:05 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-01-22 17:54:55 -05:00
|
|
|
bucket, objPrefix, hs, clientToken, forceStart, apiErr := extractHealInitParams(r)
|
|
|
|
if apiErr != ErrNone {
|
|
|
|
writeErrorResponseJSON(w, apiErr, r.URL)
|
2017-01-23 03:32:55 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-01-22 17:54:55 -05:00
|
|
|
type healResp struct {
|
|
|
|
respBytes []byte
|
|
|
|
errCode APIErrorCode
|
|
|
|
errBody string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Define a closure to start sending whitespace to client
|
|
|
|
// after 10s unless a response item comes in
|
|
|
|
keepConnLive := func(w http.ResponseWriter, respCh chan healResp) {
|
|
|
|
ticker := time.NewTicker(time.Second * 10)
|
2018-05-04 13:43:20 -04:00
|
|
|
defer ticker.Stop()
|
2018-01-22 17:54:55 -05:00
|
|
|
started := false
|
|
|
|
forLoop:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
|
|
|
if !started {
|
|
|
|
// Start writing response to client
|
|
|
|
started = true
|
|
|
|
setCommonHeaders(w)
|
|
|
|
w.Header().Set("Content-Type", string(mimeJSON))
|
|
|
|
// Set 200 OK status
|
|
|
|
w.WriteHeader(200)
|
|
|
|
}
|
|
|
|
// Send whitespace and keep connection open
|
|
|
|
w.Write([]byte("\n\r"))
|
|
|
|
w.(http.Flusher).Flush()
|
|
|
|
case hr := <-respCh:
|
|
|
|
switch {
|
|
|
|
case hr.errCode == ErrNone:
|
|
|
|
writeSuccessResponseJSON(w, hr.respBytes)
|
|
|
|
case hr.errBody == "":
|
|
|
|
writeErrorResponseJSON(w, hr.errCode, r.URL)
|
|
|
|
default:
|
|
|
|
writeCustomErrorResponseJSON(w, hr.errCode, hr.errBody, r.URL)
|
|
|
|
}
|
|
|
|
break forLoop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// find number of disks in the setup
|
2018-03-14 15:01:47 -04:00
|
|
|
info := objLayer.StorageInfo(ctx)
|
2018-01-22 17:54:55 -05:00
|
|
|
numDisks := info.Backend.OfflineDisks + info.Backend.OnlineDisks
|
|
|
|
|
|
|
|
if clientToken == "" {
|
|
|
|
// Not a status request
|
2018-03-02 18:23:04 -05:00
|
|
|
nh := newHealSequence(bucket, objPrefix, handlers.GetSourceIP(r),
|
2018-01-22 17:54:55 -05:00
|
|
|
numDisks, hs, forceStart)
|
|
|
|
|
|
|
|
respCh := make(chan healResp)
|
|
|
|
go func() {
|
|
|
|
respBytes, errCode, errMsg := globalAllHealState.LaunchNewHealSequence(nh)
|
|
|
|
hr := healResp{respBytes, errCode, errMsg}
|
|
|
|
respCh <- hr
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Due to the force-starting functionality, the Launch
|
|
|
|
// call above can take a long time - to keep the
|
|
|
|
// connection alive, we start sending whitespace
|
|
|
|
keepConnLive(w, respCh)
|
|
|
|
} else {
|
|
|
|
// Since clientToken is given, fetch heal status from running
|
|
|
|
// heal sequence.
|
|
|
|
path := bucket + "/" + objPrefix
|
|
|
|
respBytes, errCode := globalAllHealState.PopHealStatusJSON(
|
|
|
|
path, clientToken)
|
|
|
|
if errCode != ErrNone {
|
|
|
|
writeErrorResponseJSON(w, errCode, r.URL)
|
|
|
|
} else {
|
|
|
|
writeSuccessResponseJSON(w, respBytes)
|
|
|
|
}
|
2017-01-23 03:32:55 -05:00
|
|
|
}
|
|
|
|
}
|
2017-02-20 15:58:50 -05:00
|
|
|
|
2018-01-22 17:54:55 -05:00
|
|
|
// GetConfigHandler - GET /minio/admin/v1/config
|
2017-02-20 15:58:50 -05:00
|
|
|
// Get config.json of this minio setup.
|
2018-01-22 17:54:55 -05:00
|
|
|
func (a adminAPIHandlers) GetConfigHandler(w http.ResponseWriter, r *http.Request) {
|
2018-08-17 21:51:34 -04:00
|
|
|
ctx := newContext(r, w, "GetConfigHandler")
|
|
|
|
|
|
|
|
// Get current object layer instance.
|
|
|
|
objectAPI := newObjectLayerFn()
|
|
|
|
if objectAPI == nil {
|
|
|
|
writeErrorResponseJSON(w, ErrServerNotInitialized, r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-02-20 15:58:50 -05:00
|
|
|
// Validate request signature.
|
2018-06-07 13:37:31 -04:00
|
|
|
adminAPIErr := checkAdminRequestAuthType(r, "")
|
2017-02-20 15:58:50 -05:00
|
|
|
if adminAPIErr != ErrNone {
|
2018-01-22 17:54:55 -05:00
|
|
|
writeErrorResponseJSON(w, adminAPIErr, r.URL)
|
2017-02-20 15:58:50 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-08-17 21:51:34 -04:00
|
|
|
config, err := readServerConfig(ctx, objectAPI)
|
|
|
|
if err != nil {
|
|
|
|
writeErrorResponseJSON(w, toAdminAPIErrCode(err), r.URL)
|
2018-01-22 21:09:12 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-09-06 11:03:18 -04:00
|
|
|
configData, err := json.MarshalIndent(config, "", "\t")
|
2017-02-20 15:58:50 -05:00
|
|
|
if err != nil {
|
2018-08-17 21:51:34 -04:00
|
|
|
logger.LogIf(ctx, err)
|
2018-01-22 17:54:55 -05:00
|
|
|
writeErrorResponseJSON(w, toAdminAPIErrCode(err), r.URL)
|
2017-02-20 15:58:50 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-08-17 21:51:34 -04:00
|
|
|
password := config.GetCredential().SecretKey
|
2018-08-15 00:41:47 -04:00
|
|
|
econfigData, err := madmin.EncryptServerConfigData(password, configData)
|
|
|
|
if err != nil {
|
2018-08-17 21:51:34 -04:00
|
|
|
logger.LogIf(ctx, err)
|
2018-08-15 00:41:47 -04:00
|
|
|
writeErrorResponseJSON(w, toAdminAPIErrCode(err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
writeSuccessResponseJSON(w, econfigData)
|
2017-02-20 15:58:50 -05:00
|
|
|
}
|
2017-02-27 14:40:27 -05:00
|
|
|
|
2018-09-06 11:03:18 -04:00
|
|
|
// Disable tidwall json array notation in JSON key path so
|
|
|
|
// users can set json with a key as a number.
|
|
|
|
// In tidwall json, notify.webhook.0 = val means { "notify" : { "webhook" : [val] }}
|
|
|
|
// In Minio, notify.webhook.0 = val means { "notify" : { "webhook" : {"0" : val}}}
|
|
|
|
func normalizeJSONKey(input string) (key string) {
|
|
|
|
subKeys := strings.Split(input, ".")
|
|
|
|
for i, k := range subKeys {
|
|
|
|
if i > 0 {
|
|
|
|
key += "."
|
|
|
|
}
|
|
|
|
if _, err := strconv.Atoi(k); err == nil {
|
|
|
|
key += ":" + k
|
|
|
|
} else {
|
|
|
|
key += k
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetConfigHandler - GET /minio/admin/v1/config-keys
|
|
|
|
// Get some keys in config.json of this minio setup.
|
|
|
|
func (a adminAPIHandlers) GetConfigKeysHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := newContext(r, w, "GetConfigKeysHandler")
|
|
|
|
|
|
|
|
// Get current object layer instance.
|
|
|
|
objectAPI := newObjectLayerFn()
|
|
|
|
if objectAPI == nil {
|
|
|
|
writeErrorResponseJSON(w, ErrServerNotInitialized, r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate request signature.
|
|
|
|
adminAPIErr := checkAdminRequestAuthType(r, "")
|
|
|
|
if adminAPIErr != ErrNone {
|
|
|
|
writeErrorResponseJSON(w, adminAPIErr, r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var keys []string
|
|
|
|
queries := r.URL.Query()
|
|
|
|
|
|
|
|
for k := range queries {
|
|
|
|
keys = append(keys, k)
|
|
|
|
}
|
|
|
|
|
|
|
|
config, err := readServerConfig(ctx, objectAPI)
|
|
|
|
if err != nil {
|
|
|
|
writeErrorResponseJSON(w, toAdminAPIErrCode(err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
configData, err := json.Marshal(config)
|
|
|
|
if err != nil {
|
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
writeErrorResponseJSON(w, toAdminAPIErrCode(err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
configStr := string(configData)
|
|
|
|
newConfigStr := `{}`
|
|
|
|
|
|
|
|
for _, key := range keys {
|
|
|
|
// sjson.Set does not return an error if key is empty
|
|
|
|
// we should check by ourselves here
|
|
|
|
if key == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
val := gjson.Get(configStr, key)
|
|
|
|
if j, err := sjson.Set(newConfigStr, normalizeJSONKey(key), val.Value()); err == nil {
|
|
|
|
newConfigStr = j
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
password := config.GetCredential().SecretKey
|
|
|
|
econfigData, err := madmin.EncryptServerConfigData(password, []byte(newConfigStr))
|
|
|
|
if err != nil {
|
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
writeErrorResponseJSON(w, toAdminAPIErrCode(err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
writeSuccessResponseJSON(w, []byte(econfigData))
|
|
|
|
}
|
|
|
|
|
2017-02-27 14:40:27 -05:00
|
|
|
// toAdminAPIErrCode - converts errXLWriteQuorum error to admin API
|
|
|
|
// specific error.
|
|
|
|
func toAdminAPIErrCode(err error) APIErrorCode {
|
|
|
|
switch err {
|
|
|
|
case errXLWriteQuorum:
|
|
|
|
return ErrAdminConfigNoQuorum
|
2018-08-06 13:26:40 -04:00
|
|
|
default:
|
|
|
|
return toAPIErrorCode(err)
|
2017-02-27 14:40:27 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-22 17:54:55 -05:00
|
|
|
// SetConfigHandler - PUT /minio/admin/v1/config
|
|
|
|
func (a adminAPIHandlers) SetConfigHandler(w http.ResponseWriter, r *http.Request) {
|
2018-08-17 21:51:34 -04:00
|
|
|
ctx := newContext(r, w, "SetConfigHandler")
|
2018-01-22 17:54:55 -05:00
|
|
|
|
2017-02-27 14:40:27 -05:00
|
|
|
// Get current object layer instance.
|
|
|
|
objectAPI := newObjectLayerFn()
|
|
|
|
if objectAPI == nil {
|
2018-01-22 17:54:55 -05:00
|
|
|
writeErrorResponseJSON(w, ErrServerNotInitialized, r.URL)
|
2017-02-27 14:40:27 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate request signature.
|
2018-06-07 13:37:31 -04:00
|
|
|
adminAPIErr := checkAdminRequestAuthType(r, "")
|
2017-02-27 14:40:27 -05:00
|
|
|
if adminAPIErr != ErrNone {
|
2018-01-22 17:54:55 -05:00
|
|
|
writeErrorResponseJSON(w, adminAPIErr, r.URL)
|
2017-02-27 14:40:27 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-09-06 11:03:18 -04:00
|
|
|
// Deny if WORM is enabled
|
|
|
|
if globalWORMEnabled {
|
|
|
|
writeErrorResponseJSON(w, ErrMethodNotAllowed, r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-02-27 14:40:27 -05:00
|
|
|
// Read configuration bytes from request body.
|
2018-01-11 02:06:36 -05:00
|
|
|
configBuf := make([]byte, maxConfigJSONSize+1)
|
|
|
|
n, err := io.ReadFull(r.Body, configBuf)
|
|
|
|
if err == nil {
|
|
|
|
// More than maxConfigSize bytes were available
|
2018-06-06 21:10:51 -04:00
|
|
|
writeErrorResponseJSON(w, ErrAdminConfigTooLarge, r.URL)
|
2018-01-11 02:06:36 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if err != io.ErrUnexpectedEOF {
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(ctx, err)
|
2018-06-06 21:10:51 -04:00
|
|
|
writeErrorResponseJSON(w, toAPIErrorCode(err), r.URL)
|
2017-02-27 14:40:27 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-08-15 00:41:47 -04:00
|
|
|
password := globalServerConfig.GetCredential().SecretKey
|
|
|
|
configBytes, err := madmin.DecryptServerConfigData(password, bytes.NewReader(configBuf[:n]))
|
|
|
|
if err != nil {
|
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
writeErrorResponseJSON(w, ErrAdminConfigBadJSON, r.URL)
|
|
|
|
return
|
|
|
|
}
|
2018-01-11 02:06:36 -05:00
|
|
|
|
|
|
|
// Validate JSON provided in the request body: check the
|
|
|
|
// client has not sent JSON objects with duplicate keys.
|
2018-04-13 18:14:19 -04:00
|
|
|
if err = quick.CheckDuplicateKeys(string(configBytes)); err != nil {
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(ctx, err)
|
2018-06-06 21:10:51 -04:00
|
|
|
writeErrorResponseJSON(w, ErrAdminConfigBadJSON, r.URL)
|
2018-01-11 02:06:36 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-11-29 16:12:47 -05:00
|
|
|
var config serverConfig
|
2017-09-07 14:16:13 -04:00
|
|
|
err = json.Unmarshal(configBytes, &config)
|
|
|
|
if err != nil {
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(ctx, err)
|
2018-07-18 14:22:29 -04:00
|
|
|
writeCustomErrorResponseJSON(w, ErrAdminConfigBadJSON, err.Error(), r.URL)
|
2017-09-07 14:16:13 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-01-11 02:06:36 -05:00
|
|
|
// If credentials for the server are provided via environment,
|
|
|
|
// then credentials in the provided configuration must match.
|
2017-09-07 14:16:13 -04:00
|
|
|
if globalIsEnvCreds {
|
2017-11-29 16:12:47 -05:00
|
|
|
creds := globalServerConfig.GetCredential()
|
2017-09-07 14:16:13 -04:00
|
|
|
if config.Credential.AccessKey != creds.AccessKey ||
|
|
|
|
config.Credential.SecretKey != creds.SecretKey {
|
2018-01-22 17:54:55 -05:00
|
|
|
writeErrorResponseJSON(w, ErrAdminCredentialsMismatch, r.URL)
|
2017-09-07 14:16:13 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-17 21:51:34 -04:00
|
|
|
if err = config.Validate(); err != nil {
|
2018-07-18 14:22:29 -04:00
|
|
|
writeCustomErrorResponseJSON(w, ErrAdminConfigBadJSON, err.Error(), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-09-06 11:03:18 -04:00
|
|
|
if err = saveServerConfig(ctx, objectAPI, &config); err != nil {
|
2018-08-17 21:51:34 -04:00
|
|
|
writeErrorResponseJSON(w, toAdminAPIErrCode(err), r.URL)
|
2017-02-27 14:40:27 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-08-17 21:51:34 -04:00
|
|
|
// Reply to the client before restarting minio server.
|
|
|
|
writeSuccessResponseHeadersOnly(w)
|
2017-02-27 14:40:27 -05:00
|
|
|
}
|
2018-01-22 17:54:55 -05:00
|
|
|
|
2018-09-06 11:03:18 -04:00
|
|
|
func convertValueType(elem []byte, jsonType gjson.Type) (interface{}, error) {
|
|
|
|
str := string(elem)
|
|
|
|
switch jsonType {
|
|
|
|
case gjson.False, gjson.True:
|
|
|
|
return strconv.ParseBool(str)
|
|
|
|
case gjson.JSON:
|
|
|
|
return gjson.Parse(str).Value(), nil
|
|
|
|
case gjson.String:
|
|
|
|
return str, nil
|
|
|
|
case gjson.Number:
|
|
|
|
return strconv.ParseFloat(str, 64)
|
|
|
|
default:
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetConfigKeysHandler - PUT /minio/admin/v1/config-keys
|
|
|
|
func (a adminAPIHandlers) SetConfigKeysHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := newContext(r, w, "SetConfigKeysHandler")
|
|
|
|
|
|
|
|
// Get current object layer instance.
|
|
|
|
objectAPI := newObjectLayerFn()
|
|
|
|
if objectAPI == nil {
|
|
|
|
writeErrorResponseJSON(w, ErrServerNotInitialized, r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deny if WORM is enabled
|
|
|
|
if globalWORMEnabled {
|
|
|
|
writeErrorResponseJSON(w, ErrMethodNotAllowed, r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate request signature.
|
|
|
|
adminAPIErr := checkAdminRequestAuthType(r, "")
|
|
|
|
if adminAPIErr != ErrNone {
|
|
|
|
writeErrorResponseJSON(w, adminAPIErr, r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load config
|
|
|
|
configStruct, err := readServerConfig(ctx, objectAPI)
|
|
|
|
if err != nil {
|
|
|
|
writeErrorResponseJSON(w, toAdminAPIErrCode(err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert config to json bytes
|
|
|
|
configBytes, err := json.Marshal(configStruct)
|
|
|
|
if err != nil {
|
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
writeErrorResponseJSON(w, toAdminAPIErrCode(err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
configStr := string(configBytes)
|
|
|
|
|
|
|
|
queries := r.URL.Query()
|
|
|
|
password := globalServerConfig.GetCredential().SecretKey
|
|
|
|
|
|
|
|
// Set key values in the JSON config
|
|
|
|
for k := range queries {
|
|
|
|
// Decode encrypted data associated to the current key
|
|
|
|
encryptedElem, dErr := base64.StdEncoding.DecodeString(queries.Get(k))
|
|
|
|
if dErr != nil {
|
|
|
|
reqInfo := (&logger.ReqInfo{}).AppendTags("key", k)
|
|
|
|
ctx = logger.SetReqInfo(ctx, reqInfo)
|
|
|
|
logger.LogIf(ctx, dErr)
|
|
|
|
writeErrorResponseJSON(w, ErrAdminConfigBadJSON, r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
elem, dErr := madmin.DecryptServerConfigData(password, bytes.NewBuffer([]byte(encryptedElem)))
|
|
|
|
if dErr != nil {
|
|
|
|
logger.LogIf(ctx, dErr)
|
|
|
|
writeErrorResponseJSON(w, ErrAdminConfigBadJSON, r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Calculate the type of the current key from the
|
|
|
|
// original config json
|
|
|
|
jsonFieldType := gjson.Get(configStr, k).Type
|
|
|
|
// Convert passed value to json filed type
|
|
|
|
val, cErr := convertValueType(elem, jsonFieldType)
|
|
|
|
if cErr != nil {
|
|
|
|
writeCustomErrorResponseJSON(w, ErrAdminConfigBadJSON, cErr.Error(), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Set the key/value in the new json document
|
|
|
|
if s, sErr := sjson.Set(configStr, normalizeJSONKey(k), val); sErr == nil {
|
|
|
|
configStr = s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
configBytes = []byte(configStr)
|
|
|
|
|
|
|
|
// Validate config
|
|
|
|
var config serverConfig
|
|
|
|
if err = json.Unmarshal(configBytes, &config); err != nil {
|
|
|
|
writeCustomErrorResponseJSON(w, ErrAdminConfigBadJSON, err.Error(), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = config.Validate(); err != nil {
|
|
|
|
writeCustomErrorResponseJSON(w, ErrAdminConfigBadJSON, err.Error(), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = config.TestNotificationTargets(); err != nil {
|
|
|
|
writeCustomErrorResponseJSON(w, ErrAdminConfigBadJSON, err.Error(), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// If credentials for the server are provided via environment,
|
|
|
|
// then credentials in the provided configuration must match.
|
|
|
|
if globalIsEnvCreds {
|
|
|
|
creds := globalServerConfig.GetCredential()
|
|
|
|
if config.Credential.AccessKey != creds.AccessKey ||
|
|
|
|
config.Credential.SecretKey != creds.SecretKey {
|
|
|
|
writeErrorResponseJSON(w, ErrAdminCredentialsMismatch, r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = saveServerConfig(ctx, objectAPI, &config); err != nil {
|
|
|
|
writeErrorResponseJSON(w, toAdminAPIErrCode(err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send success response
|
|
|
|
writeSuccessResponseHeadersOnly(w)
|
|
|
|
}
|
|
|
|
|
2018-08-17 21:51:34 -04:00
|
|
|
// UpdateCredsHandler - POST /minio/admin/v1/config/credential
|
2018-01-22 17:54:55 -05:00
|
|
|
// ----------
|
|
|
|
// Update credentials in a minio server. In a distributed setup,
|
|
|
|
// update all the servers in the cluster.
|
|
|
|
func (a adminAPIHandlers) UpdateCredentialsHandler(w http.ResponseWriter,
|
|
|
|
r *http.Request) {
|
|
|
|
|
2018-08-17 21:51:34 -04:00
|
|
|
ctx := newContext(r, w, "UpdateCredentialsHandler")
|
|
|
|
|
|
|
|
// Get current object layer instance.
|
|
|
|
objectAPI := newObjectLayerFn()
|
|
|
|
if objectAPI == nil {
|
|
|
|
writeErrorResponseJSON(w, ErrServerNotInitialized, r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Avoid setting new credentials when they are already passed
|
|
|
|
// by the environment. Deny if WORM is enabled.
|
|
|
|
if globalIsEnvCreds {
|
|
|
|
writeErrorResponseJSON(w, ErrMethodNotAllowed, r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-01-22 17:54:55 -05:00
|
|
|
// Authenticate request
|
2018-06-07 13:37:31 -04:00
|
|
|
adminAPIErr := checkAdminRequestAuthType(r, "")
|
2018-01-22 17:54:55 -05:00
|
|
|
if adminAPIErr != ErrNone {
|
2018-06-06 21:10:51 -04:00
|
|
|
writeErrorResponseJSON(w, adminAPIErr, r.URL)
|
2018-01-22 17:54:55 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-08-17 21:51:34 -04:00
|
|
|
// Read configuration bytes from request body.
|
|
|
|
configBuf := make([]byte, maxConfigJSONSize+1)
|
|
|
|
n, err := io.ReadFull(r.Body, configBuf)
|
|
|
|
if err == nil {
|
|
|
|
// More than maxConfigSize bytes were available
|
|
|
|
writeErrorResponseJSON(w, ErrAdminConfigTooLarge, r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err != io.ErrUnexpectedEOF {
|
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
writeErrorResponseJSON(w, toAPIErrorCode(err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
password := globalServerConfig.GetCredential().SecretKey
|
|
|
|
configBytes, err := madmin.DecryptServerConfigData(password, bytes.NewReader(configBuf[:n]))
|
|
|
|
if err != nil {
|
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
writeErrorResponseJSON(w, ErrAdminConfigBadJSON, r.URL)
|
2018-01-22 17:54:55 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Decode request body
|
|
|
|
var req madmin.SetCredsReq
|
2018-08-17 21:51:34 -04:00
|
|
|
if err = json.Unmarshal(configBytes, &req); err != nil {
|
|
|
|
logger.LogIf(ctx, err)
|
2018-01-22 17:54:55 -05:00
|
|
|
writeErrorResponseJSON(w, ErrRequestBodyParse, r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
creds, err := auth.CreateCredentials(req.AccessKey, req.SecretKey)
|
|
|
|
if err != nil {
|
2018-06-06 21:10:51 -04:00
|
|
|
writeErrorResponseJSON(w, toAPIErrorCode(err), r.URL)
|
2018-01-22 17:54:55 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-01-31 11:15:54 -05:00
|
|
|
// Acquire lock before updating global configuration.
|
|
|
|
globalServerConfigMu.Lock()
|
|
|
|
defer globalServerConfigMu.Unlock()
|
|
|
|
|
2018-01-22 17:54:55 -05:00
|
|
|
// Update local credentials in memory.
|
|
|
|
globalServerConfig.SetCredential(creds)
|
2018-08-15 00:41:47 -04:00
|
|
|
|
2018-09-06 11:03:18 -04:00
|
|
|
if err = saveServerConfig(ctx, objectAPI, globalServerConfig); err != nil {
|
2018-08-15 00:41:47 -04:00
|
|
|
writeErrorResponseJSON(w, toAdminAPIErrCode(err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-06-06 04:51:56 -04:00
|
|
|
// Notify all other Minio peers to update credentials
|
2018-08-15 00:41:47 -04:00
|
|
|
for host, err := range globalNotificationSys.LoadCredentials() {
|
2018-09-06 11:03:18 -04:00
|
|
|
if err != nil {
|
|
|
|
logger.GetReqInfo(ctx).SetTags("peerAddress", host.String())
|
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
}
|
2018-06-06 04:51:56 -04:00
|
|
|
}
|
|
|
|
|
2018-08-17 21:51:34 -04:00
|
|
|
// Reply to the client before restarting minio server.
|
|
|
|
writeSuccessResponseHeadersOnly(w)
|
2018-01-22 17:54:55 -05:00
|
|
|
}
|