-
Notifications
You must be signed in to change notification settings - Fork 8.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(core): Store SSH key pair for source control in DB settings (#…
- Loading branch information
Showing
8 changed files
with
201 additions
and
33 deletions.
There are no files selected for viewing
106 changes: 106 additions & 0 deletions
106
packages/cli/src/databases/migrations/common/1711390882123-MoveSshKeysToDatabase.ts
This file contains 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,106 @@ | ||
import path from 'node:path'; | ||
import { readFile, writeFile, rm } from 'node:fs/promises'; | ||
import Container from 'typedi'; | ||
import { Cipher, InstanceSettings } from 'n8n-core'; | ||
import { jsonParse } from 'n8n-workflow'; | ||
import type { MigrationContext, ReversibleMigration } from '@db/types'; | ||
|
||
/** | ||
* Move SSH key pair from file system to database, to enable SSH connections | ||
* when running n8n in multiple containers - mains, webhooks, workers, etc. | ||
*/ | ||
export class MoveSshKeysToDatabase1711390882123 implements ReversibleMigration { | ||
private readonly settingsKey = 'features.sourceControl.sshKeys'; | ||
|
||
private readonly privateKeyPath = path.join( | ||
Container.get(InstanceSettings).n8nFolder, | ||
'ssh', | ||
'key', | ||
); | ||
|
||
private readonly publicKeyPath = this.privateKeyPath + '.pub'; | ||
|
||
private readonly cipher = Container.get(Cipher); | ||
|
||
async up({ escape, runQuery, logger, migrationName }: MigrationContext) { | ||
let privateKey, publicKey; | ||
|
||
try { | ||
[privateKey, publicKey] = await Promise.all([ | ||
readFile(this.privateKeyPath, { encoding: 'utf8' }), | ||
readFile(this.publicKeyPath, { encoding: 'utf8' }), | ||
]); | ||
} catch { | ||
logger.info(`[${migrationName}] No SSH keys in filesystem, skipping`); | ||
return; | ||
} | ||
|
||
const settings = escape.tableName('settings'); | ||
|
||
const rows: Array<{ value: string }> = await runQuery( | ||
`SELECT value FROM ${settings} WHERE key = '${this.settingsKey}';`, | ||
); | ||
|
||
if (rows.length === 1) { | ||
logger.info(`[${migrationName}] SSH keys already in database, skipping`); | ||
return; | ||
} | ||
|
||
const value = JSON.stringify({ | ||
encryptedPrivateKey: this.cipher.encrypt(privateKey), | ||
publicKey, | ||
}); | ||
|
||
await runQuery( | ||
`INSERT INTO ${settings} (key, value) VALUES ('${this.settingsKey}', '${value}');`, | ||
); | ||
|
||
try { | ||
await Promise.all([rm(this.privateKeyPath), rm(this.publicKeyPath)]); | ||
} catch (e) { | ||
const error = e instanceof Error ? e : new Error(`${e}`); | ||
logger.error( | ||
`[${migrationName}] Failed to remove SSH keys from filesystem: ${error.message}`, | ||
); | ||
} | ||
} | ||
|
||
async down({ escape, runQuery, logger, migrationName }: MigrationContext) { | ||
const settings = escape.tableName('settings'); | ||
|
||
const rows: Array<{ value: string }> = await runQuery( | ||
`SELECT value FROM ${settings} WHERE key = '${this.settingsKey}';`, | ||
); | ||
|
||
if (rows.length !== 1) { | ||
logger.info(`[${migrationName}] No SSH keys in database, skipping revert`); | ||
return; | ||
} | ||
|
||
const [row] = rows; | ||
|
||
type KeyPair = { publicKey: string; encryptedPrivateKey: string }; | ||
|
||
const dbKeyPair = jsonParse<KeyPair | null>(row.value, { fallbackValue: null }); | ||
|
||
if (!dbKeyPair) { | ||
logger.info(`[${migrationName}] Malformed SSH keys in database, skipping revert`); | ||
return; | ||
} | ||
|
||
const privateKey = this.cipher.decrypt(dbKeyPair.encryptedPrivateKey); | ||
const { publicKey } = dbKeyPair; | ||
|
||
try { | ||
await Promise.all([ | ||
writeFile(this.privateKeyPath, privateKey, { encoding: 'utf8', mode: 0o600 }), | ||
writeFile(this.publicKeyPath, publicKey, { encoding: 'utf8', mode: 0o600 }), | ||
]); | ||
} catch { | ||
logger.error(`[${migrationName}] Failed to write SSH keys to filesystem, skipping revert`); | ||
return; | ||
} | ||
|
||
await runQuery(`DELETE ${settings} WHERE WHERE key = 'features.sourceControl.sshKeys';`); | ||
} | ||
} |
This file contains 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 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 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 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 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 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 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