|
1 | 1 | import { TransformCallback, Writable } from 'node:stream'; |
2 | 2 | import { createBuildLoggerWithSecretsFilter } from '../logger'; |
3 | 3 | import z from 'zod'; |
| 4 | +import { EnvironmentSecretType } from '@expo/eas-build-job'; |
| 5 | + |
| 6 | +async function waitForStreamFlush(): Promise<void> { |
| 7 | + await new Promise(resolve => setImmediate(resolve)); |
| 8 | +} |
4 | 9 |
|
5 | 10 | describe('logger', () => { |
6 | | - async function waitForStreamFlush(): Promise<void> { |
7 | | - await new Promise(resolve => setImmediate(resolve)); |
8 | | - } |
| 11 | + it('obfuscates secrets in logs', async () => { |
| 12 | + const { logger, outputStream } = await createBuildLoggerWithSecretsFilter([ |
| 13 | + { name: 'TEST_SECRET', value: 'secret', type: EnvironmentSecretType.STRING }, |
| 14 | + { |
| 15 | + name: 'ANOTHER_SECRET_BASE64', |
| 16 | + value: 'YW5vdGhlclNlY3JldA==', |
| 17 | + type: EnvironmentSecretType.STRING, |
| 18 | + }, |
| 19 | + ]); |
| 20 | + |
| 21 | + const logs: any[] = []; |
| 22 | + |
| 23 | + const writable = new Writable({ |
| 24 | + objectMode: true, |
| 25 | + write(chunk: any, _encoding: BufferEncoding, callback: TransformCallback) { |
| 26 | + logs.push(chunk); |
| 27 | + callback(null, chunk); |
| 28 | + }, |
| 29 | + }); |
| 30 | + |
| 31 | + outputStream.pipe(writable); |
| 32 | + |
| 33 | + logger.info('this is a secret'); |
| 34 | + logger.info(`another secret in base64 is ${Buffer.from('anotherSecret').toString('base64')}`); |
| 35 | + |
| 36 | + await waitForStreamFlush(); |
| 37 | + |
| 38 | + expect(logs.length).toBe(2); |
| 39 | + expect(logs[0].msg).toBe('this is a ******'); |
| 40 | + expect(logs[1].msg).toBe('another ****** in base64 is ********************'); |
| 41 | + }); |
9 | 42 |
|
10 | 43 | it('adds logId to each log', async () => { |
11 | 44 | const { logger, outputStream } = await createBuildLoggerWithSecretsFilter([]); |
|
0 commit comments