Lesson 1 of 5 Artifact and Image Repositories

Managing Software Packages with AWS CodeArtifact

Modern applications depend on hundreds of open-source and internal packages. AWS CodeArtifact is a managed artifact repository that stores, caches and shares these packages for package managers such as npm, pip, Maven and NuGet. This lesson explains how CodeArtifact is organised into domains and repositories, how upstream repositories and external connections work, how build tools authenticate and how packages are promoted and protected.

Learning outcomes

After completing this lesson, you will be able to:

  • Describe the CodeArtifact resource hierarchy of domains, repositories, packages and package versions.
  • Explain how upstream repositories and external connections resolve and cache public packages.
  • Explain how build tools authenticate to CodeArtifact with authorisation tokens.
  • Describe package origin controls, package version status and promotion between repositories.

Why Use a Managed Package Repository

Builds that download dependencies directly from public registries such as npmjs.com, PyPI and Maven Central have several weaknesses. A public registry outage stops all builds. A package version can be removed or, worse, replaced by a malicious one. There is no record of which external packages the organisation uses, and internal packages need somewhere private to be published. A managed repository sits between builds and public registries, caches every package version that is used, hosts internal packages and applies access control and auditing. CodeArtifact provides this as a managed service, with no servers or storage to operate, and charges for storage, requests and data transfer.

CodeArtifact supports the npm, PyPI, Maven (including Gradle), NuGet, Swift, Ruby, Cargo and generic package formats. Generic packages store arbitrary files, such as compiled binaries or archives, with a namespace, name and version.

Domains, Repositories and Packages

CodeArtifact organises content in a hierarchy, and understanding each level explains where policies and encryption apply.

Domain
The top-level container, normally one per organisation. All package assets in a domain are stored once and deduplicated across repositories, and they are encrypted with the domain's KMS key, which is chosen when the domain is created and cannot be changed. A domain can contain repositories owned by several accounts, and a domain policy controls which accounts may create repositories in it.
Repository
A collection of package versions that clients connect to through format-specific endpoints. One repository can serve several formats. Repository policies control who can read from or publish to it.
Package
A named package in a format and optional namespace, such as the npm package @acme/billing or the Maven group and artifact com.acme:billing.
Package version
A specific version with its assets, such as 2.4.1. Versions have a status, described later in this lesson.

A common structure has a public-cache repository with external connections to public registries, a shared repository for internal packages that uses public-cache as its upstream, and team repositories that use shared as their upstream. Builds connect only to their team repository and see internal and public packages through it.

Upstream Repositories and External Connections

An upstream repository is another repository in the same domain that CodeArtifact searches when a requested package version is not in the repository the client is using. A repository can have up to ten upstream repositories, searched in the configured order, and upstreams can have their own upstreams. When a version is found in an upstream, it is made available in the downstream repository, so later requests are served without searching again.

An external connection links a repository to a public registry, such as npmjs, PyPI, Maven Central, NuGet Gallery or crates.io. A repository can have only one external connection, which is why the usual design dedicates one cache repository per public registry or combines formats in a single cache repository. When a client requests a package that is not yet cached, CodeArtifact fetches it from the public registry and retains a copy, so the version remains available even if it is later removed from the public registry and builds continue during public registry outages.

The following diagram shows the order in which repositories are searched.

How CodeArtifact resolves a package. A build tool asks its repository for a package version. If the version is not there, CodeArtifact searches the repository's upstream repositories in order, up to ten. A cache repository with an external connection fetches the version from the public registry and retains a copy, so it stays available if the public registry removes it or is unavailable. Each repository can have only one external connection.
A cache repository keeps every public version the organisation uses.

Caching public packages

When a scenario requires that builds keep working if a public registry is unavailable, or that the organisation keeps copies of every open-source package it uses, the answer is CodeArtifact with an external connection, usually through a dedicated upstream cache repository.

True or false

CodeArtifact repositories

Exam skills 1.3.1

Check four facts about CodeArtifact upstreams, connections and promotion.

Log in to try this activity Interactive activities are free with an account.

Authenticating Build Tools

Package managers authenticate to CodeArtifact with a short-lived authorisation token obtained from the GetAuthorizationToken API. The token is valid for 12 hours by default, which is also the maximum; a shorter duration can be requested, and when the caller is using an assumed role the token cannot outlive the role session. The calling principal needs codeartifact:GetAuthorizationToken on the domain and sts:GetServiceBearerToken, because the token is a bearer token issued through AWS STS. Reading packages additionally needs codeartifact:ReadFromRepository, and publishing needs actions such as codeartifact:PublishPackageVersion.

