Files
Charles Swanberg 8d005d70d0 fix(codebuild): run a build as its project's service role (#7844)
* fix(codebuild): run a build as its project's service role
* fix(codebuild): fail a build whose role does not exist in PROVISIONING, and share confirmed removal
2026-11-18 21:10:52 -04:00

142 lines
8.6 KiB
Markdown

# CodeBuild
Floci implements the CodeBuild API — stored-state management plus real build execution inside Docker containers.
**Protocol:** JSON 1.1 — `POST /` with `X-Amz-Target: CodeBuild_20161006.<Action>`
**ARN formats:**
- `arn:aws:codebuild:<region>:<account>:project/<name>`
- `arn:aws:codebuild:<region>:<account>:report-group/<name>`
- `arn:aws:codebuild:<region>:<account>:token/<type>-<uuid>`
- `arn:aws:codebuild:<region>:<account>:build/<project>:<uuid>`
## Supported Operations (20 total)
### Projects
| Operation | Notes |
|---|---|
| `CreateProject` | Stores project config; requires `name`, `source.type`, `artifacts.type`, `environment`, `serviceRole` |
| `UpdateProject` | Partial update — only supplied fields are modified |
| `DeleteProject` | Removes project by name |
| `BatchGetProjects` | Returns found projects and a `projectsNotFound` list |
| `ListProjects` | Returns project names in the region, with optional name, creation-time, or last-modified-time sorting |
### Build Execution
| Operation | Notes |
|---|---|
| `StartBuild` | Launches a real Docker container using the project's image; runs buildspec phases (`INSTALL`, `PRE_BUILD`, `BUILD`, `POST_BUILD`); returns immediately with `IN_PROGRESS` status |
| `BatchGetBuilds` | Returns current build state; poll until `buildComplete` is `true` |
| `ListBuilds` | Returns all build IDs in the region, most recent first |
| `ListBuildsForProject` | Returns build IDs for a specific project |
| `StopBuild` | Signals a running build to stop; build transitions to `STOPPED` |
| `RetryBuild` | Starts a new build using the same config as a completed build; returns a new build record |
### Report Groups
| Operation | Notes |
|---|---|
| `CreateReportGroup` | Stores report group config |
| `UpdateReportGroup` | Partial update by ARN |
| `DeleteReportGroup` | Removes report group by ARN |
| `BatchGetReportGroups` | Returns found report groups and a `reportGroupsNotFound` list |
| `ListReportGroups` | Returns all report group ARNs in the region |
### Source Credentials
| Operation | Notes |
|---|---|
| `ImportSourceCredentials` | Stores server type and auth type; deduplicated by `serverType+authType`; token is accepted but not returned |
| `ListSourceCredentials` | Returns stored credential metadata (no tokens) |
| `DeleteSourceCredentials` | Removes source credentials by ARN |
### Images
| Operation | Notes |
|---|---|
| `ListCuratedEnvironmentImages` | Returns a static list of standard CodeBuild images for AL2 and Ubuntu |
## Build Execution Model
Each `StartBuild` call:
1. Pulls the project's Docker image (e.g. `public.ecr.aws/docker/library/alpine:latest`)
2. Starts a container with the working directories pre-created
3. Injects source files into the container via `docker cp` (`NO_SOURCE` builds skip this step)
4. Executes buildspec phases sequentially inside the container via `docker exec`
5. Streams phase output to CloudWatch Logs under `/aws/codebuild/<project>`
6. Extracts artifact files from the container via `docker cp` and uploads them to S3 if `artifacts.type=S3`
7. Marks the build complete with `SUCCEEDED`, `FAILED`, or `STOPPED`
Source injection and artifact extraction both use the Docker API's archive copy endpoints — no bind mounts are required. This works correctly when Floci itself runs inside a Docker container (Docker-in-Docker).
### VPC (`vpcConfig`)
CodeBuild runs a build of a project with a `vpcConfig` with a network interface in one of the project's subnets. Floci puts the build container on that VPC's Docker network, at an address leased from the first subnet, before the first phase runs, the way it places EC2 instances, VPC-configured Lambda functions and MWAA environments, so a build reaches a database or anything else in the VPC by its private IP. The container keeps the network it was created on as well. A subnet EC2 does not know, or `FLOCI_SERVICES_EC2_VPC_NETWORKS_ENABLED=false`, leaves it there alone. Security groups are not enforced, as for EC2. While a build runs, `DeleteSubnet` refuses its subnet and `DeleteVpc` its VPC (`DependencyViolation`), as the build's network interface does on AWS; a project's `vpcConfig` alone holds none. `BatchGetBuilds` returns the project's `vpcConfig` on each build.
## The Build's Credentials
A build runs as its project's `serviceRole`, as CodeBuild uses the service role for every operation it performs on the account's behalf. Floci mints a session of that role in the project's account, named `AWSCodeBuild-<build id after the project's name>`, so `aws sts get-caller-identity` inside the build answers `arn:aws:sts::<account>:assumed-role/<role name>/AWSCodeBuild-<id>`, and with `FLOCI_SERVICES_IAM_ENFORCEMENT_ENABLED=true` the role's policies decide what the build may call. Floci puts no access keys of its own in the build's environment.
How the session reaches the build depends on the Docker network the build joins, `FLOCI_SERVICES_CODEBUILD_DOCKER_NETWORK`, else `FLOCI_SERVICES_DOCKER_NETWORK`, else Floci's own when Floci runs in a container:
- **On a user-defined network**, the session is served the way CodeBuild serves it: the build gets `AWS_CONTAINER_CREDENTIALS_RELATIVE_URI` and none of `AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY`, `AWS_SESSION_TOKEN`, `AWS_CONTAINER_CREDENTIALS_FULL_URI` or `AWS_CONTAINER_AUTHORIZATION_TOKEN`, and the AWS SDKs and CLI read the role's credentials at `http://169.254.170.2$AWS_CONTAINER_CREDENTIALS_RELATIVE_URI`, with no code change. Floci serves that address with the same credentials server and per-network proxy container ECS task roles use (see [ECS](ecs.md#task-iam-role-credentials)), and gives the build's container its own address in `169.254.0.0/16` so it can reach it. Reads near expiry return a fresh session at the same path, and the path answers nothing once the build has ended.
- **On Docker's default bridge**, where no container can hold a link-local address, the same session is handed as `AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY` and `AWS_SESSION_TOKEN`, so the build still runs as the role; these keys are not renewed during the build, and they expire after `FLOCI_SERVICES_ECS_TASK_ROLE_CREDENTIALS_TTL_SECONDS` (6 hours by default), which a build on the bridge should not outlive.
A build whose project's role does not exist in its account fails in `PROVISIONING`, before its first command, with `CLIENT_ERROR` and the message CodeBuild gives, `CodeBuild is not authorized to perform: sts:AssumeRole on <role ARN>`. A build on a user-defined network whose endpoint cannot be served, as where the Docker runtime does not honour link-local addresses, is handed its session as the three keys instead, with a warning. A variable of any of these names that the project, the buildspec or `StartBuild` sets still reaches the build.
Unlike CodeBuild, Floci does not evaluate the role's trust policy: a role that does not trust `codebuild.amazonaws.com` is still assumed.
## Buildspec Support
Floci parses the `buildspec.yml` embedded in the project or provided via `buildspecOverride`. Supported fields:
- `phases` — `install`, `pre_build`, `build`, `post_build` command lists
- `artifacts.files` — list of file patterns to collect; supports `**/*` glob, specific filenames, and path patterns
- `artifacts.base-directory` — base directory for artifact collection (default: `$CODEBUILD_SRC_DIR`)
## Artifact Upload
When `artifacts.type=S3`, collected files are uploaded to the configured S3 bucket. The bucket must exist (created via `CreateBucket`). File paths in S3 match the relative path from the artifact base directory.
## Configuration
| Variable | Default | Description |
|---|---|---|
| `FLOCI_SERVICES_CODEBUILD_ENABLED` | `true` | Enable or disable the service |
## CLI Examples
```bash
# Create a project with S3 artifacts
aws --endpoint-url http://localhost:4566 codebuild create-project \
--name my-project \
--source type=NO_SOURCE \
--artifacts type=S3,location=my-bucket \
--environment type=LINUX_CONTAINER,image=public.ecr.aws/docker/library/alpine:latest,computeType=BUILD_GENERAL1_SMALL \
--service-role arn:aws:iam::000000000000:role/codebuild-role
# Start a build with inline buildspec
aws --endpoint-url http://localhost:4566 codebuild start-build \
--project-name my-project \
--buildspec-override 'version: 0.2
phases:
build:
commands:
- echo hello > output.txt
artifacts:
files:
- output.txt'
# Poll until complete
aws --endpoint-url http://localhost:4566 codebuild batch-get-builds --ids <build-id>
# List all builds
aws --endpoint-url http://localhost:4566 codebuild list-builds
# List curated images
aws --endpoint-url http://localhost:4566 codebuild list-curated-environment-images
```