-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhttp_bundle_client.go
107 lines (85 loc) · 2.89 KB
/
http_bundle_client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package openpolicyagent
import (
"bytes"
"fmt"
"strings"
"io"
"mime/multipart"
"net/http"
"net/url"
"os"
"path/filepath"
"github.com/hexa-org/policy-mapper/providers/openpolicyagent/compressionsupport"
)
const BundleTypeHttp string = "HTTP"
/*
Do(req *http.Request) (resp *http.Response, err error)
NewRequest(method, url string, body io.Reader) (req *http.Request, err error)
*/
type HTTPClient interface {
Get(url string) (resp *http.Response, err error)
Post(url, contentType string, body io.Reader) (resp *http.Response, err error)
Do(request *http.Request) (resp *http.Response, err error)
}
type HTTPBundleClient struct {
BundleServerURL string
Authorization *string
HttpClient HTTPClient
}
func (b *HTTPBundleClient) Type() string {
return BundleTypeHttp
}
func CheckBundleUrl(bundleUrl string) string {
parse, _ := url.Parse(bundleUrl)
if parse.Path == "" || parse.Path == "/" {
revised := parse.JoinPath("/bundles/bundle.tar.gz")
bundleUrl = revised.String()
}
return bundleUrl
}
func (b *HTTPBundleClient) GetDataFromBundle(path string) ([]byte, error) {
get, err := b.newRequest(http.MethodGet, CheckBundleUrl(b.BundleServerURL), nil, nil)
if err != nil {
return nil, err
}
defer get.Body.Close()
gz, gzipErr := compressionsupport.UnGzip(get.Body)
if gzipErr != nil {
return nil, fmt.Errorf("unable to ungzip: %w", gzipErr)
}
tarErr := compressionsupport.UnTarToPath(bytes.NewReader(gz), path)
if tarErr != nil {
return nil, fmt.Errorf("unable to untar to path: %w", tarErr)
}
return os.ReadFile(filepath.Join(path, "/bundle/data.json"))
}
// todo - ignoring errors for the moment while spiking
func (b *HTTPBundleClient) PostBundle(bundle []byte) (int, error) {
// todo - Log out the errors at minimum.
buf := new(bytes.Buffer)
writer := multipart.NewWriter(buf)
formFile, _ := writer.CreateFormFile("bundle", "bundle.tar.gz")
_, _ = formFile.Write(bundle)
_ = writer.Close()
contentType := writer.FormDataContentType()
resp, err := b.newRequest(http.MethodPost, CheckBundleUrl(b.BundleServerURL), &contentType, buf)
if resp != nil {
return resp.StatusCode, err
}
// resp, err := http.Post(fmt.Sprintf("%s://%s/bundles", parse.Scheme, parse.Host), contentType, buf)
return http.StatusInternalServerError, err
}
func (b *HTTPBundleClient) newRequest(method, url string, contentType *string, body io.Reader) (*http.Response, error) {
req, err := http.NewRequest(method, url, body)
if err != nil {
return nil, err
}
if b.Authorization != nil {
token := *b.Authorization
req.Header.Set("Authorization", strings.TrimSpace(token))
}
if contentType != nil {
req.Header.Set("Content-Type", *contentType)
}
return b.HttpClient.Do(req)
}