Skip to content

Commit e4f8b4d

Browse files
committed
moved taskGenerateGossipKeys from node.mjs to key_manager.mjs
Signed-off-by: Jeromy Cannon <[email protected]>
1 parent 426e8f3 commit e4f8b4d

File tree

2 files changed

+108
-103
lines changed

2 files changed

+108
-103
lines changed

src/commands/node.mjs

Lines changed: 4 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import * as helpers from '../core/helpers.mjs'
2424
import {
2525
getNodeAccountMap,
2626
getNodeLogs,
27-
getTmpDir,
2827
sleep,
2928
validatePath
3029
} from '../core/helpers.mjs'
@@ -317,104 +316,7 @@ export class NodeCommand extends BaseCommand {
317316
}
318317

319318
/**
320-
* Return a list of subtasks to generate gossip keys
321-
*
322-
* WARNING: These tasks MUST run in sequence.
323-
*
324-
* @param keyFormat key format (pem | pfx)
325-
* @param nodeIds node ids
326-
* @param keysDir keys directory
327-
* @param curDate current date
328-
* @param allNodeIds includes the nodeIds to get new keys as well as existing nodeIds that will be included in the public.pfx file
329-
* @return a list of subtasks
330-
* @private
331-
*/
332-
// TODO: move to KeyManager
333-
taskGenerateGossipKeys (keyFormat, nodeIds, keysDir, curDate = new Date(), allNodeIds = null) {
334-
allNodeIds = allNodeIds || nodeIds
335-
if (!Array.isArray(nodeIds) || !nodeIds.every((nodeId) => typeof nodeId === 'string')) {
336-
throw new IllegalArgumentError('nodeIds must be an array of strings')
337-
}
338-
const self = this
339-
const subTasks = []
340-
341-
switch (keyFormat) {
342-
case constants.KEY_FORMAT_PFX: {
343-
const tmpDir = getTmpDir()
344-
const keytool = self.keytoolDepManager.getKeytool()
345-
346-
subTasks.push({
347-
title: `Check keytool exists (Version: ${self.keytoolDepManager.getKeytoolVersion()})`,
348-
task: async () => self.keytoolDepManager.checkVersion(true)
349-
350-
})
351-
352-
subTasks.push({
353-
title: 'Backup old files',
354-
task: () => helpers.backupOldPfxKeys(nodeIds, keysDir, curDate)
355-
})
356-
357-
for (const nodeId of nodeIds) {
358-
subTasks.push({
359-
title: `Generate ${Templates.renderGossipPfxPrivateKeyFile(nodeId)} for node: ${chalk.yellow(nodeId)}`,
360-
task: async () => {
361-
await self.keyManager.generatePrivatePfxKeys(keytool, nodeId, keysDir, tmpDir)
362-
}
363-
})
364-
}
365-
366-
subTasks.push({
367-
title: `Generate ${constants.PUBLIC_PFX} file`,
368-
task: async () => {
369-
await self.keyManager.updatePublicPfxKey(keytool, allNodeIds, keysDir, tmpDir)
370-
}
371-
})
372-
373-
subTasks.push({
374-
title: 'Clean up temp files',
375-
task: async () => {
376-
if (fs.existsSync(tmpDir)) {
377-
fs.rmSync(tmpDir, { recursive: true })
378-
}
379-
}
380-
})
381-
break
382-
}
383-
384-
case constants.KEY_FORMAT_PEM: {
385-
subTasks.push({
386-
title: 'Backup old files',
387-
task: () => helpers.backupOldPemKeys(nodeIds, keysDir, curDate)
388-
}
389-
)
390-
391-
for (const nodeId of nodeIds) {
392-
subTasks.push({
393-
title: `Gossip ${keyFormat} key for node: ${chalk.yellow(nodeId)}`,
394-
task: async () => {
395-
const signingKey = await this.keyManager.generateSigningKey(nodeId)
396-
const signingKeyFiles = await this.keyManager.storeSigningKey(nodeId, signingKey, keysDir)
397-
this.logger.debug(`generated Gossip signing keys for node ${nodeId}`, { keyFiles: signingKeyFiles })
398-
399-
const agreementKey = await this.keyManager.generateAgreementKey(nodeId, signingKey)
400-
const agreementKeyFiles = await this.keyManager.storeAgreementKey(nodeId, agreementKey, keysDir)
401-
this.logger.debug(`generated Gossip agreement keys for node ${nodeId}`, { keyFiles: agreementKeyFiles })
402-
}
403-
})
404-
}
405-
406-
break
407-
}
408-
409-
default:
410-
throw new FullstackTestingError(`unsupported key-format: ${keyFormat}`)
411-
}
412-
413-
return subTasks
414-
}
415-
416-
/**
417-
* Return a list of subtasks to generate gRPC TLS keys
319+
* Return a list of subtasks to generate gRPC TLS keys
418320
*
419321
* WARNING: These tasks should run in sequence
420322
*
@@ -744,7 +646,7 @@ export class NodeCommand extends BaseCommand {
744646
task: async (ctx, parentTask) => {
745647
const config = /** @type {NodeSetupConfigClass} **/ ctx.config
746648

