Authentication and access control for AWS CodeCommit - AWS CodeCommit

Authentication and access control for AWS CodeCommit

Access to AWS CodeCommit requires credentials. Those credentials must have permissions to access AWS resources, such as CodeCommit repositories, and your IAM user, which you use to manage your Git credentials or the SSH public key that you use for making Git connections. The following sections provide details on how you can use AWS Identity and Access Management (IAM) and CodeCommit to help secure access to your resources:

Authentication

Because CodeCommit repositories are Git-based and support the basic functionality of Git, including Git credentials, we recommend that you use an IAM user when working with CodeCommit. You can access CodeCommit with other identity types, but the other identity types are subject to limitations, as described below.

Identity types:

  • IAM user – An IAM user is an identity within your Amazon Web Services account that has specific custom permissions. For example, an IAM user can have permissions to create and manage Git credentials for accessing CodeCommit repositories. This is the recommended user type for working with CodeCommit. You can use an IAM user name and password to sign in to secure AWS webpages like the AWS Management Console, AWS Discussion Forums, or the AWS Support Center.

    You can generate Git credentials or associate SSH public keys with your IAM user, or you can install and configure git-remote-codecommit. These are the easiest ways to set up Git to work with your CodeCommit repositories. With Git credentials, you generate a static user name and password in IAM. You then use these credentials for HTTPS connections with Git and any third-party tool that supports Git user name and password authentication. With SSH connections, you create public and private key files on your local machine that Git and CodeCommit use for SSH authentication. You associate the public key with your IAM user, and you store the private key on your local machine. git-remote-codecommit extends Git itself, and does not require setting up Git credentials for the user.

    In addition, you can generate access keys for each user. Use access keys when you access AWS services programmatically, either through one of the AWS SDKs or by using the AWS Command Line Interface (AWS CLI). The SDK and CLI tools use the access keys to cryptographically sign your requests. If you don’t use the AWS tools, you must sign the requests yourself. CodeCommit supports Signature Version 4, a protocol for authenticating inbound API requests. For more information about authenticating requests, see Signature Version 4 Signing Process in the AWS General Reference.

  • Amazon Web Services account root user – When you sign up for AWS, you provide an email address and password that is associated with your Amazon Web Services account. These are your root credentials, and they provide complete access to all of your AWS resources. Some CodeCommit features are not available for root account users. In addition, the only way to use Git with your root account is to either install and configure git-remote-codecommit (recommended) or to configure the AWS credential helper, which is included with the AWS CLI. You cannot use Git credentials or SSH public-private key pairs with your root account user. For these reasons, we do not recommend using your root account user when interacting with CodeCommit.

    Important

    For security reasons, we recommend that you use the root credentials only to create an administrator user, which is an IAM user with full permissions to your AWS account. Then, you can use this administrator user to create other IAM users and roles with limited permissions. For more information, see IAM Best Practices and Creating an Admin User and Group in the IAM User Guide.

  • IAM Identity Center and users in IAM Identity Center – AWS IAM Identity Center expands the capabilities of AWS Identity and Access Management to provide a central place that brings together administration of users and their access to AWS accounts and cloud applications. While recommended as a best practice for most users working with AWS, IAM Identity Center does not currently provide mechanisms for Git credentials or SSH key pairs. These users can install and configure git-remote-codecommit to locally clone CodeCommit repositories, but not all integrated development environments (IDEs) support cloning, pushing, or pulling with git-remote-codecommit.

    As a best practice, require human users, including users that require administrator access, to use federation with an identity provider to access AWS services by using temporary credentials.

    A federated identity is a user from your enterprise user directory, a web identity provider, the AWS Directory Service, the Identity Center directory, or any user that accesses AWS services by using credentials provided through an identity source. When federated identities access AWS accounts, they assume roles, and the roles provide temporary credentials.

    For centralized access management, we recommend that you use AWS IAM Identity Center. You can create users and groups in IAM Identity Center, or you can connect and synchronize to a set of users and groups in your own identity source for use across all your AWS accounts and applications. For information about IAM Identity Center, see What is IAM Identity Center? in the AWS IAM Identity Center User Guide.

  • IAM role – Like an IAM user, an IAM role is an IAM identity that you can create in your account to grant specific permissions.

    An IAM role is an identity within your AWS account that has specific permissions. It is similar to an IAM user, but is not associated with a specific person. You can temporarily assume an IAM role in the AWS Management Console by switching roles. You can assume a role by calling an AWS CLI or AWS API operation or by using a custom URL. For more information about methods for using roles, see Using IAM roles in the IAM User Guide.

    IAM roles with temporary credentials are useful in the following situations:

    • Federated user access – To assign permissions to a federated identity, you create a role and define permissions for the role. When a federated identity authenticates, the identity is associated with the role and is granted the permissions that are defined by the role. For information about roles for federation, see Creating a role for a third-party Identity Provider in the IAM User Guide. If you use IAM Identity Center, you configure a permission set. To control what your identities can access after they authenticate, IAM Identity Center correlates the permission set to a role in IAM. For information about permissions sets, see Permission sets in the AWS IAM Identity Center User Guide.

    • Temporary IAM user permissions – An IAM user or role can assume an IAM role to temporarily take on different permissions for a specific task.

    • Cross-account access – You can use an IAM role to allow someone (a trusted principal) in a different account to access resources in your account. Roles are the primary way to grant cross-account access. However, with some AWS services, you can attach a policy directly to a resource (instead of using a role as a proxy). To learn the difference between roles and resource-based policies for cross-account access, see How IAM roles differ from resource-based policies in the IAM User Guide.

    • Cross-service access – Some AWS services use features in other AWS services. For example, when you make a call in a service, it's common for that service to run applications in Amazon EC2 or store objects in Amazon S3. A service might do this using the calling principal's permissions, using a service role, or using a service-linked role.

      • Forward access sessions (FAS) – When you use an IAM user or role to perform actions in AWS, you are considered a principal. When you use some services, you might perform an action that then initiates another action in a different service. FAS uses the permissions of the principal calling an AWS service, combined with the requesting AWS service to make requests to downstream services. FAS requests are only made when a service receives a request that requires interactions with other AWS services or resources to complete. In this case, you must have permissions to perform both actions. For policy details when making FAS requests, see Forward access sessions.

      • Service role – A service role is an IAM role that a service assumes to perform actions on your behalf. An IAM administrator can create, modify, and delete a service role from within IAM. For more information, see Creating a role to delegate permissions to an AWS service in the IAM User Guide.

      • Service-linked role – A service-linked role is a type of service role that is linked to an AWS service. The service can assume the role to perform an action on your behalf. Service-linked roles appear in your AWS account and are owned by the service. An IAM administrator can view, but not edit the permissions for service-linked roles.

    • Applications running on Amazon EC2 – You can use an IAM role to manage temporary credentials for applications that are running on an EC2 instance and making AWS CLI or AWS API requests. This is preferable to storing access keys within the EC2 instance. To assign an AWS role to an EC2 instance and make it available to all of its applications, you create an instance profile that is attached to the instance. An instance profile contains the role and enables programs that are running on the EC2 instance to get temporary credentials. For more information, see Using an IAM role to grant permissions to applications running on Amazon EC2 instances in the IAM User Guide.

    To learn whether to use IAM roles or IAM users, see When to create an IAM role (instead of a user) in the IAM User Guide.

    Note

    You cannot use Git credentials or SSH public-private key pairs with federated users. In addition, user preferences are not available for federated users. For information about how to set up connections using federated access, see Setup steps for HTTPS connections to AWS CodeCommit with git-remote-codecommit.

