| 1 | import { test, before } from 'node:test' |
| 2 | import assert from 'node:assert/strict' |
| 3 | import { mkdtempSync, rmSync } from 'node:fs' |
| 4 | import { join, dirname } from 'node:path' |
| 5 | import { fileURLToPath, pathToFileURL } from 'node:url' |
| 6 | import { build } from 'esbuild' |
| 7 | |
| 8 | const here = dirname(fileURLToPath(import.meta.url)) |
| 9 | let credentialsAdapter |
| 10 | let brokeredFetch |
| 11 | |
| 12 | before(async () => { |
| 13 | const dir = mkdtempSync(join(here, '..', 'node_modules', '.cw-network-')) |
| 14 | const outfile = join(dir, 'network.mjs') |
| 15 | await build({ entryPoints: [join(here, '../src/dsh/network.ts')], outfile, bundle: true, platform: 'node', format: 'esm', packages: 'external', absWorkingDir: join(here, '..'), logLevel: 'silent' }) |
| 16 | ;({ credentialsAdapter, brokeredFetch } = await import(pathToFileURL(outfile).href)) |
| 17 | process.on('exit', () => rmSync(dir, { recursive: true, force: true })) |
| 18 | }) |
| 19 | |
| 20 | function brokerHost(answers) { |
| 21 | const calls = [] |
| 22 | return { |
| 23 | calls, |
| 24 | host: { |
| 25 | tier: 'plugin', |
| 26 | broker: async (ctx, service, method, input) => { |
| 27 | calls.push({ service, method, input }) |
| 28 | const answer = answers[method] |
| 29 | if (answer instanceof Error) throw answer |
| 30 | return typeof answer === 'function' ? answer(input) : answer |
| 31 | }, |
| 32 | }, |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | test('credentials resolve to an opaque handle and go only to the network broker', async () => { |
| 37 | const { host, calls } = brokerHost({ |
| 38 | 'credentials.resolve': { ref: 'KEY', value: 'codewhale-credential:KEY' }, |
| 39 | 'credentials.describe': { ref: 'KEY', set: true, site: 'https://api.example.com' }, |
| 40 | 'credentials.set': { ref: 'KEY', set: true }, |
| 41 | }) |
| 42 | const Credentials = credentialsAdapter.define(host) |
| 43 | const service = Object.create(Credentials.prototype) |
| 44 | service.ctx = {} |
| 45 | assert.deepEqual(await service.resolve('KEY'), { value: 'codewhale-credential:KEY' }) |
| 46 | assert.deepEqual(await service.describe('KEY'), { ref: 'KEY', set: true, site: 'https://api.example.com' }) |
| 47 | await service.set('KEY', 'sk-secret', 'https://API.example.com/') |
| 48 | assert.deepEqual(calls[2].input, { ref: 'KEY', value: 'sk-secret', site: 'https://api.example.com' }) |
| 49 | for (const site of [undefined, 'http://api.example.com', 'https://user:pw@api.example.com', 'https://api.example.com/v1', 'https://api.example.com/?q=1', 'api.example.com']) { |
| 50 | await assert.rejects(service.set('KEY', 'sk-secret', site), /one https site/, String(site)) |
| 51 | } |
| 52 | assert.equal(calls.length, 3, 'a site that is not an https origin never reaches the broker') |
| 53 | assert.deepEqual(calls.map((c) => [c.service, c.method]), [ |
| 54 | ['network', 'credentials.resolve'], ['network', 'credentials.describe'], ['network', 'credentials.set'], |
| 55 | ]) |
| 56 | await assert.rejects(service.resolve(''), /non-empty/) |
| 57 | }) |
| 58 | |
| 59 | test('a resolve answer that is not a handle is refused rather than passed on', async () => { |
| 60 | const { host } = brokerHost({ 'credentials.resolve': { value: 'sk-raw' } }) |
| 61 | const service = Object.create(credentialsAdapter.define(host).prototype) |
| 62 | service.ctx = {} |
| 63 | await assert.rejects(service.resolve('KEY'), /invalid credential handle/) |
| 64 | const unset = Object.create(credentialsAdapter.define(brokerHost({ 'credentials.resolve': { value: null } }).host).prototype) |
| 65 | unset.ctx = {} |
| 66 | assert.equal(await unset.resolve('KEY'), undefined) |
| 67 | }) |
| 68 | |
| 69 | test('brokeredFetch sends a typed fetch and rebuilds a Response from the broker answer', async () => { |
| 70 | const { host, calls } = brokerHost({ |
| 71 | fetch: { |
| 72 | status: 201, url: 'https://api.example.com/v1/x', redirected: true, |
| 73 | headers: [['content-type', 'application/json']], |
| 74 | body_base64: Buffer.from('{"ok":true}').toString('base64'), |
| 75 | }, |
| 76 | }) |
| 77 | const response = await brokeredFetch(host, {}, 'https://api.example.com/v1/x', { |
| 78 | method: 'POST', headers: { authorization: 'Bearer codewhale-credential:KEY' }, body: 'hi', |
| 79 | }) |
| 80 | assert.equal(response.status, 201) |
| 81 | assert.equal(response.url, 'https://api.example.com/v1/x') |
| 82 | assert.equal(response.redirected, true) |
| 83 | assert.deepEqual(await response.json(), { ok: true }) |
| 84 | assert.equal(calls[0].service, 'network') |
| 85 | assert.equal(calls[0].method, 'fetch') |
| 86 | assert.deepEqual(calls[0].input.headers, [['authorization', 'Bearer codewhale-credential:KEY']]) |
| 87 | assert.equal(Buffer.from(calls[0].input.body_base64, 'base64').toString(), 'hi') |
| 88 | }) |
| 89 | |
| 90 | test('a broker refusal reaches the plugin as the refusal', async () => { |
| 91 | const { host } = brokerHost({ fetch: new Error('this site is not one of the plugin\'s reviewed network sites') }) |
| 92 | await assert.rejects(brokeredFetch(host, {}, 'https://evil.example/'), /not one of the plugin's reviewed network sites/) |
| 93 | }) |
| 94 |