From 2b513f16dbd96e5218b10004d54ff70468dd4c5f Mon Sep 17 00:00:00 2001 From: Gene Wood Date: Tue, 29 Nov 2022 13:39:21 -0800 Subject: [PATCH] Add support for non-role/non-profile authentication This adds support for cases where you're using this tool either with an assumed role in a profile (which has a session token) or with [environment variables](https://docs.aws.amazon.com/sdkref/latest/guide/environment-variables.html) (instead of a role or profile) --- tools/launch-configuration-inventory/inventory.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/tools/launch-configuration-inventory/inventory.py b/tools/launch-configuration-inventory/inventory.py index e1d3d22..61e22c2 100644 --- a/tools/launch-configuration-inventory/inventory.py +++ b/tools/launch-configuration-inventory/inventory.py @@ -259,8 +259,17 @@ def main(): credentials = None if role_arn: credentials = get_credentials_for_role(role_arn, None) - else: + elif profile_name: credentials = get_credentials_for_profile(profile_name) + else: + session = boto3.Session() + session_credentials = session.get_credentials() + credentials = { + 'aws_access_key_id' : session_credentials.access_key, + 'aws_secret_access_key' : session_credentials.secret_key + } + if hasattr(session_credentials, 'token'): + credentials['aws_session_token'] = session_credentials.token if credentials is not None: @@ -332,4 +341,4 @@ def main(): return None if __name__ == "__main__": - main() \ No newline at end of file + main()