Access control

You can have valid credentials to authenticate your requests, but unless you have permissions you cannot create or access CodeCommit resources. For example, you must have permissions to view repositories, push code, create and manage Git credentials, and so on.

The following sections describe how to manage permissions for CodeCommit. We recommend that you read the overview first.

Overview of managing access permissions to your CodeCommit resources

Every AWS resource is owned by an Amazon Web Services account. Permissions to create or access a resource are governed by permissions policies. An account administrator can attach permissions policies to IAM identities (that is, users, groups, and roles). Some services, such as AWS Lambda, also support attaching permissions policies to resources.

Note

An account administrator (or administrator user) is a user with administrator privileges. For more information, see IAM Best Practices in the IAM User Guide.

When granting permissions, you decide who gets the permissions, the resources they get permissions for, and the specific actions that you want to allow on those resources.

CodeCommit resources and operations

In CodeCommit, the primary resource is a repository. Each resource has a unique Amazon Resource Names (ARN) associated with it. In a policy, you use an Amazon Resource Name (ARN) to identify the resource that the policy applies to. For more information about ARNs, see Amazon Resource Names (ARN) and AWS Service Namespaces in the Amazon Web Services General Reference. CodeCommit does not currently support other resource types, which are referred to as subresources.

The following table describes how to specify CodeCommit resources.

