<?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>DEV Community: Mauricio Klein</title>
    <description>The latest articles on DEV Community by Mauricio Klein (@mklein).</description>
    <link>https://dev.to/mklein</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%2F167165%2Fdeeb5973-fa8e-478b-b6ff-12a4283e52ea.jpeg</url>
      <title>DEV Community: Mauricio Klein</title>
      <link>https://dev.to/mklein</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mklein"/>
    <language>en</language>
    <item>
      <title>Replace your long-term keys by temporary credentials with AWS CLI &amp; Console</title>
      <dc:creator>Mauricio Klein</dc:creator>
      <pubDate>Tue, 09 Feb 2021 13:52:24 +0000</pubDate>
      <link>https://dev.to/mklein/replace-your-long-term-keys-by-temporary-credentials-with-aws-cli-console-2d56</link>
      <guid>https://dev.to/mklein/replace-your-long-term-keys-by-temporary-credentials-with-aws-cli-console-2d56</guid>
      <description>&lt;p&gt;E-commerce, serverless application, data lake, container service...&lt;/p&gt;

&lt;p&gt;Doesn't matter what kind of service you're creating. If you're building it on AWS, there's&lt;br&gt;
one thing you can't run away from: IAM.&lt;/p&gt;

&lt;p&gt;AWS IAM (&lt;em&gt;Identity &amp;amp; Access Management&lt;/em&gt;) is the gatekeeper of your entire AWS account. It says who can access your account and what this person/service can and cannot do.&lt;/p&gt;

&lt;p&gt;For applications running in the cloud, IAM roles are the recommended way to grant the necessary permissions your service requires. However, when the topic is accessing your AWS account from localhost using the AWS CLI, most users still rely on long-term credentials.&lt;/p&gt;

&lt;p&gt;While long-term credentials are convenient, they can put you in trouble if they get leaked. With enough permissions, anyone in possession of your keys can start services at your expense, steal sensitive data or even put your service down.&lt;/p&gt;

&lt;p&gt;In this post, I'll show you how to replace your long-term credentials by temporary ones, so you can enhance the security of your local setup and reduce the blast radius in case of a leak.&lt;/p&gt;

&lt;p&gt;By the end of this post, you will have a fully configured setup to acquire temporary credentials whenever needed and renew them when expired.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This article will focus on environments with few accounts (like personal accounts and startups). For large environments, &lt;a href="https://aws.amazon.com/organizations/"&gt;AWS Organizations&lt;/a&gt; and &lt;a href="https://aws.amazon.com/single-sign-on/"&gt;AWS SSO&lt;/a&gt; gives you a much more scalable and maintainable way to set up access via federation.&lt;/p&gt;
&lt;/blockquote&gt;


&lt;h2&gt;
  
  
  Requirements
&lt;/h2&gt;
&lt;h3&gt;
  
  
  Valid credentials
&lt;/h3&gt;

