Skip to content

Commit 7d9714b

Browse files
committed
preserve api env during login
1 parent a20f8e4 commit 7d9714b

File tree

5 files changed

+75
-10
lines changed

5 files changed

+75
-10
lines changed

lib/commands/login/index.js

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,45 @@ import terminalLink from 'terminal-link'
77
import { AuthError, InputError } from '../../utils/errors.js'
88
import { FREE_API_KEY, setupSdk } from '../../utils/sdk.js'
99
import { getSetting, updateSetting } from '../../utils/settings.js'
10+
import { prepareFlags } from '../../utils/flags.js'
11+
import { printFlagList } from '../../utils/formatting.js'
1012

1113
const description = 'Socket API login'
1214

1315
/** @type {import('../../utils/meow-with-subcommands').CliSubcommand} */
1416
export const login = {
1517
description,
1618
run: async (argv, importMeta, { parentName }) => {
19+
const flags = prepareFlags({
20+
apiBaseUrl: {
21+
type: 'string',
22+
description: 'API server to connect to for login',
23+
},
24+
apiProxy: {
25+
type: 'string',
26+
description: 'Proxy to use when making connection to API server'
27+
}
28+
})
1729
const name = parentName + ' login'
1830
const cli = meow(`
1931
Usage
2032
$ ${name}
2133
2234
Logs into the Socket API by prompting for an API key
2335
36+
Options
37+
${printFlagList({
38+
'api-base-url': flags.apiBaseUrl.description,
39+
'api-proxy': flags.apiProxy.description
40+
}, 8)}
41+
2442
Examples
2543
$ ${name}
2644
`, {
2745
argv,
2846
description,
2947
importMeta,
48+
flags
3049
})
3150

3251
/**
@@ -58,13 +77,27 @@ export const login = {
5877

5978
const apiKey = result.apiKey || FREE_API_KEY
6079

80+
/**
81+
* @type {string | null | undefined}
82+
*/
83+
let apiBaseUrl = cli.flags.apiBaseUrl
84+
apiBaseUrl ??= getSetting('apiBaseUrl') ??
85+
undefined
86+
87+
/**
88+
* @type {string | null | undefined}
89+
*/
90+
let apiProxy = cli.flags.apiProxy
91+
apiProxy ??= getSetting('apiProxy') ??
92+
undefined
93+
6194
const spinner = ora('Verifying API key...').start()
6295

6396
/** @type {import('@socketsecurity/sdk').SocketSdkReturnType<'getOrganizations'>['data']} */
6497
let orgs
6598

6699
try {
67-
const sdk = await setupSdk(apiKey)
100+
const sdk = await setupSdk(apiKey, apiBaseUrl, apiProxy)
68101
const result = await sdk.getOrganizations()
69102
if (!result.success) throw new AuthError()
70103
orgs = result.data
@@ -131,6 +164,7 @@ export const login = {
131164
updateSetting('enforcedOrgs', enforcedOrgs)
132165
const oldKey = getSetting('apiKey')
133166
updateSetting('apiKey', apiKey)
167+
updateSetting('apiBaseUrl', apiBaseUrl)
134168
spinner.succeed(`API credentials ${oldKey ? 'updated' : 'set'}`)
135169
}
136170
}

lib/commands/report/create.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,6 @@ async function setupCommand (name, description, argv, importMeta) {
179179
}
180180
})
181181

