<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Kaustubh Khavnekar</title>
    <description>The latest articles on Forem by Kaustubh Khavnekar (@kaustubhk).</description>
    <link>https://forem.com/kaustubhk</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1046114%2F7d7d9dfd-02d8-4c01-8556-41552c98b2b5.png</url>
      <title>Forem: Kaustubh Khavnekar</title>
      <link>https://forem.com/kaustubhk</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/kaustubhk"/>
    <language>en</language>
    <item>
      <title>IAM Roles Anywhere: Introduction and Demo</title>
      <dc:creator>Kaustubh Khavnekar</dc:creator>
      <pubDate>Thu, 16 Mar 2023 00:18:59 +0000</pubDate>
      <link>https://forem.com/aws-builders/iam-roles-anywhere-introduction-and-demo-5h2j</link>
      <guid>https://forem.com/aws-builders/iam-roles-anywhere-introduction-and-demo-5h2j</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fajlkb6jk1wass0wz5ddz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fajlkb6jk1wass0wz5ddz.png" alt="IAM Logo"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;This article will introduce you to &lt;a href="https://docs.aws.amazon.com/rolesanywhere/latest/userguide/introduction.html" rel="noopener noreferrer"&gt;IAM Roles Anywhere&lt;/a&gt;, the underlying concept of public key infrastructure, and finally, a demo shell script that showcases the steps required to use IAM Role Anywhere as well as the steps required to generate certificates for it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Public Key Infrastructure (PKI)
&lt;/h2&gt;

&lt;p&gt;Before we jump into IAM Roles Anywhere, it is important to understand some basic concepts related to PKI:&lt;/p&gt;

&lt;h3&gt;
  
  
  Public-key/Asymmetric cryptography
&lt;/h3&gt;

&lt;p&gt;It uses a pair of a public key (not secret) and a private key (secret). It can be used for encryption or digital verification, as illustrated by the following diagrams &lt;a href="https://en.wikipedia.org/wiki/File:Private_key_signing.svg" rel="noopener noreferrer"&gt;from Wikipedia&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5i6oxf4cxjx1iupqk22t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5i6oxf4cxjx1iupqk22t.png" alt="Encryption"&gt;&lt;/a&gt;&lt;br&gt;
The public key can be used to encrypt text, and the encrypted text can only be decrypted by someone who has the corresponding private key. &lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgac8zsr5yslf5yz85nf6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgac8zsr5yslf5yz85nf6.png" alt="Signing"&gt;&lt;/a&gt;&lt;br&gt;
Alternatively, the private key can be used to sign text, and the public key can be used to verify that the text was signed using the private key.&lt;/p&gt;

&lt;h3&gt;
  
  
  Hybrid cryptography
&lt;/h3&gt;

&lt;p&gt;Since asymmetric cryptography is computationally expensive, it is usually combined with symmetric cryptography. This is done by using asymmetric cryptography to encrypt and exchange a symmetric key (a single key used to both encrypt and decrypt data) which is then used to encrypt the actual data being exchanged.&lt;/p&gt;

&lt;h3&gt;
  
  
  Certificate Authority (CA)
&lt;/h3&gt;

&lt;p&gt;The Certificate Authority verifies that a public key is actually associated with the claimed user since the public key needs to be uncompromised (not modified by a man-in-the-middle attack for example) for asymmetric key based communication between two entities to be actually secure. It does the verification using its own private key, so it is important for any entity using the Certificate Authority for verification to already have built-in trust in the Certificate Authority.&lt;/p&gt;

&lt;h3&gt;
  
  
  X.509
&lt;/h3&gt;

&lt;p&gt;X.509 is an International Telecommunication Union (ITU) standard that defines the format used for public key certificates. It associates a public key with an identity using a digital signature.&lt;/p&gt;

&lt;h2&gt;
  
  
  IAM Roles Anywhere
&lt;/h2&gt;

&lt;p&gt;IAM Roles Anywhere use Public Key Infrastructure to verify the identity of an on-premise workload and then generate valid AWS credentials for the workload. &lt;/p&gt;

&lt;h3&gt;
  
  
  Trust Anchor
