2.0.3 • Published 2 years ago

@softchef/cdk-iot-security v2.0.3

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
2 years ago

CDK Construct - IoT Security

npm version Release npm

cdk-iot-security is a project surrounding the topic of the AWS IoT Device registration. In order to utilize the AWS IoT services securely, we might need to maintain our device certificates and the relative resources. AWS IoT holds an organized architecture allowing us to register multiple certificates to it, and provides APIs for fetching information supporting our management. This project focuses on the autherization work flows of AWS IoT, provides CDK constructs which satisfy the requirements of Just-in-time registration, Just-in-time provisioning, and Fleet-provisioning. Utilizing cdk-iot-security, you do not need to implement the resource provisioning flow by yourself, and can focus on other functionalities of your IoT products.

Installation

NPM

npm install @softchef/cdk-iot-security

Yarn

yarn add @softchef/cdk-iot-security

Examples

Just-in-Time Registration

JITR work flow is usually applied in a situation that the devices are able to generate their own certificates. The scenario would probably like the following: the service provider deploy the JITR construct; the service provider create registered CA; the service provider make a copy of the CA certificate on a device and provide the device for the user client; an user client turn on the device and the device generate its certificate; the device connect to the AWS IoT through MQTT connection. The AWS IoT JITR service will be triggered and the JITR MQTT message will be passed through the topic rule, the SQS queue, eventually reach the Device Activator. Finally, the Device Activator verifies and activates the device certificate, and provision the AWS IoT resources for the device certificate.

Structure

npm.io

Endogenous Components

Device Activator

The NodeJS Lambda Function with the functionality of activating a device certificate requesting for JITR.

CA Registrator

The NodeJS Lambda Function with the functionality of registering a CA certificate on AWS IoT.

Verifiers Fetcher

The NodeJS Lambda Function with the functionality of returning the names of the verfifiers.

JITR Topic Rule

The AWS IoT Topic Rule with the functionality of collecting the MQTT message originating from the JITR request.

Review Receptor

The SQS queue with the functionality of recepting the MQTT message collected by the JITP Topic Rule and pass to the Device Activation for further review.

Exogenous Components

Vault

The S3 Bucket provided by the user for storing the created CA certificate secerts, including certificate, private key, and public key, also the CA certificate ID and ARN.

Verifiers

The Lambda Function provided by the user for device verification. If must return a payload with the following format:

{
    ...
    "verified": "true", // or false
}

If it returns {"verified": "true"}, the Device Activator would complete the provision. Otherwise, the process is interrupted.

API

You can integrate your own API to the CA registrator and Verifiers Fetcher for utilization.

Flow

npm.io

Usage

Overview

The process of applying JITR is mainly consist of the following steps:

  1. Initialize the JITR construct.

  2. Create CA through calling the CA Registrator.

  3. Create the device certificate with the device.

  4. Connect the device to the AWS IoT.

Some details informations of those three steps are discussed in the following sections. For step-by-step guide, please read the JITR demonstration files.

Initialize the JITR Construct

import { JustInTimeRegistration } from '@softchef/cdk-iot-security';
import * as cdk from '@aws-cdk/core';
import * as s3 from '@aws-cdk/aws-s3';
import * as lambda from '@aws-cdk/aws-lambda';

const app = new cdk.App();
const id = 'JitrDemo';
const stack = new cdk.Stack(app, id);
const anotherStack = new cdk.Stack(app, 'anotherStack');
new JustInTimeRegistration(stack, id, {
    vault: {
        bucket: new s3.Bucket(anotherStack, 'myVault2'),
        prefix: 'my/ca/path',
    },
    verifiers: [
        new lambda.Function(anotherStack, 'verifier1', {
            code: lambda.Code.fromInline('exports.handler = async (_event) => { return JSON.stringify({ verified: true }); }'),
            handler: 'handler',
            runtime: lambda.Runtime.NODEJS_12_X,
        }),
        new lambda.Function(anotherStack, 'verifier2', {
            code: lambda.Code.fromInline('exports.handler = async (event) => { return JSON.stringify({ verified: event? true : false }); }'),
            handler: 'handler',
            runtime: lambda.Runtime.NODEJS_12_X,
        })
    ]
});

Call the CA Registrator

You call the CA Registrator to registrate a new CA on the AWS IoT before generating a device certificate.

CA Registrator assumes receiving an event object with the following format:

event = {
    ...
    "body": {
        "csrSubjects": {
            "commonName": "myName",
            "countryName": "TW",
            "stateName": "TP",
            "localityName": "TW",
            "organizationName": "Soft Chef",
            "organizationUnitName": "web"
        },
        "verifierName": "verifier_name",
    }
}