182-
// TODO: setupSdk(getDefaultKey() || FREE_API_KEY)
183182
const socketSdk = await setupSdk()
184183
const supportedFiles = await socketSdk.getReportSupportedFiles()
185184
.then(res => {

lib/utils/sdk.js

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,43 @@ export function getDefaultKey () {
2424
return defaultKey
2525
}
2626

27+
/**
28+
* The API server that should be used for operations
29+
*
30+
* @type {string | undefined}
31+
*/
32+
let defaultAPIBaseUrl
33+
34+
/**
35+
* @returns {string | undefined}
36+
*/
37+
export function getDefaultAPIBaseUrl () {
38+
defaultAPIBaseUrl = process.env['SOCKET_SECURITY_API_BASE_URL'] || getSetting('apiBaseUrl') || undefined
39+
return defaultAPIBaseUrl
40+
}
41+
42+
/**
43+
* The API server that should be used for operations
44+
*
45+
* @type {string | undefined}
46+
*/
47+
let defaultApiProxy
48+
49+
/**
50+
* @returns {string | undefined}
51+
*/
52+
export function getDefaultHTTPProxy () {
53+
defaultApiProxy = process.env['SOCKET_SECURITY_API_PROXY'] || getSetting('apiProxy') || undefined
54+
return defaultApiProxy
55+
}
56+
2757
/**
2858
* @param {string} [apiKey]
59+
* @param {string} [apiBaseUrl]
60+
* @param {string} [proxy]
2961
* @returns {Promise<import('@socketsecurity/sdk').SocketSdk>}
3062
*/
31-
export async function setupSdk (apiKey = getDefaultKey()) {
63+
export async function setupSdk (apiKey = getDefaultKey(), apiBaseUrl = getDefaultAPIBaseUrl(), proxy = getDefaultHTTPProxy()) {
3264
if (apiKey == null && isInteractive()) {
3365
/**
3466
* @type {{ apiKey: string }}
@@ -49,11 +81,11 @@ export async function setupSdk (apiKey = getDefaultKey()) {
4981
/** @type {import('@socketsecurity/sdk').SocketSdkOptions["agent"]} */
5082
let agent
5183

52-
if (process.env['SOCKET_SECURITY_API_PROXY']) {
84+
if (proxy) {
5385
const { HttpProxyAgent, HttpsProxyAgent } = await import('hpagent')
5486
agent = {
55-
http: new HttpProxyAgent({ proxy: process.env['SOCKET_SECURITY_API_PROXY'] }),
56-
https: new HttpsProxyAgent({ proxy: process.env['SOCKET_SECURITY_API_PROXY'] }),
87+
http: new HttpProxyAgent({ proxy }),
88+
https: new HttpsProxyAgent({ proxy }),
5789
}
5890
}
5991
const packageJsonPath = join(dirname(fileURLToPath(import.meta.url)), '../../package.json')
@@ -62,7 +94,7 @@ export async function setupSdk (apiKey = getDefaultKey()) {
6294
/** @type {import('@socketsecurity/sdk').SocketSdkOptions} */
6395
const sdkOptions = {
6496
agent,
65-
baseUrl: process.env['SOCKET_SECURITY_API_BASE_URL'],
97+
baseUrl: apiBaseUrl,
6698
userAgent: createUserAgentFromPkgJson(JSON.parse(packageJson))
6799
}
68100

lib/utils/settings.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const settingsPath = path.join(dataHome, 'socket', 'settings')
2323
* @typedef {Record<string, boolean | {action: 'error' | 'warn' | 'ignore' | 'defer'}>} IssueRules
2424
*/
2525

26-
/** @type {{apiKey?: string | null, enforcedOrgs?: string[] | null}} */
26+
/** @type {{apiKey?: string | null, enforcedOrgs?: string[] | null, apiBaseUrl?: string | null, apiProxy?: string | null}} */
2727
let settings = {}
2828

2929
if (fs.existsSync(settingsPath)) {

tsconfig.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99
"test/**/*"
1010
],
1111
"exclude": [
12-
"alib/shadow/**"
12+
"lib/shadow/**"
1313
],
1414
"compilerOptions": {
1515
"allowJs": true,
1616
"checkJs": true,
1717
"noEmit": true,
1818
"resolveJsonModule": true,
19-
"module": "NodeNext",
19+
"module": "ESNext",
2020
"moduleResolution": "node",
2121
"lib": ["ES2022"],
2222
"target": "es2022",

0 commit comments

Comments
 (0)