2016-12-20 17:45:17 -05:00
|
|
|
/*
|
2019-04-09 14:39:42 -04:00
|
|
|
* MinIO Cloud Storage, (C) 2016 MinIO, Inc.
|
2016-12-20 17:45:17 -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 madmin
|
|
|
|
|
|
|
|
import (
|
2018-01-22 17:54:55 -05:00
|
|
|
"encoding/json"
|
2016-12-20 17:45:17 -05:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
|
2020-07-14 12:38:05 -04:00
|
|
|
"github.com/minio/minio-go/v7/pkg/s3utils"
|
2016-12-20 17:45:17 -05:00
|
|
|
)
|
|
|
|
|
2020-04-07 22:30:59 -04:00
|
|
|
// AdminAPIVersion - admin api version used in the request.
|
2019-10-23 00:01:14 -04:00
|
|
|
const (
|
2020-04-07 22:30:59 -04:00
|
|
|
AdminAPIVersion = "v3"
|
|
|
|
AdminAPIVersionV2 = "v2"
|
|
|
|
adminAPIPrefix = "/" + AdminAPIVersion
|
2019-10-23 00:01:14 -04:00
|
|
|
)
|
|
|
|
|
2018-01-22 17:54:55 -05:00
|
|
|
// jsonDecoder decode json to go type.
|
|
|
|
func jsonDecoder(body io.Reader, v interface{}) error {
|
|
|
|
d := json.NewDecoder(body)
|
2017-01-17 17:25:59 -05:00
|
|
|
return d.Decode(v)
|
|
|
|
}
|
|
|
|
|
2016-12-20 17:45:17 -05:00
|
|
|
// getEndpointURL - construct a new endpoint.
|
|
|
|
func getEndpointURL(endpoint string, secure bool) (*url.URL, error) {
|
|
|
|
if strings.Contains(endpoint, ":") {
|
|
|
|
host, _, err := net.SplitHostPort(endpoint)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !s3utils.IsValidIP(host) && !s3utils.IsValidDomain(host) {
|
|
|
|
msg := "Endpoint: " + endpoint + " does not follow ip address or domain name standards."
|
|
|
|
return nil, ErrInvalidArgument(msg)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if !s3utils.IsValidIP(endpoint) && !s3utils.IsValidDomain(endpoint) {
|
|
|
|
msg := "Endpoint: " + endpoint + " does not follow ip address or domain name standards."
|
|
|
|
return nil, ErrInvalidArgument(msg)
|
|
|
|
}
|
|
|
|
}
|
2020-06-29 15:31:07 -04:00
|
|
|
|
2016-12-20 17:45:17 -05:00
|
|
|
// If secure is false, use 'http' scheme.
|
|
|
|
scheme := "https"
|
|
|
|
if !secure {
|
|
|
|
scheme = "http"
|
|
|
|
}
|
|
|
|
|
2020-06-29 15:31:07 -04:00
|
|
|
// Strip the obvious :443 and :80 from the endpoint
|
|
|
|
// to avoid the signature mismatch error.
|
|
|
|
if secure && strings.HasSuffix(endpoint, ":443") {
|
|
|
|
endpoint = strings.TrimSuffix(endpoint, ":443")
|
|
|
|
}
|
|
|
|
if !secure && strings.HasSuffix(endpoint, ":80") {
|
|
|
|
endpoint = strings.TrimSuffix(endpoint, ":80")
|
|
|
|
}
|
|
|
|
|
2016-12-20 17:45:17 -05:00
|
|
|
// Construct a secured endpoint URL.
|
|
|
|
endpointURLStr := scheme + "://" + endpoint
|
|
|
|
endpointURL, err := url.Parse(endpointURLStr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate incoming endpoint URL.
|
|
|
|
if err := isValidEndpointURL(endpointURL.String()); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return endpointURL, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify if input endpoint URL is valid.
|
|
|
|
func isValidEndpointURL(endpointURL string) error {
|
|
|
|
if endpointURL == "" {
|
|
|
|
return ErrInvalidArgument("Endpoint url cannot be empty.")
|
|
|
|
}
|
|
|
|
url, err := url.Parse(endpointURL)
|
|
|
|
if err != nil {
|
|
|
|
return ErrInvalidArgument("Endpoint url cannot be parsed.")
|
|
|
|
}
|
|
|
|
if url.Path != "/" && url.Path != "" {
|
|
|
|
return ErrInvalidArgument("Endpoint url cannot have fully qualified paths.")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// closeResponse close non nil response with any response Body.
|
|
|
|
// convenient wrapper to drain any remaining data on response body.
|
|
|
|
//
|
|
|
|
// Subsequently this allows golang http RoundTripper
|
|
|
|
// to re-use the same connection for future requests.
|
|
|
|
func closeResponse(resp *http.Response) {
|
|
|
|
// Callers should close resp.Body when done reading from it.
|
|
|
|
// If resp.Body is not closed, the Client's underlying RoundTripper
|
|
|
|
// (typically Transport) may not be able to re-use a persistent TCP
|
|
|
|
// connection to the server for a subsequent "keep-alive" request.
|
|
|
|
if resp != nil && resp.Body != nil {
|
|
|
|
// Drain any remaining Body and then close the connection.
|
|
|
|
// Without this closing connection would disallow re-using
|
|
|
|
// the same connection for future uses.
|
|
|
|
// - http://stackoverflow.com/a/17961593/4465767
|
|
|
|
io.Copy(ioutil.Discard, resp.Body)
|
|
|
|
resp.Body.Close()
|
|
|
|
}
|
|
|
|
}
|