Resource Type ARN Format
Repository

arn:aws:codecommit:region:account-id:repository-name

All CodeCommit repositories

arn:aws:codecommit:*

All CodeCommit repositories owned by the specified account in the specified AWS Region

arn:aws:codecommit:region:account-id:*

Note

Most AWS services treat a colon (:) or a forward slash (/) in ARNs as the same character. However, CodeCommit requires an exact match in resource patterns and rules. When creating event patterns, be sure to use the correct ARN characters so that they match the ARN syntax in the resource.

For example, you can indicate a specific repository (MyDemoRepo) in your statement using its ARN as follows:

"Resource": "arn:aws:codecommit:us-west-2:111111111111:MyDemoRepo"

To specify all repositories that belong to a specific account, use the wildcard character (*) as follows:

"Resource": "arn:aws:codecommit:us-west-2:111111111111:*"

To specify all resources, or if a specific API action does not support ARNs, use the wildcard character (*) in the Resource element as follows:

"Resource": "*"

You can also use the wildcard character(*) to specify all resources that match part of a repository name. For example, the following ARN specifies any CodeCommit repository that begins with the name MyDemo and that is registered to the Amazon Web Services account 111111111111 in the us-east-2 AWS Region:

arn:aws:codecommit:us-east-2:111111111111:MyDemo*

For a list of available operations that work with the CodeCommit resources, see CodeCommit permissions reference.

Understanding resource ownership

The Amazon Web Services account owns the resources that are created in the account, regardless of who created them. Specifically, the resource owner is the Amazon Web Services account of the principal entity (that is, the root account, an IAM user, or an IAM role) that authenticates the resource creation request. The following examples illustrate how this works:

  • If you create an IAM user in your Amazon Web Services account and grant permissions to create CodeCommit resources to that user, the user can create CodeCommit resources. However, your Amazon Web Services account, to which the user belongs, owns the CodeCommit resources.

  • If you use the root account credentials of your Amazon Web Services account to create a rule, your Amazon Web Services account is the owner of the CodeCommit resource.

  • If you create an IAM role in your Amazon Web Services account with permissions to create CodeCommit resources, anyone who can assume the role can create CodeCommit resources. Your Amazon Web Services account, to which the role belongs, owns the CodeCommit resources.

Managing access to resources

To manage access to AWS resources, you use permissions policies. A permissions policy describes who has access to what. The following section explains the options for creating permissions policies.

Note

This section discusses using IAM in the context of CodeCommit. It doesn't provide detailed information about the IAM service. For more information about IAM, see What Is IAM? in the IAM User Guide. For information about IAM policy syntax and descriptions, see IAM Policy Reference in the IAM User Guide.

Permissions policies that are attached to an IAM identity are referred to as identity-based policies (IAM policies). Permissions policies that are attached to a resource are referred to as resource-based policies. Currently, CodeCommit supports only identity-based policies (IAM policies).

Identity-based policies (IAM policies)

