Skip to content

Commit 6e8f5e0

Browse files
authored
Fixed subnet Health report upload (#2927)
Fixed subnet file upload to allow upload of different report types
1 parent 3ce377d commit 6e8f5e0

File tree

5 files changed

+149
-43
lines changed

5 files changed

+149
-43
lines changed

pkg/subnet/utils.go

Lines changed: 116 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,20 @@ package subnet
1919
import (
2020
"bytes"
2121
"compress/gzip"
22+
"crypto/tls"
2223
"encoding/base64"
2324
"encoding/json"
25+
"errors"
2426
"fmt"
2527
"io"
2628
"mime/multipart"
29+
"net"
2730
"net/http"
31+
"time"
2832

33+
"github.com/mattn/go-ieproxy"
2934
xhttp "github.com/minio/console/pkg/http"
35+
"github.com/tidwall/gjson"
3036

3137
"github.com/minio/madmin-go/v3"
3238
mc "github.com/minio/mc/cmd"
@@ -73,16 +79,23 @@ func UploadAuthHeaders(apiKey string) map[string]string {
7379
return map[string]string{"x-subnet-api-key": apiKey}
7480
}
7581

76-
func UploadFileToSubnet(info interface{}, client *xhttp.Client, filename string, reqURL string, headers map[string]string) (string, error) {
77-
req, e := subnetUploadReq(info, reqURL, filename)
82+
func ProcessUploadInfo(info interface{}, uploadType string, filename string) ([]byte, string, error) {
83+
if uploadType == "health" {
84+
return processHealthReport(info, filename)
85+
}
86+
return nil, "", errors.New("invalid Subnet upload type")
87+
}
88+
89+
func UploadFileToSubnet(info []byte, client *xhttp.Client, reqURL string, headers map[string]string, formDataType string) (string, error) {
90+
req, e := subnetUploadReq(info, reqURL, formDataType)
7891
if e != nil {
7992
return "", e
8093
}
8194
resp, e := subnetReqDo(client, req, headers)
8295
return resp, e
8396
}
8497

85-
func subnetUploadReq(info interface{}, url string, filename string) (*http.Request, error) {
98+
func processHealthReport(info interface{}, filename string) ([]byte, string, error) {
8699
var body bytes.Buffer
87100
writer := multipart.NewWriter(&body)
88101
zipWriter := gzip.NewWriter(&body)
@@ -94,29 +107,33 @@ func subnetUploadReq(info interface{}, url string, filename string) (*http.Reque
94107
}{Version: version}
95108

96109
if e := enc.Encode(header); e != nil {
97-
return nil, e
110+
return nil, "", e
98111
}
99112

100113
if e := enc.Encode(info); e != nil {
101-
return nil, e
114+
return nil, "", e
102115
}
103116
zipWriter.Close()
104117
temp := body
105118
part, e := writer.CreateFormFile("file", filename)
106119
if e != nil {
107-
return nil, e
120+
return nil, "", e
108121
}
109122
if _, e = io.Copy(part, &temp); e != nil {
110-
return nil, e
123+
return nil, "", e
111124
}
112125

113126
writer.Close()
127+
return body.Bytes(), writer.FormDataContentType(), nil
128+
}
114129

115-
r, e := http.NewRequest(http.MethodPost, url, &body)
130+
func subnetUploadReq(body []byte, url string, formDataType string) (*http.Request, error) {
131+
uploadDataBody := bytes.NewReader(body)
132+
r, e := http.NewRequest(http.MethodPost, url, uploadDataBody)
116133
if e != nil {
117134
return nil, e
118135
}
119-
r.Header.Add("Content-Type", writer.FormDataContentType())
136+
r.Header.Add("Content-Type", formDataType)
120137

121138
return r, nil
122139
}
@@ -226,3 +243,93 @@ func getDriveSpaceInfo(admInfo madmin.InfoMessage) (uint64, uint64) {
226243
}
227244
return total, used
228245
}
246+
247+
func GetSubnetAPIKeyUsingLicense(lic string) (string, error) {
248+
return getSubnetAPIKeyUsingAuthHeaders(subnetLicenseAuthHeaders(lic))
249+
}
250+
251+
func getSubnetAPIKeyUsingAuthHeaders(authHeaders map[string]string) (string, error) {
252+
resp, e := subnetGetReqMC(subnetAPIKeyURL(), authHeaders)
253+
if e != nil {
254+
return "", e
255+
}
256+
return extractSubnetCred("api_key", gjson.Parse(resp))
257+
}
258+
259+
func extractSubnetCred(key string, resp gjson.Result) (string, error) {
260+
result := resp.Get(key)
261+
if result.Index == 0 {
262+
return "", fmt.Errorf("Couldn't extract %s from SUBNET response: %s", key, resp)
263+
}
264+
return result.String(), nil
265+
}
266+
267+
func subnetLicenseAuthHeaders(lic string) map[string]string {
268+
return map[string]string{"x-subnet-license": lic}
269+
}
270+
271+
func subnetGetReqMC(reqURL string, headers map[string]string) (string, error) {
272+
r, e := http.NewRequest(http.MethodGet, reqURL, nil)
273+
if e != nil {
274+
return "", e
275+
}
276+
return subnetReqDoMC(r, headers)
277+
}
278+
279+
func subnetReqDoMC(r *http.Request, headers map[string]string) (string, error) {
280+
for k, v := range headers {
281+
r.Header.Add(k, v)
282+
}
283+
284+
ct := r.Header.Get("Content-Type")
285+
if len(ct) == 0 {
286+
r.Header.Add("Content-Type", "application/json")
287+
}
288+
289+
resp, e := subnetHTTPDo(r)
290+
if e != nil {
291+
return "", e
292+
}
293+
294+
defer resp.Body.Close()
295+
respBytes, e := io.ReadAll(io.LimitReader(resp.Body, subnetRespBodyLimit))
296+
if e != nil {
297+
return "", e
298+
}
299+
respStr := string(respBytes)
300+
301+
if resp.StatusCode == http.StatusOK {
302+
return respStr, nil
303+
}
304+
return respStr, fmt.Errorf("Request failed with code %d with error: %s", resp.StatusCode, respStr)
305+
}
306+
307+
func subnetHTTPDo(req *http.Request) (*http.Response, error) {
308+
return getSubnetClient().Do(req)
309+
}
310+
311+
func getSubnetClient() *http.Client {
312+
client := httpClientSubnet(0)
313+
return client
314+
}
315+
316+
func httpClientSubnet(reqTimeout time.Duration) *http.Client {
317+
return &http.Client{
318+
Timeout: reqTimeout,
319+
Transport: &http.Transport{
320+
DialContext: (&net.Dialer{
321+
Timeout: 10 * time.Second,
322+
}).DialContext,
323+
Proxy: ieproxy.GetProxyFunc(),
324+
TLSClientConfig: &tls.Config{
325+
// Can't use SSLv3 because of POODLE and BEAST
326+
// Can't use TLSv1.0 because of POODLE and BEAST using CBC cipher
327+
// Can't use TLSv1.1 because of RC4 cipher usage
328+
MinVersion: tls.VersionTLS12,
329+
},
330+
IdleConnTimeout: 90 * time.Second,
331+
TLSHandshakeTimeout: 10 * time.Second,
332+
ExpectContinueTimeout: 10 * time.Second,
333+
},
334+
}
335+
}

portal-ui/src/screens/Console/HealthInfo/HealthInfo.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,9 @@ const HealthInfo = ({ classes }: IHealthInfo) => {
362362
"Cluster Health Report will be uploaded to Subnet, and is viewable from your Subnet Diagnostics dashboard."
363363
}
364364
iconComponent={<InfoIcon />}
365-
help={""}
365+
help={
366+
"If the Health report cannot be generated at this time, please wait a moment and try again."
367+
}
366368
/>
367369
</Fragment>
368370
)}

restapi/admin_health_info.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,13 @@ func startHealthInfo(ctx context.Context, conn WSConn, client MinioAdmin, deadli
6868
return err
6969
}
7070
encodedDiag := b64.StdEncoding.EncodeToString(compressedDiag)
71-
7271
type messageReport struct {
7372
Encoded string `json:"encoded"`
7473
ServerHealthInfo interface{} `json:"serverHealthInfo"`
7574
SubnetResponse string `json:"subnetResponse"`
7675
}
7776

7877
ctx = context.WithValue(ctx, utils.ContextClientIP, conn.remoteAddress())
79-
8078
subnetResp, err := sendHealthInfoToSubnet(ctx, healthInfo, client)
8179
report := messageReport{
8280
Encoded: encodedDiag,
@@ -142,9 +140,22 @@ func sendHealthInfoToSubnet(ctx context.Context, healthInfo interface{}, client
142140
if e != nil {
143141
return "", e
144142
}
145-
apiKey := subnetTokenConfig.APIKey
143+
var apiKey string
144+
if len(subnetTokenConfig.APIKey) != 0 {
145+
apiKey = subnetTokenConfig.APIKey
146+
} else {
147+
apiKey, e = subnet.GetSubnetAPIKeyUsingLicense(subnetTokenConfig.License)
148+
if e != nil {
149+
return "", e
150+
}
151+
}
146152
headers := subnet.UploadAuthHeaders(apiKey)
147-
resp, e := subnet.UploadFileToSubnet(healthInfo, subnetHTTPClient, filename, subnetUploadURL, headers)
153+
uploadInfo, formDataType, e := subnet.ProcessUploadInfo(healthInfo, "health", filename)
154+
if e != nil {
155+
return "", e
156+
}
157+
resp, e := subnet.UploadFileToSubnet(uploadInfo, subnetHTTPClient, subnetUploadURL, headers, formDataType)
158+
148159
if e != nil {
149160
return "", e
150161
}

restapi/client-admin.go

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ import (
3232

3333
"github.com/minio/console/models"
3434
"github.com/minio/madmin-go/v3"
35-
mcCmd "github.com/minio/mc/cmd"
3635
"github.com/minio/minio-go/v7/pkg/credentials"
3736
iampolicy "github.com/minio/pkg/iam/policy"
3837
)
@@ -388,43 +387,29 @@ func (ac AdminClient) getBucketQuota(ctx context.Context, bucket string) (madmin
388387

389388
// serverHealthInfo implements mc.ServerHealthInfo - Connect to a minio server and call Health Info Management API
390389
func (ac AdminClient) serverHealthInfo(ctx context.Context, healthDataTypes []madmin.HealthDataType, deadline time.Duration) (interface{}, string, error) {
391-
resp, version, err := ac.Client.ServerHealthInfo(ctx, healthDataTypes, deadline)
392-
if err != nil {
393-
return nil, version, err
394-
}
395-
390+
info := madmin.HealthInfo{}
396391
var healthInfo interface{}
397-
398-
decoder := json.NewDecoder(resp.Body)
399-
switch version {
400-
case madmin.HealthInfoVersion0:
401-
info := madmin.HealthInfoV0{}
402-
for {
403-
if err = decoder.Decode(&info); err != nil {
404-
break
405-
}
406-
}
407-
408-
// Old minio versions don't return the MinIO info in
409-
// response of the healthinfo api. So fetch it separately
410-
minioInfo, err := ac.Client.ServerInfo(ctx)
392+
var version string
393+
var tryCount int
394+
for info.Version == "" && tryCount < 10 {
395+
resp, version, err := ac.Client.ServerHealthInfo(ctx, healthDataTypes, deadline)
411396
if err != nil {
412-
info.Minio.Error = err.Error()
413-
} else {
414-
info.Minio.Info = minioInfo
397+
return nil, version, err
415398
}
416-
417-
healthInfo = mcCmd.MapHealthInfoToV1(info, nil)
418-
version = madmin.HealthInfoVersion1
419-
case madmin.HealthInfoVersion:
420-
info := madmin.HealthInfo{}
399+
decoder := json.NewDecoder(resp.Body)
421400
for {
422401
if err = decoder.Decode(&info); err != nil {
423402
break
424403
}
425404
}
426-
healthInfo = info
405+
tryCount++
406+
time.Sleep(2 * time.Second)
407+
408+
}
409+
if info.Version == "" {
410+
return nil, "", ErrHealthReportFail
427411
}
412+
healthInfo = info
428413

429414
return healthInfo, version, nil
430415
}

restapi/errors.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ var (
7272
ErrPolicyNotFound = errors.New("policy does not exist")
7373
ErrLoginNotAllowed = errors.New("login not allowed")
7474
ErrSubnetUploadFail = errors.New("Subnet upload failed")
75+
ErrHealthReportFail = errors.New("failure to generate Health report")
7576
)
7677

7778
// ErrorWithContext :

0 commit comments

Comments
 (0)