Skip to content

Commit

Permalink
feat: add script to migrate device certificates
Browse files Browse the repository at this point in the history
  • Loading branch information
coderbyheart committed Jan 6, 2025
1 parent 59740d9 commit cb22cdc
Showing 1 changed file with 88 additions and 0 deletions.
88 changes: 88 additions & 0 deletions migrate-device-certificates.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import {
AttachThingPrincipalCommand,
CreateThingCommand,
IoTClient,
ListThingsCommand,
RegisterCertificateCommand,
} from '@aws-sdk/client-iot'
import chalk from 'chalk'
import { readFile } from 'node:fs/promises'
import path from 'node:path'
import { dirname } from 'path'
import { fileURLToPath } from 'url'

const FROM_REGION = 'eu-central-1'
const fromIot = new IoTClient({ region: FROM_REGION })
const toIot = new IoTClient({ region: 'us-west-2' })

const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)

const listDevices = async (iot: IoTClient) =>
new Map(
(
(
await iot.send(
new ListThingsCommand({
maxResults: 250,
}),
)
).things ?? []
)
.filter(
(device) =>
device.thingTypeName !== 'mesh-node' &&
device.thingTypeName !== 'wirepas-5g-mesh-gateway' &&
device.thingTypeName !== 'nrplus-gateway',
)
.map((device) => [device.thingName, device]),
)

const fromDevices = await listDevices(fromIot)
const toDevices = await listDevices(toIot)

const devicesToMigrate = new Set(
fromDevices.values().map((device) => device.thingName),
).difference(new Set(toDevices.values().map((device) => device.thingName)))

for (const device of devicesToMigrate) {
try {
const { clientCert } = JSON.parse(
await readFile(
path.join(
__dirname,
'certificates',
FROM_REGION,
`device-${device}.json`,
),
'utf-8',
),
)

const registeredCert = await toIot.send(
new RegisterCertificateCommand({
certificatePem: clientCert,
status: 'ACTIVE',
}),
)

await toIot.send(
new CreateThingCommand({
thingName: device,
attributePayload: fromDevices.get(device)!.attributes,
}),
)

await toIot.send(
new AttachThingPrincipalCommand({
thingName: device,
principal: registeredCert.certificateArn!,
}),
)

console.log(chalk.green(`Successfully migrated ${device}!`))
} catch (error) {
console.error(chalk.red(`Failed to migrate ${device}!`))
console.error(chalk.red((error as Error).message))
}
}

0 comments on commit cb22cdc

Please sign in to comment.