-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathapiclient.go
More file actions
237 lines (182 loc) · 5.91 KB
/
apiclient.go
File metadata and controls
237 lines (182 loc) · 5.91 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"path"
"time"
)
type DNSRecord struct {
DomainName string `json:"domainName"`
ResolvedIPAddress string `json:"ipAddress"`
TimeStamp time.Time `json:"timestamp"`
MatchedPolicy string `json:"matched_policy,omitempty"`
Reason string `json:"reason,omitempty"`
}
type Tool struct {
Name string `json:"name"`
SHA256 string `json:"sha256"`
Parent *Tool `json:"parent"`
}
type FileEvent struct {
FileType string `json:"filetype"` // this can be source, dependency, artifact
TimeStamp time.Time `json:"timestamp"`
Tool Tool `json:"tool"`
}
type NetworkConnection struct {
IPAddress string `json:"ipAddress,omitempty"`
Port string `json:"port,omitempty"`
DomainName string `json:"domainName,omitempty"`
TimeStamp time.Time `json:"timestamp"`
Tool Tool `json:"tool"`
Status string `json:"status,omitempty"`
MatchedPolicy string `json:"matched_policy,omitempty"`
Reason string `json:"reason,omitempty"`
}
type ApiClient struct {
Client *http.Client
APIURL string
TelemetryURL string
DisableTelemetry bool
EgressPolicy string
OneTimeKey string
}
const agentApiBaseUrl = "https://apiurl/v1"
func (apiclient *ApiClient) sendDNSRecord(correlationId, repo, domainName, ipAddress, matchedPolicy, reason string) error {
if !apiclient.DisableTelemetry || apiclient.EgressPolicy == EgressPolicyAudit {
dnsRecord := &DNSRecord{}
dnsRecord.DomainName = domainName
dnsRecord.ResolvedIPAddress = ipAddress
dnsRecord.TimeStamp = time.Now().UTC()
dnsRecord.MatchedPolicy = matchedPolicy
dnsRecord.Reason = reason
url := fmt.Sprintf("%s/github/%s/actions/jobs/%s/dns", apiclient.TelemetryURL, repo, correlationId)
return apiclient.sendApiRequest("POST", url, dnsRecord)
}
return nil
}
func (apiclient *ApiClient) sendNetConnection(correlationId, repo, ipAddress, port, domainName, status, matchedPolicy, reason string, timestamp time.Time, tool Tool) error {
if !apiclient.DisableTelemetry || apiclient.EgressPolicy == EgressPolicyAudit {
networkConnection := &NetworkConnection{}
networkConnection.IPAddress = ipAddress
networkConnection.Port = port
networkConnection.DomainName = domainName
networkConnection.Status = status
networkConnection.TimeStamp = timestamp
networkConnection.Tool = tool
networkConnection.MatchedPolicy = matchedPolicy
networkConnection.Reason = reason
url := fmt.Sprintf("%s/github/%s/actions/jobs/%s/networkconnection", apiclient.TelemetryURL, repo, correlationId)
return apiclient.sendApiRequest("POST", url, networkConnection)
}
return nil
}
func (apiclient *ApiClient) getSubscriptionStatus(repo string) bool {
url := fmt.Sprintf("%s/github/%s/actions/subscription", apiclient.APIURL, repo)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return true
}
resp, err := apiclient.Client.Do(req)
if err != nil {
return true
}
if resp.StatusCode == 403 {
return false
}
return true
}
func (apiclient *ApiClient) getGlobalFeatureFlags() GlobalFeatureFlags {
u, err := url.Parse(apiclient.APIURL)
if err != nil {
return GlobalFeatureFlags{}
}
u.Path = path.Join(u.Path, "global-feature-flags")
// Add query parameters
values := url.Values{}
values.Add("agent_type", AgentTypeOSS)
values.Add("version", ReleaseTag) // v1.3.6
u.RawQuery = values.Encode()
req, err := http.NewRequest(http.MethodGet, u.String(), nil)
if err != nil {
fmt.Println("Error creating request:", err)
return GlobalFeatureFlags{}
}
resp, err := apiclient.Client.Do(req)
if err != nil {
fmt.Println("Error sending request:", err)
return GlobalFeatureFlags{}
}
body, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Println("Error reading response body:", err)
return GlobalFeatureFlags{}
}
var globalFeatureFlags GlobalFeatureFlags
err = json.Unmarshal(body, &globalFeatureFlags)
if err != nil {
fmt.Println("Error unmarshalling response body:", err)
return GlobalFeatureFlags{}
}
return globalFeatureFlags
}
func (apiclient *ApiClient) getGlobalBlocklist() (*GlobalBlocklistResponse, error) {
url := fmt.Sprintf("%s/global-blocklist", apiclient.APIURL)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, fmt.Errorf("[getGlobalBlocklist] error occurred while getting global blocklist: %s", err)
}
resp, err := apiclient.Client.Do(req)
if err != nil {
return nil, fmt.Errorf("[getGlobalBlocklist] error while sending request: %s", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("[getGlobalBlocklist] response status not okay: status %d", resp.StatusCode)
}
reqBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("[getGlobalBlocklist] unable to read reqBody: %s", err)
}
out := new(GlobalBlocklistResponse)
err = json.Unmarshal(reqBody, out)
if err != nil {
return nil, fmt.Errorf("[getGlobalBlocklist] unable to unmarshal reqBody: %s", err)
}
return out, nil
}
func (apiclient *ApiClient) sendApiRequest(method, url string, body interface{}) error {
jsonData, _ := json.Marshal(body)
req, err := http.NewRequest(method, url, bytes.NewBuffer(jsonData))
if err != nil {
return err
}
req.Header.Add("x-one-time-key", apiclient.OneTimeKey)
if body != nil {
req.Header.Add("Content-Type", "application/json; charset=UTF-8")
}
retryCounter := 0
var httpError error
for retryCounter < 3 {
_, httpError = apiclient.sendHttpRequest(req)
if httpError != nil {
retryCounter++
} else {
break
}
}
return httpError
}
func (apiclient *ApiClient) sendHttpRequest(req *http.Request) (*http.Response, error) {
resp, err := apiclient.Client.Do(req)
if err != nil {
return nil, err
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("API call error, status code: %d", resp.StatusCode)
}
return resp, nil
}