| 1 | import { FQSN, PackageSlug, encodePackageSlug } from "./slugs.js"; |
| 2 | |
| 3 | export enum SecretType { |
| 4 | User = "user", |
| 5 | Package = "package", |
| 6 | Organization = "organization", |
| 7 | NotFound = "not_found", |
| 8 | ModelsAddOn = "models_add_on", |
| 9 | FreeTrial = "free_trial", |
| 10 | LocalEnv = "local_env", |
| 11 | ProcessEnv = "process_env", |
| 12 | } |
| 13 | |
| 14 | export interface OrgSecretLocation { |
| 15 | secretType: SecretType.Organization; |
| 16 | orgSlug: string; |
| 17 | secretName: string; |
| 18 | } |
| 19 | |
| 20 | export interface PackageSecretLocation { |
| 21 | secretType: SecretType.Package; |
| 22 | packageSlug: PackageSlug; |
| 23 | secretName: string; |
| 24 | } |
| 25 | |
| 26 | export interface UserSecretLocation { |
| 27 | secretType: SecretType.User; |
| 28 | userSlug: string; |
| 29 | secretName: string; |
| 30 | } |
| 31 | |
| 32 | export interface ModelsAddOnSecretLocation { |
| 33 | secretType: SecretType.ModelsAddOn; |
| 34 | blockSlug: PackageSlug; |
| 35 | secretName: string; |
| 36 | } |
| 37 | |
| 38 | export interface FreeTrialSecretLocation { |
| 39 | secretType: SecretType.FreeTrial; |
| 40 | blockSlug: PackageSlug; |
| 41 | secretName: string; |
| 42 | } |
| 43 | |
| 44 | export interface LocalEnvSecretLocation { |
| 45 | secretType: SecretType.LocalEnv; |
| 46 | secretName: string; |
| 47 | } |
| 48 | |
| 49 | export interface ProcessEnvSecretLocation { |
| 50 | secretType: SecretType.ProcessEnv; |
| 51 | secretName: string; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * If not found in user/package/org secrets, then there's a chance it's in |
| 56 | * - the on-prem proxy |
| 57 | * - models add-on |
| 58 | * - free trial |
| 59 | */ |
| 60 | export interface NotFoundSecretLocation { |
| 61 | secretType: SecretType.NotFound; |
| 62 | secretName: string; |
| 63 | } |
| 64 | |
| 65 | export type SecretLocation = |
| 66 | | OrgSecretLocation |
| 67 | | PackageSecretLocation |
| 68 | | UserSecretLocation |
| 69 | | NotFoundSecretLocation |
| 70 | | ModelsAddOnSecretLocation |
| 71 | | FreeTrialSecretLocation |
| 72 | | LocalEnvSecretLocation |
| 73 | | ProcessEnvSecretLocation; |
| 74 | |
| 75 | export function encodeSecretLocation(secretLocation: SecretLocation): string { |
| 76 | if (secretLocation.secretType === SecretType.Organization) { |
| 77 | return `${SecretType.Organization}:${secretLocation.orgSlug}/${secretLocation.secretName}`; |
| 78 | } else if (secretLocation.secretType === SecretType.User) { |
| 79 | return `${SecretType.User}:${secretLocation.userSlug}/${secretLocation.secretName}`; |
| 80 | } else if (secretLocation.secretType === SecretType.Package) { |
| 81 | return `${SecretType.Package}:${encodePackageSlug(secretLocation.packageSlug)}/${secretLocation.secretName}`; |
| 82 | } else if (secretLocation.secretType === SecretType.NotFound) { |
| 83 | return `${SecretType.NotFound}:${secretLocation.secretName}`; |
| 84 | } else if (secretLocation.secretType === SecretType.ModelsAddOn) { |
| 85 | return `${SecretType.ModelsAddOn}:${encodePackageSlug(secretLocation.blockSlug)}/${secretLocation.secretName}`; |
| 86 | } else if (secretLocation.secretType === SecretType.FreeTrial) { |
| 87 | return `${SecretType.FreeTrial}:${encodePackageSlug(secretLocation.blockSlug)}/${secretLocation.secretName}`; |
| 88 | } else if (secretLocation.secretType === SecretType.LocalEnv) { |
| 89 | return `${SecretType.LocalEnv}:${secretLocation.secretName}`; |
| 90 | } else if (secretLocation.secretType === SecretType.ProcessEnv) { |
| 91 | return `${SecretType.ProcessEnv}:${secretLocation.secretName}`; |
| 92 | } else { |
| 93 | throw new Error(`Invalid secret type: ${secretLocation}`); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | export function decodeSecretLocation(secretLocation: string): SecretLocation { |
| 98 | const [secretType, rest] = secretLocation.split(":"); |
| 99 | const parts = rest.split("/"); |
| 100 | const secretName = parts[parts.length - 1]; |
| 101 | |
| 102 | switch (secretType) { |
| 103 | case SecretType.Organization: |
| 104 | return { |
| 105 | secretType: SecretType.Organization, |
| 106 | orgSlug: parts[0], |
| 107 | secretName, |
| 108 | }; |
| 109 | case SecretType.User: |
| 110 | return { |
| 111 | secretType: SecretType.User, |
| 112 | userSlug: parts[0], |
| 113 | secretName, |
| 114 | }; |
| 115 | case SecretType.Package: |
| 116 | return { |
| 117 | secretType: SecretType.Package, |
| 118 | packageSlug: { ownerSlug: parts[0], packageSlug: parts[1] }, |
| 119 | secretName, |
| 120 | }; |
| 121 | case SecretType.NotFound: |
| 122 | return { |
| 123 | secretType: SecretType.NotFound, |
| 124 | secretName, |
| 125 | }; |
| 126 | case SecretType.ModelsAddOn: |
| 127 | return { |
| 128 | secretType: SecretType.ModelsAddOn, |
| 129 | secretName, |
| 130 | blockSlug: { |
| 131 | ownerSlug: parts[0], |
| 132 | packageSlug: parts[1], |
| 133 | }, |
| 134 | }; |
| 135 | case SecretType.FreeTrial: |
| 136 | return { |
| 137 | secretType: SecretType.FreeTrial, |
| 138 | secretName, |
| 139 | blockSlug: { |
| 140 | ownerSlug: parts[0], |
| 141 | packageSlug: parts[1], |
| 142 | }, |
| 143 | }; |
| 144 | case SecretType.LocalEnv: |
| 145 | return { |
| 146 | secretType: SecretType.LocalEnv, |
| 147 | secretName, |
| 148 | }; |
| 149 | case SecretType.ProcessEnv: |
| 150 | return { |
| 151 | secretType: SecretType.ProcessEnv, |
| 152 | secretName, |
| 153 | }; |
| 154 | default: |
| 155 | throw new Error(`Invalid secret type: ${secretType}`); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | export interface NotFoundSecretResult { |
| 160 | found: false; |
| 161 | secretLocation: NotFoundSecretLocation; |
| 162 | fqsn: FQSN; |
| 163 | } |
| 164 | |
| 165 | export interface FoundSecretResult { |
| 166 | found: true; |
| 167 | secretLocation: |
| 168 | | OrgSecretLocation |
| 169 | | PackageSecretLocation |
| 170 | | ModelsAddOnSecretLocation |
| 171 | | FreeTrialSecretLocation |
| 172 | | LocalEnvSecretLocation |
| 173 | | ProcessEnvSecretLocation; |
| 174 | fqsn: FQSN; |
| 175 | } |
| 176 | |
| 177 | export interface FoundUserSecretResult { |
| 178 | found: true; |
| 179 | secretLocation: UserSecretLocation; |
| 180 | value: string; |
| 181 | fqsn: FQSN; |
| 182 | } |
| 183 | |
| 184 | export type SecretResult = |
| 185 | | FoundSecretResult |
| 186 | | FoundUserSecretResult |
| 187 | | NotFoundSecretResult; |