|
| 1 | +import { handleApiCall } from '../../utils/api' |
| 2 | +import { supportedConfigKeys } from '../../utils/config' |
| 3 | +import { getDefaultToken, setupSdk } from '../../utils/sdk' |
| 4 | + |
| 5 | +import type { LocalConfig } from '../../utils/config' |
| 6 | + |
| 7 | +export async function discoverConfigValue( |
| 8 | + key: string |
| 9 | +): Promise<{ success: boolean; value: unknown; message: string }> { |
| 10 | + // This will have to be a specific implementation per key because certain |
| 11 | + // keys should request information from particular API endpoints while |
| 12 | + // others should simply return their default value, like endpoint URL. |
| 13 | + |
| 14 | + if (!supportedConfigKeys.has(key as keyof LocalConfig)) { |
| 15 | + return { |
| 16 | + success: false, |
| 17 | + value: undefined, |
| 18 | + message: 'Requested key is not a valid config key.' |
| 19 | + } |
| 20 | + } |
| 21 | + |
| 22 | + if (key === 'apiBaseUrl') { |
| 23 | + // Return the default value |
| 24 | + return { |
| 25 | + success: false, |
| 26 | + value: undefined, |
| 27 | + message: |
| 28 | + "If you're unsure about the base endpoint URL then simply unset it." |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + if (key === 'apiProxy') { |
| 33 | + // I don't think we can auto-discover this with any order of reliability..? |
| 34 | + return { |
| 35 | + success: false, |
| 36 | + value: undefined, |
| 37 | + message: |
| 38 | + 'When uncertain, unset this key. Otherwise ask your network administrator.' |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + if (key === 'apiToken') { |
| 43 | + return { |
| 44 | + success: false, |
| 45 | + value: undefined, |
| 46 | + message: |
| 47 | + 'You can find/create your API token in your Socket dashboard > settings > API tokens.\nYou should then use `socket login` to login instead of this command.' |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + if (key === 'defaultOrg') { |
| 52 | + const apiToken = getDefaultToken() |
| 53 | + if (!apiToken) { |
| 54 | + return { |
| 55 | + success: false, |
| 56 | + value: undefined, |
| 57 | + message: |
| 58 | + 'No API token set, must have a token to resolve its default org.' |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + const org = await getDefaultOrgFromToken() |
| 63 | + if (!org?.length) { |
| 64 | + return { |
| 65 | + success: false, |
| 66 | + value: undefined, |
| 67 | + message: |
| 68 | + 'Was unable to determine default org for the current API token.' |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + if (Array.isArray(org)) { |
| 73 | + return { |
| 74 | + success: true, |
| 75 | + value: org, |
| 76 | + message: 'These are the orgs that the current API token can access.' |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + return { |
| 81 | + success: true, |
| 82 | + value: org, |
| 83 | + message: 'This is the org that belongs to the current API token.' |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + if (key === 'enforcedOrgs') { |
| 88 | + const apiToken = getDefaultToken() |
| 89 | + if (!apiToken) { |
| 90 | + return { |
| 91 | + success: false, |
| 92 | + value: undefined, |
| 93 | + message: |
| 94 | + 'No API token set, must have a token to resolve orgs to enforce.' |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + const orgs = await getEnforceableOrgsFromToken() |
| 99 | + if (!orgs?.length) { |
| 100 | + return { |
| 101 | + success: false, |
| 102 | + value: undefined, |
| 103 | + message: |
| 104 | + 'Was unable to determine any orgs to enforce for the current API token.' |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + return { |
| 109 | + success: true, |
| 110 | + value: orgs, |
| 111 | + message: 'These are the orgs whose security policy you can enforce.' |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + if (key === 'test') { |
| 116 | + return { |
| 117 | + success: false, |
| 118 | + value: undefined, |
| 119 | + message: '' |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + // Mostly to please TS, because we're not telling it `key` is keyof LocalConfig |
| 124 | + return { |
| 125 | + success: false, |
| 126 | + value: undefined, |
| 127 | + message: 'unreachable?' |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +async function getDefaultOrgFromToken(): Promise< |
| 132 | + string[] | string | undefined |
| 133 | +> { |
| 134 | + const sockSdk = await setupSdk() |
| 135 | + const result = await handleApiCall( |
| 136 | + sockSdk.getOrganizations(), |
| 137 | + 'looking up organizations' |
| 138 | + ) |
| 139 | + |
| 140 | + if (result.success) { |
| 141 | + const arr = Array.from(Object.values(result.data.organizations)).map( |
| 142 | + ({ slug }) => slug |
| 143 | + ) |
| 144 | + if (arr.length === 0) { |
| 145 | + return undefined |
| 146 | + } |
| 147 | + if (arr.length === 1) { |
| 148 | + return arr[0] |
| 149 | + } |
| 150 | + return arr |
| 151 | + } |
| 152 | + |
| 153 | + return undefined |
| 154 | +} |
| 155 | + |
| 156 | +async function getEnforceableOrgsFromToken(): Promise<string[] | undefined> { |
| 157 | + const sockSdk = await setupSdk() |
| 158 | + const result = await handleApiCall( |
| 159 | + sockSdk.getOrganizations(), |
| 160 | + 'looking up organizations' |
| 161 | + ) |
| 162 | + |
| 163 | + if (result.success) { |
| 164 | + const arr = Array.from(Object.values(result.data.organizations)).map( |
| 165 | + ({ slug }) => slug |
| 166 | + ) |
| 167 | + if (arr.length === 0) { |
| 168 | + return undefined |
| 169 | + } |
| 170 | + return arr |
| 171 | + } |
| 172 | + |
| 173 | + return undefined |
| 174 | +} |
0 commit comments