fbpx

Using Amazon Verified Permissions to manage authorization for AWS IoT smart home applications

This blog post introduces how manufacturers and smart appliance consumers can use Amazon Verified Permissions to centrally manage permissions and fine-grained authorizations. Developers can offer more intuitive, user-friendly experiences by designing interfaces that align with user personas and multi-tenancy authorization strategies, which can lead to higher user satisfaction and adoption. Traditionally, implementing authorization logic using role based access control (RBAC) or attribute based access control (ABAC) within IoT applications can become complex as the number of connected devices and associated user roles grows. This often leads to an unmanageable increase in access rules that must be hard-coded into each application, requiring excessive compute power for evaluation. By using Verified Permissions, you can externalize the authorization logic using Cedar policy language, enabling you to define fine-grained permissions that combine RBAC and ABAC models. This decouples permissions from your application’s business logic, providing a centralized and scalable way to manage authorization while reducing development effort.

   <p>In this post, we walk you through a reference architecture that outlines an end-to-end smart thermostat application solution using <a href="https://aws.amazon.com/iot-core/" target="_blank" rel="noopener">AWS IoT Core</a>, Verified Permissions, and other AWS services. We show you how to use Verified Permissions to build an authorization solution using Cedar policy language to define dynamic policy-based access controls for different user personas. The post includes a link to a <a href="https://github.com/aws-samples/amazon-verified-permissions-iot-amplify-smart-home-application" target="_blank" rel="noopener">GitHub repository</a> that houses the code for the web dashboard and the Verified Permissions logic to control access to the solution APIs.</p> 
   <h2>Solution overview</h2> 
   <p>This solution consists of a smart thermostat IoT device and an AWS hosted web application using Verified Permissions for fine-grained access to various application APIs. For this use case, the AWS IoT Core device is being simulated by an <a href="https://aws.amazon.com/cloud9/" target="_blank" rel="noopener">AWS Cloud9 </a>environment and communicates with the IoT service using <a href="https://docs.aws.amazon.com/iot/latest/developerguide/iot-sdks.html" target="_blank" rel="noopener">AWS IoT Device SDK</a> for Python. After being configured, the device connects to AWS IoT Core to receive commands and send messages to various <a href="https://mqtt.org/" target="_blank" rel="noopener">MQTT</a> topics.</p> 
   <p>As a general practice, when a user-facing IoT solution is implemented, the manufacturer performs administrative tasks such as:</p> 
   <ol> 
    <li>Embedding <a href="https://aws.amazon.com/private-ca/" target="_blank" rel="noopener">AWS Private Certificate Authority</a> certificates into each IoT device (in this case a smart thermostat). Usually this is done on the assembly line and the certificates used to verify the IoT endpoints are burned into device memory along with the firmware.</li> 
    <li>Creating an <a href="https://docs.aws.amazon.com/cognito/latest/developerguide/what-is-amazon-cognito.html" target="_blank" rel="noopener">Amazon Cognito</a> user pool that provides sign-up and sign-in options for web and mobile application users and hosts the authentication process.</li> 
    <li>Creating policy stores and policy templates in Verified Permissions. Based on who signs up, the manufacturer creates policies with Verified Permissions to link each signed-up user to certain allowed resources or IoT devices.</li> 
    <li>The mapping of user to device is stored in a datastore. For this solution, you’ll use an <a href="https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Introduction.html" target="_blank" rel="noopener">Amazon DynamoDB</a> table to record the relationship.</li> 
   </ol> 
   <p>The user who purchases the device (the primary device owner) performs the following tasks:</p> 
   <ol> 
    <li>Signs up on the manufacturer’s web application or mobile app and registers the IoT device by entering a unique serial number. The mapping between user details and the device serial number is stored in the datastore through an automated process that is initiated after sign-up and device claim.</li> 
    <li>Connects the new device to an existing wireless network, which initiates a registration process to securely connect to AWS IoT Core services within the manufacturer’s account.</li> 
    <li>Invites other users (such as guests, family members, or the power company) through a referral, invitation link, or a designated OAuth process.</li> 
    <li>Assign roles to the other users and therefore permissions.<br> </li> 
   </ol> 
   <div id="attachment_33962" class="wp-caption aligncenter"> 
    <img aria-describedby="caption-attachment-33962" src="https://infracom.com.sg/wp-content/uploads/2024/04/img1-5.png" alt="Figure 1: Sample smart home application architecture built using AWS services" width="780" class="size-full wp-image-33962"> 
    <p id="caption-attachment-33962" class="wp-caption-text">Figure 1: Sample smart home application architecture built using AWS services</p> 
   </div> 
   <p>Figure 1 depicts the solution as three logical components:</p> 
   <ol> 
    <li>The first component depicts device operations through AWS IoT Core. The smart thermostat is on site and it communicates with AWS IoT Core and its state is managed through the AWS <a href="https://docs.aws.amazon.com/iot/latest/developerguide/iot-device-shadows.html" target="_blank" rel="noopener">IoT Device Shadow Service</a>.</li> 
    <li>The second component depicts the web application, which is the application interface that customers use. It’s a ReactJS-backed single page application deployed using <a href="https://aws.amazon.com/amplify/" target="_blank" rel="noopener">AWS Amplify</a>. </li> 
    <li>The third component shows the backend application, which is built using <a href="https://aws.amazon.com/api-gateway/" target="_blank" rel="noopener">Amazon API Gateway</a>, <a href="https://aws.amazon.com/lambda/" target="_blank" rel="noopener">AWS Lambda</a>, and DynamoDB. A Cognito user pool is used to manage application users and their authentication. Authorization is handled by Verified Permissions where you create and manage policies that are evaluated when the web application calls backend APIs. These policies are evaluated against each authorization policy to provide an access decision to deny or allow an action.</li> 
   </ol> 
   <p>The solution flow itself can be broken down into three steps after the device is onboarded and users have signed up:</p> 
   <ol> 
    <li>The smart thermostat device connects and communicates with AWS IoT Core using the <a href="https://docs.aws.amazon.com/iot/latest/developerguide/mqtt.html" target="_blank" rel="noopener">MQTT protocol</a>. A classic Device Shadow is created for the AWS IoT thing <em>Thermostat1</em> when the <a href="https://docs.aws.amazon.com/iot/latest/apireference/API_iotdata_UpdateThingShadow.html" target="_blank" rel="noopener">UpdateThingShadow</a> call is made the first time through the AWS SDK for a new device. <a href="https://docs.aws.amazon.com/iot/latest/developerguide/iot-device-shadows.html" target="_blank" rel="noopener">AWS IoT Device Shadow service</a> lets the web application query and update the device’s state in case of connectivity issues.</li> 
    <li>Users sign up or sign in to the Amplify hosted smart home application and authenticate themselves against a Cognito user pool. They’re mapped to a device, which is stored in a DynamoDB table.</li> 
    <li>After the users sign in, they’re allowed to perform certain tasks and view certain sections of the dashboard based on the different roles and policies managed by Verified Permissions. The underlying Lambda function that’s responsible for handling the API calls queries the DynamoDB table to provide user context to Verified Permissions.</li> 
   </ol> 
   <h2>Prerequisites</h2> 
   <ol> 
    <li>To deploy this solution, you need access to the <a href="https://aws.amazon.com/console/" target="_blank" rel="noopener">AWS Management Console</a> and <a href="https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html" target="_blank" rel="noopener">AWS Command Line Interface (AWS CLI)</a> on your local machine with sufficient <a href="https://docs.aws.amazon.com/iot/latest/developerguide/iot-device-shadows.html" target="_blank" rel="noopener">permissions to access</a> required services, including Amplify, Verified Permissions, and AWS IoT Core. For this solution, you’ll give the services full access to interact with different underlying services. But in production, we recommend following <a href="https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html" target="_blank" rel="noopener">security best practices with AWS Identity and Access Management (IAM)</a>, which involves scoping down policies.</li> 
    <li>Set up Amplify CLI by following <a href="https://docs.amplify.aws/cli/start/install/" target="_blank" rel="noopener">these instructions</a>. We recommend the latest NodeJS stable long-term support (LTS) version. At the time of publishing this post, the <a href="https://nodejs.org/en/download" target="_blank" rel="noopener">LTS</a> version was v20.11.1. Users can manage multiple NodeJS versions on their machines by using a tool such as <a href="https://www.google.com/search?q=nvm&rlz=1C5GCEM_enUS947US948&oq=nvm&gs_lcrp=EgZjaHJvbWUyBggAEEUYOTIGCAEQRRg7MgYIAhBFGD3SAQc2MTlqMGo3qAIAsAIA&sourceid=chrome&ie=UTF-8" target="_blank" rel="noopener">Node Version Manager (nvm)</a>.</li> 
   </ol> 
   <h2>Walkthrough</h2> 
   <p>The following table describes the actions, resources, and authorization decisions that will be enforced through Verified Permissions policies to achieve fine-grained access control. In this example, John is the primary device owner and has purchased and provisioned a new smart thermostat device called <em>Thermostat1</em>. He has invited Jane to access his device and has given her restricted permissions. John has full control over the device whereas Jane is only allowed to read the temperature and set the temperature between 72°F and 78°F.</p> 
   <p>John has also decided to give his local energy provider (Power Company) access to the device so that they can set the optimum temperature during the day to manage grid load and offer him maximum savings on his energy bill. However, they can only do so between 2:00 PM and 5:00 PM.</p> 
   <p>For security purposes the verified permissions default decision is DENY for unauthorized principals.</p> 
   <table width="100%"> 
    <tbody> 
     <tr> 
      <td width="16%"><strong>Name</strong></td> 
      <td width="16%"><strong>Principal</strong></td> 
      <td width="16%"><strong>Action</strong></td> 
      <td width="16%"><strong>Resource</strong></td> 
      <td width="36%"><strong>Authorization decision</strong></td> 
     </tr> 
     <tr> 
      <td width="16%">Any</td> 
      <td width="16%">Default</td> 
      <td width="16%">Default</td> 
      <td width="16%">Default</td> 
      <td width="36%">Deny</td> 
     </tr> 
     <tr> 
      <td width="16%">John</td> 
      <td width="16%">john_doe</td> 
      <td width="16%">Any</td> 
      <td width="16%">Thermostat1</td> 
      <td width="36%">Allow</td> 
     </tr> 
     <tr> 
      <td width="16%">Jane</td> 
      <td width="16%">jane_doe</td> 
      <td width="16%">GetTemperature </td> 
      <td width="16%">Thermostat1</td> 
      <td width="36%">Allow</td> 
     </tr> 
     <tr> 
      <td width="16%">Jane</td> 
      <td width="16%">jane_doe</td> 
      <td width="16%">SetTemperature </td> 
      <td width="16%">Thermostat1</td> 
      <td width="36%">Allow only if desired temperature is between 72°F and 78°F. </td> 
     </tr> 
     <tr> 
      <td width="16%">Power Company</td> 
      <td width="16%">powercompany</td> 
      <td width="16%">GetTemperature</td> 
      <td width="16%">Thermostat1</td> 
      <td width="36%">Allow only if accessed between the hours of 2:00 PM and 5:00 PM </td> 
     </tr> 
     <tr> 
      <td width="16%">Power Company</td> 
      <td width="16%">powercompany</td> 
      <td width="16%">SetTemperature </td> 
      <td width="16%">Thermostat1</td> 
      <td width="36%">Allow only if the temperature is set between the hours of 2:00 PM and 5:00 PM</td> 
     </tr> 
    </tbody> 
   </table> 
   <h3>Create a Verified Permissions policy store</h3> 
   <p>Verified Permissions is a scalable permissions management and fine-grained authorization service for the applications that you build. The policies are created using Cedar, a dedicated language for defining access permissions in applications. Cedar seamlessly integrates with popular authorization models such as RBAC and ABAC.</p> 
   <p>A policy is a statement that either permits or forbids a principal to take one or more actions on a resource. A <em>policy store</em> is a logical container that stores your Cedar policies, schema, and principal sources. A schema helps you to validate your policy and identify errors based on the definitions you specify. See <a href="https://docs.cedarpolicy.com/schema/schema.html" target="_blank" rel="noopener">Cedar schema</a> to learn about the structure and formal grammar of a Cedar schema.</p> 
   <h4>To create the policy store</h4> 
   <ol> 
    <li>Sign in to the Amazon Verified Permissions console and choose <strong>Create policy store</strong>.</li> 
    <li>In the <strong>Configuration Method</strong> section, select <strong>Empty Policy Store</strong> and choose <strong>Create policy store</strong>.<br> </li> 
   </ol> 
   <div id="attachment_33966" class="wp-caption aligncenter"> 
    <img aria-describedby="caption-attachment-33966" src="https://infracom.com.sg/wp-content/uploads/2024/04/img2-5.png" alt="Figure 2: Create an empty policy store" width="780" class="size-full wp-image-33966"> 
    <p id="caption-attachment-33966" class="wp-caption-text">Figure 2: Create an empty policy store</p> 
   </div> 
   <blockquote> 
    <p><strong>Note:</strong> Make a note of the policy store ID to use when you deploy the solution.</p> 
   </blockquote> 
   <h4>To create a schema for the application</h4> 
   <ol> 
    <li>On the <strong>Verified Permissions</strong> page, select <strong>Schema</strong>.</li> 
    <li>In the <strong>Schema</strong> section, choose <strong>Create schema</strong>.<br>  
     <div id="attachment_33967" class="wp-caption aligncenter"> 
      <img aria-describedby="caption-attachment-33967" src="https://infracom.com.sg/wp-content/uploads/2024/04/img3-5.png" alt="Figure 3: Create a schema" width="740" class="size-full wp-image-33967"> 
      <p id="caption-attachment-33967" class="wp-caption-text">Figure 3: Create a schema</p> 
     </div> </li> 
    <li>In the <strong>Edit schema</strong> section, choose <strong>JSON mode</strong>, paste the following sample schema for your application, and choose <strong>Save changes</strong>. <pre class="unlimited-height-code"><code class="lang-js">{
"AwsIotAvpWebApp": {
    "entityTypes": {
        "Device": {
            "shape": {
                "attributes": {
                    "primaryOwner": {
                        "name": "User",
                        "required": true,
                        "type": "Entity"
                    }
                },
                "type": "Record"
            },
            "memberOfTypes": []
        },
        "User": {}
    },
    "actions": {
        "GetTemperature": {
            "appliesTo": {
                "context": {
                    "attributes": {
                        "desiredTemperature": {
                            "type": "Long"
                        },
                        "time": {
                            "type": "Long"
                        }
                    },
                    "type": "Record"
                },
                "resourceTypes": [
                    "Device"
                ],
                "principalTypes": [
                    "User"
                ]
            }
        },
        "SetTemperature": {
            "appliesTo": {
                "resourceTypes": [
                    "Device"
                ],
                "principalTypes": [
                    "User"
                ],
                "context": {
                    "attributes": {
                        "desiredTemperature": {
                            "type": "Long"
                        },
                        "time": {
                            "type": "Long"
                        }
                    },
                    "type": "Record"
                }
            }
        }
    }
}

}

   When creating policies in Cedar, you can define authorization rules using a static policy or a template-linked policy.</p> 
   <h3>Static policies</h3> 
   <p>In scenarios where a policy explicitly defines both the principal and the resource, the policy is categorized as a <a href="https://docs.aws.amazon.com/verifiedpermissions/latest/userguide/policies_create.html" target="_blank" rel="noopener">static policy</a>. These policies are immediately applicable for authorization decisions, as they are fully defined and ready for implementation.</p> 
   <h3>Template-linked policies</h3> 
   <p>On the other hand, there are situations where a single set of authorization rules needs to be applied across a variety of principals and resources. Consider an IoT application where actions such as <em>SetTemperature</em> and <em>GetTemperature</em> must be permitted for specific devices. Using static policies for each unique combination of principal and resource can lead to an excessive number of almost identical policies, differing only in their principal and resource components. This redundancy can be efficiently addressed with policy templates. <a href="https://docs.aws.amazon.com/verifiedpermissions/latest/userguide/policy-templates.html" target="_blank" rel="noopener">Policy templates</a> allow for the creation of policies using placeholders for the principal, the resource, or both. After a policy template is established, individual policies can be generated by referencing this template and specifying the desired principal and resource. These <a href="https://docs.aws.amazon.com/verifiedpermissions/latest/userguide/policies_create-from-template.html" target="_blank" rel="noopener">template-linked policies</a> function the same as static policies, offering a streamlined and scalable solution for policy management.</p> 
   <h4>To create a policy that allows access to the primary owner of the device using a static policy</h4> 
   <ol> 
    <li>In the Verified Permissions console, on the left pane, select <strong>Policies</strong>, then choose <strong>Create policy</strong> and select <strong>Create static policy</strong> from the drop-down menu.<br />  
     <div id="attachment_33969" class="wp-caption aligncenter"> 
      <img aria-describedby="caption-attachment-33969" src="https://infracom.com.sg/wp-content/uploads/2024/04/img4-5.png" alt="Figure 4: Create static policy" width="740" class="size-full wp-image-33969" /> 
      <p id="caption-attachment-33969" class="wp-caption-text">Figure 4: Create static policy</p> 
     </div> </li> 
    <li>Define the policy scope: 
     <ol> 
      <li>Select <strong>Permit</strong> for the <strong>Policy effect</strong>.<br />  
       <div id="attachment_33970" class="wp-caption aligncenter"> 
        <img aria-describedby="caption-attachment-33970" src="https://infracom.com.sg/wp-content/uploads/2024/04/img5-4.png" alt="Figure 5: Define policy effect" width="700" class="size-full wp-image-33970" /> 
        <p id="caption-attachment-33970" class="wp-caption-text">Figure 5: Define policy effect</p> 
       </div> </li> 
      <li>Select <strong>All Principals</strong> for <strong>Principals scope</strong>.</li> 
      <li>Select <strong>All Resources</strong> for <strong>Resource scope</strong>.</li> 
      <li>Select <strong>All Actions</strong> for <strong>Actions scope</strong> and choose <strong>Next</strong>.<br />  
       <div id="attachment_33971" class="wp-caption aligncenter"> 
        <img aria-describedby="caption-attachment-33971" src="https://infracom.com.sg/wp-content/uploads/2024/04/img6-2.png" alt="Figure 6: Define policy scope" width="700" class="size-full wp-image-33971" /> 
        <p id="caption-attachment-33971" class="wp-caption-text">Figure 6: Define policy scope</p> 
       </div> </li> 
     </ol> </li> 
    <li>On the <strong>Details</strong> page, under <strong>Policy</strong>, paste the following full-access policy, which grants the primary owner permission to perform both SetTemperature and GetTemperature actions on the smart thermostat unconditionally. Choose <strong>Create policy</strong>. <pre class="unlimited-height-code"><code class="lang-cedar">   permit (principal, action, resource)
