-
Notifications
You must be signed in to change notification settings - Fork 10
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
Draft
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.
Draft
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e518846
feat(workflow-executor): add OAuth credential store + deposit endpoint
hercemer42 b20ff7c
docs(workflow-executor): note intentional empty HKDF salt
hercemer42 b236200
test(workflow-executor): cover oauth wiring and delete response
hercemer42 564bb77
feat(workflow-executor): validate oauth deposit body strictly
hercemer42 e1838a9
refactor(workflow-executor): harden and reorganize oauth deposit endp…
hercemer42 270031d
docs(workflow-executor): document FOREST_EXECUTOR_ENCRYPTION_KEY in e…
hercemer42 d7cee9a
docs(workflow-executor): add FOREST_EXECUTOR_ENCRYPTION_KEY to env re…
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
89 changes: 89 additions & 0 deletions
89
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,89 @@ | ||
| 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 OAuth credentials. The HKDF key (from FOREST_EXECUTOR_ENCRYPTION_KEY) is | ||
| // read lazily — an OAuth-less executor 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)); | ||
| } | ||
| } | ||
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
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.