add license banner for GNU AGPLv3 (#15178)

Bonus: rewrite subnet re-use of Transport
This commit is contained in:
Harshavardhana
2022-06-27 03:58:25 -07:00
committed by GitHub
parent 7d4fce09dc
commit 7b9b7cef11
7 changed files with 80 additions and 68 deletions

View File

@@ -18,6 +18,10 @@
package subnet
import (
"net/http"
"net/url"
"strings"
"github.com/minio/minio/internal/config"
"github.com/minio/pkg/env"
xnet "github.com/minio/pkg/net"
@@ -49,10 +53,13 @@ type Config struct {
// The HTTP(S) proxy URL to use for connecting to SUBNET
ProxyURL *xnet.URL `json:"proxy_url"`
// Transport configured with proxy_url if set optionally.
transport *http.Transport
}
// LookupConfig - lookup config and override with valid environment settings if any.
func LookupConfig(kvs config.KVS) (cfg Config, err error) {
func LookupConfig(kvs config.KVS, transport http.RoundTripper) (cfg Config, err error) {
if err = config.CheckValidKeys(config.SubnetSubSys, kvs, DefaultKVS); err != nil {
return cfg, err
}
@@ -63,10 +70,24 @@ func LookupConfig(kvs config.KVS) (cfg Config, err error) {
if err != nil {
return cfg, err
}
}
cfg.License = env.Get(config.EnvMinIOSubnetLicense, kvs.Get(config.License))
cfg.APIKey = env.Get(config.EnvMinIOSubnetAPIKey, kvs.Get(config.APIKey))
cfg.License = strings.TrimSpace(env.Get(config.EnvMinIOSubnetLicense, kvs.Get(config.License)))
cfg.APIKey = strings.TrimSpace(env.Get(config.EnvMinIOSubnetAPIKey, kvs.Get(config.APIKey)))
if transport == nil {
// when transport is nil, it means we are just validating the
// inputs not performing any network calls.
return cfg, nil
}
// Make sure to clone the transport before editing the ProxyURL
ctransport := transport.(*http.Transport).Clone()
if cfg.ProxyURL != nil {
ctransport.Proxy = http.ProxyURL((*url.URL)(cfg.ProxyURL))
}
cfg.transport = ctransport
return cfg, nil
}

View File

@@ -0,0 +1,76 @@
// Copyright (c) 2015-2022 MinIO, Inc.
//
// This file is part of MinIO Object Storage stack
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package subnet
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"time"
xhttp "github.com/minio/minio/internal/http"
)
const (
respBodyLimit = 1 << 20 // 1 MiB
)
// Post submit 'payload' to specified URL
func (c Config) Post(reqURL string, payload interface{}) (string, error) {
if len(c.APIKey) == 0 {
return "", errors.New("Deployment is not registered with SUBNET. Please register the deployment via 'mc support register ALIAS'")
}
body, err := json.Marshal(payload)
if err != nil {
return "", err
}
r, err := http.NewRequest(http.MethodPost, reqURL, bytes.NewReader(body))
if err != nil {
return "", err
}
r.Header.Set("Authorization", c.APIKey)
r.Header.Set("Content-Type", "application/json")
client := &http.Client{
Timeout: 10 * time.Second,
Transport: c.transport,
}
resp, err := client.Do(r)
if err != nil {
return "", err
}
defer xhttp.DrainBody(resp.Body)
respBytes, err := ioutil.ReadAll(io.LimitReader(resp.Body, respBodyLimit))
if err != nil {
return "", err
}
respStr := string(respBytes)
if resp.StatusCode == http.StatusOK {
return respStr, nil
}
return respStr, fmt.Errorf("SUBNET request failed with code %d and error: %s", resp.StatusCode, respStr)
}