when { resource.primaryOwner == principal };</code></pre> 
     <div id="attachment_33972" class="wp-caption aligncenter"> 
      <img aria-describedby="caption-attachment-33972" src="https://infracom.com.sg/wp-content/uploads/2024/04/img7-1.png" alt="Figure 7: Write and review policy statement" width="740" class="size-full wp-image-33972" /> 
      <p id="caption-attachment-33972" class="wp-caption-text">Figure 7: Write and review policy statement</p> 
     </div> </li> 
   </ol> 
   <h4>To create a static policy to allow a guest user to read the temperature</h4> 
   <p>In this example, the guest user is Jane (username: jane_doe).</p> 
   <ol> 
    <li>Create another static policy and specify the policy scope. 
     <ol> 
      <li>Select <strong>Permit</strong> for the <strong>Policy effect</strong>.<br />  
       <div id="attachment_33974" class="wp-caption aligncenter"> 
        <img aria-describedby="caption-attachment-33974" src="https://infracom.com.sg/wp-content/uploads/2024/04/img8-1-1.png" alt="Figure 8: Define the policy effect" width="700" class="size-full wp-image-33974" /> 
        <p id="caption-attachment-33974" class="wp-caption-text">Figure 8: Define the policy effect</p> 
       </div> </li> 
      <li>Select <strong>Specific principal</strong> for the <strong>Principals scope</strong>.</li> 
      <li>Select <strong>AwsIotAvpWebApp::User </strong>and enter <span>jane_doe</span>.<br />  
       <div id="attachment_33975" class="wp-caption aligncenter"> 
        <img aria-describedby="caption-attachment-33975" src="https://infracom.com.sg/wp-content/uploads/2024/04/img9-1.png" alt="Figure 9: Define the policy scope" width="700" class="size-full wp-image-33975" /> 
        <p id="caption-attachment-33975" class="wp-caption-text">Figure 9: Define the policy scope</p> 
       </div> </li> 
      <li>Select <strong>Specific resource</strong> for the <strong>Resources scope</strong>.</li> 
      <li>Select <strong>AwsIotAvpWebApp::Device</strong> and enter <span>Thermostat1</span>.</li> 
      <li>Select <strong>Specific set of actions</strong> for the <strong>Actions scope</strong>.</li> 
      <li>Select <strong>GetTemperature</strong> and choose <strong>Next</strong>.<br />  
       <div id="attachment_33976" class="wp-caption aligncenter"> 
        <img aria-describedby="caption-attachment-33976" src="https://infracom.com.sg/wp-content/uploads/2024/04/img10-1.png" alt="Figure 10: Define resource and action scopes" width="700" class="size-full wp-image-33976" /> 
        <p id="caption-attachment-33976" class="wp-caption-text">Figure 10: Define resource and action scopes</p> 
       </div> </li> 
      <li>Enter the <strong>Policy description</strong>: <span>Allow jane_doe to read thermostat1</span>.</li> 
      <li>Choose <strong>Create policy</strong>.</li> 
     </ol> </li> 
   </ol> 
   <p>Next, you will create reusable policy templates to manage policies efficiently. To create a policy template for a guest user with restricted temperature settings that limit the temperature range they can set to between 72°F and 78°F. In this case, the guest user is going to be Jane (username: jane_doe)</p> 
   <h4>To create a reusable policy template</h4> 
   <ol> 
    <li>Select <strong>Policy template</strong> and enter <span>Guest user template</span> as the description.</li> 
    <li>Paste the following sample policy in the <strong>Policy body</strong> and choose <strong>Create policy template</strong>. <pre class="unlimited-height-code"><code class="lang-cedar">permit (
principal == ?principal,
action in [AwsIotAvpWebApp::Action::"SetTemperature"],
resource == ?resource

)
when { context.desiredTemperature >= 72 && context.desiredTemperature <= 78 };

