Certopolis

AWS Certified DevOps Engineer – Professional

CodeArtifact authorization tokens and upstream repositories

A package manager reaches CodeArtifact with an authorization token from the GetAuthorizationToken API. The CodeArtifact User Guide says the caller needs both codeartifact:GetAuthorizationToken and sts:GetServiceBearerToken, and the token lasts 12 hours by default. Upstream repositories let one endpoint serve packages from several repositories, searched in the order listed.

How does a build get a token?

aws codeartifact login fetches a token and configures the package manager with the token and the repository endpoint in one step. It supports dotnet, npm, nuget, pip, swift and twine. For other package managers, call aws codeartifact get-authorization-token and put the token in a configuration file or an environment variable.

The IAM user or role needs both codeartifact:GetAuthorizationToken and sts:GetServiceBearerToken. Adding sts:GetServiceBearerToken to a CodeArtifact domain resource policy has no effect, so it must be allowed in the identity's own policy. Root users cannot call GetAuthorizationToken. When the domain belongs to another account, pass --domain-owner.

How long does a token last?

A token is valid for 12 hours by default. --duration-seconds accepts 900 to 43200, which is 15 minutes to 12 hours. The lifetime starts when login or get-authorization-token is called, and a new token must be fetched when it ends.

The token lifetime is independent of the role session that fetched it. A token fetched in a 15-minute assumed-role session is still valid for the full 12 hours. The exception is --duration-seconds 0, which is valid only when assuming a role and makes the token expire with the remaining session time.

How are upstream repositories searched?

A repository can list other CodeArtifact repositories as upstreams, so a client using one endpoint can get packages held in several repositories. Associating one needs the AssociateWithDownstreamRepository permission on the upstream repository. A repository can have up to 10 upstream repositories and only one external connection.

A request looks in the repository itself first, then in its upstreams in the order they were listed in create-repository or update-repository. The search stops at the first repository that has the requested version. If an upstream has its own upstreams, those are searched before the next upstream at the same level. describe-repository shows the order.

If an upstream has an external connection to a public repository such as npm, repositories downstream of it can pull public packages through it. list-package-versions looks only in the repository named and does not list versions held in its upstreams.

Where the exam catches people

  • Believing codeartifact:GetAuthorizationToken alone is enough: the caller also needs sts:GetServiceBearerToken.
  • Believing sts:GetServiceBearerToken can be granted in the domain policy: it has no effect there and must be in the IAM identity's policy.
  • Believing a token dies when the role session ends: it keeps its own lifetime, 12 hours by default, unless it was created with --duration-seconds 0.
  • Believing the order of upstreams does not matter: the first upstream in the list that has the version wins and the search stops.
  • Believing a repository can have several external connections: it can have one, and up to 10 upstream repositories.

Written from these sources

Try 3 questions on this

Pick an answer to see why each option is right or wrong.

  1. A DevOps team builds a Node.js service with an AWS CodeBuild project. The packages come from an AWS CodeArtifact repository in the same AWS account, and the buildspec runs aws codeartifact login --tool npm in the install phase. The project's service role allows codeartifact:GetAuthorizationToken on the domain and codeartifact:ReadFromRepository on the repository. Every build stops at the login command with an access denied error before npm runs. The builds must authenticate with the service role. What should a DevOps engineer do to meet these requirements?

    Right. In the game that’s Renown for your SDLC Automation building.

    Not that one: C is right. In the game your SDLC Automation building would lose a little Renown, and this question would come back later.

    Show the full explanation and doc quotes

    Adding sts:GetServiceBearerToken to the service role's IAM policy lets the same login command fetch a token and configure npm, so the install phase can then read from the repository with the permissions the role already holds.

    Written from docs.aws.amazon.com/codeartifact/latest/ug/tokens-authentication.html

  2. A company's self-hosted build runners get AWS credentials for each pipeline job through web identity federation with an IAM role. A new security baseline limits every federated session to 15 minutes, and the runners do not refresh credentials while a job runs. Release jobs run aws codeartifact login --tool pip once at the start, then spend up to three hours in separate test steps that install Python packages from an AWS CodeArtifact repository. Release jobs must keep installing packages for their full length under the new baseline. What should a DevOps engineer do to meet these requirements?

    Right. In the game that’s Renown for your SDLC Automation building.

    Not that one: D is right. In the game your SDLC Automation building would lose a little Renown, and this question would come back later.

    Show the full explanation and doc quotes

    The baseline shortens the federated session to 15 minutes, but the token pip received at the start of the job keeps working for 12 hours, which covers a three-hour release. Only the special value 0 would tie the token to the session, and that is exactly what this job must avoid.

    Written from docs.aws.amazon.com/codeartifact/latest/ug/tokens-authentication.html

  3. A platform team publishes shared npm libraries to an AWS CodeArtifact repository named platform-libs. Each product team has its own repository in the same domain, and today each team repository has one upstream repository, npm-store, which holds the external connection to npmjs. The platform team also keeps a patched fork of a public package, published under the same name and version as the public release. No team repository has fetched that package yet. Builds against a team repository must get the fork and every platform library, including future versions, through that one endpoint. What should a DevOps engineer do to meet these requirements?

    Right. In the game that’s Renown for your SDLC Automation building.

    Not that one: A is right. In the game your SDLC Automation building would lose a little Renown, and this question would come back later.

    Show the full explanation and doc quotes

    The order matters here because the fork and the public release share a name and version: CodeArtifact checks the upstreams in the order given to --upstreams and stops at the first one that has the version. Listing platform-libs before npm-store makes the fork the version every team build gets.

    Written from docs.aws.amazon.com/codeartifact/latest/ug/repos-upstream.html

Ask for something