-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathget-security-policy.ts
72 lines (64 loc) · 2.17 KB
/
get-security-policy.ts
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
import { logger } from '@socketsecurity/registry/lib/logger'
import constants from '../../constants'
import { handleApiCall, handleUnsuccessfulApiResponse } from '../../utils/api'
import { AuthError } from '../../utils/errors'
import { mdTableOfPairs } from '../../utils/markdown'
import { getDefaultToken, setupSdk } from '../../utils/sdk'
export async function getSecurityPolicy(
orgSlug: string,
format: 'text' | 'json' | 'markdown'
): Promise<void> {
const apiToken = getDefaultToken()
if (!apiToken) {
throw new AuthError(
'User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key.'
)
}
await getSecurityPolicyWithToken(apiToken, orgSlug, format)
}
async function getSecurityPolicyWithToken(
apiToken: string,
orgSlug: string,
format: 'text' | 'json' | 'markdown'
) {
// Lazily access constants.spinner.
const { spinner } = constants
spinner.start('Fetching organization quota...')
const socketSdk = await setupSdk(apiToken)
const result = await handleApiCall(
socketSdk.getOrgSecurityPolicy(orgSlug),
'looking up organization quota'
)
if (!result.success) {
handleUnsuccessfulApiResponse('getOrgSecurityPolicy', result)
return
}
spinner.stop()
switch (format) {
case 'json': {
logger.log(JSON.stringify(result.data, null, 2))
return
}
default: {
logger.log('# Security policy\n')
logger.log(
`The default security policy setting is: "${result.data.securityPolicyDefault}"\n`
)
logger.log(
'These are the security policies per setting for your organization:\n'
)
const data = result.data
const rules = data.securityPolicyRules
const entries: Array<
[string, { action: 'defer' | 'error' | 'warn' | 'monitor' | 'ignore' }]
// @ts-ignore -- not sure why TS is complaining tbh but it does not like it
> = Object.entries(rules)
const mapped: Array<[string, string]> = entries.map(([key, value]) => [
key,
value.action
])
mapped.sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0))
logger.log(mdTableOfPairs(mapped, ['name', 'action']))
}
}
}