Skip to content
This repository has been archived by the owner on Feb 3, 2021. It is now read-only.

Commit

Permalink
feat: add CLI to flash certificates
Browse files Browse the repository at this point in the history
  • Loading branch information
coderbyheart committed Aug 28, 2019
1 parent 76fa039 commit bfca423
Show file tree
Hide file tree
Showing 5 changed files with 445 additions and 39 deletions.
1 change: 1 addition & 0 deletions @types/modemtalk.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare module '@bifravst/modemtalk'
2 changes: 2 additions & 0 deletions cli/bifravst.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { reactConfigCommand } from './commands/react-config'
import { infoCommand } from './commands/info'
import { registerCaCommand } from './commands/register-ca'
import { historicalDataCommand } from './commands/historical-data'
import { flashCertificate } from './commands/flash-cert'

const stackId = process.env.STACK_ID || 'bifravst'
const region = process.env.AWS_DEFAULT_REGION || ''
Expand Down Expand Up @@ -58,6 +59,7 @@ const bifravstCLI = async () => {

const commands = [
registerCaCommand({ stackId, certsDir, region }),
flashCertificate({ certsDir }),
generateCertCommand({ endpoint }),
connectCommand({ endpoint, deviceUiUrl, certsDir }),
reactConfigCommand({ stackId, region }),
Expand Down
84 changes: 84 additions & 0 deletions cli/commands/flash-cert.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { ComandDefinition } from './CommandDefinition'
import { ModemPort } from '@bifravst/modemtalk'
import chalk from 'chalk'
import { deviceFileLocations } from '../jitp/deviceFileLocations'
import { promises as fs } from 'fs'
import * as path from 'path'

export const flashCertificate = ({
certsDir,
}: {
certsDir: string
}): ComandDefinition => ({
command: 'flash <deviceId>',
options: [
{
flags: '-p, --port <port>',
description: 'Serial port, defaults to /dev/ttyACM0',
},
{
flags: '-t, --sectag <secTag>',
description: 'sec tag, defaults to 42',
},
],
action: async (
deviceId: string,
{ port, secTag }: { port?: string; secTag?: string },
) => {
const deviceFiles = deviceFileLocations({ certsDir, deviceId })
const PORT = port || '/dev/ttyACM0'
const SEC_TAG = parseInt(secTag || '', 10) || 42
const device = new ModemPort(PORT, {
writeCallback: (data: string) => {
console.log(chalk.magenta(data.trim()))
},
})

device.on('event', (...args: any) => {
console.log('even', JSON.stringify(args))
})
device.on('error', (err: Error) => {
console.error(chalk.red(`Serial port error: ${err.message}`))
})
device.on('disconnect', () => {
console.log(chalk.magenta('Serial port has been disconnected'))
})
device.on('rx', (data: string) => {
console.debug(chalk.grey.bold('device <<'), chalk.grey(data.trim()))
})

await device.open()

const [caCert, clientCert, privateKey] = await Promise.all([
fs.readFile(
path.join(process.cwd(), 'data', 'AmazonRootCA1.pem'),
'utf-8',
),
fs.readFile(deviceFiles.certWithCA, 'utf-8'),
fs.readFile(deviceFiles.key, 'utf-8'),
])

console.log(chalk.yellow('Port:'), chalk.cyan(PORT))
console.log(chalk.yellow('SecTag:'), chalk.cyan(`${SEC_TAG}`))

await device.writeAT('+CGSN', {
timeout: 2000,
})
// FIXME: How to read the response?

console.log(chalk.cyan('Turning off modem'))
await device.writeAT('+CFUN=4', {
timeout: 2000,
})

console.log(chalk.cyan('Writing credentials'))
await device.writeTLSCredential(SEC_TAG, 0, caCert) // CA certificate
await device.writeTLSCredential(SEC_TAG, 1, clientCert) // client certificate
await device.writeTLSCredential(SEC_TAG, 2, privateKey) // private key

await device.close()

console.log(chalk.yellow('Done. Restart the device now.'))
},
help: 'Flash the certificate onto the device',
})
Loading

0 comments on commit bfca423

Please sign in to comment.