Skip to content

Commit dd913de

Browse files
Use global HTTP client whenever applicable (#2682)
1 parent 372852e commit dd913de

File tree

9 files changed

+39
-57
lines changed

9 files changed

+39
-57
lines changed

cluster/config.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ package cluster
1919
import (
2020
"io/ioutil"
2121
"net"
22-
"net/http"
2322
"strings"
2423
"time"
2524

2625
xhttp "github.com/minio/console/pkg/http"
26+
"github.com/minio/console/restapi"
2727

2828
"github.com/minio/console/pkg/utils"
2929

@@ -69,11 +69,11 @@ func GetMinioImage() (*string, error) {
6969
if image != "" {
7070
return &image, nil
7171
}
72+
client := restapi.GetConsoleHTTPClient("")
73+
client.Timeout = 5 * time.Second
7274
latestMinIOImage, errLatestMinIOImage := utils.GetLatestMinIOImage(
7375
&xhttp.Client{
74-
Client: &http.Client{
75-
Timeout: 5 * time.Second,
76-
},
76+
Client: client,
7777
})
7878

7979
if errLatestMinIOImage != nil {

operatorapi/marketplace.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
"github.com/minio/console/operatorapi/operations"
3333
"github.com/minio/console/operatorapi/operations/operator_api"
3434
"github.com/minio/console/pkg"
35+
"github.com/minio/console/restapi"
3536
errors "github.com/minio/console/restapi"
3637
"github.com/minio/pkg/env"
3738
corev1 "k8s.io/api/core/v1"
@@ -147,7 +148,8 @@ func makePostRequestToMP(url, email string) error {
147148
if err != nil {
148149
return err
149150
}
150-
client := &http.Client{Timeout: 3 * time.Second}
151+
client := restapi.GetConsoleHTTPClient("")
152+
client.Timeout = 3 * time.Second
151153
if res, err := client.Do(request); err != nil {
152154
return err
153155
} else if res.StatusCode >= http.StatusBadRequest {

operatorapi/proxy.go

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package operatorapi
1919
import (
2020
"bytes"
2121
"crypto/sha1"
22-
"crypto/tls"
2322
"encoding/json"
2423
"errors"
2524
"fmt"
@@ -38,6 +37,7 @@ import (
3837
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3938

4039
"github.com/minio/console/cluster"
40+
"github.com/minio/console/restapi"
4141

4242
"github.com/minio/console/pkg/auth"
4343
)
@@ -148,12 +148,8 @@ func serveProxy(responseWriter http.ResponseWriter, req *http.Request) {
148148
}
149149
loginReq.Header.Add("Content-Type", "application/json")
150150

151-
// FIXME: in the future we should use restapi.GetConsoleHTTPClient()
152-
tr := &http.Transport{
153-
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
154-
}
155-
client := &http.Client{Transport: tr}
156-
151+
// use localhost to ensure 'InsecureSkipVerify'
152+
client := restapi.GetConsoleHTTPClient("http://127.0.0.1")
157153
loginResp, err := client.Do(loginReq)
158154
if err != nil {
159155
log.Println(err)
@@ -222,16 +218,11 @@ func serveProxy(responseWriter http.ResponseWriter, req *http.Request) {
222218
}
223219

224220
func handleHTTPRequest(responseWriter http.ResponseWriter, req *http.Request, proxyCookieJar *cookiejar.Jar, tenantBase string, targetURL *url2.URL) {
225-
tr := &http.Transport{
226-
// FIXME: use restapi.GetConsoleHTTPClient()
227-
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
228-
}
229-
client := &http.Client{
230-
Transport: tr,
231-
Jar: proxyCookieJar,
232-
CheckRedirect: func(req *http.Request, via []*http.Request) error {
233-
return http.ErrUseLastResponse
234-
},
221+
// use localhost to ensure 'InsecureSkipVerify'
222+
client := restapi.GetConsoleHTTPClient("http://127.0.0.1")
223+
client.Jar = proxyCookieJar
224+
client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
225+
return http.ErrUseLastResponse
235226
}
236227

237228
// are we proxying something with cp=y? (console proxy) then add cpb (console proxy base) so the console

operatorapi/tenants.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import (
2626
"errors"
2727
"fmt"
2828
"net"
29-
"net/http"
3029
"sort"
3130
"strconv"
3231
"strings"
@@ -1440,10 +1439,10 @@ func getUpdateTenantResponse(session *models.Principal, params operator_api.Upda
14401439
opClient := &operatorClient{
14411440
client: opClientClientSet,
14421441
}
1442+
client := restapi.GetConsoleHTTPClient("")
1443+
client.Timeout = 4 * time.Second
14431444
httpC := &utils2.Client{
1444-
Client: &http.Client{
1445-
Timeout: 4 * time.Second,
1446-
},
1445+
Client: client,
14471446
}
14481447
if err := updateTenantAction(ctx, opClient, k8sClient, httpC, params.Namespace, params); err != nil {
14491448
return restapi.ErrorWithContext(ctx, err, errors.New("unable to update tenant"))

restapi/admin_config.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,6 @@ func getListConfigResponse(session *models.Principal, params cfgApi.ListConfigPa
124124
}
125125

126126
// getConfig gets the key values for a defined configuration.
127-
//
128-
// FIXME: This currently only returns config parameters in the default target
129-
// `madmin.Default`. Some configuration sub-systems are multi-target and since
130-
// this function does not accept a target argument, it ignores all non-default
131-
// targets.
132127
func getConfig(ctx context.Context, client MinioAdmin, name string) ([]*models.Configuration, error) {
133128
configBytes, err := client.getConfigKV(ctx, name)
134129
if err != nil {

restapi/admin_releases.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"encoding/json"
2222
"fmt"
2323
"net/http"
24+
"net/url"
2425
"time"
2526

2627
"github.com/go-openapi/runtime/middleware"
@@ -78,20 +79,23 @@ func getReleaseServiceURL() string {
7879
return fmt.Sprintf("%s/releases", host)
7980
}
8081

81-
func getReleases(url, repo, currentRelease, search, filter string) (*models.ReleaseListResponse, error) {
82+
func getReleases(endpoint, repo, currentRelease, search, filter string) (*models.ReleaseListResponse, error) {
8283
rl := &models.ReleaseListResponse{}
83-
client := &http.Client{Timeout: time.Second * 5}
84-
req, err := http.NewRequest("GET", url, nil)
85-
q := req.URL.Query()
84+
req, err := http.NewRequest(http.MethodGet, endpoint, nil)
85+
if err != nil {
86+
return nil, err
87+
}
88+
q := &url.Values{}
8689
q.Add("repo", repo)
87-
q.Add("current", currentRelease)
8890
q.Add("search", search)
8991
q.Add("filter", filter)
92+
q.Add("current", currentRelease)
9093
req.URL.RawQuery = q.Encode()
91-
if err != nil {
92-
return nil, err
93-
}
9494
req.Header.Set("Content-Type", "application/json")
95+
96+
client := GetConsoleHTTPClient("")
97+
client.Timeout = time.Second * 5
98+
9599
resp, err := client.Do(req)
96100
if err != nil {
97101
return nil, err

restapi/admin_subnet.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -195,23 +195,17 @@ func SubnetLoginWithMFA(client xhttp.ClientI, username, mfaToken, otp string) (*
195195

196196
// GetSubnetHTTPClient will return a client with proxy if configured, otherwise will return the default console http client
197197
func GetSubnetHTTPClient(ctx context.Context, minioClient MinioAdmin) (*xhttp.Client, error) {
198-
var subnetHTTPClient *http.Client
199-
var proxy string
200-
envProxy := getSubnetProxy()
198+
subnetHTTPClient := GetConsoleHTTPClient("")
201199
subnetKey, err := GetSubnetKeyFromMinIOConfig(ctx, minioClient)
202200
if err != nil {
203201
return nil, err
204202
}
203+
204+
proxy := getSubnetProxy()
205205
if subnetKey.Proxy != "" {
206206
proxy = subnetKey.Proxy
207-
} else if envProxy != "" {
208-
proxy = envProxy
209207
}
210208
if proxy != "" {
211-
transport := PrepareSTSClientTransport(false)
212-
subnetHTTPClient = &http.Client{
213-
Transport: transport,
214-
}
215209
subnetProxyURL, err := url.Parse(proxy)
216210
if err != nil {
217211
return nil, err

restapi/license.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package restapi
1919
import (
2020
"os"
2121

22-
"github.com/minio/console/pkg/http"
2322
"github.com/minio/console/pkg/subnet"
2423
)
2524

@@ -45,10 +44,7 @@ func (sp SubnetPlan) String() string {
4544
var InstanceLicensePlan = PlanAGPL
4645

4746
func fetchLicensePlan() {
48-
client := &http.Client{
49-
Client: GetConsoleHTTPClient(""),
50-
}
51-
licenseInfo, err := subnet.ParseLicense(client, os.Getenv(EnvSubnetLicense))
47+
licenseInfo, err := subnet.ParseLicense(GetConsoleHTTPClient(""), os.Getenv(EnvSubnetLicense))
5248
if err != nil {
5349
return
5450
}

restapi/user_version.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package restapi
1818

1919
import (
2020
"context"
21-
"net/http"
2221
"time"
2322

2423
xhttp "github.com/minio/console/pkg/http"
@@ -44,10 +43,12 @@ func registerVersionHandlers(api *operations.ConsoleAPI) {
4443
func getVersionResponse(params systemApi.CheckMinIOVersionParams) (*models.CheckVersionResponse, *models.Error) {
4544
ctx, cancel := context.WithCancel(params.HTTPRequest.Context())
4645
defer cancel()
46+
47+
client := GetConsoleHTTPClient("")
48+
client.Timeout = 15 * time.Second
49+
4750
ver, err := utils.GetLatestMinIOImage(&xhttp.Client{
48-
Client: &http.Client{
49-
Timeout: 15 * time.Second,
50-
},
51+
Client: client,
5152
})
5253
if err != nil {
5354
return nil, ErrorWithContext(ctx, err)

0 commit comments

Comments
 (0)