Figure 11: Create guest user policy template

Figure 11: Create guest user policy template

As you can see, you don’t specify the principal and resource yet. You enter those when you create an actual policy from the policy template. The context object will be populated with the desiredTemperature property in the application and used to evaluate the decision.

You also need to create a policy template for the Power Company user with restricted time settings. Cedar policies don’t support date/time format, so you must represent 2:00 PM and 5:00 PM as elapsed minutes from midnight.

To create a policy template for the power company

  1. Select Policy template and enter Power company user template as the description.
  2. Paste the following sample policy in the Policy body and choose Create policy template.
    permit (
        principal == ?principal,
        action in [AwsIotAvpWebApp::Action::"SetTemperature", AwsIotAvpWebApp::Action::"GetTemperature"],
        resource == ?resource
    )
    when { context.time >= 840 && context.time < 1020 };

The policy templates accept the user and resource. The next step is to create a template-linked policy for Jane to set and get thermostat readings based on the Guest user template that you created earlier. For simplicity, you will manually create this policy using the Verified Permissions console. In production, application policies can be dynamically created using the Verified Permissions API.

To create a template-linked policy for a guest user

  1. In the Verified Permissions console, on the left pane, select Policies, then choose Create policy and select Create template-linked policy from the drop-down menu.
     
    Figure 12: Create new template-linked policy

    Figure 12: Create new template-linked policy

  2. Select the Guest user template and choose next.
     
    Figure 13: Select Guest user template

    Figure 13: Select Guest user template

  3. Under parameter selection:
    1. For Principal enter AwsIotAvpWebApp::User::”jane_doe”.
    2. For Resource enter AwsIotAvpWebApp::Device::”Thermostat1″.
    3. Choose Create template-linked policy.
       
      Figure 14: Create guest user template-linked policy

      Figure 14: Create guest user template-linked policy