&lt;/h3&gt;

&lt;p&gt;In order to use IAM Roles Anywhere, you need to register a Certificate Authority (CA) with IAM Roles Anywhere. IAM Roles Anywhere uses the term &lt;em&gt;trust anchor&lt;/em&gt; for registered certificate authorities.&lt;/p&gt;

&lt;p&gt;There are two options for Certificate Authorities with IAM Roles Anywhere:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use &lt;a href="https://docs.aws.amazon.com/privateca/latest/userguide/PcaWelcome.html" rel="noopener noreferrer"&gt;AWS Private CA&lt;/a&gt; to create your own private CA managed by AWS. Make sure to review &lt;a href="https://aws.amazon.com/private-ca/pricing/" rel="noopener noreferrer"&gt;pricing&lt;/a&gt; for AWS Private CA if you are planning to use this option since the price can be quite steep for hobby users/non-corporate users who just want to try IAM Roles Anywhere.&lt;/li&gt;
&lt;li&gt;Use your own external CA certificate. This will be the preferred option if you want to manage the CA on your own or if you already have a private CA used in your organization.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhxc8lcarruyl7dbndfii.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhxc8lcarruyl7dbndfii.png" alt="Trust Anchor"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  IAM Role
&lt;/h3&gt;

&lt;p&gt;An IAM Role is an IAM identity in an AWS account that has a specific set of permissions. Based on its trust policy, it can be assumed by whoever needs access to it. This can be an AWS service, a program, a human user, etc. based on how it is configured. IAM Roles Anywhere allows entities outside AWS to assume IAM Roles and generate temporary AWS credentials, without having to use long-term credentials which can be potentially compromised. In order for a role to be usable with IAM Roles Anywhere, it should trust &lt;code&gt;rolesanywhere.amazonaws.com&lt;/code&gt; in its trust policy. More details on different trust policy configurations and how they should be restricted can be found &lt;a href="https://docs.aws.amazon.com/rolesanywhere/latest/userguide/trust-model.html#role-trusts" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Important:&lt;/strong&gt; The demo script given below uses a very simple trust policy with no condition keys, but for actual production implementations, it is highly recommended to add condition keys to your trust policy.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F45auy595vqmb76qqz80i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F45auy595vqmb76qqz80i.png" alt="IAM Role"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Profile
&lt;/h3&gt;

&lt;p&gt;A profile is created in IAM Roles Anywhere to determine which Roles can be assumed by a workload through IAM Roles Anywhere. Optionally, you can also add IAM policy statements to further restrict actions that are allowed, so that the workload only has a subset of the permissions provided by the IAM Role. Note that IAM policies added here cannot be used to grant additional permissions which are not already allowed by the role, they can only restrict permissions allowed by the role.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7vk5bidegbxaset1g7pe.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7vk5bidegbxaset1g7pe.png" alt="Profile"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Requirements for Certificate Authority/Trust Anchor certificate
&lt;/h3&gt;

&lt;p&gt;Certificates for your certificate authority need to fulfill the following requirements (&lt;a href="https://docs.aws.amazon.com/rolesanywhere/latest/userguide/trust-model.html#signature-verification" rel="noopener noreferrer"&gt;Reference&lt;/a&gt;):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The certificates MUST be X.509v3.&lt;/li&gt;
&lt;li&gt;Basic constraints MUST include CA: true.&lt;/li&gt;
&lt;li&gt;The key usage MUST include Certificate Sign, and MAY include CRL Sign. Certificate Revocation Lists (CRLs) are an optional feature of IAM Roles Anywhere.&lt;/li&gt;
&lt;li&gt;The signing algorithm MUST include SHA256 or stronger. MD5 and SHA1 signing algorithms are rejected.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Requirements for end entity/client certificate
&lt;/h3&gt;

