AWS IOT JITR with Thing and Policy creation using JAVA.
This POC will provide Just In Time Registration (JITR) of custom certificate and Thing creation with connect policy for AWS IOT Devices. You just need to add name of thing in common name while creation of device certificate and thing will be created with attached policy & certificate and common name as thing name.Project Overview:
- Get certificate details from certificate id.
- Parse certificate details and get common name from certificate.
- Creates IOT policy having action of connect.
- Creates IOT thing with name from certificate common name.
- Attach policy and thing to certificate.
- Activate Certificate.
- Now your device can connect to AWS using this custom certificate.
Step for JITR & Thing creation
Create CA Certificate:
- openssl genrsa -out CACertificate.key 2048
- openssl req -x509 -new -nodes -key CACertificate.key -sha256 -days 365 -out CACertificate.pem
- Enter necessary details like city, country, etc.
Create private key verification certificate using CA certificate
- aws iot get-registration-code
- This registration code will be used in following step
- openssl genrsa -out privateKeyVerification.key 2048
- openssl req -new -key privateKeyVerification.key -out privateKeyVerification.csr
- Enter necessary details. Note: In
common name
, Enterregistration code
which copied before.
- openssl x509 -req -in privateKeyVerification.csr -CA CACertificate.pem -CAkey CACertificate.key -CAcreateserial -out privateKeyVerification.crt -days 365 -sha256
- After this step, CA certificate will create. Now We need to register it to AWS.
Register & Activate Certificate to AWS
- aws iot register-ca-certificate --ca-certificate file://CACertificate.pem --verification-certificate file://privateKeyVerification.crt
- This will output like: { "certificateArn": "< certificateArn >", "certificateId": "< certificateId >" }
- certId=< output certificateId >
- Assign certificateId to certId variable.
- aws iot describe-ca-certificate --certificate-id $certId
- It will give output with certificate details.
- aws iot update-ca-certificate --certificate-id $certId --new-status ACTIVE
- It will activate CA certificate. You can do it from AWS console also.
- aws iot update-ca-certificate --certificate-id $certId --new-auto-registration-status ENABLE
- It will enable auto registration of certificate.
Device Certificate Registration
- To verify certificate registration request subscribe to the following topic from AWS IOT core Test.
- $aws/events/certificates/registered/#
- you can skip this step.
- openssl genrsa -out device.key 2048
- openssl req -new -key device.key -out device.csr
- openssl x509 -req -in device.csr -CA CACertificate.pem -CAkey CACertificate.key -CAcreateserial -out device.crt -days 365 -sha256
- cat device.crt CACertificate.pem > deviceAndCACert.crt
Create AWS lamda function and IAM role.
- create AWS lambda function with JAVA 8 and with IAM role which having IOT Full Access policy. Create JAR from this project using
mvn clean install
and upload JAR to Lambda. - When new device will send request with new certificate which signed with registered CA certificate then it will send MQTT message to $aws/events/certificates/registered/# topic.
- We need to create IOT rule from Act section to trigger lambda function when new certification registered message will trigger.
- Add rule query statement
SELECT * FROM '$aws/events/certificates/registered/#'
and action asSend message to Lambda function
by selecting created Lambda function.
- Add rule query statement
Test
- mosquitto_pub --cafile root.cert --cert deviceAndCACert.crt --key device.key -h
-p 8883 -q 1 -t foo/bar -i anyclientID --tls-version tlsv1.2 -m "RegisterCertificateAndCreateThingAndPolicy" -d - Note: you need mosquito client to use above command. You can get endpoint by: aws iot describe-endpoint. you can get root.cert from here.
- Now you can verify that new thing is created. You will also see that this thing and policy attached to new certificate. Certificate will be marked as active.
- I added policy with just only for connect action. You can change policy as per your requirement like publish, subscribe, etc.
Excellent stuff, this is really helpful for beginners and I am glad to visit this page.
ReplyDeleteAWS Training in Chennai
AWS course in Chennai
VMware Training in Chennai
Vmware Training center in Chennai
Cloud Computing Courses in Chennai
Cloud Computing Training in Velachery
DevOps Certification in Chennai
AWS Training in Chennai
AWS course in Chennai