feat(core): Add rsa option to ssh key generation (#7154)
PR adds a new field to the SourceControlPreferences as well as to the POST parameters for the `source-control/preferences` and `source-control/generate-key-pair` endpoints. Both now accept an optional string parameter `keyGeneratorType` of `'ed25519' | 'rsa'` Calling the `source-control/generate-key-pair` endpoint with the parameter set, it will also update the stored preferences accordingly (so that in the future new keys will use the same method) By default ed25519 is being used. The default may be changed using a new environment parameter: `N8N_SOURCECONTROL_DEFAULT_SSH_KEY_TYPE` which can be `rsa` or `ed25519` RSA keys are generated with a length of 4096 bytes.
This commit is contained in:
committed by
GitHub
parent
aaf87c3edd
commit
fdac2c8572
@@ -19,6 +19,8 @@ import {
|
||||
SOURCE_CONTROL_PREFERENCES_DB_KEY,
|
||||
} from './constants';
|
||||
import path from 'path';
|
||||
import type { KeyPairType } from './types/keyPairType';
|
||||
import config from '@/config';
|
||||
|
||||
@Service()
|
||||
export class SourceControlPreferencesService {
|
||||
@@ -86,9 +88,15 @@ export class SourceControlPreferencesService {
|
||||
* Will generate an ed25519 key pair and save it to the database and the file system
|
||||
* Note: this will overwrite any existing key pair
|
||||
*/
|
||||
async generateAndSaveKeyPair(): Promise<SourceControlPreferences> {
|
||||
async generateAndSaveKeyPair(keyPairType?: KeyPairType): Promise<SourceControlPreferences> {
|
||||
sourceControlFoldersExistCheck([this.gitFolder, this.sshFolder]);
|
||||
const keyPair = await generateSshKeyPair('ed25519');
|
||||
if (!keyPairType) {
|
||||
keyPairType =
|
||||
this.getPreferences().keyGeneratorType ??
|
||||
(config.get('sourceControl.defaultKeyPairType') as KeyPairType) ??
|
||||
'ed25519';
|
||||
}
|
||||
const keyPair = await generateSshKeyPair(keyPairType);
|
||||
if (keyPair.publicKey && keyPair.privateKey) {
|
||||
try {
|
||||
await fsWriteFile(this.sshKeyName + '.pub', keyPair.publicKey, {
|
||||
@@ -100,6 +108,10 @@ export class SourceControlPreferencesService {
|
||||
throw Error(`Failed to save key pair: ${(error as Error).message}`);
|
||||
}
|
||||
}
|
||||
// update preferences only after generating key pair to prevent endless loop
|
||||
if (keyPairType !== this.getPreferences().keyGeneratorType) {
|
||||
await this.setPreferences({ keyGeneratorType: keyPairType });
|
||||
}
|
||||
return this.getPreferences();
|
||||
}
|
||||
|
||||
@@ -146,8 +158,11 @@ export class SourceControlPreferencesService {
|
||||
): Promise<SourceControlPreferences> {
|
||||
sourceControlFoldersExistCheck([this.gitFolder, this.sshFolder]);
|
||||
if (!this.hasKeyPairFiles()) {
|
||||
LoggerProxy.debug('No key pair files found, generating new pair');
|
||||
await this.generateAndSaveKeyPair();
|
||||
const keyPairType =
|
||||
preferences.keyGeneratorType ??
|
||||
(config.get('sourceControl.defaultKeyPairType') as KeyPairType);
|
||||
LoggerProxy.debug(`No key pair files found, generating new pair using type: ${keyPairType}`);
|
||||
await this.generateAndSaveKeyPair(keyPairType);
|
||||
}
|
||||
this.sourceControlPreferences = preferences;
|
||||
if (saveToDb) {
|
||||
|
||||
Reference in New Issue
Block a user