-
Notifications
You must be signed in to change notification settings - Fork 8.8k
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
feat(core): Add SSH key generation #6006
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6332410
basic prefs and ssh key generation
flipswitchingmonkey 4f703be
Merge branch 'master' into pay-380-add-ssh-key-packages
flipswitchingmonkey 8d54370
review change
flipswitchingmonkey ebbfef6
cleanup save
flipswitchingmonkey d4aeac8
lint fix
flipswitchingmonkey 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 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
18 changes: 18 additions & 0 deletions
18
packages/cli/src/environment/versionControl/versionControlHelper.ee.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,18 @@ | ||
import Container from 'typedi'; | ||
import { License } from '../../License'; | ||
import { generateKeyPairSync } from 'crypto'; | ||
|
||
export function isVersionControlEnabled() { | ||
const license = Container.get(License); | ||
return license.isVersionControlLicensed(); | ||
} | ||
|
||
export async function generateSshKeyPair() { | ||
const keyPair = generateKeyPairSync('ed25519', { | ||
privateKeyEncoding: { format: 'pem', type: 'pkcs8' }, | ||
publicKeyEncoding: { format: 'pem', type: 'spki' }, | ||
}); | ||
|
||
console.log(keyPair.privateKey); | ||
console.log(keyPair.publicKey); | ||
} |
7 changes: 0 additions & 7 deletions
7
packages/cli/src/environment/versionControl/versionControlHelper.ts
This file was deleted.
Oops, something went wrong.
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 @@ | ||
export const VERSION_CONTROL_PREFERENCES_DB_KEY = 'features.versionControl'; |
34 changes: 34 additions & 0 deletions
34
packages/cli/src/environments/versionControl/middleware/versionControlEnabledMiddleware.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,34 @@ | ||
import type { RequestHandler } from 'express'; | ||
import type { AuthenticatedRequest } from '@/requests'; | ||
import { | ||
isVersionControlLicensed, | ||
isVersionControlLicensedAndEnabled, | ||
} from '../versionControlHelper'; | ||
|
||
export const versionControlLicensedOwnerMiddleware: RequestHandler = ( | ||
req: AuthenticatedRequest, | ||
res, | ||
next, | ||
) => { | ||
if (isVersionControlLicensed() && req.user?.globalRole.name === 'owner') { | ||
next(); | ||
} else { | ||
res.status(401).json({ status: 'error', message: 'Unauthorized' }); | ||
} | ||
}; | ||
|
||
export const versionControlLicensedAndEnabledMiddleware: RequestHandler = (req, res, next) => { | ||
if (isVersionControlLicensedAndEnabled()) { | ||
next(); | ||
} else { | ||
res.status(401).json({ status: 'error', message: 'Unauthorized' }); | ||
} | ||
}; | ||
|
||
export const versionControlLicensedMiddleware: RequestHandler = (req, res, next) => { | ||
if (isVersionControlLicensed()) { | ||
next(); | ||
} else { | ||
res.status(401).json({ status: 'error', message: 'Unauthorized' }); | ||
} | ||
}; |
4 changes: 4 additions & 0 deletions
4
packages/cli/src/environments/versionControl/types/keyPair.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,4 @@ | ||
export interface KeyPair { | ||
privateKey: string; | ||
publicKey: string; | ||
} |
9 changes: 9 additions & 0 deletions
9
packages/cli/src/environments/versionControl/types/versionControlPreferences.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,9 @@ | ||
import { IsString } from 'class-validator'; | ||
|
||
export class VersionControlPreferences { | ||
@IsString() | ||
privateKey: string; | ||
|
||
@IsString() | ||
publicKey: string; | ||
} |
22 changes: 22 additions & 0 deletions
22
packages/cli/src/environments/versionControl/versionControl.controller.ee.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,22 @@ | ||
import { Get, RestController } from '../../decorators'; | ||
import { | ||
versionControlLicensedMiddleware, | ||
versionControlLicensedOwnerMiddleware, | ||
} from './middleware/versionControlEnabledMiddleware'; | ||
import { VersionControlService } from './versionControl.service.ee'; | ||
|
||
@RestController('/versionControl') | ||
export class VersionControlController { | ||
constructor(private versionControlService: VersionControlService) {} | ||
|
||
@Get('/preferences', { middlewares: [versionControlLicensedMiddleware] }) | ||
async getPreferences() { | ||
return this.versionControlService.versionControlPreferences; | ||
} | ||
|
||
//TODO: temporary function to generate key and save new pair | ||
@Get('/generateKeyPair', { middlewares: [versionControlLicensedOwnerMiddleware] }) | ||
async generateKeyPair() { | ||
return this.versionControlService.generateAndSaveKeyPair(); | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
packages/cli/src/environments/versionControl/versionControl.service.ee.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,70 @@ | ||
import { Service } from 'typedi'; | ||
import { generateSshKeyPair } from './versionControlHelper'; | ||
import { VersionControlPreferences } from './types/versionControlPreferences'; | ||
import { VERSION_CONTROL_PREFERENCES_DB_KEY } from './constants'; | ||
import * as Db from '@/Db'; | ||
import { jsonParse } from 'n8n-workflow'; | ||
|
||
@Service() | ||
export class VersionControlService { | ||
private _versionControlPreferences: VersionControlPreferences = new VersionControlPreferences(); | ||
|
||
async init(): Promise<void> { | ||
await this.loadFromDbAndApplyVersionControlPreferences(); | ||
} | ||
|
||
public get versionControlPreferences(): VersionControlPreferences { | ||
return { | ||
...this._versionControlPreferences, | ||
privateKey: '', | ||
}; | ||
} | ||
|
||
async generateAndSaveKeyPair(keyType: 'ed25519' | 'rsa' = 'ed25519') { | ||
const keyPair = generateSshKeyPair(keyType); | ||
if (keyPair.publicKey && keyPair.privateKey) { | ||
this.setPreferences({ ...keyPair }); | ||
await this.saveSamlPreferencesToDb(); | ||
} | ||
return keyPair; | ||
} | ||
|
||
setPreferences(prefs: Partial<VersionControlPreferences>) { | ||
this._versionControlPreferences = { | ||
...this._versionControlPreferences, | ||
...prefs, | ||
}; | ||
} | ||
|
||
async loadFromDbAndApplyVersionControlPreferences(): Promise< | ||
VersionControlPreferences | undefined | ||
> { | ||
const loadedPrefs = await Db.collections.Settings.findOne({ | ||
where: { key: VERSION_CONTROL_PREFERENCES_DB_KEY }, | ||
}); | ||
if (loadedPrefs) { | ||
try { | ||
const prefs = jsonParse<VersionControlPreferences>(loadedPrefs.value); | ||
if (prefs) { | ||
this.setPreferences(prefs); | ||
return prefs; | ||
} | ||
} catch {} | ||
} | ||
return; | ||
} | ||
|
||
async saveSamlPreferencesToDb(): Promise<VersionControlPreferences | undefined> { | ||
const settingsValue = JSON.stringify(this._versionControlPreferences); | ||
const result = await Db.collections.Settings.save({ | ||
key: VERSION_CONTROL_PREFERENCES_DB_KEY, | ||
value: settingsValue, | ||
loadOnStartup: true, | ||
}); | ||
if (result) | ||
try { | ||
return jsonParse<VersionControlPreferences>(result.value); | ||
} catch {} | ||
return; | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
packages/cli/src/environments/versionControl/versionControlHelper.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,53 @@ | ||
import Container from 'typedi'; | ||
import { License } from '../../License'; | ||
import { generateKeyPairSync } from 'crypto'; | ||
import sshpk from 'sshpk'; | ||
import type { KeyPair } from './types/keyPair'; | ||
|
||
export function isVersionControlLicensed() { | ||
const license = Container.get(License); | ||
return license.isVersionControlLicensed(); | ||
} | ||
|
||
export function isVersionControlEnabled() { | ||
// TODO: VERSION CONTROL check if enabled | ||
return true; | ||
} | ||
|
||
export function isVersionControlLicensedAndEnabled() { | ||
return isVersionControlLicensed() && isVersionControlEnabled(); | ||
} | ||
|
||
export function generateSshKeyPair(keyType: 'ed25519' | 'rsa' = 'ed25519') { | ||
const keyPair: KeyPair = { | ||
publicKey: '', | ||
privateKey: '', | ||
}; | ||
let generatedKeyPair: KeyPair; | ||
switch (keyType) { | ||
case 'ed25519': | ||
generatedKeyPair = generateKeyPairSync('ed25519', { | ||
privateKeyEncoding: { format: 'pem', type: 'pkcs8' }, | ||
publicKeyEncoding: { format: 'pem', type: 'spki' }, | ||
}); | ||
break; | ||
case 'rsa': | ||
generatedKeyPair = generateKeyPairSync('rsa', { | ||
modulusLength: 4096, | ||
publicKeyEncoding: { | ||
type: 'spki', | ||
format: 'pem', | ||
}, | ||
privateKeyEncoding: { | ||
type: 'pkcs8', | ||
format: 'pem', | ||
}, | ||
}); | ||
break; | ||
} | ||
const keyPublic = sshpk.parseKey(generatedKeyPair.publicKey, 'pem'); | ||
keyPair.publicKey = keyPublic.toString('ssh'); | ||
const keyPrivate = sshpk.parsePrivateKey(generatedKeyPair.privateKey, 'pem'); | ||
keyPair.privateKey = keyPrivate.toString('ssh-private'); | ||
return keyPair; | ||
} |
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
38 changes: 38 additions & 0 deletions
38
packages/cli/test/integration/environments/VersionControl.test.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,38 @@ | ||
import { Container } from 'typedi'; | ||
import type { SuperAgentTest } from 'supertest'; | ||
import type { User } from '@db/entities/User'; | ||
import { License } from '@/License'; | ||
import * as testDb from '../shared/testDb'; | ||
import * as utils from '../shared/utils'; | ||
import { VersionControlService } from '../../../src/environments/versionControl/versionControl.service.ee'; | ||
|
||
let owner: User; | ||
let authOwnerAgent: SuperAgentTest; | ||
|
||
beforeAll(async () => { | ||
Container.get(License).isVersionControlLicensed = () => true; | ||
const app = await utils.initTestServer({ endpointGroups: ['versionControl'] }); | ||
owner = await testDb.createOwner(); | ||
authOwnerAgent = utils.createAuthAgent(app)(owner); | ||
}); | ||
|
||
afterAll(async () => { | ||
await testDb.terminate(); | ||
}); | ||
|
||
describe('GET /versionControl/preferences', () => { | ||
test('should return Version Control preferences', async () => { | ||
await Container.get(VersionControlService).generateAndSaveKeyPair(); | ||
await authOwnerAgent | ||
.get('/versionControl/preferences') | ||
.expect(200) | ||
.expect((res) => { | ||
return ( | ||
'privateKey' in res.body && | ||
'publicKey' in res.body && | ||
res.body.publicKey.includes('ssh-ed25519') && | ||
res.body.privateKey.includes('BEGIN OPENSSH PRIVATE KEY') | ||
); | ||
}); | ||
}); | ||
}); |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm guessing this function could check if a key already exists and throw an error in case it does - or at least in the future, check if there is a connected repo.
If a repo is connected, we want to prevent generating new keys at random WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is really just a temporary helper function for now - once it is used in the context of the actual saving of settings, it will be adjusted to check. Right now there is just nothing to check yet and I wanted to keeps this PR small and just contain the SSH key related functions.