Skip to content

Commit b016f3d

Browse files
committed
refactor(simulator): generate certificate locations
1 parent a24844e commit b016f3d

File tree

3 files changed

+68
-40
lines changed

3 files changed

+68
-40
lines changed

cli/certificates.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { mkdirSync, statSync } from 'node:fs'
2+
import path from 'node:path'
3+
4+
/**
5+
* Provide the directory to store certificates
6+
*/
7+
export const ensureCertificateDir = (): string => {
8+
const dirName = path.join(process.cwd(), 'certificates')
9+
try {
10+
statSync(dirName)
11+
} catch {
12+
mkdirSync(dirName)
13+
}
14+
return dirName
15+
}
16+
17+
export const simulatorCALocations = (
18+
certificatesDir: string,
19+
): {
20+
privateKey: string
21+
certificate: string
22+
} => ({
23+
privateKey: path.join(certificatesDir, 'simulator.CA.key'),
24+
certificate: path.join(certificatesDir, 'simulator.CA.pem'),
25+
})
26+
27+
export const deviceCertificateLocations = (
28+
certificatesDir: string,
29+
deviceId: string,
30+
): {
31+
privateKey: string
32+
certificate: string
33+
CSR: string
34+
signedCert: string
35+
} => ({
36+
privateKey: path.join(certificatesDir, `${deviceId}.key`),
37+
certificate: path.join(certificatesDir, `${deviceId}.pem`),
38+
CSR: path.join(certificatesDir, `${deviceId}.csr`),
39+
signedCert: path.join(certificatesDir, `${deviceId}.signed.pem`),
40+
})

cli/commands/register-simulator-device.ts

Lines changed: 18 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ import { apiClient } from '../../nrfcloud/apiClient.js'
88
import { getAPISettings } from '../../nrfcloud/settings.js'
99
import { run } from '../../util/run.js'
1010
import { ulid } from '../../util/ulid.js'
11+
import {
12+
deviceCertificateLocations,
13+
ensureCertificateDir,
14+
simulatorCALocations,
15+
} from '../certificates.js'
1116
import type { CommandDefinition } from './CommandDefinition.js'
1217

1318
export const registerSimulatorDeviceCommand = ({
@@ -33,17 +38,13 @@ export const registerSimulatorDeviceCommand = ({
3338
apiKey,
3439
})
3540

41+
const dir = ensureCertificateDir()
42+
3643
// CA certificate
37-
const caPrivateKeyLocation = path.join(
38-
process.cwd(),
39-
'certificates',
40-
'simulator.CA.key',
41-
)
42-
const caCertificateLocation = path.join(
43-
process.cwd(),
44-
'certificates',
45-
'simulator.CA.pem',
46-
)
44+
const {
45+
privateKey: caPrivateKeyLocation,
46+
certificate: caCertificateLocation,
47+
} = simulatorCALocations(dir)
4748
try {
4849
await stat(caCertificateLocation)
4950
} catch {
@@ -80,11 +81,13 @@ export const registerSimulatorDeviceCommand = ({
8081
console.log(chalk.yellow('Device ID:'), chalk.blue(deviceId))
8182

8283
// Device private key
83-
const devicePrivateKeyLocation = path.join(
84-
process.cwd(),
85-
'certificates',
86-
`${deviceId}.key`,
87-
)
84+
const {
85+
privateKey: devicePrivateKeyLocation,
86+
certificate: deviceCertificateLocation,
87+
CSR: deviceCSRLocation,
88+
signedCert: deviceSignedCertLocation,
89+
} = deviceCertificateLocations(dir, deviceId)
90+
8891
await run({
8992
command: 'openssl',
9093
args: [
@@ -102,11 +105,6 @@ export const registerSimulatorDeviceCommand = ({
102105
)
103106

104107
// Device certificate
105-
const deviceCertificateLocation = path.join(
106-
process.cwd(),
107-
'certificates',
108-
`${deviceId}.pem`,
109-
)
110108
await run({
111109
command: 'openssl',
112110
args: [
@@ -130,11 +128,6 @@ export const registerSimulatorDeviceCommand = ({
130128
)
131129

132130
// Sign device cert
133-
const deviceCSRLocation = path.join(
134-
process.cwd(),
135-
'certificates',
136-
`${deviceId}.csr`,
137-
)
138131
await run({
139132
command: 'openssl',
140133
args: [
@@ -148,11 +141,6 @@ export const registerSimulatorDeviceCommand = ({
148141
`/CN=${deviceId}`,
149142
],
150143
})
151-
const deviceSignedCertLocation = path.join(
152-
process.cwd(),
153-
'certificates',
154-
`${deviceId}.signed.pem`,
155-
)
156144
await run({
157145
command: 'openssl',
158146
args: [

cli/commands/simulate-device.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ import { getDeviceFingerprint } from '../../devices/getDeviceFingerprint.js'
1111
import { apiClient } from '../../nrfcloud/apiClient.js'
1212
import { getAPISettings } from '../../nrfcloud/settings.js'
1313
import { version } from '../../package.json'
14+
import {
15+
deviceCertificateLocations,
16+
ensureCertificateDir,
17+
} from '../certificates.js'
1418
import type { CommandDefinition } from './CommandDefinition.js'
1519

1620
export const simulateDeviceCommand = ({
@@ -59,22 +63,18 @@ export const simulateDeviceCommand = ({
5963
chalk.blue(accountInfo.account.mqttEndpoint),
6064
)
6165

66+
const dir = ensureCertificateDir()
67+
const {
68+
privateKey: devicePrivateKeyLocation,
69+
signedCert: deviceCertificateLocation,
70+
} = deviceCertificateLocations(dir, deviceId)
71+
6272
// Device private key
63-
const devicePrivateKeyLocation = path.join(
64-
process.cwd(),
65-
'certificates',
66-
`${deviceId}.key`,
67-
)
6873
console.log(
6974
chalk.yellow('Private key:'),
7075
chalk.blue(devicePrivateKeyLocation),
7176
)
7277
// Device certificate
73-
const deviceCertificateLocation = path.join(
74-
process.cwd(),
75-
'certificates',
76-
`${deviceId}.signed.pem`,
77-
)
7878
console.log(
7979
chalk.yellow('Signed certificate:'),
8080
chalk.blue(deviceCertificateLocation),

0 commit comments

Comments
 (0)