&lt;p&gt;Certificates for your end entity need to fulfill the following requirements (&lt;a href="https://docs.aws.amazon.com/rolesanywhere/latest/userguide/trust-model.html#signature-verification" rel="noopener noreferrer"&gt;Reference&lt;/a&gt;):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The certificates MUST be X.509v3.&lt;/li&gt;
&lt;li&gt;Basic constraints MUST include CA: false.&lt;/li&gt;
&lt;li&gt;The key usage MUST include Digital Signature.&lt;/li&gt;
&lt;li&gt;The signing algorithm MUST include SHA256 or stronger. MD5 and SHA1 signing algorithms are rejected.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Demo
&lt;/h2&gt;

&lt;p&gt;You can find all the scripts used for the demo and supporting config files &lt;a href="https://github.com/KMK-Git/iam-roles-anywhere" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Prerequisites:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://linux.die.net/man/1/openssl" rel="noopener noreferrer"&gt;openssl&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://stedolan.github.io/jq/" rel="noopener noreferrer"&gt;jq&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.aws.amazon.com/rolesanywhere/latest/userguide/credential-helper.html" rel="noopener noreferrer"&gt;IAM Roles Anywhere credential helper&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html" rel="noopener noreferrer"&gt;AWS CLI v2&lt;/a&gt; configured with credentials that have at least the following level of access:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"Version"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"2012-10-17"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"Statement"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"Sid"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"IAM"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"Effect"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Allow"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"Action"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
                &lt;/span&gt;&lt;span class="s2"&gt;"iam:PassRole"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
                &lt;/span&gt;&lt;span class="s2"&gt;"iam:CreateRole"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
                &lt;/span&gt;&lt;span class="s2"&gt;"iam:DeleteRole"&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"Resource"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"arn:aws:iam::*:role/demo-rolesanywhere"&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"Sid"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"RolesAnywhere"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"Effect"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Allow"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"Action"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
                &lt;/span&gt;&lt;span class="s2"&gt;"rolesanywhere:DeleteTrustAnchor"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
                &lt;/span&gt;&lt;span class="s2"&gt;"rolesanywhere:ListProfiles"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
                &lt;/span&gt;&lt;span class="s2"&gt;"rolesanywhere:DeleteProfile"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
                &lt;/span&gt;&lt;span class="s2"&gt;"rolesanywhere:ListTrustAnchors"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
                &lt;/span&gt;&lt;span class="s2"&gt;"rolesanywhere:CreateTrustAnchor"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
                &lt;/span&gt;&lt;span class="s2"&gt;"rolesanywhere:CreateProfile"&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"Resource"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"*"&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Generating the CA/Trust Anchor and Client certificates
&lt;/h3&gt;

&lt;p&gt;The following script is used to generate:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Private key for Certificate Authority &lt;code&gt;certificate_authority_private.key&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Certificate for Certificate Authority &lt;code&gt;certificate_authority_cert.pem&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Private key for client &lt;code&gt;client_private.key&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Certificate Signing Request for client &lt;code&gt;client.csr&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Certificate for client signed by Certificate Authority &lt;code&gt;client_cert.pem&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# openssl commands are adapted from https://jimmydqv.com/iam-anywhere/&lt;/span&gt;

&lt;span class="c"&gt;# The following files are required:&lt;/span&gt;
&lt;span class="c"&gt;# - certificate_authority.conf Configuration for CA certificate&lt;/span&gt;
&lt;span class="c"&gt;# - client.conf Configuration for client CSR&lt;/span&gt;
&lt;span class="c"&gt;# - client_v3.ext Extensions file for CSR&lt;/span&gt;
&lt;span class="c"&gt;# See repository code for samples&lt;/span&gt;

&lt;span class="c"&gt;# Generate private key for CA&lt;/span&gt;
openssl genrsa &lt;span class="nt"&gt;-out&lt;/span&gt; certificate_authority_private.key 4096
&lt;span class="c"&gt;# Generate certificate for CA&lt;/span&gt;
openssl req &lt;span class="nt"&gt;-new&lt;/span&gt; &lt;span class="nt"&gt;-x509&lt;/span&gt; &lt;span class="nt"&gt;-days&lt;/span&gt; 365 &lt;span class="nt"&gt;-config&lt;/span&gt; certificate_authority.conf &lt;span class="nt"&gt;-key&lt;/span&gt; certificate_authority_private.key &lt;span class="nt"&gt;-out&lt;/span&gt; certificate_authority_cert.pem &lt;span class="nt"&gt;-extensions&lt;/span&gt; v3_ca
&lt;span class="c"&gt;# Show certificate for CA&lt;/span&gt;
openssl x509 &lt;span class="nt"&gt;-text&lt;/span&gt; &lt;span class="nt"&gt;-noout&lt;/span&gt; &lt;span class="nt"&gt;-in&lt;/span&gt; certificate_authority_cert.pem