To manage access to AWS resources, you attach permissions policies to IAM identities. In CodeCommit, you use identity-based policies to control access to repositories. For example, you can do the following:

  • Attach a permissions policy to a user or a group in your account – To grant a user permissions to view CodeCommit resources in the CodeCommit console, attach an identity-based permissions policy to a user or group that the user belongs to.

  • Attach a permissions policy to a role (to grant cross-account permissions) – Delegation, such as when you want to grant cross-account access, involves setting up a trust between the account that owns the resource (the trusting account), and the account that contains the users who need to access the resource (the trusted account). A permissions policy grants the user of a role the needed permissions to carry out the intended tasks on the resource. A trust policy specifies which trusted accounts are allowed to grant its users permissions to assume the role. For more information, see IAM Terms and Concepts.

    To grant cross-account permissions, attach an identity-based permissions policy to an IAM role. For example, the administrator in Account A can create a role to grant cross-account permissions to another Amazon Web Services account (for example, Account B) or an AWS service as follows:

    1. Account A administrator creates an IAM role and attaches a permissions policy to the role that grants permissions on resources in Account A.

    2. Account A administrator attaches a trust policy to the role identifying Account B as the principal who can assume the role.

    3. Account B administrator can then delegate permissions to assume the role to any users in Account B. Doing this allows users in Account B to create or access resources in Account A. If you want to grant an AWS service permission to assume the role, the principal in the trust policy can also be an AWS service principal. For more information, see Delegation in IAM Terms and Concepts.

    For more information about using IAM to delegate permissions, see Access Management in the IAM User Guide.

The following example policy allows a user to create a branch in a repository named MyDemoRepo:

{ "Version": "2012-10-17", "Statement" : [ { "Effect" : "Allow", "Action" : [ "codecommit:CreateBranch" ], "Resource" : "arn:aws:codecommit:us-east-2:111111111111:MyDemoRepo" } ] }

To restrict the calls and resources that users in your account have access to, create specific IAM policies, and then attach those policies to IAM users. For more information about how to create IAM roles and to explore example IAM policy statements for CodeCommit, see Customer managed identity policy examples.

Resource-based policies

Some services, such as Amazon S3, also support resource-based permissions policies. For example, you can attach a resource-based policy to an S3 bucket to manage access permissions to that bucket. CodeCommit doesn't support resource-based policies, but you can use tags to identify resources, which you can then use in IAM policies. For an example of a tag-based policy, see Identity-based policies (IAM policies).

Resource scoping in CodeCommit

In CodeCommit, you can scope identity-based policies and permissions to resources, as described in CodeCommit resources and operations. However, you cannot scope the ListRepositories permission to a resource. Instead, you must scope it to all resources (using the wildcard *). Otherwise, the action fails.

All other CodeCommit permissions can be scoped to resources.

Specifying policy elements: resources, actions, effects, and principals

You can create policies to allow or deny users access to resources, or allow or deny users to take specific actions on those resources. CodeCommit defines a set of public API operations that define how users work with the service, whether that is through the CodeCommit console, the SDKs, the AWS CLI, or by directly calling those APIs. To grant permissions for these API operations, CodeCommit defines a set of actions that you can specify in a policy.

Some API operations can require permissions for more than one action. For more information about resources and API operations, see CodeCommit resources and operations and CodeCommit permissions reference.

The following are the basic elements of a policy:

  • Resource – To identify the resource that the policy applies to, you use an Amazon Resource Name (ARN). For more information, see CodeCommit resources and operations.

  • Action – To identify resource operations that you want to allow or deny, you use action keywords. For example, depending on the specified Effect, the codecommit:GetBranch permission either allows or denies the user to perform the GetBranch operation, which gets details about a branch in a CodeCommit repository.

  • Effect – You specify the effect, either allow or deny, that takes place when the user requests the specific action. If you don't explicitly grant access to (allow) a resource, access is implicitly denied. You can also explicitly deny access to a resource to make sure that a user cannot access it, even if a different policy grants access.

  • Principal – In identity-based policies (IAM policies), the only type of policies that CodeCommit supports, the user that the policy is attached to is the implicit principal.

To learn more about IAM policy syntax, see IAM Policy Reference in the IAM User Guide.

For a table showing all of the CodeCommit API actions and the resources that they apply to, see CodeCommit permissions reference.

Specifying conditions in a policy

When you grant permissions, you use the access policy language for IAM to specify the conditions under which a policy should take effect. For example, you might want a policy to be applied only after a specific date. For more information about specifying conditions in a policy language, see Condition and Policy Grammar in the IAM User Guide.

To express conditions, you use predefined condition keys. There are no condition keys specific to CodeCommit. However, there are AWS-wide condition keys that you can use as appropriate. For a complete list of AWS-wide keys, see Available Keys for Conditions in the IAM User Guide.