@@ -19,14 +19,20 @@ package subnet
19
19
import (
20
20
"bytes"
21
21
"compress/gzip"
22
+ "crypto/tls"
22
23
"encoding/base64"
23
24
"encoding/json"
25
+ "errors"
24
26
"fmt"
25
27
"io"
26
28
"mime/multipart"
29
+ "net"
27
30
"net/http"
31
+ "time"
28
32
33
+ "github.com/mattn/go-ieproxy"
29
34
xhttp "github.com/minio/console/pkg/http"
35
+ "github.com/tidwall/gjson"
30
36
31
37
"github.com/minio/madmin-go/v3"
32
38
mc "github.com/minio/mc/cmd"
@@ -73,16 +79,23 @@ func UploadAuthHeaders(apiKey string) map[string]string {
73
79
return map [string ]string {"x-subnet-api-key" : apiKey }
74
80
}
75
81
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 )
78
91
if e != nil {
79
92
return "" , e
80
93
}
81
94
resp , e := subnetReqDo (client , req , headers )
82
95
return resp , e
83
96
}
84
97
85
- func subnetUploadReq (info interface {}, url string , filename string ) (* http. Request , error ) {
98
+ func processHealthReport (info interface {}, filename string ) ([] byte , string , error ) {
86
99
var body bytes.Buffer
87
100
writer := multipart .NewWriter (& body )
88
101
zipWriter := gzip .NewWriter (& body )
@@ -94,29 +107,33 @@ func subnetUploadReq(info interface{}, url string, filename string) (*http.Reque
94
107
}{Version : version }
95
108
96
109
if e := enc .Encode (header ); e != nil {
97
- return nil , e
110
+ return nil , "" , e
98
111
}
99
112
100
113
if e := enc .Encode (info ); e != nil {
101
- return nil , e
114
+ return nil , "" , e
102
115
}
103
116
zipWriter .Close ()
104
117
temp := body
105
118
part , e := writer .CreateFormFile ("file" , filename )
106
119
if e != nil {
107
- return nil , e
120
+ return nil , "" , e
108
121
}
109
122
if _ , e = io .Copy (part , & temp ); e != nil {
110
- return nil , e
123
+ return nil , "" , e
111
124
}
112
125
113
126
writer .Close ()
127
+ return body .Bytes (), writer .FormDataContentType (), nil
128
+ }
114
129
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 )
116
133
if e != nil {
117
134
return nil , e
118
135
}
119
- r .Header .Add ("Content-Type" , writer . FormDataContentType () )
136
+ r .Header .Add ("Content-Type" , formDataType )
120
137
121
138
return r , nil
122
139
}
@@ -226,3 +243,93 @@ func getDriveSpaceInfo(admInfo madmin.InfoMessage) (uint64, uint64) {
226
243
}
227
244
return total , used
228
245
}
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
+ }
0 commit comments