&lt;span class="c"&gt;# Generate private key for client&lt;/span&gt;
openssl genrsa &lt;span class="nt"&gt;-out&lt;/span&gt; client_private.key 4096
&lt;span class="c"&gt;# Generate Certificate Signing Request&lt;/span&gt;
openssl req &lt;span class="nt"&gt;-new&lt;/span&gt; &lt;span class="nt"&gt;-config&lt;/span&gt; client.conf &lt;span class="nt"&gt;-key&lt;/span&gt; client_private.key &lt;span class="nt"&gt;-out&lt;/span&gt; client.csr
&lt;span class="c"&gt;# Show CSR&lt;/span&gt;
openssl req &lt;span class="nt"&gt;-text&lt;/span&gt; &lt;span class="nt"&gt;-in&lt;/span&gt; client.csr
&lt;span class="c"&gt;# Generate certificate signed using CA&lt;/span&gt;
openssl x509 &lt;span class="nt"&gt;-req&lt;/span&gt; &lt;span class="nt"&gt;-in&lt;/span&gt; client.csr &lt;span class="nt"&gt;-CA&lt;/span&gt; certificate_authority_cert.pem &lt;span class="nt"&gt;-CAkey&lt;/span&gt; certificate_authority_private.key &lt;span class="nt"&gt;-set_serial&lt;/span&gt; 01 &lt;span class="nt"&gt;-out&lt;/span&gt; client_cert.pem &lt;span class="nt"&gt;-days&lt;/span&gt; 365 &lt;span class="nt"&gt;-sha256&lt;/span&gt; &lt;span class="nt"&gt;-extfile&lt;/span&gt; client_v3.ext
&lt;span class="c"&gt;# Show certificate for client&lt;/span&gt;
openssl x509 &lt;span class="nt"&gt;-text&lt;/span&gt; &lt;span class="nt"&gt;-noout&lt;/span&gt; &lt;span class="nt"&gt;-in&lt;/span&gt; client_cert.pem
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Creating Trust Anchors and Profiles
&lt;/h3&gt;