Note that with this policy in place, jane_doe can only set the temperature of the device Thermostat1 to between 72°F and 78°F.

To create a template-linked policy for the power company user

Based on the template that was set up for power company, you now need an actual policy for it.

  1. In the Verified Permissions console, go to the left pane and select Policies, then choose Create policy and select Create template-linked policy from the drop-down menu.
  2. Select the Power company user template and choose next.
  3. Under Parameter selection, for Principal enter AwsIotAvpWebApp::User::”powercompany”, and for Resource enter AwsIotAvpWebApp::Device::”Thermostat1″, and choose Create template-linked policy.

Now that you have a set of policies in a policy store, you need to update the backend codebase to include this information and then deploy the web application using Amplify.

The policy statements in this post intentionally use human-readable values such as jane_doe and powercompany for the principal entity. This is useful when discussing general concepts but in production systems, customers should use unique and immutable values for entities. See Get the best out of Amazon Verified Permissions by using fine-grained authorization methods for more information.

Deploy the solution code from GitHub

Go to the GitHub repository to set up the Amplify web application. The repository Readme file provides detailed instructions on how to set up the web application. You will need your Verified Permissions policy store ID to deploy the application. For convenience, we’ve provided an onboarding script—deploy.sh—which you can use to deploy the application.

To deploy the application

  1. Close the repository.
    git clone https://github.com/aws-samples/amazon-verified-permissions-iot-
    amplify-smart-home-application.git

  2. Deploy the application.
    ./deploy.sh  

