-
Notifications
You must be signed in to change notification settings - Fork 2
/
analytics.go
369 lines (312 loc) · 10.1 KB
/
analytics.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
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
package analytics
import (
"bytes"
"crypto/sha1"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"os"
"os/user"
"regexp"
"runtime"
"strings"
"time"
"github.com/hashicorp/go-uuid"
"github.com/khulnasoft-lab/go-application-framework/internal/api"
"github.com/khulnasoft-lab/go-application-framework/pkg/utils"
)
// Analytics is an interface for managing analytics.
type Analytics interface {
SetCmdArguments(args []string)
SetOrg(org string)
SetVersion(version string)
SetApiUrl(apiUrl string)
SetIntegration(name string, version string)
SetCommand(command string)
SetOperatingSystem(os string)
AddError(err error)
AddHeader(headerFunc func() http.Header)
SetClient(clientFunc func() *http.Client)
IsCiEnvironment() bool
GetOutputData() *analyticsOutput
GetRequest() (*http.Request, error)
Send() (*http.Response, error)
}
// AnalyticsImpl is the default implementation of the Analytics interface.
type AnalyticsImpl struct {
clientFunc func() *http.Client
headerFunc func() http.Header
apiUrl string
org string
version string
created time.Time
args []string
errorList []error
integrationName string
integrationVersion string
os string
command string
}
// metadataOutput defines the metadataOutput payload.
type metadataOutput struct {
ErrorMessage string `json:"error-message,omitempty"`
ErrorCode string `json:"error-code,omitempty"`
}
// metricsOutput defines the metricsOutput payload.
type metricsOutput struct {
}
// analyticsOutput defines the analyticsOutput payload.
type analyticsOutput struct {
Command string `json:"command"`
Args []string `json:"args"`
OsPlatform string `json:"osPlatform"`
OsArch string `json:"osArch"`
Os string `json:"os"`
OsRelease string `json:"osRelease"`
Metadata metadataOutput `json:"metadata"`
Id string `json:"id"`
Version string `json:"version"`
DurationMs int64 `json:"durationMs"`
Metrics metricsOutput `json:"metrics"`
Ci bool `json:"ci"`
IntegrationName string `json:"integrationName"`
IntegrationVersion string `json:"integrationVersion"`
IntegrationEnvironment string `json:"integrationEnvironment"`
IntegrationEnvironmentVersion string `json:"integrationEnvironmentVersion"`
NodeVersion string `json:"nodeVersion"`
Standalone bool `json:"standalone"`
}
// dataOutput defines the dataOutput payload.
type dataOutput struct {
Data analyticsOutput `json:"data"`
}
var (
// ciEnvironments is a list of environment variables that indicate a CI environment.
// it is used to determine if the command is running in a CI environment.
// if it is, the x-is-ci header is set to true.
ciEnvironments = []string{
"VULNMAP_CI",
"CI",
"CONTINUOUS_INTEGRATION",
"BUILD_ID",
"BUILD_NUMBER",
"TEAMCITY_VERSION",
"TRAVIS",
"CIRCLECI",
"JENKINS_URL",
"HUDSON_URL",
"bamboo.buildKey",
"PHPCI",
"GOCD_SERVER_HOST",
"BUILDKITE",
"TF_BUILD",
"SYSTEM_TEAMFOUNDATIONSERVERURI", // for Azure DevOps Pipelines
}
// sensitiveFieldNames is a list of field names that should be sanitized.
// data sanitization is used to prevent sensitive data from being sent to the analytics server.
sensitiveFieldNames = []string{
"headers",
"user",
"passw",
"token",
"key",
"secret",
}
)
const (
sanitize_replacement_string string = "REDACTED"
api_endpoint = "/v1/analytics/cli"
)
// New creates a new Analytics instance.
func New() Analytics {
a := &AnalyticsImpl{}
a.headerFunc = func() http.Header { return http.Header{} }
a.created = time.Now()
a.clientFunc = func() *http.Client { return &http.Client{} }
a.os = runtime.GOOS
return a
}
// SetCmdArguments sets the command arguments.
func (a *AnalyticsImpl) SetCmdArguments(args []string) {
a.args = args
}
// SetOrg sets the organization.
func (a *AnalyticsImpl) SetOrg(org string) {
a.org = org
}
// SetVersion sets the version.
func (a *AnalyticsImpl) SetVersion(version string) {
a.version = version
}
// SetApiUrl sets the API URL.
func (a *AnalyticsImpl) SetApiUrl(apiUrl string) {
a.apiUrl = apiUrl
}
// SetIntegration sets the integration name and version.
func (a *AnalyticsImpl) SetIntegration(name string, version string) {
a.integrationName = name
a.integrationVersion = version
}
func (a *AnalyticsImpl) SetCommand(command string) {
a.command = command
}
func (a *AnalyticsImpl) SetOperatingSystem(os string) {
a.os = os
}
// AddError adds an error to the error list.
func (a *AnalyticsImpl) AddError(err error) {
a.errorList = append(a.errorList, err)
}
// AddHeader adds a header to the request.
func (a *AnalyticsImpl) AddHeader(headerFunc func() http.Header) {
a.headerFunc = headerFunc
}
// SetClient sets the HTTP client.
func (a *AnalyticsImpl) SetClient(clientFunc func() *http.Client) {
a.clientFunc = clientFunc
}
// IsCiEnvironment returns true if the command is running in a CI environment.
func (a *AnalyticsImpl) IsCiEnvironment() bool {
result := false
envMap := utils.ToKeyValueMap(os.Environ(), "=")
for i := range ciEnvironments {
if _, ok := envMap[ciEnvironments[i]]; ok {
result = true
break
}
}
return result
}
// GetOutputData returns the analyticsOutput data.
func (a *AnalyticsImpl) GetOutputData() *analyticsOutput {
output := &analyticsOutput{}
errorCount := len(a.errorList)
if errorCount > 0 {
lastError := a.errorList[errorCount-1]
output.Metadata = metadataOutput{
ErrorMessage: lastError.Error(),
}
}
// deepcode ignore InsecureHash: It is just being used to generate an id, without any security concerns
shasum := sha1.New()
uuid, _ := uuid.GenerateUUID()
io.WriteString(shasum, uuid)
output.Id = fmt.Sprintf("%x", shasum.Sum(nil))
output.Args = a.args
if len(a.command) > 0 {
output.Command = a.command
} else if len(a.args) > 0 {
output.Command = a.args[0]
}
output.OsPlatform = a.os
output.OsArch = runtime.GOARCH
output.Version = a.version
output.NodeVersion = runtime.Version()
output.Ci = a.IsCiEnvironment()
output.IntegrationName = a.integrationName
output.IntegrationVersion = a.integrationVersion
output.DurationMs = int64(time.Since(a.created).Milliseconds())
output.Standalone = true // standalone means binary deployment, which is always true for go applications.
return output
}
// GetRequest returns the HTTP request.
func (a *AnalyticsImpl) GetRequest() (*http.Request, error) {
if !a.isEnabled() {
return nil, DisabledInFedrampErr
}
output := a.GetOutputData()
outputJson, err := json.Marshal(dataOutput{Data: *output})
if err != nil {
return nil, err
}
outputJson, err = SanitizeValuesByKey(sensitiveFieldNames, sanitize_replacement_string, outputJson)
if err != nil {
return nil, err
}
user, err := user.Current()
if err != nil {
return nil, err
}
outputJson, err = SanitizeUsername(user.Username, user.HomeDir, sanitize_replacement_string, outputJson)
if err != nil {
return nil, err
}
analyticsUrl, _ := url.Parse(a.apiUrl + api_endpoint)
if len(a.org) > 0 {
query := url.Values{}
query.Add("org", a.org)
analyticsUrl.RawQuery = query.Encode()
}
body := bytes.NewReader(outputJson)
request, err := http.NewRequest(http.MethodPost, analyticsUrl.String(), body)
if err != nil {
return nil, err
}
if a.headerFunc != nil {
request.Header = a.headerFunc()
}
request.Header.Set("Content-Type", "application/json; charset=utf-8")
return request, err
}
func (a *AnalyticsImpl) Send() (*http.Response, error) {
if !a.isEnabled() {
return nil, DisabledInFedrampErr
}
request, err := a.GetRequest()
if err != nil {
return nil, err
}
client := a.clientFunc()
response, err := client.Do(request)
return response, err
}
func (a *AnalyticsImpl) isEnabled() bool {
return !api.IsFedramp(a.apiUrl)
}
var DisabledInFedrampErr = errors.New("analytics are disabled in FedRAMP environments")
// This method sanitizes the given content by searching for key-value mappings. It thereby replaces all keys defined in keysToFilter by the replacement string
// Supported patterns are:
// * key : "value"
// * key = "value"
// * key = value
// * key "value"
func SanitizeValuesByKey(keysToFilter []string, replacementValue string, content []byte) ([]byte, error) {
for i := range keysToFilter {
filter := keysToFilter[i]
r, err := regexp.Compile("(?i)([\"']?\\w*" + filter + "\\w*\"?)(((\\s?[:]\\s?[\"'])[^\n\"']*([\"']))|((\\s?[= ,]\\s?[\"']?)[^\n\"']*([\"']?)))")
if err != nil {
return nil, err
}
content = r.ReplaceAll(content, []byte("${1}${4}${7}"+replacementValue+"${5}${8}"))
}
return content, nil
}
// SanitizeUsername sanitizes the given content by replacing the given username with the replacement string.
func SanitizeUsername(rawUserName string, userHomeDir string, replacementValue string, content []byte) ([]byte, error) {
valuesToSanitize := []string{rawUserName, strings.ReplaceAll(userHomeDir, "\\", "\\\\")}
if strings.Contains(rawUserName, "\\") {
segments := strings.Split(rawUserName, "\\")
segmentsLen := len(segments)
if segmentsLen < 2 {
// this should never happen because we already checked for the existence of a backslash
return nil, fmt.Errorf("could not sanitize username")
} else if segmentsLen == 2 {
valuesToSanitize = append(valuesToSanitize, segments[1])
} else {
// don't recognize this format
fmt.Println(segments)
return nil, fmt.Errorf("could not sanitize username - unrecognized format")
}
}
return SanitizeStaticValues(valuesToSanitize, replacementValue, content)
}
func SanitizeStaticValues(valuesToSanitize []string, replacementValue string, content []byte) ([]byte, error) {
contentStr := string(content)
for _, valueToReplace := range valuesToSanitize {
contentStr = strings.ReplaceAll(contentStr, valueToReplace, replacementValue)
}
return []byte(contentStr), nil
}