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

Commit

Permalink
refactor: moved files
Browse files Browse the repository at this point in the history
  • Loading branch information
coderbyheart committed Jul 26, 2019
1 parent 017c6e2 commit fd9c9e9
Show file tree
Hide file tree
Showing 15 changed files with 44 additions and 8 deletions.
43 changes: 40 additions & 3 deletions cli/bifravst.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import * as program from 'commander'
import { connect } from './connect'
import chalk from 'chalk'
import { Iot } from 'aws-sdk'
import { stackOutput } from '../scripts/cloudformation/stackOutput'
import { stackOutput } from './cloudformation/stackOutput'
import { StackOutputs } from '../cdk/stacks/Bifravst'
import * as path from 'path'
import { stackOutputToCRAEnvironment } from '../scripts/cloudformation/stackOutputToCRAEnvironment'
import { registerCA } from '../scripts/jitp/registerCA'
import { stackOutputToCRAEnvironment } from './cloudformation/stackOutputToCRAEnvironment'
import { registerCA } from './jitp/registerCA'
import { generateDeviceCertificate } from './jitp/generateDeviceCertificate'
import { randomWords } from '@bifravst/random-words'

const stackId = process.env.STACK_ID || 'bifravst'
const region = process.env.AWS_DEFAULT_REGION
Expand Down Expand Up @@ -116,6 +118,41 @@ const bifravstCLI = async () => {
console.log(chalk.yellow('Registers a CA for Just-in-time provisioning.'))
console.log('')
})

program
.command('generate-cert')
.option(
'-d, --deviceId <deviceId>',
'Device ID, if left blank a random ID will be generated',
)
.action(async ({ deviceId }) => {
ran = true
const id = deviceId || (await randomWords({ numWords: 3 })).join('-')
await generateDeviceCertificate({
deviceId: id,
certsDir: path.resolve(process.cwd(), 'certificates'),
log: (...message: any[]) => {
console.log(...message.map(m => chalk.magenta(m)))
},
debug: (...message: any[]) => {
console.log(...message.map(m => chalk.cyan(m)))
},
})
console.log(
chalk.green(`Certificate for device ${chalk.yellow(id)} generated.`),
)
console.log(chalk.green('You can now connect to the broker.'))
})
.on('--help', () => {
console.log('')
console.log(
chalk.yellow(
'Generate a certificate for a device, signed with the CA.',
),
)
console.log('')
})

program.parse(process.argv)

if (!ran) {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions cli/connect.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { promises as fs } from 'fs'
import { thingShadow } from 'aws-iot-device-sdk'
import { deviceFileLocations } from '../scripts/jitp/deviceFileLocations'
import { deviceFileLocations } from './jitp/deviceFileLocations'
import chalk from 'chalk'
import { uiServer } from '../scripts/device/ui-server'
import { uiServer } from './device/ui-server'

/**
* Connect to the AWS IoT broker using a generated device certificate
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as os from 'os'
import { promises as fs } from 'fs'
import { randomWords } from '@bifravst/random-words'
import { caFileLocations } from './caFileLocations'
import { deviceFileLocations } from './deviceFileLocations'
import { run } from '../process/run'
Expand All @@ -11,18 +10,18 @@ import { run } from '../process/run'
*/
export const generateDeviceCertificate = async (args: {
certsDir: string
deviceId: string
log: (...message: any[]) => void
debug: (...message: any[]) => void
}): Promise<{ deviceId: string }> => {
const { certsDir, log, debug } = args
const { certsDir, log, debug, deviceId } = args

try {
await fs.stat(certsDir)
} catch {
throw new Error(`${certsDir} does not exist.`)
}

const deviceId = (await randomWords()).join('-')
log(`Generating certificate for device ${deviceId}`)
const caFiles = caFileLocations(certsDir)
const deviceFiles = deviceFileLocations(certsDir, deviceId)
Expand Down
File renamed without changes.
File renamed without changes.

0 comments on commit fd9c9e9

Please sign in to comment.