After the web dashboard has been deployed, you’ll create an IoT device using AWS IoT Core.

Create an IoT device and connect it to AWS IoT Core

With the users, policies, and templates, and the Amplify smart home application in place, you can now create a device and connect it to AWS IoT Core to complete the solution.

To create Thermostat1” device and connect it to AWS IoT Core

  1. From the left pane in the AWS IoT console, select Connect one device.
     
    Figure 15: Connect device using AWS IoT console

    Figure 15: Connect device using AWS IoT console

  2. Review how IoT Thing works and then choose Next.
     
    Figure 16: Review how IoT Thing works before proceeding

    Figure 16: Review how IoT Thing works before proceeding

  3. Choose Create a new thing and enter Thermostat1 as the Thing name and choose next.
    &bsp;
    Figure 17: Create the new IoT thing

    Figure 17: Create the new IoT thing

  4. Select Linux/macOS as the Device platform operating system and Python as the AWS IoT Core Device SDK and choose next.
     
    Figure 18: Choose the platform and SDK for the device

    Figure 18: Choose the platform and SDK for the device

  5. Choose Download connection kit and choose next.
     
    Figure 19: Download the connection kit to use for creating the Thermostat1 device

    Figure 19: Download the connection kit to use for creating the Thermostat1 device

  6. Review the three steps to display messages from your IoT device. You will use them to verify the thermostat1 IoT device connectivity to the AWS IoT Core platform. They are:
    1. Step 1: Add execution permissions
    2. Step 2: Run the start script
    3. Step 3: Return to the AWS IoT Console to view the device’s message
       
      Figure 20: How to display messages from an IoT device

      Figure 20: How to display messages from an IoT device

