Repository navigation
feat(encrypt): add raw output format #1321
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jsskeen-utah
wants to merge
1
commit into
dmno-dev:main
Choose a base branch
from
jsskeen-utah:encrypt-raw-output
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| varlock: minor | ||
| --- | ||
|
|
||
| Add raw output format to varlock encrypt |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
packages/varlock/src/cli/commands/test/encrypt.command.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| import { | ||
| afterEach, beforeEach, describe, expect, test, vi, | ||
| } from 'vitest'; | ||
| import { Readable } from 'node:stream'; | ||
|
|
||
| vi.mock('../../../lib/local-encrypt', () => ({ | ||
| ensureKey: vi.fn(), | ||
| encryptValue: vi.fn(async () => 'encrypted-payload'), | ||
| getBackendInfo: vi.fn(() => ({ | ||
| type: 'file-based', | ||
| hardwareBacked: false, | ||
| biometricAvailable: false, | ||
| })), | ||
| })); | ||
|
|
||
| vi.mock('../../helpers/prompts', () => ({ | ||
| multiselect: vi.fn(), | ||
| password: vi.fn(async (opts: { output?: NodeJS.WritableStream }) => { | ||
| (opts.output ?? process.stdout).write('password prompt output'); | ||
| return 'test-secret'; | ||
| }), | ||
| })); | ||
|
|
||
| import { password } from '../../helpers/prompts'; | ||
| import { commandFn } from '../encrypt.command.js'; | ||
|
|
||
| const originalStdinDescriptor = Object.getOwnPropertyDescriptor(process, 'stdin')!; | ||
| const originalStdin = process.stdin; | ||
| const originalStdinIsTTY = process.stdin.isTTY; | ||
|
|
||
| describe('encrypt command output format', () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| Object.defineProperty(process.stdin, 'isTTY', { configurable: true, value: true }); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| vi.restoreAllMocks(); | ||
| Object.defineProperty(originalStdin, 'isTTY', { configurable: true, value: originalStdinIsTTY }); | ||
| Object.defineProperty(process, 'stdin', originalStdinDescriptor); | ||
| }); | ||
|
|
||
| test('raw format requires piped stdin rather than prompting interactively', async () => { | ||
| const stdoutWrite = vi.spyOn(process.stdout, 'write').mockReturnValue(true); | ||
|
|
||
| await expect(commandFn({ values: { format: 'raw' } } as any)) | ||
| .rejects.toThrow('--format raw requires a value on stdin'); | ||
|
|
||
| expect(stdoutWrite).not.toHaveBeenCalled(); | ||
| expect(password).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| test('raw format writes only the varlock expression to stdout when stdin is piped', async () => { | ||
| const stdin = Readable.from([Buffer.from('test-secret')]); | ||
| Object.defineProperty(stdin, 'isTTY', { configurable: true, value: false }); | ||
| Object.defineProperty(process, 'stdin', { configurable: true, value: stdin }); | ||
| const stdoutWrite = vi.spyOn(process.stdout, 'write').mockReturnValue(true); | ||
| const log = vi.spyOn(console, 'log').mockReturnValue(undefined); | ||
| const error = vi.spyOn(console, 'error').mockReturnValue(undefined); | ||
|
|
||
| await commandFn({ values: { format: 'raw' } } as any); | ||
|
|
||
| expect(stdoutWrite).toHaveBeenCalledExactlyOnceWith('varlock("local:encrypted-payload")\n'); | ||
| expect(log).not.toHaveBeenCalled(); | ||
| expect(password).not.toHaveBeenCalled(); | ||
| expect(error).toHaveBeenCalledWith('Using file-based backend (file-based)'); | ||
| }); | ||
|
|
||
| test('pretty format keeps the existing copy/paste output', async () => { | ||
| const stdoutWrite = vi.spyOn(process.stdout, 'write').mockReturnValue(true); | ||
| const log = vi.spyOn(console, 'log').mockReturnValue(undefined); | ||
|
|
||
| await commandFn({ values: { format: 'pretty' } } as any); | ||
|
|
||
| expect(stdoutWrite).toHaveBeenCalledWith('password prompt output'); | ||
| expect(stdoutWrite.mock.calls.map(([chunk]) => String(chunk)).join('')) | ||
| .toContain('password prompt output'); | ||
| expect(log).toHaveBeenCalledWith('SOME_SENSITIVE_KEY=varlock("local:encrypted-payload")'); | ||
| }); | ||
|
|
||
| test('raw format cannot be combined with --file', async () => { | ||
| await expect(commandFn({ values: { format: 'raw', file: '.env.local' } } as any)) | ||
| .rejects.toThrow('--format raw is only available when encrypting a single value'); | ||
| }); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.