forked from step-security/agent
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapiclient.go
More file actions
196 lines (148 loc) · 4.48 KB
/
apiclient.go
File metadata and controls
196 lines (148 loc) · 4.48 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
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"`
}
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"`
}
type ApiClient struct {
Client *http.Client
APIURL string
DisableTelemetry bool
EgressPolicy string
OneTimeKey string
}
const agentApiBaseUrl = "https://apiurl/v1"
func (apiclient *ApiClient) sendDNSRecord(correlationId, repo, domainName, ipAddress string) error {
if !apiclient.DisableTelemetry || apiclient.EgressPolicy == EgressPolicyAudit {
dnsRecord := &DNSRecord{}
dnsRecord.DomainName = domainName
dnsRecord.ResolvedIPAddress = ipAddress
dnsRecord.TimeStamp = time.Now().UTC()
url := fmt.Sprintf("%s/github/%s/actions/jobs/%s/dns", apiclient.APIURL, repo, correlationId)
return apiclient.sendApiRequest("POST", url, dnsRecord)
}
return nil
}
func (apiclient *ApiClient) sendNetConnection(correlationId, repo, ipAddress, port, domainName, status 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
url := fmt.Sprintf("%s/github/%s/actions/jobs/%s/networkconnection", apiclient.APIURL, 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) 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
}