The aws codeartifact login command fetches a token and configures a tool in one step. In a CodeBuild buildspec it is usually placed in the pre_build or install phase.

# Configure pip in a build to install from the team repository
aws codeartifact login --tool pip --domain acme --domain-owner 111122223333 --repository team-orders

# Configure npm and publish an internal package
aws codeartifact login --tool npm --domain acme --domain-owner 111122223333 --repository shared
npm publish

# Maven and Gradle use the token in settings.xml or gradle.properties
export CODEARTIFACT_AUTH_TOKEN=$(aws codeartifact get-authorization-token --domain acme \
  --domain-owner 111122223333 --query authorizationToken --output text)

Builds in a VPC without internet access reach CodeArtifact through two interface VPC endpoints, com.amazonaws.<region>.codeartifact.api and com.amazonaws.<region>.codeartifact.repositories, plus an S3 gateway endpoint, because package assets are downloaded from Amazon S3.

Package Origin Controls and Dependency Substitution

A dependency substitution (or dependency confusion) attack publishes a public package with the same name as an internal package, hoping that a build will resolve the public one. CodeArtifact defends against this with package origin controls. Each package in a repository has two settings: publish, which allows or blocks direct publishing to the repository, and upstream, which allows or blocks versions from upstream repositories and external connections. For an internal package, setting upstream to BLOCK ensures that only internally published versions are used. By default, CodeArtifact sets the controls automatically: a package first published directly blocks upstream versions, and a package first fetched from upstream blocks direct publishing.

Scenario questions

Secure the packages

Exam skills 1.3.1 · 1.4.1

Practise CodeArtifact security controls.

Log in to try this activity Interactive activities are free with an account.

Package Version Status and Promotion

Every package version has a status: Published (available), Unfinished (an upload in progress), Unlisted (downloadable by exact version but not listed for resolution), Archived (cannot be downloaded or restored by package managers) and Disposed (assets deleted, record retained). Changing a vulnerable version to Archived stops new builds from using it while keeping the record.

Package versions can be copied between repositories in the same domain with CopyPackageVersions. This supports promotion: a build publishes to a staging repository, tests run against it, and an approved version is copied to a release repository that production builds use. Because assets are deduplicated in the domain, copying is fast and does not double storage. CodeArtifact emits EventBridge events when package versions are created, modified or deleted, which can start downstream pipelines when a new internal library version is published.

Worked example

Securing Python Dependencies

A company's builds install Python packages directly from PyPI. A security review requires that builds do not depend on PyPI availability, that every external package used is retained, and that the internal package acme-auth can never be substituted by a public package of the same name.

  1. Create the structure

    Create a domain with a customer managed KMS key, a pypi-cache repository with an external connection to PyPI, and a python-shared repository with pypi-cache as its upstream.

  2. Publish internally

    Publish acme-auth to python-shared with twine after aws codeartifact login --tool twine. Its origin controls block upstream versions automatically.

  3. Connect builds

    Change buildspecs to run aws codeartifact login --tool pip against python-shared, and give the CodeBuild roles GetAuthorizationToken, sts:GetServiceBearerToken and ReadFromRepository.

  4. Restrict egress

    Optionally remove direct internet access from builds so that CodeArtifact is the only package source.

Check yourself

A build fails to authenticate to CodeArtifact although its role allows codeartifact:GetAuthorizationToken. Which permission is most likely missing?

Show answer

sts:GetServiceBearerToken. CodeArtifact tokens are bearer tokens issued through AWS STS, so the caller needs this permission, usually with a condition on sts:AWSServiceName equal to codeartifact.amazonaws.com.

Exam Tips

Remember for the exam

  • CodeArtifact external connections cache public packages, so builds continue during public registry outages and removed versions stay available; a repository has at most one external connection.
  • Authenticate with aws codeartifact login or GetAuthorizationToken; tokens last up to 12 hours and require sts:GetServiceBearerToken.
  • Package origin controls (publish and upstream ALLOW or BLOCK) prevent dependency substitution attacks.
  • Promote package versions by copying them between repositories in the same domain.
  • Private builds need the codeartifact.api and codeartifact.repositories interface endpoints plus an S3 gateway endpoint.