Since the event is mainly a HTTP POST request, it has a body section containing attached information. The body consist of three parts, CSR subjects, verifier name, and template body.

  • CSR subjects define the information to fill up the subject fields of the CA certificate. CSR subjects are optional. If some of the fields are leaved blank, those fields will be fill up with empty string. Mandated by the AWS, the common name field would be replaced by the registration code, thus is unnecessary.

  • Verifier name specifies the verifier applied in the device verification. Verifier name is Optional.

Call the Verifiers Fetcher

You can checkout the names of the verifiers through the Verifiers Fetcher when you forget the names.

Verifiers Fetcher assumes receiving an event object with the following format:

event = {
    ...
    body: {}
}

Since the event is mainly a HTTP GET request, no body content is expected. However, no matter what the request content is, the Verifiers Fetcher always return all the verifiers' name.

Connect the Device to the AWS IoT

To trigger the JITR and the provisioning of resources, the deivce has to send a MQTT message to the AWS IoT. You need to have the basic knowledge about the MQTT. You can complete this step with either a pure MQTT connection, or aws-iot-deivce-sdk. For the former, please read mqtt-connect.js, for the later, please read device.js

Just-in-Time Provision

JITP work flow is usually applied in a situation that the devices are not able to generate their own certificates. The scenario would probably like the following: the service provider deploies the JITP construct and provides the API for the user client; the service provider creates registered CA; an user client get the generated device certificate through the API; the user client pass the device certificate to the device; the device connect to the AWS IoT through MQTT connection. Finally, the AWS IoT JITP service will be triggered and provision the expected resources.

Structure

npm.io

Endogenous Components

CA Registrator

The NodeJS Lambda Function with the functionality of registering a CA certificate on AWS IoT.

Verifiers Fetcher

The NodeJS Lambda Function with the functionality of returning the names of the verfifiers.

Device Certificate Generator

The NodeJS Lambda Function with the functionality of generating the device certificate.

Exogenous Components

Vault

The S3 Bucket provided by the user for storing the created CA certificate secerts, including certificate, private key, and public key, also the CA certificate ID and ARN.

Verifiers

The Lambda Function provided by the user for device verification. If must return a payload with the following format:

{
    ...
    "verified": "true", // or false
}

If it returns {"verified": "true"}, the Device Certificate Generator would complete the device certificate generation. Otherwise, the process is interrupted.

API

You can integrate your own API to the CA registrator, Device Certificate Generator, and Verifiers Fetcher for further utilization.

Flow

npm.io

Usage

Overview

The process of applying JITP is mainly consist of the following steps:

  1. Initialize the JITP construct.

  2. Create CA through calling the CA Registrator.

  3. Create Device Certificate through calling the Device Certificate Generator.

  4. Connect the device to the AWS IoT.

Some details informations of those four steps are discussed in the following sections. For step-by-step guide, please read the JITP demonstration files.

Initialize the JITP Construct

import { JustInTimeProvision } from '@softchef/cdk-iot-security';
import { Bucket } from '@aws-cdk/aws-s3';
import * as cdk from '@aws-cdk/core';

const app = new cdk.App();
const id = 'JitpDemo';
const stack = new cdk.Stack(app, id);
const justInTimeProvision = new JustInTimeProvision(stack, id, {
    vault: {
        bucket: new Bucket(stack, 'myVault'),
    },
    verifiers: [
        new lambda.Function(anotherStack, 'verifier1', {
            code: lambda.Code.fromInline('exports.handler = async (_event) => { return JSON.stringify({ verified: true }); }'),
            handler: 'handler',
            runtime: lambda.Runtime.NODEJS_12_X,
        }),
        new lambda.Function(anotherStack, 'verifier2', {
            code: lambda.Code.fromInline('exports.handler = async (event) => { return JSON.stringify({ verified: event? true : false }); }'),
            handler: 'handler',
            runtime: lambda.Runtime.NODEJS_12_X,
        })
    ],
});

Call the CA Registrator

You call the CA Registrator to registrate a new CA on the AWS IoT before generating a device certificate.

CA Registrator assumes receiving an event object with the following format:

