Skip to content

Commit 79d707f

Browse files
committed
Adding encryption capabilities in CloudRequest
1 parent b634cc5 commit 79d707f

File tree

1 file changed

+45
-7
lines changed

1 file changed

+45
-7
lines changed

packages/server/lib/cloud/api/cloud_request.ts

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
* The axios Cloud instance should not be used.
33
*/
44
import os from 'os'
5-
65
import followRedirects from 'follow-redirects'
76
import axios, { AxiosInstance } from 'axios'
87
import pkg from '@packages/root'
@@ -13,18 +12,52 @@ import { installErrorTransform } from './axios_middleware/transform_error'
1312
import { installLogging } from './axios_middleware/logging'
1413
import { installEncryption } from './axios_middleware/encryption'
1514

15+
export interface CreateCloudRequestOptions {
16+
/**
17+
* The baseURL for all requests for this Cloud Request instance
18+
*/
19+
baseURL?: string
20+
/**
21+
* Additional headers for the Cloud Request
22+
*/
23+
addditionalHeaders?: Record<string, string>
24+
/**
25+
* Whether to include e2e encryption for requests:
26+
* true = encrypt request, decrypt response if x-cypress-encrypted signature is set
27+
* always = encrypt request, always decrypt response
28+
* signed = send version in x-cypress-signature, check response's x-cypress-signature
29+
* false = no encryption
30+
*
31+
* @default false
32+
*/
33+
enableEncryption?: 'always' | 'signed' | true | false
34+
/**
35+
* Whether to include the default logging middleware
36+
* @default true
37+
*/
38+
enableLogging?: boolean
39+
/**
40+
* Whether to include the default error transformation
41+
* @default true
42+
*/
43+
enableErrorTransform?: boolean
44+
}
45+
1646
// Allows us to create customized Cloud Request instances w/ different baseURL & encryption configuration
17-
export const createCloudRequest = (options: { baseURL?: string, encrypt?: 'always' | 'signed' | true } = {}): AxiosInstance => {
47+
export const createCloudRequest = (options: CreateCloudRequestOptions = {}): AxiosInstance => {
1848
const cfgKey = process.env.CYPRESS_CONFIG_ENV || process.env.CYPRESS_INTERNAL_ENV || 'development'
49+
const { baseURL = app_config[cfgKey].api_url, enableEncryption = false, enableLogging = true, enableErrorTransform = true } = options
1950

2051
const instance = axios.create({
21-
baseURL: options.baseURL ?? app_config[cfgKey].api_url,
52+
baseURL,
2253
httpAgent: agent,
2354
httpsAgent: agent,
55+
allowAbsoluteUrls: false,
2456
headers: {
2557
'x-os-name': os.platform(),
2658
'x-cypress-version': pkg.version,
2759
'User-Agent': `cypress/${pkg.version}`,
60+
...options.addditionalHeaders,
2861
},
2962
transport: {
3063
// https://github.com/axios/axios/issues/6313#issue-2198831362
@@ -44,12 +77,17 @@ export const createCloudRequest = (options: { baseURL?: string, encrypt?: 'alway
4477
},
4578
})
4679

47-
if (options.encrypt) {
48-
installEncryption(instance, options.encrypt)
80+
if (enableEncryption) {
81+
installEncryption(instance, enableEncryption)
4982
}
5083

51-
installLogging(instance)
52-
installErrorTransform(instance)
84+
if (enableLogging) {
85+
installLogging(instance)
86+
}
87+
88+
if (enableErrorTransform) {
89+
installErrorTransform(instance)
90+
}
5391

5492
return instance
5593
}

0 commit comments

Comments
 (0)