From 3f3085e8f93e329cd088729ff22928f46b12ea76 Mon Sep 17 00:00:00 2001 From: Renaud HAGER Date: Tue, 6 Mar 2018 12:13:34 +0000 Subject: [PATCH 1/2] Added a feature : Configuration can be set in a config file. Keymaker will fall back on a config file if there is no config token present in role description. --- README.rst | 11 +++++++++-- keymaker/__init__.py | 11 +++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/README.rst b/README.rst index d63fc5e..00a42f5 100644 --- a/README.rst +++ b/README.rst @@ -70,16 +70,23 @@ Cross-account authentication Some AWS security models put IAM users in one AWS account, and resources (EC2 instances, S3 buckets, etc.) in a family of other federated AWS accounts (for example, a dev account and a prod account). Users then assume roles in those federated accounts, -subject to their permissions, with `sts:AssumeRole `_. +subject to their permissions, with `sts:AssumeRole `_. When users connect via SSH to instances running in federated accounts, Keymaker can be instructed to look up the user identity and SSH public key in the other AWS account (called the "ID resolver" account). -Keymaker expects to find this configuration information by introspecting the instance's own IAM role description. The +Keymaker can find this configuration information into two different places : +- first by introspecting the instance's own IAM role description. The description is expected to contain a list of space-separated config tokens, for example, ``keymaker_id_resolver_account=123456789012 keymaker_id_resolver_role=id_resolver``. For ``sts:AssumeRole`` to work, the role ``id_resolver`` in account 123456789012 is expected to have a trust policy allowing the instance's IAM role to perform sts:AssumeRole on ``id_resolver``. +- then if role description does not contain any config token by reading a configuration file located here : `/etc/keymaker/keymaker.config`. Keymaker expect one config token by line. +``` +keymaker_id_resolver_account=123456789012 +keymaker_id_resolver_role=id_resolver +``` + Run the following command in the ID resolver account (that contains the IAM users) to apply this configuration automatically: ``keymaker configure --instance-iam-role arn:aws:iam::987654321098:role/ROLE_NAME --cross-account-profile AWS_CLI_PROFILE_NAME``. Here, 987654321098 is the account ID of the federated account where EC2 instances will run, and AWS_CLI_PROFILE_NAME diff --git a/keymaker/__init__.py b/keymaker/__init__.py index d801b3f..1e539e4 100644 --- a/keymaker/__init__.py +++ b/keymaker/__init__.py @@ -33,6 +33,7 @@ def __str__(self): ARN.__new__.__defaults__ = ("aws", "", "", "", "") iam_linux_group_prefix = "keymaker_" +config_file_path = "/etc/keymaker/keymaker.config" def parse_arn(arn): return ARN(*arn.split(":", 5)[1:]) @@ -107,6 +108,16 @@ def parse_keymaker_config(iam_role_description): for role_desc_word in re.split("[\s\,]+", iam_role_description or ""): if role_desc_word.startswith("keymaker_") and role_desc_word.count("=") == 1: config.update([shlex.split(role_desc_word)[0].split("=")]) + + if len(config) == 0 and os.path.isfile(config_file_path) : + try: + config_fd = open(config_file_path, 'r') + for line in config_fd: + if line.startswith("keymaker_") and line.count("=") == 1: + config.update([shlex.split(line)[0].split("=")]) + except Exception as e: + logger.warn(str(e)) + return config def get_assume_role_session(sts, role_arn): From 130c6c279ceb0fe56ada357776861f175ffc917a Mon Sep 17 00:00:00 2001 From: Renaud HAGER Date: Tue, 24 Apr 2018 15:39:51 +0100 Subject: [PATCH 2/2] - Config file format is now yaml. - Fixed a bug when Role description is empty. - Updated README.md. --- README.rst | 16 ++++-- keymaker/__init__.py | 117 ++++++++++++++++++++++++++++++++++++------- 2 files changed, 109 insertions(+), 24 deletions(-) diff --git a/README.rst b/README.rst index 00a42f5..ab89d6c 100644 --- a/README.rst +++ b/README.rst @@ -74,17 +74,23 @@ subject to their permissions, with `sts:AssumeRole