event = {
    ...
    "body": {
        "csrSubjects": {
            "commonName": "myName",
            "countryName": "TW",
            "stateName": "TP",
            "localityName": "TW",
            "organizationName": "Soft Chef",
            "organizationUnitName": "web"
        },
        "verifierName": "verifier_name",
        "templateBody": "{ \"Parameters\" : { \"AWS::IoT::Certificate::Country\" : { \"Type\" : \"String\" }, \"AWS::IoT::Certificate::Id\" : { \"Type\" : \"String\" } }, \"Resources\" : { \"thing\" : { \"Type\" : \"AWS::IoT::Thing\", \"Properties\" : { \"ThingName\" : {\"Ref\" : \"AWS::IoT::Certificate::Id\"}, \"AttributePayload\" : { \"version\" : \"v1\", \"country\" : {\"Ref\" : \"AWS::IoT::Certificate::Country\"}} } }, \"certificate\" : { \"Type\" : \"AWS::IoT::Certificate\", \"Properties\" : { \"CertificateId\": {\"Ref\" : \"AWS::IoT::Certificate::Id\"}, \"Status\" : \"ACTIVE\" } }, \"policy\" : {\"Type\" : \"AWS::IoT::Policy\", \"Properties\" : { \"PolicyDocument\" : \"{\\\"Version\\\": \\\"2012-10-17\\\",\\\"Statement\\\": [{\\\"Effect\\\":\\\"Allow\\\",\\\"Action\\\": [\\\"iot:Connect\\\",\\\"iot:Publish\\\"],\\\"Resource\\\" : [\\\"*\\\"]}]}\" } } } }"
        }
    }
}

Since the event is mainly a HTTP POST request, it has a body section containing attached information. The body consist of three parts, CSR subjects, verifier name, and template body.

  • CSR subjects define the information to fill up the subject fields of the CA certificate. CSR subjects are optional. If some of the fields are leaved blank, those fields will be fill up with empty string. Mandated by the AWS, the common name field would be replaced by the registration code, thus is unnecessary.

  • Verifier name specifies the verifier applied in the device verification. Verifier name is Optional.

  • Template body is a string defining the resources provisioned for the device. Template body is Optional. If no template body being specified, a default template body will be applied. See more information about defining a template body from here.

Call the Device Certificate Generator

You call the Device Certificate Generator to generate a device certificate after register a CA on AWS IoT.

Device Certificate Generator assumes receiving an event object with the following format:

event = {
    ...
    body: {
        "caCertificateId": "myCaCertificateId",
        "csrSubjects": {
            "commonName": "myThingName", // data in this field would be the thing name
            "countryName": "TW",
            "stateName": "TP",
            "localityName": "TW",
            "organizationName": "Soft Chef",
            "organizationUnitName": "web"
        },
        "deviceInfo": {
            ...
        },
    }        
}

Since the event is mainly a HTTP POST request, it has a body section containing attached information. The body consist of three parts, CA certificate ID, CSR subjects, and device information.

  • CA certificate ID is the ID of the CA created by the CA Registrator. CA certificate ID is required by the Device Certificate Generator. The CA will authenticate the device certificate.

  • CSR subjects define the information to fill up the subject fields of the device certificate. CSR subjects are optional. If some of the fields are leaved blank, those fields will be fill up with empty string. The string data in the common name field will be set as the name of the AWS IoT Thing. Thing name is the desirable name you give to the AWS IoT thing provisioned for the device certificate. If no thing name is specified, the thing name would be an UUID.

  • Device information is the information provided by the device for verification. Whether it is required or not and its form depends on your configuration of the verifiers.

Call the Verifiers Fetcher

You can checkout the names of the verifiers through the Verifiers Fetcher when you forget the names.

Verifiers Fetcher assumes receiving an event object with the following format:

event = {
    ...
    body: {}
}

Since the event is mainly a HTTP GET request, no body content is expected. However, no matter what the request content is, the Verifiers Fetcher always return all the verifiers' name.

Connect the Device to the AWS IoT

To trigger the JITP and the provisioning of resources, the deivce has to send a MQTT message to the AWS IoT. You need to have the basic knowledge about the MQTT. You can complete this step with either a pure MQTT connection, or aws-iot-deivce-sdk. For the former, please read this file, for the later, please read this file.

Fleet Provision

Structure

npm.io

Endogenous Components

Fleet Generator

The NodeJS Lambda Function with the functionality of registering a Fleet-Provisioning Template and a Provisioning Claim Certificate on AWS IoT.

Exogenous Components

Vault

The S3 Bucket provided by the user for storing the created Provisioning Claim Certificate secerts, including certificate, private key, and public key, also the Provisioning Claim Certificate ID and ARN.

API

You can integrate your own API to the Fleet Generator for further utilization.

Flow

npm.io

Usage

Overview

The process of applying Fleet-Provision is mainly consist of the following steps:

  1. Initialize the Fleet-Provision construct.

  2. Create the Fleet-Provisioning Template and Provisioning Claim Certificate through calling the Fleet Generator.

  3. Connect the device to the AWS IoT with Provisioning Claim Certificate and save the formal device certificates in the device.

Some details informations of those three steps are discussed in the following sections. For step-by-step guide, please read the Fleet-Provision demonstration files.

Initialize the Fleet-Provision Construct

import * as apigateway from '@aws-cdk/aws-apigateway';
import * as s3 from '@aws-cdk/aws-s3';
import * as cdk from '@aws-cdk/core';
import { FleetProvision } from '@softchef/cdk-iot-security';