Solution validation

With all of the pieces in place, you can now test the solution.

Primary owner signs in to the web application to set Thermostat1 temperature to 82°F

Figure 21: Thermostat1 temperature update by John

Figure 21: Thermostat1 temperature update by John

  1. Sign in to the Amplify web application as John. You should be able to view the Thermostat1 controller on the dashboard.
  2. Set the temperature to 82°F.
  3. The Lambda function processes the request and performs an API call to Verified Permissions to determine whether to ALLOW or DENY the action based on the policies. Verified Permissions sends back an ALLOW, as the policy that was previously set up allows unrestricted access for primary owners.
  4. Upon receiving the response from Verified Permissions, the Lambda function sends ALLOW permission back to the web application and an API call to the AWS IoT Device Shadow service to update the device (Thermostat1) temperature to 82°F.
     
Figure 22: Policy evaluation decision is ALLOW when a primary owner calls SetTemperature

Figure 22: Policy evaluation decision is ALLOW when a primary owner calls SetTemperature

Guest user signs in to the web application to set Thermostat1 temperature to 80°F

Figure 23: Thermostat1 temperature update by Jane

Figure 23: Thermostat1 temperature update by Jane

  1. If you sign in as Jane to the Amplify web application, you can view the Thermostat1 controller on the dashboard.
  2. Set the temperature to 80°F.
  3. The Lambda function validates the actions by sending an API call to Verified Permissions to determine whether to ALLOW or DENY the action based on the established policies. Verified Permissions sends back a DENY, as the policy only permits temperature adjustments between 72°F and 78°F.
  4. Upon receiving the response from Verified Permissions, the Lambda function sends DENY permissions back to the web application and an unauthorized response is returned.
     
    Figure 24: Guest user jane_doe receives a DENY when calling SetTemperature for a desired temperature of 80°F

    Figure 24: Guest user jane_doe receives a DENY when calling SetTemperature for a desired temperature of 80°F

  5. If you repeat the process (still as Jane) but set Thermostat1 to 75°F, the policy will cause the request to be allowed.
     
    Figure 25: Guest user jane_doe receives an ALLOW when calling SetTemperature for a desired temperature of 75°F

    Figure 25: Guest user jane_doe receives an ALLOW when calling SetTemperature for a desired temperature of 75°F

  6. Similarly, jane_doe is allowed run GetTemperature on the device Thermostat1. When the temperature is set to 74°F, the device shadow is updated. The IoT device being simulated by your AWS Cloud9 instance reads desired the temperature field and sets the reported value to 74.
  7. Now, when jane_doe runs GetTemperature, the value of the device is reported as 74 as shown in Figure 26. We encourage you to try different restrictions in the World Settings (outside temperature and time) by adding restrictions to the static policy that allows GetTemperature for guest user.
     
    Figure 26: Guest user jane_doe receives an ALLOW when calling GetTemperature for the reported temperature

    Figure 26: Guest user jane_doe receives an ALLOW when calling GetTemperature for the reported temperature

