Use global HTTP client whenever applicable (#2682)

This commit is contained in:
Harshavardhana
2023-02-27 17:19:56 -08:00
committed by GitHub
parent 372852ee86
commit dd913decc6
9 changed files with 40 additions and 58 deletions

View File

@@ -19,11 +19,11 @@ package cluster
import ( import (
"io/ioutil" "io/ioutil"
"net" "net"
"net/http"
"strings" "strings"
"time" "time"
xhttp "github.com/minio/console/pkg/http" xhttp "github.com/minio/console/pkg/http"
"github.com/minio/console/restapi"
"github.com/minio/console/pkg/utils" "github.com/minio/console/pkg/utils"
@@ -69,11 +69,11 @@ func GetMinioImage() (*string, error) {
if image != "" { if image != "" {
return &image, nil return &image, nil
} }
client := restapi.GetConsoleHTTPClient("")
client.Timeout = 5 * time.Second
latestMinIOImage, errLatestMinIOImage := utils.GetLatestMinIOImage( latestMinIOImage, errLatestMinIOImage := utils.GetLatestMinIOImage(
&xhttp.Client{ &xhttp.Client{
Client: &http.Client{ Client: client,
Timeout: 5 * time.Second,
},
}) })
if errLatestMinIOImage != nil { if errLatestMinIOImage != nil {

View File

@@ -32,6 +32,7 @@ import (
"github.com/minio/console/operatorapi/operations" "github.com/minio/console/operatorapi/operations"
"github.com/minio/console/operatorapi/operations/operator_api" "github.com/minio/console/operatorapi/operations/operator_api"
"github.com/minio/console/pkg" "github.com/minio/console/pkg"
"github.com/minio/console/restapi"
errors "github.com/minio/console/restapi" errors "github.com/minio/console/restapi"
"github.com/minio/pkg/env" "github.com/minio/pkg/env"
corev1 "k8s.io/api/core/v1" corev1 "k8s.io/api/core/v1"
@@ -147,7 +148,8 @@ func makePostRequestToMP(url, email string) error {
if err != nil { if err != nil {
return err return err
} }
client := &http.Client{Timeout: 3 * time.Second} client := restapi.GetConsoleHTTPClient("")
client.Timeout = 3 * time.Second
if res, err := client.Do(request); err != nil { if res, err := client.Do(request); err != nil {
return err return err
} else if res.StatusCode >= http.StatusBadRequest { } else if res.StatusCode >= http.StatusBadRequest {

View File

@@ -19,7 +19,6 @@ package operatorapi
import ( import (
"bytes" "bytes"
"crypto/sha1" "crypto/sha1"
"crypto/tls"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
@@ -38,6 +37,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/minio/console/cluster" "github.com/minio/console/cluster"
"github.com/minio/console/restapi"
"github.com/minio/console/pkg/auth" "github.com/minio/console/pkg/auth"
) )
@@ -148,12 +148,8 @@ func serveProxy(responseWriter http.ResponseWriter, req *http.Request) {
} }
loginReq.Header.Add("Content-Type", "application/json") loginReq.Header.Add("Content-Type", "application/json")
// FIXME: in the future we should use restapi.GetConsoleHTTPClient() // use localhost to ensure 'InsecureSkipVerify'
tr := &http.Transport{ client := restapi.GetConsoleHTTPClient("http://127.0.0.1")
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{Transport: tr}
loginResp, err := client.Do(loginReq) loginResp, err := client.Do(loginReq)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
@@ -222,16 +218,11 @@ func serveProxy(responseWriter http.ResponseWriter, req *http.Request) {
} }
func handleHTTPRequest(responseWriter http.ResponseWriter, req *http.Request, proxyCookieJar *cookiejar.Jar, tenantBase string, targetURL *url2.URL) { func handleHTTPRequest(responseWriter http.ResponseWriter, req *http.Request, proxyCookieJar *cookiejar.Jar, tenantBase string, targetURL *url2.URL) {
tr := &http.Transport{ // use localhost to ensure 'InsecureSkipVerify'
// FIXME: use restapi.GetConsoleHTTPClient() client := restapi.GetConsoleHTTPClient("http://127.0.0.1")
TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, client.Jar = proxyCookieJar
} client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
client := &http.Client{ return http.ErrUseLastResponse
Transport: tr,
Jar: proxyCookieJar,
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
} }
// are we proxying something with cp=y? (console proxy) then add cpb (console proxy base) so the console // are we proxying something with cp=y? (console proxy) then add cpb (console proxy base) so the console

View File

@@ -26,7 +26,6 @@ import (
"errors" "errors"
"fmt" "fmt"
"net" "net"
"net/http"
"sort" "sort"
"strconv" "strconv"
"strings" "strings"
@@ -1440,10 +1439,10 @@ func getUpdateTenantResponse(session *models.Principal, params operator_api.Upda
opClient := &operatorClient{ opClient := &operatorClient{
client: opClientClientSet, client: opClientClientSet,
} }
client := restapi.GetConsoleHTTPClient("")
client.Timeout = 4 * time.Second
httpC := &utils2.Client{ httpC := &utils2.Client{
Client: &http.Client{ Client: client,
Timeout: 4 * time.Second,
},
} }
if err := updateTenantAction(ctx, opClient, k8sClient, httpC, params.Namespace, params); err != nil { if err := updateTenantAction(ctx, opClient, k8sClient, httpC, params.Namespace, params); err != nil {
return restapi.ErrorWithContext(ctx, err, errors.New("unable to update tenant")) return restapi.ErrorWithContext(ctx, err, errors.New("unable to update tenant"))

View File

@@ -124,11 +124,6 @@ func getListConfigResponse(session *models.Principal, params cfgApi.ListConfigPa
} }
// getConfig gets the key values for a defined configuration. // getConfig gets the key values for a defined configuration.
//
// FIXME: This currently only returns config parameters in the default target
// `madmin.Default`. Some configuration sub-systems are multi-target and since
// this function does not accept a target argument, it ignores all non-default
// targets.
func getConfig(ctx context.Context, client MinioAdmin, name string) ([]*models.Configuration, error) { func getConfig(ctx context.Context, client MinioAdmin, name string) ([]*models.Configuration, error) {
configBytes, err := client.getConfigKV(ctx, name) configBytes, err := client.getConfigKV(ctx, name)
if err != nil { if err != nil {

View File

@@ -21,6 +21,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"net/http" "net/http"
"net/url"
"time" "time"
"github.com/go-openapi/runtime/middleware" "github.com/go-openapi/runtime/middleware"
@@ -78,20 +79,23 @@ func getReleaseServiceURL() string {
return fmt.Sprintf("%s/releases", host) return fmt.Sprintf("%s/releases", host)
} }
func getReleases(url, repo, currentRelease, search, filter string) (*models.ReleaseListResponse, error) { func getReleases(endpoint, repo, currentRelease, search, filter string) (*models.ReleaseListResponse, error) {
rl := &models.ReleaseListResponse{} rl := &models.ReleaseListResponse{}
client := &http.Client{Timeout: time.Second * 5} req, err := http.NewRequest(http.MethodGet, endpoint, nil)
req, err := http.NewRequest("GET", url, nil)
q := req.URL.Query()
q.Add("repo", repo)
q.Add("current", currentRelease)
q.Add("search", search)
q.Add("filter", filter)
req.URL.RawQuery = q.Encode()
if err != nil { if err != nil {
return nil, err return nil, err
} }
q := &url.Values{}
q.Add("repo", repo)
q.Add("search", search)
q.Add("filter", filter)
q.Add("current", currentRelease)
req.URL.RawQuery = q.Encode()
req.Header.Set("Content-Type", "application/json") req.Header.Set("Content-Type", "application/json")
client := GetConsoleHTTPClient("")
client.Timeout = time.Second * 5
resp, err := client.Do(req) resp, err := client.Do(req)
if err != nil { if err != nil {
return nil, err return nil, err

View File

@@ -195,23 +195,17 @@ func SubnetLoginWithMFA(client xhttp.ClientI, username, mfaToken, otp string) (*
// GetSubnetHTTPClient will return a client with proxy if configured, otherwise will return the default console http client // GetSubnetHTTPClient will return a client with proxy if configured, otherwise will return the default console http client
func GetSubnetHTTPClient(ctx context.Context, minioClient MinioAdmin) (*xhttp.Client, error) { func GetSubnetHTTPClient(ctx context.Context, minioClient MinioAdmin) (*xhttp.Client, error) {
var subnetHTTPClient *http.Client subnetHTTPClient := GetConsoleHTTPClient("")
var proxy string
envProxy := getSubnetProxy()
subnetKey, err := GetSubnetKeyFromMinIOConfig(ctx, minioClient) subnetKey, err := GetSubnetKeyFromMinIOConfig(ctx, minioClient)
if err != nil { if err != nil {
return nil, err return nil, err
} }
proxy := getSubnetProxy()
if subnetKey.Proxy != "" { if subnetKey.Proxy != "" {
proxy = subnetKey.Proxy proxy = subnetKey.Proxy
} else if envProxy != "" {
proxy = envProxy
} }
if proxy != "" { if proxy != "" {
transport := PrepareSTSClientTransport(false)
subnetHTTPClient = &http.Client{
Transport: transport,
}
subnetProxyURL, err := url.Parse(proxy) subnetProxyURL, err := url.Parse(proxy)
if err != nil { if err != nil {
return nil, err return nil, err

View File

@@ -19,7 +19,6 @@ package restapi
import ( import (
"os" "os"
"github.com/minio/console/pkg/http"
"github.com/minio/console/pkg/subnet" "github.com/minio/console/pkg/subnet"
) )
@@ -45,10 +44,7 @@ func (sp SubnetPlan) String() string {
var InstanceLicensePlan = PlanAGPL var InstanceLicensePlan = PlanAGPL
func fetchLicensePlan() { func fetchLicensePlan() {
client := &http.Client{ licenseInfo, err := subnet.ParseLicense(GetConsoleHTTPClient(""), os.Getenv(EnvSubnetLicense))
Client: GetConsoleHTTPClient(""),
}
licenseInfo, err := subnet.ParseLicense(client, os.Getenv(EnvSubnetLicense))
if err != nil { if err != nil {
return return
} }

View File

@@ -18,7 +18,6 @@ package restapi
import ( import (
"context" "context"
"net/http"
"time" "time"
xhttp "github.com/minio/console/pkg/http" xhttp "github.com/minio/console/pkg/http"
@@ -44,10 +43,12 @@ func registerVersionHandlers(api *operations.ConsoleAPI) {
func getVersionResponse(params systemApi.CheckMinIOVersionParams) (*models.CheckVersionResponse, *models.Error) { func getVersionResponse(params systemApi.CheckMinIOVersionParams) (*models.CheckVersionResponse, *models.Error) {
ctx, cancel := context.WithCancel(params.HTTPRequest.Context()) ctx, cancel := context.WithCancel(params.HTTPRequest.Context())
defer cancel() defer cancel()
client := GetConsoleHTTPClient("")
client.Timeout = 15 * time.Second
ver, err := utils.GetLatestMinIOImage(&xhttp.Client{ ver, err := utils.GetLatestMinIOImage(&xhttp.Client{
Client: &http.Client{ Client: client,
Timeout: 15 * time.Second,
},
}) })
if err != nil { if err != nil {
return nil, ErrorWithContext(ctx, err) return nil, ErrorWithContext(ctx, err)