|
| 1 | +import { generateKeyPairSync } from 'node:crypto'; |
| 2 | +import { beforeAll, describe, expect, it } from 'vitest'; |
| 3 | +import { encryptWithPublicKey } from '../../utils/encryption.js'; |
| 4 | +import { buildFrameEnv } from './frame-env.js'; |
| 5 | + |
| 6 | +let publicKey: string; |
| 7 | +let privateKey: string; |
| 8 | + |
| 9 | +beforeAll(() => { |
| 10 | + ({ publicKey, privateKey } = generateKeyPairSync('rsa', { |
| 11 | + modulusLength: 2048, |
| 12 | + publicKeyEncoding: { type: 'spki', format: 'pem' }, |
| 13 | + privateKeyEncoding: { type: 'pkcs8', format: 'pem' }, |
| 14 | + })); |
| 15 | +}); |
| 16 | + |
| 17 | +function secret(value: string) { |
| 18 | + return encryptWithPublicKey(value, publicKey); |
| 19 | +} |
| 20 | + |
| 21 | +describe('buildFrameEnv', () => { |
| 22 | + it('returns the same env and no secret keys when there are no secrets', () => { |
| 23 | + const specEnv = { PLAIN: 'value', KILOCODE_TOKEN: 'grant-token' }; |
| 24 | + const result = buildFrameEnv({ specEnv, encryptedSecrets: undefined, privateKey }); |
| 25 | + expect(result.env).toEqual(specEnv); |
| 26 | + expect(result.secretEnvKeys).toEqual([]); |
| 27 | + }); |
| 28 | + |
| 29 | + it('overlays a secret over plaintext of the same key', () => { |
| 30 | + const result = buildFrameEnv({ |
| 31 | + specEnv: { DATABASE_URL: 'plaintext', KEEP: 'kept' }, |
| 32 | + encryptedSecrets: { DATABASE_URL: secret('encrypted-value') }, |
| 33 | + privateKey, |
| 34 | + }); |
| 35 | + expect(result.env).toEqual({ DATABASE_URL: 'encrypted-value', KEEP: 'kept' }); |
| 36 | + expect(result.secretEnvKeys).toEqual(['DATABASE_URL']); |
| 37 | + }); |
| 38 | + |
| 39 | + it.each([ |
| 40 | + 'KILOCODE_TOKEN', |
| 41 | + 'GH_TOKEN', |
| 42 | + 'GITHUB_TOKEN', |
| 43 | + 'GITLAB_TOKEN', |
| 44 | + 'GLAB_IS_OAUTH2', |
| 45 | + 'GITLAB_HOST', |
| 46 | + 'GITLAB_SUBFOLDER', |
| 47 | + 'BITBUCKET_TOKEN', |
| 48 | + 'KILO_BITBUCKET_WORKSPACE_SLUG', |
| 49 | + 'KILO_BITBUCKET_REPOSITORY_SLUG', |
| 50 | + 'KILO_BITBUCKET_WORKSPACE_UUID', |
| 51 | + 'KILO_BITBUCKET_REPOSITORY_UUID', |
| 52 | + ])('restores the issued %s the spec already carried over a same-named secret', key => { |
| 53 | + const result = buildFrameEnv({ |
| 54 | + specEnv: { [key]: 'grant-value' }, |
| 55 | + encryptedSecrets: { [key]: secret('secret-value') }, |
| 56 | + privateKey, |
| 57 | + }); |
| 58 | + expect(result.env?.[key]).toBe('grant-value'); |
| 59 | + // The grant value survived, not the decrypted secret, so nothing to redact. |
| 60 | + expect(result.secretEnvKeys).toEqual([]); |
| 61 | + }); |
| 62 | + |
| 63 | + it('keeps a plaintext GH_TOKEN the grant left in place when a different secret overlays', () => { |
| 64 | + const result = buildFrameEnv({ |
| 65 | + specEnv: { GH_TOKEN: 'grant-gh', GITHUB_TOKEN: 'grant-gh' }, |
| 66 | + encryptedSecrets: { DATABASE_URL: secret('db') }, |
| 67 | + privateKey, |
| 68 | + }); |
| 69 | + expect(result.env?.GH_TOKEN).toBe('grant-gh'); |
| 70 | + expect(result.env?.GITHUB_TOKEN).toBe('grant-gh'); |
| 71 | + expect(result.secretEnvKeys).toEqual(['DATABASE_URL']); |
| 72 | + }); |
| 73 | + |
| 74 | + it('deletes an org id a secret added when the uncontained spec did not carry it', () => { |
| 75 | + const result = buildFrameEnv({ |
| 76 | + specEnv: {}, |
| 77 | + encryptedSecrets: { KILOCODE_ORGANIZATION_ID: secret('org-from-secret') }, |
| 78 | + privateKey, |
| 79 | + }); |
| 80 | + expect(Object.hasOwn(result.env ?? {}, 'KILOCODE_ORGANIZATION_ID')).toBe(false); |
| 81 | + expect(result.secretEnvKeys).toEqual([]); |
| 82 | + }); |
| 83 | + |
| 84 | + it('keeps a contained spec org id, including a value the grant did not touch', () => { |
| 85 | + const result = buildFrameEnv({ |
| 86 | + specEnv: { KILOCODE_ORGANIZATION_ID: 'org-from-grant' }, |
| 87 | + encryptedSecrets: { KILOCODE_ORGANIZATION_ID: secret('org-from-secret') }, |
| 88 | + privateKey, |
| 89 | + }); |
| 90 | + expect(result.env?.KILOCODE_ORGANIZATION_ID).toBe('org-from-grant'); |
| 91 | + }); |
| 92 | + |
| 93 | + it('deletes a control-runtime name a secret added when the spec did not carry it', () => { |
| 94 | + const result = buildFrameEnv({ |
| 95 | + specEnv: {}, |
| 96 | + encryptedSecrets: { SANDBOX_CONTROL_CREDENTIAL: secret('forged') }, |
| 97 | + privateKey, |
| 98 | + }); |
| 99 | + expect(Object.hasOwn(result.env ?? {}, 'SANDBOX_CONTROL_CREDENTIAL')).toBe(false); |
| 100 | + expect(result.secretEnvKeys).toEqual([]); |
| 101 | + }); |
| 102 | + |
| 103 | + it('keeps a secret name the grant did not issue and the spec did not carry', () => { |
| 104 | + const result = buildFrameEnv({ |
| 105 | + specEnv: {}, |
| 106 | + encryptedSecrets: { GH_TOKEN: secret('user-gh') }, |
| 107 | + privateKey, |
| 108 | + }); |
| 109 | + expect(result.env?.GH_TOKEN).toBe('user-gh'); |
| 110 | + }); |
| 111 | + |
| 112 | + it('throws when secrets are present and no private key is configured', () => { |
| 113 | + expect(() => |
| 114 | + buildFrameEnv({ |
| 115 | + specEnv: {}, |
| 116 | + encryptedSecrets: { DATABASE_URL: secret('value') }, |
| 117 | + privateKey: undefined, |
| 118 | + }) |
| 119 | + ).toThrow(/Private key is required/); |
| 120 | + }); |
| 121 | +}); |
0 commit comments