-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcreate-provision-certificate.ts
66 lines (56 loc) · 1.9 KB
/
create-provision-certificate.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import { CloudFormationClient } from '@aws-sdk/client-cloudformation'
import {
AttachPolicyCommand,
CreateKeysAndCertificateCommand,
IoTClient,
} from '@aws-sdk/client-iot'
import { stackOutput } from '@nordicsemiconductor/cloudformation-helpers'
import chalk from 'chalk'
import { mkdir, stat, writeFile } from 'node:fs/promises'
import path from 'node:path'
import { StackOutputs } from './cdk/FleetProvisioningStack'
const { magenta, yellow, blue, gray, green } = chalk
const cf = new CloudFormationClient({})
const iot = new IoTClient({})
const { unprovisionedCertificatePolicyName } = await stackOutput(cf)<
typeof StackOutputs
>('fleet-provisioning')
console.debug(
magenta('Policy name:'),
yellow(unprovisionedCertificatePolicyName),
)
const certsDir = path.join(process.cwd(), 'certificates')
try {
await stat(certsDir)
console.debug(blue.dim(certsDir), gray('exists'))
} catch {
await mkdir(certsDir)
console.debug(green.dim(certsDir), gray('created'))
}
const cert = await iot.send(
new CreateKeysAndCertificateCommand({
setAsActive: true,
}),
)
const keyFile = path.join(certsDir, `${cert.certificateId}.pem.key`)
const certFile = path.join(certsDir, `${cert.certificateId}.pem.crt`)
await writeFile(keyFile, cert.keyPair?.PrivateKey ?? '', 'utf-8')
console.debug(blue.dim(keyFile), gray('written'))
await writeFile(certFile, cert.certificatePem ?? '', 'utf-8')
console.debug(blue.dim(certFile), gray('written'))
await iot.send(
new AttachPolicyCommand({
policyName: unprovisionedCertificatePolicyName,
target: cert.certificateArn,
}),
)
console.debug(
gray(`Attached policy`),
blue.dim(unprovisionedCertificatePolicyName),
gray(`to certificate`),
blue.dim(cert.certificateArn),
)
console.debug(magenta(`Credential ID`), yellow(cert.certificateId))
console.log('')
console.log(green(`You can now provision a new device using`))
console.log(blue(`npx tsx provision.ts`), yellow.dim(cert.certificateId))