Power company signs in to the web application to set Thermostat1 to 78°F at 3.30 PM

Figure 27: Thermostat1 temperature set to 78°F by powercompany user at a specified time

Figure 27: Thermostat1 temperature set to 78°F by powercompany user at a specified time

  1. Sign in as the powercompany user to the Amplify web application using an API. You can view the Thermostat1 controller on the dashboard.
  2. To test this scenario, set the current time to 3:30 PM, and try to set the temperature to 78°F.
  3. The Lambda function validates the actions by sending an API call to Verified Permissions to determine whether to ALLOW or DENY the action based on pre-established policies. Verified Permissions returns ALLOW permission, because the policy for powercompany permits device temperature changes between 2:00 PM and 5:00 PM.
  4. Upon receiving the response from Verified Permissions, the Lambda function sends ALLOW permission back to the web application and an API call to the AWS IoT Device Shadow service to update the Thermostat1 temperature to 78°F.
     
    Figure 28: powercompany receives an ALLOW when SetTemperature is called with the desired temperature of 78°F

    Figure 28: powercompany receives an ALLOW when SetTemperature is called with the desired temperature of 78°F

Note: As an optional exercise, we also made jane_doe a device owner for device Thermostat2. This can be observed in the users.json file in the Github repository. We encourage you to create your own policies and restrict functions for Thermostat2 after going through this post. You will need to create separate Verified Permissions policies and update the Lambda functions to interact with these policies.