&lt;p&gt;The following script is used for the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create an IAM Roles Anywhere Trust Anchor&lt;/li&gt;
&lt;li&gt;Create an IAM Role&lt;/li&gt;
&lt;li&gt;Create an IAM Roles Anywhere Profile linking the IAM Role and IAM Roles Anywhere Trust Anchor&lt;/li&gt;
&lt;li&gt;Generate credentials for the IAM Role using client private key&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;aws sts get-caller-identity&lt;/code&gt; to verify credentials
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Get value of CA certificate&lt;/span&gt;
&lt;span class="nv"&gt;value&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sb"&gt;`&lt;/span&gt;&lt;span class="nb"&gt;cat &lt;/span&gt;certificate_authority_cert.pem&lt;span class="sb"&gt;`&lt;/span&gt;
&lt;span class="c"&gt;# Create roles anywhere trust anchor using CA certificate&lt;/span&gt;
&lt;span class="nv"&gt;trust_anchor_arn&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;aws rolesanywhere create-trust-anchor &lt;span class="nt"&gt;--enabled&lt;/span&gt; &lt;span class="nt"&gt;--name&lt;/span&gt; demo-trust-anchor &lt;span class="nt"&gt;--source&lt;/span&gt; &lt;span class="s2"&gt;"sourceData={x509CertificateData=&lt;/span&gt;&lt;span class="nv"&gt;$value&lt;/span&gt;&lt;span class="s2"&gt;},sourceType=CERTIFICATE_BUNDLE"&lt;/span&gt; &lt;span class="nt"&gt;--query&lt;/span&gt; &lt;span class="s1"&gt;'trustAnchor.trustAnchorArn'&lt;/span&gt; &lt;span class="nt"&gt;--output&lt;/span&gt; text&lt;span class="si"&gt;)&lt;/span&gt;
&lt;span class="c"&gt;# Create IAM role which will be assumed&lt;/span&gt;
&lt;span class="nv"&gt;role_arn&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;aws iam create-role &lt;span class="nt"&gt;--role-name&lt;/span&gt; demo-rolesanywhere &lt;span class="nt"&gt;--assume-role-policy-document&lt;/span&gt; file://iam_role_trust_policy.json &lt;span class="nt"&gt;--query&lt;/span&gt; &lt;span class="s1"&gt;'Role.Arn'&lt;/span&gt; &lt;span class="nt"&gt;--output&lt;/span&gt; text&lt;span class="si"&gt;)&lt;/span&gt;
&lt;span class="c"&gt;# Create roles anywhere profile linking trust anchor to role&lt;/span&gt;
&lt;span class="nv"&gt;profile_arn&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;aws rolesanywhere create-profile &lt;span class="nt"&gt;--enabled&lt;/span&gt; &lt;span class="nt"&gt;--name&lt;/span&gt; demo-profile &lt;span class="nt"&gt;--role-arns&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$role_arn&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="nt"&gt;--query&lt;/span&gt; &lt;span class="s1"&gt;'profile.profileArn'&lt;/span&gt; &lt;span class="nt"&gt;--output&lt;/span&gt; text&lt;span class="si"&gt;)&lt;/span&gt;
&lt;span class="c"&gt;# Sleep to allow above changes to propogate&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Sleeping for 20 seconds"&lt;/span&gt;
&lt;span class="nb"&gt;sleep &lt;/span&gt;20
&lt;span class="c"&gt;# Generate credentials&lt;/span&gt;
&lt;span class="nv"&gt;credentials&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;./aws_signing_helper credential-process &lt;span class="se"&gt;\&lt;/span&gt;
    &lt;span class="nt"&gt;--certificate&lt;/span&gt; client_cert.pem &lt;span class="se"&gt;\&lt;/span&gt;
    &lt;span class="nt"&gt;--private-key&lt;/span&gt; client_private.key &lt;span class="se"&gt;\&lt;/span&gt;
    &lt;span class="nt"&gt;--trust-anchor-arn&lt;/span&gt; &lt;span class="nv"&gt;$trust_anchor_arn&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
    &lt;span class="nt"&gt;--profile-arn&lt;/span&gt; &lt;span class="nv"&gt;$profile_arn&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
    &lt;span class="nt"&gt;--role-arn&lt;/span&gt; &lt;span class="nv"&gt;$role_arn&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;
&lt;span class="nv"&gt;access_key_id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="nv"&gt;$credentials&lt;/span&gt; | jq &lt;span class="nt"&gt;-r&lt;/span&gt; &lt;span class="s2"&gt;".AccessKeyId"&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;
&lt;span class="nv"&gt;secret_access_key&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="nv"&gt;$credentials&lt;/span&gt; | jq &lt;span class="nt"&gt;-r&lt;/span&gt; &lt;span class="s2"&gt;".SecretAccessKey"&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;
&lt;span class="nv"&gt;session_token&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="nv"&gt;$credentials&lt;/span&gt; | jq &lt;span class="nt"&gt;-r&lt;/span&gt; &lt;span class="s2"&gt;".SessionToken"&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;
&lt;span class="c"&gt;# Verify credentials&lt;/span&gt;
&lt;span class="nv"&gt;AWS_ACCESS_KEY_ID&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nv"&gt;$access_key_id&lt;/span&gt; &lt;span class="nv"&gt;AWS_SECRET_ACCESS_KEY&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nv"&gt;$secret_access_key&lt;/span&gt; &lt;span class="nv"&gt;AWS_SESSION_TOKEN&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nv"&gt;$session_token&lt;/span&gt; aws sts get-caller-identity
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Cleanup created resources
&lt;/h3&gt;

