Package: ec2userkeyd
ec2userkeydAutomatically provide EC2 users with personalized credentials via a
local daemon intercepting requests to the EC2 metadata service.MotivationCurrently accepted security best practice for AWS EC2 instances is to
grant theminstance IAM roles, which allows applications on those
instances to use AWS services without needing to hard-code IAM user
secret access keys. Unfortunately, this carries with it the
implication that all users on a given EC2 instance ought to have the
same permissions to AWS APIs, which is not always the case. For
example, a shared analytics instance may have multiple users who each
need to have access to different S3 buckets. Therefore, there is a
need, in certain circumstances, to grant different IAM credentials to
different users local to an instance.The most obvious solution to this problem, currently, is to revert
back to the former practice of embedding IAM user secret access keys
on the instance. However, this means manually managing a fleet of
secret access keys and protecting those keys with file permissions.
Furthermore, the keys are likely not to be rotated on a defined
schedule, increasing the risk of compromise through the leak of a
long-lived credential.This application provides another potential solution. By using NAT viaiptables, it intercepts HTTP requests destined to the EC2 metadata
service and responds with short-lived credentials that are specific to
the originating process's user ID. Multiple methods of translating
from UNIX usernames to AWS credentials are supported, depending on
your AWS IAM typical practices.Credential MethodsThis daemon supports multiple different methods of translating from
UNIX users to AWS credentials. You can chain these methods, so that a
failure to retrieve credentials via the first method will fall back to
the second method in the list, and so on. You can also have different
chains for individual users or ranges of UIDs.The currently supported methods are:UserRoleThis method tries to AssumeRole to an IAM role that matches a pattern,
which defaults touser-{username}. This should be used if you can
easily maintain roles that correspond to your users, or if you are
trying to enable reduced privileges for a local service account.The only instance role permissions required for this method arests:AssumeRole. Additionally, you should ensure that the role to be
assumed has an Assume Role Policy Document that grants access to the
instance role. The following policy grants access to a specific
instance role:{
    "Version": "2012-10-17",
    "Statement": [{
        "Action": "sts:AssumeRole",
        "Principal": "arn:aws:iam::123456789012:role/my-instance-role",
        "Effect": "Allow"
    }]
}Changing the Principal above toarn:aws:iam::123456789012:role/*would allow any role in the account withsts:AssumeRoleprivileges
to use the role.CreateUserRoleThis method behaves similarly to UserRole, except that it
automatically maintains user roles that parallel IAM users. It is able
to synchronize the policies attached to user roles with those defined
on the IAM users, and it can create new roles if necessary.This method requires the following permissions to be granted to the
instance role:sts:AssumeRolests:GetCallerIdentityiam:CreateRoleiam:List*iam:GetPolicyiam:GetPolicyVersioniam:GetRolePolicyiam:GetGroupPolicyiam:AttachRolePolicyiam:PutRolePolicyIf the config optioninstance_role_linkedinsidemethod_CreateUserRoleis set to True, then only the role that
creates the user role can access it; otherwise, any role in the
account can access the created user role.RestrictedInstanceRoleThis method attempts to AssumeRole to the current instance role, while
passing a set of additional restrictive policies that are derived from
the policies discovered from an IAM user. The resulting permissions
will be no greater than those given to the instance role. This method
is most suitable for users with small attached policies, since the
Policy parameter to AssumeRole is limited to 2KB, or smaller,
depending on the policy's packed size.Because of the tight policy size restrictions with this method, there
is an optional compression feature available that tries to reduce the
size of the policy parameter by eliminating unneeded statements, such
as overlapping Deny statements. This is controlled by thecompress_user_policyparameter withinmethod_RestrictedInstanceRole.This method requires the following permissions to be granted to the
instance role:sts:AssumeRolests:GetCallerIdentityiam:GetPolicyiam:GetPolicyVersioniam:GetUserPolicyiam:GetGroupPolicyiam:ListUserPoliciesiam:ListGroupsForUseriam:ListGroupPoliciesiam:ListAttachedUserPoliciesiam:ListAttachedGroupPoliciesIfcompress_user_policyis enabled, then additional permissions are
required:iam:GetRolePolicyiam:ListRolePoliciesiam:ListAttachedRolePoliciesInstanceRoleThis method passes through the instance role privileges, with optional
policy restrictions. It can be used, for example, as the last method
in a chain so that users (typically local service accounts) that don't
match earlier methods can get privileges as defined by the instance
role.It is important to note that if this method is used as a fallback for
one of the preceding role-based methods, it may be possible for a user
to use instance role privileges to call AssumeRole, and therefore gain
access to another user's privileges. To avoid this, the method has an
option (default enabled) to deny AssumeRole access by generating new
credentials with a small attached policy.This additional restriction requires the following permissions to be
granted to the instance role:sts:AssumeRolests:GetCallerIdentityThe instance role should also have an Assume Role Policy Document that
grants access to itself; see the discussion in UserRole for details.Getting StartedRequirementsThis app requires Python 3.6 or greater to run, which should be
available in your distribution's package repository.On Amazon Linux:$ sudo yum -y install python3 python3-pipOnly Linux is supported at this time.InstallationInstall from PyPI viapip:$ sudo pip3 install ec2userkeydConfigurationThe application configuration defaults are shown inec2userkeyd/config.py. These settings can be overridden by creating
a config file (default location:/etc/ec2userkeyd.conf) in INI-style
format.StartupTo test run the app, call it with thedaemonargument.$ sudo /usr/local/bin/ec2userkeyd daemonIt will run in the foreground until killed.To autostart, refer to your distribution's init system.