{"id":22207,"date":"2015-07-01T12:31:00","date_gmt":"2015-07-01T07:01:00","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=22207"},"modified":"2015-07-01T16:50:05","modified_gmt":"2015-07-01T11:20:05","slug":"continuing-with-boto-list-iam-users-having-90-days-older-access-keys","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/continuing-with-boto-list-iam-users-having-90-days-older-access-keys\/","title":{"rendered":"Continuing with Boto: List IAM users having 90 days older Access keys"},"content":{"rendered":"<p>AWS recommends to rotate your IAM user&#8217;s Access keys periodically. Sometime we create access keys for IAM user and keep using it. We forget to rotate the keys after a period of time, which is not considered as a good practice.<\/p>\n<p>Recently, we <a href=\"http:\/\/www.tothenew.com\/success-stories\/sony-entertainment-television\">came across a use case<\/a> wherein we were supposed to rotate the access keys which were created 90 days ago. For this purpose, we needed all those access keys which were created before 90 days.<\/p>\n<p>We thought to come up with a python script using boto which describes:<\/p>\n<p>1) <strong><span style=\"font-weight: bold; text-align: justify; color: #ff9900;\">Access Key Creation Date:<\/span><\/strong> date on which access keys were created.<br \/>\n2) <strong><span style=\"font-weight: bold; text-align: justify; color: #ff9900;\">Username:<\/span><\/strong> user name associate with that keys.<\/p>\n<p>Note: You can refer our previous blog to understand <a href=\"http:\/\/www.tothenew.com\/blog\/getting-started-with-boto-python-interface-for-aws\/\"><strong><span style=\"font-weight: bold; text-align: justify; color: #ff9900;\"> How to use boto library? <\/span><\/strong><\/a><\/p>\n<p>[python]<\/p>\n<p>import datetime<br \/>\nimport dateutil<br \/>\nimport boto<br \/>\nfrom dateutil import parser<br \/>\nfrom boto import iam<\/p>\n<p>conn=iam.connect_to_region(&#8216;ap-southeast-1&#8217;)<br \/>\nusers=conn.get_all_users()<br \/>\ntimeLimit=datetime.datetime.now() &#8211; datetime.timedelta(days=90)<\/p>\n<p>print &quot;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-&quot;<br \/>\nprint &quot;Access Keys Created Date&quot; + &quot;\\t\\t&quot; + &quot;Username&quot;<br \/>\nprint &quot;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-&quot;<\/p>\n<p>for user in users.list_users_response.users:<\/p>\n<p>    accessKeys=conn.get_all_access_keys(user_name=user[&#8216;user_name&#8217;])<\/p>\n<p>    for keysCreatedDate in accessKeys.list_access_keys_response.list_access_keys_result.access_key_metadata:<\/p>\n<p>        if parser.parse(keysCreatedDate[&#8216;create_date&#8217;]).date() &lt;= timeLimit.date():<\/p>\n<p>            print(keysCreatedDate[&#8216;create_date&#8217;]) + &quot;\\t\\t&quot; + user[&#8216;user_name&#8217;]<\/p>\n<p>[\/python]<\/p>\n<p>&nbsp;<\/p>\n<p>You can download this scripts from our github profile <a href=\"https:\/\/github.com\/hiteshBhatia\/aws-boto-scripts\/\">AWS-Boto-Scripts.<\/a><\/p>\n<p>Leave a comment if you have any questions regarding this article.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>AWS recommends to rotate your IAM user&#8217;s Access keys periodically. Sometime we create access keys for IAM user and keep using it. We forget to rotate the keys after a period of time, which is not considered as a good practice. Recently, we came across a use case wherein we were supposed to rotate the [&hellip;]<\/p>\n","protected":false},"author":100,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":7},"categories":[1174,1],"tags":[1853,1611,1358],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/22207"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/100"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=22207"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/22207\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=22207"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=22207"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=22207"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}