Lesson 5 of 5 AWS CI/CD Services

Managing Software Packages with AWS CodeArtifact

This lesson explains how AWS CodeArtifact provides a managed repository for software packages, including npm, PyPI, Maven, NuGet and generic packages.

Learning outcomes

After completing this lesson, you will be able to:

  • Describe how domains, repositories and upstream repositories organise packages.
  • Explain how package managers authenticate to CodeArtifact with temporary tokens.
  • Explain how CodeArtifact helps protect against supply chain attacks such as dependency confusion.

Domains, repositories and upstreams

CodeArtifact organises packages in two levels. A domain is the top-level container, usually one per organisation. It holds the storage for all package versions, so a package used by many repositories is stored only once, and it has a resource policy and an AWS KMS key that apply to everything in it. Repositories belong to a domain, and each one contains a set of packages that package managers such as npm, pip, Maven and NuGet can read from and publish to.

A repository can have upstream repositories. When a package manager requests a package that the repository does not contain, CodeArtifact looks in its upstream repositories in order. A repository can also have an external connection to a public registry, such as npmjs, PyPI or Maven Central. The first time a public package is requested, CodeArtifact fetches it through the connection and keeps a copy, so later builds use the cached copy even if the public registry is unavailable or the package is removed from it.

A typical design has one repository with an external connection to the public registry, and a team repository that uses it as an upstream and also holds the team's own published libraries. Developers and builds point their package manager at the team repository and receive both internal and public packages from one place. Repositories can be shared with other accounts through the domain and repository policies.

Authentication and permissions

Package managers authenticate to CodeArtifact with a temporary authorisation token. The command aws codeartifact login obtains a token and configures pip, npm, twine or other tools to use the repository with it. Tokens last up to 12 hours, so build projects log in at the start of each build rather than storing a token. Obtaining a token requires two permissions, which the following table lists together with the permissions to read and publish packages.

TaskPermissions needed
Get a tokencodeartifact:GetAuthorizationToken and sts:GetServiceBearerToken
Read packagescodeartifact:ReadFromRepository
Publish packagescodeartifact:PublishPackageVersion

The second permission for getting a token, sts:GetServiceBearerToken, is easily forgotten; a login that fails with AccessDenied even though the CodeArtifact permissions are correct usually lacks it. When a build should not change the package manager's global configuration, it can fetch the token into an environment variable instead, as the following example shows.

aws codeartifact login --tool pip --domain acme --domain-owner 111122223333 --repository python-internal
pip install acme-billing-client==2.4.0

# In CodeBuild, without changing pip's global config:
export CODEARTIFACT_AUTH_TOKEN=$(aws codeartifact get-authorization-token --domain acme \
  --domain-owner 111122223333 --query authorizationToken --output text)

Supply chain security

Packages are a common route for attacks on software, known as supply chain attacks. In a dependency confusion attack, an attacker publishes a package to a public registry with the same name as a company's internal package and a higher version number, hoping that builds will fetch the public one. CodeArtifact's package origin controls prevent this by deciding, for each package, whether new versions may come from upstream public sources, from direct publishing, or both. Internal packages are set to allow only direct publishing, so a look-alike public version is never used.

CodeArtifact also emits Amazon EventBridge events when a new package version is published or copied into a repository. A rule on these events can start a vulnerability scan or a licence check, or notify the team that owns the package.

Worked example

Builds that break when npm is down

Builds fail whenever the public npm registry is slow, and the security team worries about malicious packages with the same names as internal ones. The developer routes all package traffic through CodeArtifact:

SetupEffect
A repository with an external connection to npmjsPublic packages are fetched once and cached
A team repository with that one as an upstreamOne endpoint for internal and public packages
Package origin controls on internal namesUpstream versions of internal names are blocked
aws codeartifact login --tool npm in the build's install phaseA fresh 12-hour token each build

Builds now succeed from the cache when npmjs is unavailable, and dependency substitution attacks against internal package names are blocked.

Check yourself

How does a build tool such as npm or pip authenticate to CodeArtifact?

Show answer

With a temporary authorisation token from aws codeartifact get-authorization-token, or aws codeartifact login, which fetches the token and configures the tool. The caller needs IAM permission on the domain and repository.

Exam Tips

Remember for the exam

  • A private repository for internal packages, with a cached proxy of public registries, points to CodeArtifact.
  • To configure npm or pip to use CodeArtifact, run aws codeartifact login.
  • A CodeArtifact login that fails with AccessDenied is often missing sts:GetServiceBearerToken.
  • To prevent dependency confusion attacks, use package origin controls.