const app = new cdk.App();
const id = 'FleetProvisionDemo';
const stack = new cdk.Stack(app, id);
const vault = new s3.Bucket(stack, 'myVault');
const fleetProvision = new FleetProvision(stack, id, {
    vault: {
        bucket: vault,
    },
    // Decomment the following line to apply the Greengrass V2 mode
    // greengrassV2: true,
});

Call the Fleet Generator

You call the Fleet Generator to generate a new Fleet-Provisioning Template and Provisioning Claim Certificate on the AWS IoT.

Fleet Generator assumes receiving an event object with the following format:

event = {
    ...
    "body": {
        "templateName": "myTemplateName",
    }
}

Since the event is mainly a HTTP POST request, it has a body section containing attached information. The body consist of the template name.

Connect the Device to the AWS IoT

To trigger the Fleet-Provision, the deivce has to send a MQTT message to the AWS IoT. You can complete this step with aws-iot-deivce-sdk-v2. Please read this file for detail.

1.0.60

2 years ago

1.0.39

2 years ago

1.0.38

2 years ago

1.0.40

2 years ago

1.0.44

2 years ago

1.0.43

2 years ago

1.0.42

2 years ago

1.0.41

2 years ago

1.0.48

2 years ago

1.0.47

2 years ago

1.0.46

2 years ago

1.0.45

2 years ago

1.0.49

2 years ago

1.0.51

2 years ago

1.0.50

2 years ago

1.0.55

2 years ago

1.0.54

2 years ago

1.0.53

2 years ago

1.0.52

2 years ago

1.0.59

2 years ago

1.0.58

2 years ago

1.0.57

2 years ago

1.0.56

2 years ago

1.0.22

2 years ago

1.0.21

2 years ago

1.0.20

2 years ago

1.0.26

2 years ago

1.0.25

2 years ago

1.0.24

2 years ago

1.0.23

2 years ago

1.0.29

2 years ago

1.0.28

2 years ago

1.0.27

2 years ago

1.0.33

2 years ago

1.0.32

2 years ago

1.0.31

2 years ago

1.0.30

2 years ago

1.0.37

2 years ago

1.0.36

2 years ago

1.0.35

2 years ago

1.0.34

2 years ago

1.0.19

2 years ago

1.0.18

2 years ago

1.0.17

2 years ago

1.0.16

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.11

2 years ago

1.0.10

2 years ago

1.0.15

2 years ago

1.0.14

2 years ago

1.0.13

2 years ago

1.0.12

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

2.0.3

2 years ago

2.0.2

2 years ago

2.0.1

2 years ago

2.0.0

2 years ago

0.0.72

2 years ago

0.0.70

2 years ago

0.0.71

2 years ago

0.0.68

2 years ago

0.0.69

2 years ago

0.0.67

3 years ago

0.0.62

3 years ago

0.0.63

3 years ago

0.0.64

3 years ago

0.0.65

3 years ago

0.0.66

3 years ago

0.0.60

3 years ago

0.0.61

3 years ago

0.0.59

3 years ago

0.0.56

3 years ago

0.0.57

3 years ago

0.0.58

3 years ago

0.0.51

3 years ago

0.0.52

3 years ago

0.0.53

3 years ago

0.0.54

3 years ago

0.0.55

3 years ago

0.0.50

3 years ago

0.0.49

3 years ago

0.0.48

3 years ago

0.0.46

3 years ago

0.0.47

3 years ago

0.0.44

3 years ago

0.0.45

3 years ago

0.0.43

3 years ago

0.0.42

3 years ago

0.0.41

3 years ago

0.0.40

3 years ago

0.0.38

3 years ago

0.0.39

3 years ago

0.0.37

3 years ago

0.0.36

3 years ago

0.0.32

3 years ago

0.0.33

3 years ago

0.0.34

3 years ago

0.0.35

3 years ago

0.0.30

3 years ago

0.0.31

3 years ago

0.0.29

3 years ago

0.0.28

3 years ago

0.0.26

3 years ago

0.0.27

3 years ago

0.0.25

3 years ago

0.0.24

3 years ago

0.0.23

3 years ago

0.0.21

3 years ago

0.0.22

3 years ago

0.0.20

3 years ago

0.0.17

3 years ago

0.0.18

3 years ago

0.0.19

3 years ago

0.0.16

3 years ago

0.0.15

3 years ago

0.0.10

3 years ago

0.0.11

3 years ago

0.0.12

3 years ago

0.0.13

3 years ago

0.0.14

3 years ago

0.0.9

3 years ago

0.0.8

3 years ago

0.0.7

3 years ago

0.0.6

3 years ago

0.0.3

3 years ago

0.0.5

3 years ago

0.0.4

3 years ago

0.0.2

3 years ago