|
| 1 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | + |
| 3 | +const findMany = vi.fn(); |
| 4 | + |
| 5 | +vi.mock("@dokploy/server/db", () => ({ |
| 6 | + db: { |
| 7 | + query: { |
| 8 | + vaultProvider: { |
| 9 | + findMany: (...args: unknown[]) => findMany(...args), |
| 10 | + }, |
| 11 | + }, |
| 12 | + }, |
| 13 | +})); |
| 14 | + |
| 15 | +import { prepareEnvironmentVariables } from "@dokploy/server/utils/docker/utils"; |
| 16 | +import { |
| 17 | + resolveVaultReferences, |
| 18 | + withResolvedVaultRefs, |
| 19 | +} from "@dokploy/server/utils/vault"; |
| 20 | +import { azureClient } from "@dokploy/server/utils/vault/azure"; |
| 21 | +import { dopplerClient } from "@dokploy/server/utils/vault/doppler"; |
| 22 | +import { hashicorpClient } from "@dokploy/server/utils/vault/hashicorp"; |
| 23 | + |
| 24 | +const mockFetch = vi.fn(); |
| 25 | +global.fetch = mockFetch as typeof fetch; |
| 26 | + |
| 27 | +const jsonResponse = (body: unknown, ok = true, status = 200) => |
| 28 | + ({ |
| 29 | + ok, |
| 30 | + status, |
| 31 | + json: async () => body, |
| 32 | + }) as Response; |
| 33 | + |
| 34 | +beforeEach(() => { |
| 35 | + findMany.mockReset(); |
| 36 | + mockFetch.mockReset(); |
| 37 | +}); |
| 38 | + |
| 39 | +describe("resolveVaultReferences", () => { |
| 40 | + it("returns input untouched and skips the db when no refs are present", async () => { |
| 41 | + const env = "FOO=bar\nBAZ=${{project.QUX}}"; |
| 42 | + const result = await resolveVaultReferences(env, "org-1"); |
| 43 | + expect(result).toBe(env); |
| 44 | + expect(findMany).not.toHaveBeenCalled(); |
| 45 | + }); |
| 46 | + |
| 47 | + it("returns null/empty inputs unchanged", async () => { |
| 48 | + expect(await resolveVaultReferences(null, "org-1")).toBeNull(); |
| 49 | + expect(await resolveVaultReferences("", "org-1")).toBe(""); |
| 50 | + }); |
| 51 | + |
| 52 | + it("throws when refs exist but no organization context is given", async () => { |
| 53 | + await expect( |
| 54 | + resolveVaultReferences("FOO=${{vault.prod.SECRET}}"), |
| 55 | + ).rejects.toThrow("not supported in this context"); |
| 56 | + }); |
| 57 | + |
| 58 | + it("throws for an unknown provider", async () => { |
| 59 | + findMany.mockResolvedValue([]); |
| 60 | + await expect( |
| 61 | + resolveVaultReferences("FOO=${{vault.missing.SECRET}}", "org-1"), |
| 62 | + ).rejects.toThrow('Vault provider "missing" not found'); |
| 63 | + }); |
| 64 | + |
| 65 | + it("resolves refs through a doppler provider", async () => { |
| 66 | + findMany.mockResolvedValue([ |
| 67 | + { |
| 68 | + name: "doppler-prod", |
| 69 | + providerType: "doppler", |
| 70 | + config: { providerType: "doppler", serviceToken: "dp.st.token" }, |
| 71 | + }, |
| 72 | + ]); |
| 73 | + mockFetch.mockResolvedValue( |
| 74 | + jsonResponse({ DB_URL: "postgres://real", API_KEY: "key-123" }), |
| 75 | + ); |
| 76 | + |
| 77 | + const result = await resolveVaultReferences( |
| 78 | + "DB_URL=${{vault.doppler-prod.DB_URL}}\nAPI_KEY=${{vault.doppler-prod.API_KEY}}", |
| 79 | + "org-1", |
| 80 | + ); |
| 81 | + |
| 82 | + expect(result).toBe("DB_URL=postgres://real\nAPI_KEY=key-123"); |
| 83 | + expect(mockFetch).toHaveBeenCalledTimes(1); |
| 84 | + }); |
| 85 | + |
| 86 | + it("throws when the secret is missing in the provider", async () => { |
| 87 | + findMany.mockResolvedValue([ |
| 88 | + { |
| 89 | + name: "doppler-prod", |
| 90 | + providerType: "doppler", |
| 91 | + config: { providerType: "doppler", serviceToken: "dp.st.token" }, |
| 92 | + }, |
| 93 | + ]); |
| 94 | + mockFetch.mockResolvedValue(jsonResponse({ OTHER: "x" })); |
| 95 | + |
| 96 | + await expect( |
| 97 | + resolveVaultReferences("FOO=${{vault.doppler-prod.MISSING}}", "org-1"), |
| 98 | + ).rejects.toThrow('secret "MISSING" not found'); |
| 99 | + }); |
| 100 | +}); |
| 101 | + |
| 102 | +describe("withResolvedVaultRefs + prepareEnvironmentVariables", () => { |
| 103 | + it("resolves entity env sources so sync interpolation sees real values", async () => { |
| 104 | + findMany.mockResolvedValue([ |
| 105 | + { |
| 106 | + name: "prod", |
| 107 | + providerType: "doppler", |
| 108 | + config: { providerType: "doppler", serviceToken: "dp.st.token" }, |
| 109 | + }, |
| 110 | + ]); |
| 111 | + mockFetch.mockResolvedValue(jsonResponse({ DB_PASSWORD: "s3cret" })); |
| 112 | + |
| 113 | + const entity = { |
| 114 | + env: "DATABASE_URL=postgres://user:${{project.DB_PASSWORD}}@db", |
| 115 | + environment: { |
| 116 | + env: null, |
| 117 | + project: { |
| 118 | + env: "DB_PASSWORD=${{vault.prod.DB_PASSWORD}}", |
| 119 | + organizationId: "org-1", |
| 120 | + }, |
| 121 | + }, |
| 122 | + }; |
| 123 | + |
| 124 | + const resolved = await withResolvedVaultRefs(entity); |
| 125 | + const prepared = prepareEnvironmentVariables( |
| 126 | + resolved.env, |
| 127 | + resolved.environment.project.env, |
| 128 | + resolved.environment.env, |
| 129 | + ); |
| 130 | + |
| 131 | + expect(prepared).toEqual(["DATABASE_URL=postgres://user:s3cret@db"]); |
| 132 | + }); |
| 133 | + |
| 134 | + it("resolves buildArgs and buildSecrets when present", async () => { |
| 135 | + findMany.mockResolvedValue([ |
| 136 | + { |
| 137 | + name: "prod", |
| 138 | + providerType: "doppler", |
| 139 | + config: { providerType: "doppler", serviceToken: "dp.st.token" }, |
| 140 | + }, |
| 141 | + ]); |
| 142 | + mockFetch.mockResolvedValue(jsonResponse({ NPM_TOKEN: "npm-123" })); |
| 143 | + |
| 144 | + const resolved = await withResolvedVaultRefs({ |
| 145 | + env: "FOO=bar", |
| 146 | + buildArgs: "NPM_TOKEN=${{vault.prod.NPM_TOKEN}}", |
| 147 | + buildSecrets: null, |
| 148 | + environment: { |
| 149 | + env: null, |
| 150 | + project: { env: null, organizationId: "org-1" }, |
| 151 | + }, |
| 152 | + }); |
| 153 | + |
| 154 | + expect(resolved.buildArgs).toBe("NPM_TOKEN=npm-123"); |
| 155 | + expect(resolved.buildSecrets).toBeNull(); |
| 156 | + expect(resolved.env).toBe("FOO=bar"); |
| 157 | + }); |
| 158 | + |
| 159 | + it("prepareEnvironmentVariables throws on unresolved vault refs", () => { |
| 160 | + expect(() => |
| 161 | + prepareEnvironmentVariables("FOO=${{vault.prod.SECRET}}", "", ""), |
| 162 | + ).toThrow("Unresolved vault reference"); |
| 163 | + }); |
| 164 | + |
| 165 | + it("keeps working without vault refs", () => { |
| 166 | + const prepared = prepareEnvironmentVariables( |
| 167 | + "FOO=${{project.BAR}}", |
| 168 | + "BAR=baz", |
| 169 | + ); |
| 170 | + expect(prepared).toEqual(["FOO=baz"]); |
| 171 | + expect(findMany).not.toHaveBeenCalled(); |
| 172 | + }); |
| 173 | +}); |
| 174 | + |
| 175 | +describe("hashicorp client", () => { |
| 176 | + const config = { |
| 177 | + providerType: "hashicorp" as const, |
| 178 | + url: "https://vault.example.com", |
| 179 | + token: "hvs.token", |
| 180 | + mount: "secret", |
| 181 | + }; |
| 182 | + |
| 183 | + it("rejects refs without a field", async () => { |
| 184 | + await expect( |
| 185 | + hashicorpClient.getSecrets(config, ["myapp/prod"]), |
| 186 | + ).rejects.toThrow("expected format <path>:<field>"); |
| 187 | + }); |
| 188 | + |
| 189 | + it("groups refs by path and picks fields from KV v2 data", async () => { |
| 190 | + mockFetch.mockResolvedValue( |
| 191 | + jsonResponse({ data: { data: { USER: "admin", PASS: "pw" } } }), |
| 192 | + ); |
| 193 | + |
| 194 | + const result = await hashicorpClient.getSecrets(config, [ |
| 195 | + "myapp/prod:USER", |
| 196 | + "myapp/prod:PASS", |
| 197 | + ]); |
| 198 | + |
| 199 | + expect(result).toEqual({ |
| 200 | + "myapp/prod:USER": "admin", |
| 201 | + "myapp/prod:PASS": "pw", |
| 202 | + }); |
| 203 | + expect(mockFetch).toHaveBeenCalledTimes(1); |
| 204 | + expect(mockFetch.mock.calls[0]?.[0]).toBe( |
| 205 | + "https://vault.example.com/v1/secret/data/myapp/prod", |
| 206 | + ); |
| 207 | + }); |
| 208 | + |
| 209 | + it("throws when a field is missing", async () => { |
| 210 | + mockFetch.mockResolvedValue(jsonResponse({ data: { data: { A: "1" } } })); |
| 211 | + await expect( |
| 212 | + hashicorpClient.getSecrets(config, ["myapp/prod:MISSING"]), |
| 213 | + ).rejects.toThrow('field "MISSING" not found'); |
| 214 | + }); |
| 215 | + |
| 216 | + it("lists full path:field refs by walking directories", async () => { |
| 217 | + mockFetch.mockImplementation(async (url: string) => { |
| 218 | + if (url.includes("/metadata?list=true")) { |
| 219 | + return jsonResponse({ data: { keys: ["myapp/", "shared"] } }); |
| 220 | + } |
| 221 | + if (url.includes("/metadata/myapp?list=true")) { |
| 222 | + return jsonResponse({ data: { keys: ["prod"] } }); |
| 223 | + } |
| 224 | + if (url.includes("/data/myapp/prod")) { |
| 225 | + return jsonResponse({ |
| 226 | + data: { data: { API_KEY: "a", DB_PASSWORD: "b" } }, |
| 227 | + }); |
| 228 | + } |
| 229 | + if (url.includes("/data/shared")) { |
| 230 | + return jsonResponse({ data: { data: { STRIPE_KEY: "c" } } }); |
| 231 | + } |
| 232 | + return jsonResponse({}, false, 404); |
| 233 | + }); |
| 234 | + |
| 235 | + const names = await hashicorpClient.listSecretNames?.(config); |
| 236 | + |
| 237 | + expect(names).toEqual([ |
| 238 | + "myapp/prod:API_KEY", |
| 239 | + "myapp/prod:DB_PASSWORD", |
| 240 | + "shared:STRIPE_KEY", |
| 241 | + ]); |
| 242 | + }); |
| 243 | +}); |
| 244 | + |
| 245 | +describe("azure client", () => { |
| 246 | + const config = { |
| 247 | + providerType: "azure" as const, |
| 248 | + vaultUri: "https://my-vault.vault.azure.net", |
| 249 | + tenantId: "tenant-1", |
| 250 | + clientId: "client-1", |
| 251 | + clientSecret: "secret-1", |
| 252 | + }; |
| 253 | + |
| 254 | + it("authenticates and reads secrets by name", async () => { |
| 255 | + mockFetch.mockImplementation(async (url: string) => { |
| 256 | + if (url.includes("login.microsoftonline.com/tenant-1")) { |
| 257 | + return jsonResponse({ access_token: "azure-token" }); |
| 258 | + } |
| 259 | + if (url.includes("/secrets/db-password?")) { |
| 260 | + return jsonResponse({ value: "azure-pw" }); |
| 261 | + } |
| 262 | + return jsonResponse({}, false, 404); |
| 263 | + }); |
| 264 | + |
| 265 | + const result = await azureClient.getSecrets(config, ["db-password"]); |
| 266 | + |
| 267 | + expect(result).toEqual({ "db-password": "azure-pw" }); |
| 268 | + }); |
| 269 | + |
| 270 | + it("throws a clear error for missing secrets", async () => { |
| 271 | + mockFetch.mockImplementation(async (url: string) => { |
| 272 | + if (url.includes("login.microsoftonline.com")) { |
| 273 | + return jsonResponse({ access_token: "azure-token" }); |
| 274 | + } |
| 275 | + return jsonResponse({}, false, 404); |
| 276 | + }); |
| 277 | + |
| 278 | + await expect(azureClient.getSecrets(config, ["nope"])).rejects.toThrow( |
| 279 | + 'secret "nope" not found', |
| 280 | + ); |
| 281 | + }); |
| 282 | + |
| 283 | + it("lists secret names from paged results", async () => { |
| 284 | + mockFetch.mockImplementation(async (url: string) => { |
| 285 | + if (url.includes("login.microsoftonline.com")) { |
| 286 | + return jsonResponse({ access_token: "azure-token" }); |
| 287 | + } |
| 288 | + if (url.includes("skiptoken")) { |
| 289 | + return jsonResponse({ |
| 290 | + value: [{ id: `${config.vaultUri}/secrets/second` }], |
| 291 | + nextLink: null, |
| 292 | + }); |
| 293 | + } |
| 294 | + return jsonResponse({ |
| 295 | + value: [{ id: `${config.vaultUri}/secrets/first` }], |
| 296 | + nextLink: `${config.vaultUri}/secrets?api-version=7.4&skiptoken=abc`, |
| 297 | + }); |
| 298 | + }); |
| 299 | + |
| 300 | + const names = await azureClient.listSecretNames?.(config); |
| 301 | + |
| 302 | + expect(names).toEqual(["first", "second"]); |
| 303 | + }); |
| 304 | +}); |
| 305 | + |
| 306 | +describe("doppler client", () => { |
| 307 | + it("propagates auth errors with the status code", async () => { |
| 308 | + mockFetch.mockResolvedValue(jsonResponse({}, false, 401)); |
| 309 | + await expect( |
| 310 | + dopplerClient.getSecrets( |
| 311 | + { providerType: "doppler", serviceToken: "bad" }, |
| 312 | + ["FOO"], |
| 313 | + ), |
| 314 | + ).rejects.toThrow("status 401"); |
| 315 | + }); |
| 316 | +}); |
0 commit comments