Repository navigation
Expand file tree
/
Copy pathcrash-reporter.ts
More file actions
163 lines (147 loc) · 7.36 KB
/
Copy pathcrash-reporter.ts
File metadata and controls
163 lines (147 loc) · 7.36 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
import type { Capabilities, Options } from '@wdio/types'
import { BSTACK_SERVICE_VERSION, BROWSERSTACK_TESTHUB_UUID, WDIO_NAMING_PREFIX } from './constants.js'
import type { BrowserstackConfig, CredentialsForCrashReportUpload, UserConfigforReporting } from './types.js'
import { DEFAULT_REQUEST_CONFIG, getObservabilityKey, getObservabilityUser } from './util.js'
import { BStackLogger } from './bstackLogger.js'
import APIUtils from './cli/apiUtils.js'
import { _fetch as fetch } from './fetchWrapper.js'
type Dict = Record<string, string>
export default class CrashReporter {
/* User test config for build run minus PII */
public static userConfigForReporting: UserConfigforReporting = {}
/* User credentials used for reporting crashes in browserstack service */
private static credentialsForCrashReportUpload: CredentialsForCrashReportUpload = {}
static setCredentialsForCrashReportUpload(options: BrowserstackConfig & Options.Testrunner, config: Options.Testrunner) {
this.credentialsForCrashReportUpload = {
username: getObservabilityUser(options, config),
password: getObservabilityKey(options, config)
}
process.env.CREDENTIALS_FOR_CRASH_REPORTING = JSON.stringify(this.credentialsForCrashReportUpload)
}
static setConfigDetails(userConfig: Options.Testrunner, capabilities: Capabilities.TestrunnerCapabilities, options: BrowserstackConfig & Options.Testrunner) {
const configWithoutPII = this.filterPII(userConfig)
const filteredCapabilities = this.filterCapabilities(capabilities)
this.userConfigForReporting = {
framework: userConfig.framework,
services: configWithoutPII.services,
capabilities: filteredCapabilities,
env: {
'BROWSERSTACK_BUILD': process.env.BROWSERSTACK_BUILD,
'BROWSERSTACK_BUILD_NAME': process.env.BROWSERSTACK_BUILD_NAME,
'BUILD_TAG': process.env.BUILD_TAG
}
}
process.env.USER_CONFIG_FOR_REPORTING = JSON.stringify(this.userConfigForReporting)
this.setCredentialsForCrashReportUpload(options, userConfig)
}
static async uploadCrashReport(exception: string, stackTrace: string) {
try {
if (!this.credentialsForCrashReportUpload.username || !this.credentialsForCrashReportUpload.password) {
this.credentialsForCrashReportUpload = process.env.CREDENTIALS_FOR_CRASH_REPORTING !== undefined ? JSON.parse(process.env.CREDENTIALS_FOR_CRASH_REPORTING) : this.credentialsForCrashReportUpload
}
} catch (error) {
return BStackLogger.error(`[Crash_Report_Upload] Failed to parse user credentials while reporting crash due to ${error}`)
}
if (!this.credentialsForCrashReportUpload.username || !this.credentialsForCrashReportUpload.password) {
return BStackLogger.error('[Crash_Report_Upload] Failed to parse user credentials while reporting crash')
}
try {
if (Object.keys(this.userConfigForReporting).length === 0) {
this.userConfigForReporting = process.env.USER_CONFIG_FOR_REPORTING !== undefined ? JSON.parse(process.env.USER_CONFIG_FOR_REPORTING) : {}
}
} catch (error) {
BStackLogger.error(`[Crash_Report_Upload] Failed to parse user config while reporting crash due to ${error}`)
this.userConfigForReporting = {}
}
const data = {
hashed_id: process.env[BROWSERSTACK_TESTHUB_UUID],
observability_version: {
frameworkName: WDIO_NAMING_PREFIX + (this.userConfigForReporting.framework || 'null'),
sdkVersion: BSTACK_SERVICE_VERSION
},
exception: {
error: exception.toString(),
stackTrace: stackTrace
},
config: this.userConfigForReporting
}
const url = `${APIUtils.DATA_ENDPOINT}/api/v1/analytics`
const encodedAuth = Buffer.from(`${this.credentialsForCrashReportUpload.username}:${this.credentialsForCrashReportUpload.password}`, 'utf8').toString('base64')
const headers: Record<string, string> = {
...DEFAULT_REQUEST_CONFIG.headers,
Authorization: `Basic ${encodedAuth}`,
}
const response = await fetch(url, {
method: 'POST',
body: JSON.stringify(data),
headers
})
if (response.ok) {
let body = await response.text()
try {
body = JSON.stringify(JSON.parse(body))
} catch {
// Response is not JSON, use text as-is
}
BStackLogger.debug(`[Crash_Report_Upload] Success response: ${body}`)
} else {
BStackLogger.error(`[Crash_Report_Upload] Failed due to ${response.body}`)
}
}
static recursivelyRedactKeysFromObject(obj: Dict | Array<Dict>, keys: string[]) {
if (!obj) {
return
}
if (Array.isArray(obj)) {
obj.map(ele => this.recursivelyRedactKeysFromObject(ele, keys))
} else {
for (const prop in obj) {
if (keys.includes(prop.toLowerCase())) {
obj[prop] = '[REDACTED]'
} else if (typeof obj[prop] === 'object') {
this.recursivelyRedactKeysFromObject(obj[prop], keys)
}
}
}
}
static deletePIIKeysFromObject(obj: { [key: string]: unknown }) {
if (!obj) {
return
}
['user', 'username', 'key', 'accessKey'].forEach(key => delete obj[key])
}
static filterCapabilities(capabilities: Capabilities.TestrunnerCapabilities) {
const capsCopy = JSON.parse(JSON.stringify(capabilities))
this.recursivelyRedactKeysFromObject(capsCopy, ['extensions'])
return capsCopy
}
static filterPII(userConfig: Options.Testrunner) {
const configWithoutPII = JSON.parse(JSON.stringify(userConfig))
this.deletePIIKeysFromObject(configWithoutPII)
const finalServices = []
const initialServices = configWithoutPII.services
delete configWithoutPII.services
try {
for (const serviceArray of initialServices) {
if (Array.isArray(serviceArray) && serviceArray.length >= 2 && serviceArray[0] === 'browserstack') {
for (let idx = 1; idx < serviceArray.length; idx++) {
this.deletePIIKeysFromObject(serviceArray[idx])
if (serviceArray[idx]) {
// Handle both new testReportingOptions and legacy testObservabilityOptions
this.deletePIIKeysFromObject(serviceArray[idx].testReportingOptions)
this.deletePIIKeysFromObject(serviceArray[idx].testObservabilityOptions)
}
}
finalServices.push(serviceArray)
break
}
}
} catch (err) {
/* Wrong configuration like strings instead of json objects could break this method, needs no action */
BStackLogger.error(`Error in parsing user config PII with error ${err ? ((err as Error).stack || err) : err}`)
return configWithoutPII
}
configWithoutPII.services = finalServices
return configWithoutPII
}
}