747-
const subTasks = self.taskGenerateGossipKeys(config.keyFormat, config.nodeIds, config.keysDir, config.curDate)
649+
const subTasks = self.keyManager.taskGenerateGossipKeys(self.keytoolDepManager, config.keyFormat, config.nodeIds, config.keysDir, config.curDate)
748650
// set up the sub-tasks
749651
return parentTask.newListr(subTasks, {
750652
concurrent: false,
@@ -1120,7 +1022,7 @@ export class NodeCommand extends BaseCommand {
11201022
title: 'Generate gossip keys',
11211023
task: async (ctx, parentTask) => {
11221024
const config = ctx.config
1123-
const subTasks = self.taskGenerateGossipKeys(config.keyFormat, config.nodeIds, config.keysDir, config.curDate)
1025+
const subTasks = self.keyManager.taskGenerateGossipKeys(self.keytoolDepManager, config.keyFormat, config.nodeIds, config.keysDir, config.curDate)
11241026
// set up the sub-tasks
11251027
return parentTask.newListr(subTasks, {
11261028
concurrent: false,
@@ -1584,7 +1486,7 @@ export class NodeCommand extends BaseCommand {
15841486
title: 'Generate Gossip key',
15851487
task: async (ctx, parentTask) => {
15861488
const config = /** @type {NodeAddConfigClass} **/ ctx.config
1587-
const subTasks = self.taskGenerateGossipKeys(config.keyFormat, [config.nodeId], config.keysDir, config.curDate, config.allNodeIds)
1489+
const subTasks = self.keyManager.taskGenerateGossipKeys(self.keytoolDepManager, config.keyFormat, [config.nodeId], config.keysDir, config.curDate, config.allNodeIds)
15881490
// set up the sub-tasks
15891491
return parentTask.newListr(subTasks, {
15901492
concurrent: false,

src/core/key_manager.mjs

Lines changed: 104 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,17 @@ import * as x509 from '@peculiar/x509'
1818
import crypto from 'crypto'
1919
import fs from 'fs'
2020
import path from 'path'
21-
import { FullstackTestingError, MissingArgumentError } from './errors.mjs'
21+
import {
22+
FullstackTestingError,
23+
IllegalArgumentError,
24+
MissingArgumentError
25+
} from './errors.mjs'
2226
import { getTmpDir } from './helpers.mjs'
2327
import { constants, Keytool } from './index.mjs'
2428
import { Logger } from './logging.mjs'
2529
import { Templates } from './templates.mjs'
30+
import * as helpers from './helpers.mjs'
31+
import chalk from 'chalk'
2632

2733
x509.cryptoProvider.set(crypto)
2834

@@ -704,4 +710,101 @@ export class KeyManager {
704710
}
705711
}
706712
}
713+
714+
/**
715+
* Return a list of subtasks to generate gossip keys
716+
*
717+
* WARNING: These tasks MUST run in sequence.
718+
*
719+
* @param keytoolDepManager an instance of core/KeytoolDepManager
720+
* @param keyFormat key format (pem | pfx)
721+
* @param nodeIds node ids
722+
* @param keysDir keys directory
723+
* @param curDate current date
724+
* @param allNodeIds includes the nodeIds to get new keys as well as existing nodeIds that will be included in the public.pfx file
725+
* @return a list of subtasks
726+
* @private
727+
*/
728+
taskGenerateGossipKeys (keytoolDepManager, keyFormat, nodeIds, keysDir, curDate = new Date(), allNodeIds = null) {
729+
allNodeIds = allNodeIds || nodeIds
730+
if (!Array.isArray(nodeIds) || !nodeIds.every((nodeId) => typeof nodeId === 'string')) {
731+
throw new IllegalArgumentError('nodeIds must be an array of strings')
732+
}
733+
const self = this
734+
const subTasks = []
735+
736+
switch (keyFormat) {
737+
case constants.KEY_FORMAT_PFX: {
738+
const tmpDir = getTmpDir()
739+
const keytool = keytoolDepManager.getKeytool()
740+
741+
subTasks.push({
742+
title: `Check keytool exists (Version: ${keytoolDepManager.getKeytoolVersion()})`,
743+
task: async () => keytoolDepManager.checkVersion(true)
744+
745+
})
746+
747+
subTasks.push({
748+
title: 'Backup old files',
749+
task: () => helpers.backupOldPfxKeys(nodeIds, keysDir, curDate)
750+
})
751+
752+
for (const nodeId of nodeIds) {
753+
subTasks.push({
754+
title: `Generate ${Templates.renderGossipPfxPrivateKeyFile(nodeId)} for node: ${chalk.yellow(nodeId)}`,
755+
task: async () => {
756+
await self.generatePrivatePfxKeys(keytool, nodeId, keysDir, tmpDir)
757+
}
758+
})
759+
}
760+
761+
subTasks.push({
762+
title: `Generate ${constants.PUBLIC_PFX} file`,
763+
task: async () => {
764+
await self.updatePublicPfxKey(keytool, allNodeIds, keysDir, tmpDir)
765+
}
766+
})
767+
768+
subTasks.push({
769+
title: 'Clean up temp files',
770+
task: async () => {
771+
if (fs.existsSync(tmpDir)) {
772+
fs.rmSync(tmpDir, { recursive: true })
773+
}
774+
}
775+
})
776+
break
777+
}
778+
779+
case constants.KEY_FORMAT_PEM: {
780+
subTasks.push({
781+
title: 'Backup old files',
782+
task: () => helpers.backupOldPemKeys(nodeIds, keysDir, curDate)
783+
}
784+
)
785+
786+
for (const nodeId of nodeIds) {
787+
subTasks.push({
788+
title: `Gossip ${keyFormat} key for node: ${chalk.yellow(nodeId)}`,
789+
task: async () => {
790+
const signingKey = await self.generateSigningKey(nodeId)
791+
const signingKeyFiles = await self.storeSigningKey(nodeId, signingKey, keysDir)
792+
this.logger.debug(`generated Gossip signing keys for node ${nodeId}`, { keyFiles: signingKeyFiles })
793+
794+
const agreementKey = await self.generateAgreementKey(nodeId, signingKey)
795+
const agreementKeyFiles = await self.storeAgreementKey(nodeId, agreementKey, keysDir)
796+
this.logger.debug(`generated Gossip agreement keys for node ${nodeId}`, { keyFiles: agreementKeyFiles })
797+
}
798+
})
799+
}
800+
801+
break
802+
}
803+
804+
default:
805+
throw new FullstackTestingError(`unsupported key-format: ${keyFormat}`)
806+
}
807+
808+
return subTasks
809+
}
707810
}

0 commit comments

Comments
 (0)