-
Notifications
You must be signed in to change notification settings - Fork 18
/
apiclient.go
148 lines (111 loc) · 3.44 KB
/
apiclient.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
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
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"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) 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
}