| 1 | import { SecretLocation, SecretResult, SecretType } from "./SecretResult.js"; |
| 2 | import { |
| 3 | FQSN, |
| 4 | PackageIdentifier, |
| 5 | PackageSlug, |
| 6 | packageSlugsEqual, |
| 7 | } from "./slugs.js"; |
| 8 | |
| 9 | /** |
| 10 | * A registry stores the content of packages |
| 11 | */ |
| 12 | export interface Registry { |
| 13 | getContent(fullSlug: PackageIdentifier): Promise<string>; |
| 14 | } |
| 15 | export type SecretNamesMap = Map<FQSN, string>; |
| 16 | |
| 17 | /** |
| 18 | * A secret store stores secrets |
| 19 | */ |
| 20 | export interface SecretStore { |
| 21 | get(secretName: string): Promise<string | undefined>; |
| 22 | set(secretName: string, secretValue: string): Promise<void>; |
| 23 | } |
| 24 | |
| 25 | export interface PlatformClient { |
| 26 | resolveFQSNs(fqsns: FQSN[]): Promise<(SecretResult | undefined)[]>; |
| 27 | } |
| 28 | |
| 29 | export interface PlatformSecretStore { |
| 30 | getSecretFromSecretLocation( |
| 31 | secretLocation: SecretLocation, |
| 32 | ): Promise<string | undefined>; |
| 33 | } |
| 34 | |
| 35 | export function getLocationsToLook( |
| 36 | assistantSlug: PackageSlug, |
| 37 | blockSlug: PackageSlug | undefined, |
| 38 | currentUserSlug: string, |
| 39 | secretName: string, |
| 40 | orgScopeSlug: string | null, |
| 41 | ): SecretLocation[] { |
| 42 | const locationsToLook: SecretLocation[] = [ |
| 43 | ...(blockSlug |
| 44 | ? [ |
| 45 | // Models Add-On |
| 46 | { |
| 47 | secretType: SecretType.ModelsAddOn as const, |
| 48 | secretName, |
| 49 | blockSlug, |
| 50 | }, |
| 51 | // Block |
| 52 | { |
| 53 | secretType: SecretType.Package as const, |
| 54 | packageSlug: blockSlug, |
| 55 | secretName, |
| 56 | }, |
| 57 | ] |
| 58 | : []), |
| 59 | // Assistant |
| 60 | { |
| 61 | secretType: SecretType.Package as const, |
| 62 | packageSlug: assistantSlug, |
| 63 | secretName, |
| 64 | }, |
| 65 | // Organization that owns assistant (org secrets can only be used in assistants owned by that org) |
| 66 | ...(orgScopeSlug |
| 67 | ? [ |
| 68 | { |
| 69 | secretType: SecretType.Organization as const, |
| 70 | orgSlug: orgScopeSlug, |
| 71 | secretName, |
| 72 | }, |
| 73 | ] |
| 74 | : []), |
| 75 | // User |
| 76 | { |
| 77 | secretType: SecretType.User as const, |
| 78 | userSlug: currentUserSlug, |
| 79 | secretName, |
| 80 | }, |
| 81 | // Free Trial (must be using a eligible block) |
| 82 | ...(blockSlug |
| 83 | ? [ |
| 84 | { |
| 85 | secretType: SecretType.FreeTrial as const, |
| 86 | secretName, |
| 87 | blockSlug, |
| 88 | }, |
| 89 | ] |
| 90 | : []), |
| 91 | ]; |
| 92 | return locationsToLook; |
| 93 | } |
| 94 | |
| 95 | export function listAvailableSecrets( |
| 96 | userSecretNames: string[], |
| 97 | orgSecretNames: string[], |
| 98 | assistantSecretNames: string[], |
| 99 | blockSecretNames: string[], |
| 100 | assistantSlug: PackageSlug, |
| 101 | blockSlug: PackageSlug | undefined, |
| 102 | currentUserSlug: string, |
| 103 | orgScopeSlug: string | null, |
| 104 | ): SecretLocation[] { |
| 105 | // Create a set of all secret names |
| 106 | const allSecretNames = new Set([ |
| 107 | ...userSecretNames, |
| 108 | ...orgSecretNames, |
| 109 | ...assistantSecretNames, |
| 110 | ...blockSecretNames, |
| 111 | ]); |
| 112 | |
| 113 | // Use the resolution order to get a single SecretLocation for each secret name |
| 114 | const secretLocations: SecretLocation[] = []; |
| 115 | for (const secretName of allSecretNames) { |
| 116 | // Get the order of places to look |
| 117 | const locationsToLook = getLocationsToLook( |
| 118 | assistantSlug, |
| 119 | blockSlug, |
| 120 | currentUserSlug, |
| 121 | secretName, |
| 122 | orgScopeSlug, |
| 123 | ); |
| 124 | |
| 125 | // Go through the locations one by one |
| 126 | for (const secretLocation of locationsToLook) { |
| 127 | // "Looking in a location" in this case means looking through one of the lists of secret names |
| 128 | // First we get that list of secret names |
| 129 | let secretNamesList: string[] = []; |
| 130 | switch (secretLocation.secretType) { |
| 131 | case SecretType.User: |
| 132 | secretNamesList = userSecretNames; |
| 133 | break; |
| 134 | case SecretType.Organization: |
| 135 | secretNamesList = orgSecretNames; |
| 136 | break; |
| 137 | case SecretType.Package: |
| 138 | if (packageSlugsEqual(secretLocation.packageSlug, assistantSlug)) { |
| 139 | secretNamesList = assistantSecretNames; |
| 140 | } else if ( |
| 141 | blockSlug && |
| 142 | packageSlugsEqual(secretLocation.packageSlug, blockSlug) |
| 143 | ) { |
| 144 | secretNamesList = blockSecretNames; |
| 145 | } |
| 146 | break; |
| 147 | } |
| 148 | |
| 149 | // Then we look through that list for the matching secret name |
| 150 | if (secretNamesList) { |
| 151 | const matchingSecretName = secretNamesList.find( |
| 152 | (secretName) => secretName === secretLocation.secretName, |
| 153 | ); |
| 154 | if (matchingSecretName) { |
| 155 | // If we find a matching secret name, we add the location to the list |
| 156 | secretLocations.push(secretLocation); |
| 157 | break; |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | return secretLocations; |
| 164 | } |
| 165 | |
| 166 | export async function resolveFQSN( |
| 167 | currentUserSlug: string, |
| 168 | fqsn: FQSN, |
| 169 | platformSecretStore: PlatformSecretStore, |
| 170 | orgScopeSlug: string | null, |
| 171 | ): Promise<SecretResult> { |
| 172 | // First create the list of secret locations to try in order |
| 173 | const assistantSlug = fqsn.packageSlugs[0]; |
| 174 | const blockSlug = fqsn.packageSlugs[1]; |
| 175 | const locationsToLook = getLocationsToLook( |
| 176 | assistantSlug, |
| 177 | blockSlug, |
| 178 | currentUserSlug, |
| 179 | fqsn.secretName, |
| 180 | orgScopeSlug, |
| 181 | ); |
| 182 | |
| 183 | // Then try to get the secret from each location |
| 184 | for (const secretLocation of locationsToLook) { |
| 185 | const secret = |
| 186 | await platformSecretStore.getSecretFromSecretLocation(secretLocation); |
| 187 | if (secret) { |
| 188 | if (secretLocation.secretType === SecretType.User) { |
| 189 | // Only user secret values get sent back to client |
| 190 | return { |
| 191 | found: true, |
| 192 | fqsn, |
| 193 | secretLocation, |
| 194 | value: secret, |
| 195 | }; |
| 196 | } else if (secretLocation.secretType !== SecretType.NotFound) { |
| 197 | return { |
| 198 | found: true, |
| 199 | fqsn, |
| 200 | secretLocation, |
| 201 | }; |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | return { |
| 207 | found: false, |
| 208 | secretLocation: { |
| 209 | secretName: fqsn.secretName, |
| 210 | secretType: SecretType.NotFound, |
| 211 | }, |
| 212 | fqsn, |
| 213 | }; |
| 214 | } |