&lt;p&gt;This tutorial will assume you have valid credentials stored in the named profile &lt;code&gt;default&lt;/code&gt; and that these credentials have enough permissions to perform IAM related operations. &lt;code&gt;IAMFullAccess&lt;/code&gt; is a good candidate while following the tutorial (don't worry, you can remove this policy by the end of the tutorial).&lt;/p&gt;

&lt;p&gt;In case you don't have the credentials, &lt;a href="https://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_access-keys.html#Using_CreateAccessKey"&gt;follow the official docs to create them&lt;/a&gt;.&lt;/p&gt;
&lt;h3&gt;
  
  
  MFA
&lt;/h3&gt;

&lt;p&gt;MFA (&lt;em&gt;multi factor authentication&lt;/em&gt;) provides a second layer of authentication to your setup. In this tutorial, I'll use a virtual MFA device, which is basically an app running on my personal smartphone that generates OTPs (&lt;em&gt;one-time password&lt;/em&gt;). However, this tutorial should work just fine with any MFA device supported by AWS.&lt;/p&gt;

&lt;p&gt;To set up your virtual MFA device and link to your account, &lt;a href="https://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_mfa_enable_virtual.html"&gt;follow the official documentation here&lt;/a&gt;.&lt;/p&gt;


&lt;h2&gt;
  
  
  Setup
&lt;/h2&gt;

&lt;p&gt;Our solution will leverage &lt;a href="https://docs.aws.amazon.com/cli/latest/reference/sts/assume-role.html"&gt;&lt;code&gt;STS AssumeRole&lt;/code&gt;&lt;/a&gt; to request temporary credentials based on an IAM role. By calling &lt;code&gt;AssumeRole&lt;/code&gt;, AWS returns temporary credentials granting all the permissions assigned to the assumed role. Permissions aren't cumulative, so once you assume a role using &lt;em&gt;STS&lt;/em&gt;, you forfeit access to the original permissions attached to your IAM user.&lt;/p&gt;

&lt;p&gt;Additionally, to make the authentication process more secure, we will make sure the role can only be assumed if MFA authentication is presented during the operation.&lt;/p&gt;

&lt;p&gt;So, let's start by creating the IAM role with the permissions for the temporary credentials. For this role, we need to define the assume role policy:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;file://role-policy.json&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;


&lt;/blockquote&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;"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;"Principal"&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;"AWS"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"[you user ARN]"&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;"Action"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"sts:AssumeRole"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"Condition"&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;"Bool"&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;"aws:MultiFactorAuthPresent"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"true"&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;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;p&gt;The assume role policy above grants permission to your user to assume roles, but with the condition that MFA authentication is presented. It means that, if you try to assume a role using your long-term credentials directly (without MFA), it will be rejected. This grants an additional layer of security, since, if your long-term credentials get leaked, the attacker won't have the power to assume the role unless he/she gets access to your MFA device.&lt;/p&gt;

&lt;p&gt;Now, let's create the role:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Create the IAM role&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;aws iam create-role &lt;span class="nt"&gt;--role-name&lt;/span&gt; PowerUserRole &lt;span class="nt"&gt;--assume-role-policy-document&lt;/span&gt; file://role-policy.json

&lt;span class="c"&gt;# Attach the PowerUserAccess policy to the role&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;aws iam attach-role-policy &lt;span class="nt"&gt;--role-name&lt;/span&gt; PowerUserRole &lt;span class="nt"&gt;--policy-arn&lt;/span&gt; arn:aws:iam::aws:policy/PowerUserAccess
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We attached the AWS managed policy &lt;code&gt;PowerUserAccess&lt;/code&gt; to the role. This policy grants full access to your AWS account, except for actions in &lt;code&gt;IAM&lt;/code&gt;, &lt;code&gt;Organizations&lt;/code&gt; and &lt;code&gt;Account&lt;/code&gt;. In other words, the role has full access to the account but can't change permissions, add/remove users, etc.&lt;/p&gt;

&lt;p&gt;Next, let's allow your user to call &lt;em&gt;assume-role&lt;/em&gt; action on the &lt;em&gt;PowerUserRole&lt;/em&gt; role created above. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;file: assume-role-policy.json&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&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="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="s2"&gt;"sts:AssumeRole"&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="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="s2"&gt;"[PowerUserRole ROLE ARN]"&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;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Create the user perms policy&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;aws iam create-policy &lt;span class="nt"&gt;--policy-name&lt;/span&gt; AllowAssumeRolePolicy &lt;span class="nt"&gt;--policy-document&lt;/span&gt; file://assume-role-policy.json

&lt;span class="c"&gt;# Attach the policy to your user&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;aws iam attach-user-policy &lt;span class="nt"&gt;--user-name&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;your user name] &lt;span class="nt"&gt;--policy-arn&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;AllowAssumeRolePolicy POLICY ARN]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;From now on, your user has permissions to assume the &lt;em&gt;PowerUserRole&lt;/em&gt; role in your AWS account, but MFA must be presented during the &lt;em&gt;assume-role&lt;/em&gt; operation.&lt;/p&gt;

&lt;h2&gt;
  
  
  CLI access
&lt;/h2&gt;

&lt;p&gt;Let's set up our named profile (&lt;code&gt;mfa&lt;/code&gt;). This profile will instruct AWS CLI to assume the role &lt;code&gt;PowerUserRole&lt;/code&gt; whenever the profile is used. It will also inform the MFA device used for the multi-factor authentication. Additionally, we will inform the session duration desired for the temporary credentials.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Source profile is the named profile holding the long-term credentials used for&lt;/span&gt;
&lt;span class="c"&gt;# the "assume-role" operation&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;aws configure &lt;span class="nt"&gt;--profile&lt;/span&gt; mfa &lt;span class="nb"&gt;set &lt;/span&gt;source_profile default

&lt;span class="c"&gt;# The ARN of the role to be assumed&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;aws configure &lt;span class="nt"&gt;--profile&lt;/span&gt; mfa &lt;span class="nb"&gt;set &lt;/span&gt;role_arn &lt;span class="o"&gt;[&lt;/span&gt;PowerUserRole ROLE ARN]

&lt;span class="c"&gt;# The ARN of your MFA device&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;aws configure &lt;span class="nt"&gt;--profile&lt;/span&gt; mfa &lt;span class="nb"&gt;set &lt;/span&gt;mfa_serial &lt;span class="o"&gt;[&lt;/span&gt;MFA DEVICE ARN]

&lt;span class="c"&gt;# Set session duration for 1 hour (3600 seconds)&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;aws configure &lt;span class="nt"&gt;--profile&lt;/span&gt; mfa &lt;span class="nb"&gt;set &lt;/span&gt;duration_seconds 3600
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, you just need to use the profile as usual. Whenever the temporary credentials expires, AWS CLI will ask you for a new OTP and refresh your credentials. Easy and convenient 😊&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;aws s3 &lt;span class="nb"&gt;ls&lt;/span&gt; &lt;span class="nt"&gt;--profile&lt;/span&gt; mfa
&lt;span class="c"&gt;# Enter MFA code for arn:aws:iam::XXXXXXXXXXXX:mfa/myself&lt;/span&gt;

&lt;span class="c"&gt;# 2021-01-01 00:00:00 bucket-a&lt;/span&gt;
&lt;span class="c"&gt;# 2020-01-01 00:00:00 bucket-b&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Attention:&lt;/p&gt;

&lt;p&gt;For assumed role credentials, AWS CLI treats any session with expiration within 15 min as expired. So, as an example, if your session is still valid for 10 minutes, using it for issuing a new CLI command will make AWS CLI request new ones (and ask you for a new &lt;em&gt;OTP&lt;/em&gt;).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/aws/aws-cli/issues/5880#issuecomment-769243927"&gt;More information can be found here&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Console access
&lt;/h2&gt;

&lt;p&gt;You can use the very same setup to assume a role in the console instead of using your IAM user's permissions.&lt;/p&gt;

&lt;p&gt;To do so, log in on AWS console using your regular IAM user's credentials and go to &lt;code&gt;Switch Roles&lt;/code&gt; in the dropdown menu in the upper-right part of the screen.&lt;/p&gt;

&lt;p&gt;Now, you just need to fill in the information as follows:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;AccountId&lt;/code&gt;: your AWS account ID&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Role&lt;/code&gt;: the name of the role to assume&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Display Name&lt;/code&gt;: (optional) a custom display name to present on the dropdown menu (and assume-role history).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--zR4irISl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://user-images.githubusercontent.com/11538662/107290201-6cd46780-6a66-11eb-81d0-c045b5e988e5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zR4irISl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://user-images.githubusercontent.com/11538662/107290201-6cd46780-6a66-11eb-81d0-c045b5e988e5.png" alt="" width="880" height="356"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Assuming a role on AWS console&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;By clicking on &lt;code&gt;Switch Role&lt;/code&gt; you will be redirected back to console, but now your permissions are defined by the assumed role.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3n1NzQdR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://user-images.githubusercontent.com/11538662/107290205-6e059480-6a66-11eb-87f7-b68c185a8717.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3n1NzQdR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://user-images.githubusercontent.com/11538662/107290205-6e059480-6a66-11eb-87f7-b68c185a8717.png" alt="" width="243" height="154"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Assumed IAM role on AWS console&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;In case you wanna go back to your IAM user, just go to the dropdown menu and click on &lt;code&gt;Back to [your user name]&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Once a role is assumed the first time, it remains in the dropdown assume-role history. So, switching to the role next time is one click away.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Session duration for an assumed role on the console is given by the maximum session duration defined in the role or the remaining session duration for the IAM user session, whichever is less (&lt;a href="https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_use_switch-role-console.html"&gt;more information here&lt;/a&gt;).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_use_switch-role-console.html"&gt;A full documentation about how to assume role on console can be found here&lt;/a&gt;.&lt;/p&gt;
&lt;h3&gt;
  
  
  Session duration
&lt;/h3&gt;

&lt;p&gt;IAM roles are created by default with the maximum session duration set to one hour. It means that, if you set &lt;code&gt;duration-seconds&lt;/code&gt; parameter on your named profile for anything longer than one hour, &lt;em&gt;assume-role&lt;/em&gt; will fail. If you want to allow longer sessions, you can append the parameter &lt;code&gt;max-session-duration&lt;/code&gt; to the &lt;code&gt;create-role&lt;/code&gt; command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Create the IAM role "PowerUserRole" with maximum session duration of 4 hours (i.e. 14400 seconds) &lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;aws iam create-role &lt;span class="nt"&gt;--role-name&lt;/span&gt; PowerUserRole &lt;span class="nt"&gt;--assume-role-policy-document&lt;/span&gt; file://role-policy.json &lt;span class="nt"&gt;--max-session-duration&lt;/span&gt; 14400

&lt;span class="c"&gt;# ... or update the maximum session duration of the existing "PowerUserRole" role&lt;/span&gt;
&lt;span class="nv"&gt;$ $ &lt;/span&gt;aws iam update-role &lt;span class="nt"&gt;--role-name&lt;/span&gt; PowerUserRole &lt;span class="nt"&gt;--max-session-duration&lt;/span&gt; 14400
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Maximum session duration is provided in seconds and accepts any value between 1 hour and 12 hours.&lt;/p&gt;

&lt;h2&gt;
  
  
  Admin User
&lt;/h2&gt;

&lt;p&gt;Now that you have an IAM role granting you permissions via temporary credentials, you can remove any permissions assigned directly to your IAM user (except the &lt;code&gt;AllowAssumeRolePolicy&lt;/code&gt; policy). This will guarantee that your long-term credentials can be used only to request the temporary ones. If leaked, attackers won't have much power unless they get access to your MFA device too.&lt;/p&gt;

&lt;p&gt;However, our role has the &lt;code&gt;PowerUserAccess&lt;/code&gt; policy attached, which means it can't change anything IAM related. How do you manage to adjust your setup in the future?&lt;/p&gt;

&lt;p&gt;The recommended way is setting up an admin IAM user, which Administrator access. This user will be used solely for administrative tasks (such as adjusting your IAM roles and permissions) and shouldn't be used for your day-to-day operations.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://docs.aws.amazon.com/IAM/latest/UserGuide/getting-started_create-admin-group.html"&gt;Instructions on how to create a new admin user can be found here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Finally, if you decide to use your admin user credentials via CLI (and, thus, need to generate long-term credentials), it might be a good idea to deactivate your credentials when not in use. This will require you to log in to the console in order to re-activate the credentials, but guarantee that your credentials are only valid while you're actually using them.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In this article, we saw how easy is to retire your long-term AWS keys and leverage temporary credentials.&lt;/p&gt;

&lt;p&gt;Temporary credentials enhance the security of your account. First, because in a situation of credentials leakage, the attacker has a limited amount of time to do any damage. Second, because MFA adds an extra layer of security, making it harder for attackers to hijack your account.&lt;/p&gt;

&lt;p&gt;Also, IAM roles give you more control about what permissions are in effect for a specific session. By creating fine-grained IAM roles, you can create dedicated named profiles in your localhost with the same setup presented in this article and use them according to your need. This approach provides a more secure setup and complies with the AWS concept of least privilege.&lt;/p&gt;

</description>
      <category>aws</category>
      <category>security</category>
      <category>iam</category>
    </item>
    <item>
      <title>Automating S3 bucket compliance check &amp; remediation with AWS Config</title>
      <dc:creator>Mauricio Klein</dc:creator>
      <pubDate>Thu, 07 Jan 2021 12:16:27 +0000</pubDate>
      <link>https://dev.to/mklein/automating-s3-bucket-compliance-check-remediation-with-aws-config-59kh</link>
      <guid>https://dev.to/mklein/automating-s3-bucket-compliance-check-remediation-with-aws-config-59kh</guid>
      <description>&lt;p&gt;In the last post, we explored the most common S3 security breaches and how to remediate them (&lt;a href="https://mklein.io/2020/12/17/protect-against-s3-breaches/"&gt;if you missed it, you can read it here&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;Better than knowing how to protect your data is to automate the entire process, so whenever &lt;br&gt;
a similar situation arises, you can rest assured that the problem will be automatically fixed.&lt;/p&gt;

&lt;p&gt;Today, I'll present how to automate the monitoring and remediation for the four &lt;br&gt;
recommended bucket configurations mentioned in the last post:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Bucket versioning&lt;/li&gt;
&lt;li&gt;Block public read/write access&lt;/li&gt;
&lt;li&gt;Bucket logging&lt;/li&gt;
&lt;li&gt;Server-side encryption&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By the end of this post, you will know how to automate the compliance check of your S3 buckets and automatically &lt;br&gt;
remediate them back to safety.&lt;/p&gt;

&lt;h2&gt;
  
  
  AWS Config
&lt;/h2&gt;

&lt;p&gt;The first step to automate your S3 bucket configuration is monitoring. We need to have a &lt;br&gt;
way to continuously monitor the state of our buckets and compare them &lt;br&gt;
against our defined rules.&lt;/p&gt;

&lt;p&gt;AWS provides a service exactly for this purpose: &lt;a href="https://aws.amazon.com/config/"&gt;AWS Config&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;AWS Config allows us to monitor and audit not only S3 buckets, but many other resource types. Resources&lt;br&gt;
are monitored by a recorder, that checks their states periodically and compares them against our &lt;br&gt;
defined rules. These rules inform AWS Config about the desired state of the resource and, in case it diverges, &lt;br&gt;
the resource is reported as &lt;code&gt;non-compliant&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;AWS Config provides a set of managed rules that can be used to monitor common scenarios. For example, the managed rule &lt;br&gt;
&lt;code&gt;s3-bucket-server-side-encryption-enabled&lt;/code&gt; can be used to verify if SSE (&lt;em&gt;server-side encryption&lt;/em&gt;) is enabled in a S3 &lt;br&gt;
bucket. &lt;a href="https://docs.aws.amazon.com/config/latest/developerguide/managed-rules-by-aws-config.html"&gt;Here's the full list of managed rules provided by AWS Config&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Whenever a non-complying resource is discovered, AWS Config can execute an action in response to the &lt;br&gt;
event. Among the supported actions, the one that we will use in today's experiment is the &lt;em&gt;auto-remediation&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--2yhAhJnb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://d1.awsstatic.com/Products/product-name/diagrams/product-page-diagram-Config_how-it-works.bd28728a9066c55d7ee69c0a655109001462e25b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--2yhAhJnb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://d1.awsstatic.com/Products/product-name/diagrams/product-page-diagram-Config_how-it-works.bd28728a9066c55d7ee69c0a655109001462e25b.png" alt="" width="851" height="401"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;AWS Config diagram (&lt;a href="https://d1.awsstatic.com/Products/product-name/diagrams/product-page-diagram-Config_how-it-works.bd28728a9066c55d7ee69c0a655109001462e25b.png"&gt;source: AWS&lt;/a&gt;)&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  AWS System Manager (SSM)
&lt;/h2&gt;

&lt;p&gt;AWS Config itself doesn't know how to remediate a resource. This task is delegated to &lt;a href="https://aws.amazon.com/systems-manager/"&gt;AWS System Manager&lt;/a&gt;.&lt;br&gt;
It provides a set of runbooks that can be executed to modify a resource. For example, the runbook &lt;code&gt;AWS-EnableS3BucketEncryption&lt;/code&gt; can be executed to enable&lt;br&gt;
SSE in an S3 bucket. &lt;a href="https://docs.aws.amazon.com/systems-manager/latest/userguide/automation-documents-reference-details.html"&gt;A list of all SSM managed runbooks can be found here&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Hands-on
&lt;/h2&gt;

&lt;p&gt;Today, we will focus on the CloudFormation template to automate the entire process.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;&lt;a href="https://mklein.io/2021/01/06/bucket-compliance-and-remediation-with-aws-config/#hands-on"&gt;[Continue reading on mklein.io]&lt;/a&gt;&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>aws</category>
      <category>s3</category>
      <category>security</category>
      <category>databreach</category>
    </item>
    <item>
      <title>Protect your S3 buckets from breaches</title>
      <dc:creator>Mauricio Klein</dc:creator>
      <pubDate>Thu, 17 Dec 2020 16:25:37 +0000</pubDate>
      <link>https://dev.to/mklein/protect-your-s3-buckets-from-breaches-89a</link>
      <guid>https://dev.to/mklein/protect-your-s3-buckets-from-breaches-89a</guid>
      <description>&lt;p&gt;If you have been following the latest news on AWS world, headlines like these aren't uncommon:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;em&gt;"Company A exposes customers data after major S3 bucket breach"&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;"Company B leaks the data of over 30.000 partners"&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;"Company C exposes 2M users on misconfigured AWS storage"&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Breaches related to S3 buckets are more common than it should be and, most of the time, puts companies in a bad situation, exposing their private information or, even worse, their customer's.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://twitter.com/QuinnyPig"&gt;Corey Quinn&lt;/a&gt;, cloud economist at The Duckbill Group and active member of the community, even created the &lt;em&gt;S3 Bucket Negligence Award&lt;/em&gt;, to "reward" individuals or companies that made it to the news after a major data breach.&lt;/p&gt;


&lt;blockquote class="ltag__twitter-tweet"&gt;

  &lt;div class="ltag__twitter-tweet__main"&gt;
    &lt;div class="ltag__twitter-tweet__header"&gt;
      &lt;img class="ltag__twitter-tweet__profile-image" src="https://res.cloudinary.com/practicaldev/image/fetch/s--gEXrJZmK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://pbs.twimg.com/profile_images/1247254348498743300/vTTJS5wW_normal.jpg" alt="Corey Quinn profile image"&gt;
      &lt;div class="ltag__twitter-tweet__full-name"&gt;
        Corey Quinn
      &lt;/div&gt;
      &lt;div class="ltag__twitter-tweet__username"&gt;
        @quinnypig
      &lt;/div&gt;
      &lt;div class="ltag__twitter-tweet__twitter-logo"&gt;
        &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ir1kO05j--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev.to/assets/twitter-f95605061196010f91e64806688390eb1a4dbc9e913682e043eb8b1e06ca484f.svg" alt="twitter logo"&gt;
      &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="ltag__twitter-tweet__body"&gt;
      This week's S3 Bucket Negligence Award goes to Facebook!&lt;br&gt;&lt;br&gt;"Oh, it was one of their partners--" Stop talking immediately. They were the stewards of the data. They shared it with their partner. It is their responsibility, full stop. &lt;br&gt;&lt;br&gt;&lt;a href="https://t.co/mYqYA2pAnP"&gt;upguard.com/breaches/faceb…&lt;/a&gt;
    &lt;/div&gt;
    &lt;div class="ltag__twitter-tweet__date"&gt;
      19:25 PM - 03 Apr 2019
    &lt;/div&gt;


    &lt;div class="ltag__twitter-tweet__actions"&gt;
      &lt;a href="https://twitter.com/intent/tweet?in_reply_to=1113522979772674048" class="ltag__twitter-tweet__actions__button"&gt;
        &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--fFnoeFxk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev.to/assets/twitter-reply-action-238fe0a37991706a6880ed13941c3efd6b371e4aefe288fe8e0db85250708bc4.svg" alt="Twitter reply action"&gt;
      &lt;/a&gt;
      &lt;a href="https://twitter.com/intent/retweet?tweet_id=1113522979772674048" class="ltag__twitter-tweet__actions__button"&gt;
        &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--k6dcrOn8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev.to/assets/twitter-retweet-action-632c83532a4e7de573c5c08dbb090ee18b348b13e2793175fea914827bc42046.svg" alt="Twitter retweet action"&gt;
      &lt;/a&gt;
      &lt;a href="https://twitter.com/intent/like?tweet_id=1113522979772674048" class="ltag__twitter-tweet__actions__button"&gt;
        &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--SRQc9lOp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev.to/assets/twitter-like-action-1ea89f4b87c7d37465b0eb78d51fcb7fe6c03a089805d7ea014ba71365be5171.svg" alt="Twitter like action"&gt;
      &lt;/a&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/blockquote&gt;


&lt;p&gt;But, what all these cases have in common? They all happened due to the lack of proper security applied to bucket setup.&lt;/p&gt;

&lt;p&gt;In this post, I will present the most common causes of S3 breaches and how to protect yourself against them.&lt;/p&gt;

&lt;p&gt;By the end of this post, you will have a better knowledge about the tooling available and how to apply them to protect your data and don't become the next victim.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;p&gt;Familiarity with AWS and S3 is required to take the maximum out of this post.&lt;/p&gt;

&lt;h2&gt;
  
  
  Breach #1: the public bucket
&lt;/h2&gt;

&lt;p&gt;This is by far the most common breach involving S3.&lt;/p&gt;

&lt;p&gt;S3 buckets are, by default, created with all public access denied. It means that, unless special privileges are granted, one can't access the data. When a bucket is made publicly accessible, you give up restricting access to your data.&lt;/p&gt;

&lt;p&gt;This has two major impacts: privacy and costs.&lt;/p&gt;

&lt;p&gt;By having a public bucket, you no longer have control over who can or can't access your data. While it can be OK for public files, this can be a big issue for sensitive information.&lt;/p&gt;

&lt;p&gt;Also, &lt;a href="https://aws.amazon.com/s3/pricing"&gt;as described on S3 pricing model&lt;/a&gt;, the costs associated with a bucket are determined not only by the amount of storage used but also by the number of requests and data transfer, among others. So, with no caching mechanism in place, all requests are served directly by S3, and this can make your monthly bill pretty salty.&lt;/p&gt;

&lt;h3&gt;
  
  
  How to fix it?
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;&lt;a href="https://mklein.io/2020/12/17/protect-against-s3-breaches/#how-to-fix-it"&gt;Continue reading on mklein.io&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

</description>
      <category>aws</category>
      <category>s3</category>
      <category>security</category>
    </item>
    <item>
      <title>Setting up recurring CloudWatch alarms</title>
      <dc:creator>Mauricio Klein</dc:creator>
      <pubDate>Tue, 08 Dec 2020 11:35:44 +0000</pubDate>
      <link>https://dev.to/mklein/setting-up-recurrent-cloudwatch-alarms-2c5b</link>
      <guid>https://dev.to/mklein/setting-up-recurrent-cloudwatch-alarms-2c5b</guid>
      <description>&lt;p&gt;In this post we will see how to turn a single-fired CloudWatch alarm into a recurring reminder. &lt;/p&gt;

&lt;p&gt;We will explore two different solutions using native AWS services, with detailed implementation and when you should use one or the other.&lt;/p&gt;

&lt;p&gt;🎁 BONUS: both implementations are available using Serverless Framework&lt;/p&gt;




&lt;p&gt;👉 &lt;a href="https://mklein.io/2020/12/08/recurrent-alarms/"&gt;CONTINUE READING HERE&lt;/a&gt; &lt;/p&gt;

</description>
      <category>aws</category>
      <category>cloudwatch</category>
      <category>reinvent</category>
    </item>
  </channel>
</rss>