&lt;p&gt;The following script is used for the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Remove all private keys, certificates and CSR files&lt;/li&gt;
&lt;li&gt;Delete the IAM Roles Anywhere Profile&lt;/li&gt;
&lt;li&gt;Delete the IAM Role&lt;/li&gt;
&lt;li&gt;Delete the IAM Roles Anywhere Trust Anchor
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Remove all private keys, certificates and CSR&lt;/span&gt;
&lt;span class="nb"&gt;rm&lt;/span&gt; &lt;span class="k"&gt;*&lt;/span&gt;.key
&lt;span class="nb"&gt;rm&lt;/span&gt; &lt;span class="k"&gt;*&lt;/span&gt;.pem
&lt;span class="nb"&gt;rm&lt;/span&gt; &lt;span class="k"&gt;*&lt;/span&gt;.csr
&lt;span class="c"&gt;# Get profile ID using name&lt;/span&gt;
&lt;span class="nv"&gt;profile_id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;aws rolesanywhere list-profiles &lt;span class="nt"&gt;--query&lt;/span&gt; &lt;span class="s1"&gt;'profiles[?name==`demo-profile`].profileId'&lt;/span&gt; &lt;span class="nt"&gt;--output&lt;/span&gt; text&lt;span class="si"&gt;)&lt;/span&gt;
&lt;span class="c"&gt;# Get trust anchor ID using name&lt;/span&gt;
&lt;span class="nv"&gt;trust_anchor_id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;aws rolesanywhere list-trust-anchors &lt;span class="nt"&gt;--query&lt;/span&gt; &lt;span class="s1"&gt;'trustAnchors[?name==`demo-trust-anchor`].trustAnchorId'&lt;/span&gt; &lt;span class="nt"&gt;--output&lt;/span&gt; text&lt;span class="si"&gt;)&lt;/span&gt;
&lt;span class="c"&gt;# Delete profile&lt;/span&gt;
aws rolesanywhere delete-profile &lt;span class="nt"&gt;--profile-id&lt;/span&gt; &lt;span class="nv"&gt;$profile_id&lt;/span&gt;
&lt;span class="c"&gt;# Delete role&lt;/span&gt;
aws iam delete-role &lt;span class="nt"&gt;--role-name&lt;/span&gt; demo-rolesanywhere
&lt;span class="c"&gt;# Delete trust anchor&lt;/span&gt;
aws rolesanywhere delete-trust-anchor &lt;span class="nt"&gt;--trust-anchor-id&lt;/span&gt; &lt;span class="nv"&gt;$trust_anchor_id&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Disclaimer
&lt;/h2&gt;

&lt;p&gt;While IAM Roles Anywhere does not use traditional long-lived AWS credentials like those associated with an IAM user, it still relies on the private key for your certificates remaining secret. It is important that your CA or client private keys are not compromised, otherwise, it is possible for an attacker to generate AWS credentials for your account.&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://en.wikipedia.org/wiki/Public-key_cryptography" rel="noopener noreferrer"&gt;https://en.wikipedia.org/wiki/Public-key_cryptography&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://en.wikipedia.org/wiki/Public_key_infrastructure" rel="noopener noreferrer"&gt;https://en.wikipedia.org/wiki/Public_key_infrastructure&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://en.wikipedia.org/wiki/X.509" rel="noopener noreferrer"&gt;https://en.wikipedia.org/wiki/X.509&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.aws.amazon.com/rolesanywhere/latest/userguide/public-key-infrastructure.html" rel="noopener noreferrer"&gt;https://docs.aws.amazon.com/rolesanywhere/latest/userguide/public-key-infrastructure.html&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.aws.amazon.com/rolesanywhere/latest/userguide/introduction.html" rel="noopener noreferrer"&gt;https://docs.aws.amazon.com/rolesanywhere/latest/userguide/introduction.html&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles.html" rel="noopener noreferrer"&gt;https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles.html&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://jimmydqv.com/iam-anywhere/" rel="noopener noreferrer"&gt;https://jimmydqv.com/iam-anywhere/&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>aws</category>
      <category>iam</category>
      <category>security</category>
    </item>
  </channel>
</rss>
