-
Notifications
You must be signed in to change notification settings - Fork 12
feat(workflow-executor): add OAuth credential store + deposit endpoint (PRD-367 PR1) #1619
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
hercemer42
wants to merge
7
commits into
main
Choose a base branch
from
feat/prd-367-pr1-executor-oauth-credentials
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
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6b7fb91
feat(workflow-executor): add OAuth credential store + deposit endpoint
hercemer42 9b30201
test(workflow-executor): cover store error paths + harden crypto test…
hercemer42 349076e
docs(workflow-executor): describe FOREST_EXECUTOR_ENCRYPTION_KEY gene…
hercemer42 a5440b7
refactor(workflow-executor): make OAuth credentials store a port with…
hercemer42 4e0e022
docs(workflow-executor): tighten comments and revert README table ref…
hercemer42 6a6c262
refactor(workflow-executor): drop the MCP OAuth credentials DELETE ro…
hercemer42 d42828e
feat(workflow-executor): restore the MCP OAuth credentials DELETE rou…
hercemer42 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
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
90 changes: 90 additions & 0 deletions
90
packages/workflow-executor/src/crypto/credential-encryption.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,90 @@ | ||
| import { createCipheriv, createDecipheriv, hkdfSync, randomFillSync } from 'crypto'; | ||
|
|
||
| import { ExecutorEncryptionKeyMissingError } from '../errors'; | ||
|
|
||
| const ENV_KEY = 'FOREST_EXECUTOR_ENCRYPTION_KEY'; | ||
| // Fixed context label bound into the HKDF derivation — domain-separates this key from any other | ||
| // use of the same secret. Changing it would make every existing row undecryptable. | ||
| const HKDF_INFO = 'forest-executor:mcp-oauth-credentials'; | ||
| const HKDF_DIGEST = 'sha256'; | ||
| const KEY_BYTES = 32; // AES-256 | ||
| const IV_BYTES = 12; // GCM standard nonce length | ||
| const AUTH_TAG_BYTES = 16; | ||
| const ALGORITHM = 'aes-256-gcm'; | ||
| const CURRENT_ENC_KEY_VERSION = 1; | ||
|
|
||
| export interface EncryptedValue { | ||
| // Packed layout: iv | authTag | ciphertext — stored as a single BLOB column. | ||
| ciphertext: Buffer; | ||
| encKeyVersion: number; | ||
| } | ||
|
|
||
| // Concatenate byte arrays without going through Buffer.concat — keeps everything in the concrete | ||
| // Uint8Array<ArrayBuffer> domain the Node crypto types expect. | ||
| function concatBytes(parts: Uint8Array[]): Uint8Array { | ||
| const total = parts.reduce((length, part) => length + part.length, 0); | ||
| const out = new Uint8Array(total); | ||
| let offset = 0; | ||
|
|
||
| for (const part of parts) { | ||
| out.set(part, offset); | ||
| offset += part.length; | ||
| } | ||
|
|
||
| return out; | ||
| } | ||
|
|
||
| // At-rest encryption for secrets the executor stores. The HKDF key (from | ||
| // FOREST_EXECUTOR_ENCRYPTION_KEY) is read lazily — an executor that stores no such secrets boots | ||
| // without it — and fails closed: a missing key throws rather than persisting or returning an | ||
| // unprotected value. | ||
| export default class CredentialEncryption { | ||
| private readonly encKeyVersion: number; | ||
|
|
||
| constructor(encKeyVersion: number = CURRENT_ENC_KEY_VERSION) { | ||
| this.encKeyVersion = encKeyVersion; | ||
| } | ||
|
|
||
| encrypt(plaintext: string): EncryptedValue { | ||
| const iv = randomFillSync(new Uint8Array(IV_BYTES)); | ||
| const cipher = createCipheriv(ALGORITHM, this.deriveKey(), iv); | ||
| const encrypted = concatBytes([ | ||
| new Uint8Array(cipher.update(plaintext, 'utf8')), | ||
| new Uint8Array(cipher.final()), | ||
| ]); | ||
| const authTag = new Uint8Array(cipher.getAuthTag()); | ||
|
|
||
| return { | ||
| ciphertext: Buffer.from(concatBytes([iv, authTag, encrypted])), | ||
| encKeyVersion: this.encKeyVersion, | ||
| }; | ||
| } | ||
|
|
||
| decrypt(value: Buffer): string { | ||
| const bytes = new Uint8Array(value); | ||
| const iv = bytes.subarray(0, IV_BYTES); | ||
| const authTag = bytes.subarray(IV_BYTES, IV_BYTES + AUTH_TAG_BYTES); | ||
| const encrypted = bytes.subarray(IV_BYTES + AUTH_TAG_BYTES); | ||
|
|
||
| const decipher = createDecipheriv(ALGORITHM, this.deriveKey(), iv); | ||
| decipher.setAuthTag(authTag); | ||
|
|
||
| const decrypted = concatBytes([ | ||
| new Uint8Array(decipher.update(encrypted)), | ||
| new Uint8Array(decipher.final()), | ||
| ]); | ||
|
|
||
| return Buffer.from(decrypted).toString('utf8'); | ||
| } | ||
|
|
||
| private deriveKey(): Uint8Array { | ||
| const secret = process.env[ENV_KEY]; | ||
|
|
||
| if (!secret) throw new ExecutorEncryptionKeyMissingError(); | ||
|
|
||
| // Empty salt is intentional: the fixed HKDF_INFO label gives domain separation and the | ||
| // single high-entropy secret needs no salt. Wrap hkdfSync's ArrayBuffer as a concrete | ||
| // Uint8Array<ArrayBuffer> to satisfy CipherKey (Buffer's ArrayBufferLike backing does not). | ||
| return new Uint8Array(hkdfSync(HKDF_DIGEST, secret, new Uint8Array(0), HKDF_INFO, KEY_BYTES)); | ||
|
hercemer42 marked this conversation as resolved.
|
||
| } | ||
| } | ||
|
hercemer42 marked this conversation as resolved.
|
||
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
Oops, something went wrong.
Oops, something went wrong.
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.