We encourage you to create policies for guests and the power company and restrict permissions based on the following criteria:

  1. Verify Jane Doe can perform GetTemperature and SetTemperature actions on Thermostat2.
  2. John Doe should not be able to set the temperature on device Thermostat2 outside of the time range of 4:00 PM and 6:00 PM and outside of the temperature range of 68°F and 72°F.
  3. Power Company can only perform the GetTemperature operation, but there are no restrictions on time and outside temperature.

To help you verify the solution, we’ve provided the correct policies under the challenge directory in the GitHub repository.

Clean up

Deploying the Thermostat application in your AWS account will incur costs. To avoid ongoing charges, when you’re done examining the solution, delete the resources that were created. This includes the Amplify hosted web application, API Gateway resource, AWS Cloud 9 environment, the Lambda function, DynamoDB table, Cognito user pool, AWS IoT Core resources, and Verified Permissions policy store.

Amplify resources can be deleted by going to the AWS CloudFormation console and deleting the stacks that were used to provision various services.

Conclusion

In this post, you learned about creating and managing fine-grained permissions using Verified Permissions for different user personas for your smart thermostat IoT device. With Verified Permissions, you can strengthen your security posture and build smart applications aligned with Zero Trust principles for real-time authorization decisions. To learn more, we recommend:

   <p> <br>If you have feedback about this post, submit comments in the<strong> Comments</strong> section below. If you have questions about this post, <a href="https://console.aws.amazon.com/support/home" target="_blank" rel="noopener noreferrer">contact AWS Support</a>.</p> 

   <!-- '"` -->