Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions SECURITY.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ These guards apply at every level and to every caller. An apply needs a plan of

Secrets and hardware stay with the owner at every level. An infra repo holds no secret. `devops infra` passes the SSH key of `devops ssh` and no other credential. The level stops an agent error; it is not a security boundary, because an agent with a shell can run `ansible-playbook`. So an agent changes a host only with `devops infra`, never with `ansible-playbook`, `kubectl drain`, `make` or a reboot over SSH.

## GitHub Settings

`devops tf` manages GitHub organization and repository settings under `tf/github` stacks ([ADR 0026](docs/adr/0026-github-settings-are-opentofu-code-the-owner-applies.md)). Each stack entry in `tf.stacks[].agent_access` admits only `plan` or `apply`, and every `tf/github` stack defaults to `plan`; only a person sets `apply`. When `is_agent_caller()` reports an agent session, `devops tf apply` and `devops tf destroy` refuse `tf/github` stacks before OpenTofu builds or starts, naming `tf.stacks[].agent_access`. Person sessions keep full apply and destroy access. Before any `tf/github` stack plan starts, `devops tf plan` checks the local `run_gh` core quota ledger against `DEFAULT_TF_GITHUB_CORE_BUDGET_FLOOR` (250 remaining) and refuses below the floor without making an external request. For an agent `tf/github` plan, `devops tf plan` mints a read-only installation token for `devops-settings-reader` from `tf.github_reader.app_id`, `tf.github_reader.installation_id`, and `tf.github_reader.private_key` (OS keyring `tf_github_reader_private_key`), and fails closed instead of falling back to writer App credentials. Passing `--dry-run` checks agent access and the local quota ledger without reading credentials, minting tokens, starting OpenTofu, or sending external provider requests.

## A Finding with No Fix

A scanner can name a finding that no update of the pinned package solves yet. The agent then adds a time-limited ignore entry itself and does not ask the owner. The entry expires by itself, and the failing scan after the date is the reminder. The owner set these limits on 2026-11-07, and the owner can lift them.
Expand Down
9 changes: 9 additions & 0 deletions docs/CONFIGURATION.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions docs/ENV_VARS.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions docs/ERRORS.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
schema: spec-driven
created: 2026-11-25
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Design

## Decisions

- Centralize `tf/github` stack guards in `src/devops_cli/tf/access.py` (`prepare_tf_stack_run` -> `require_agent_access`, `check_github_core_budget`, `reader_github_env`), called right after `validate_dir(directory)` in `tf_plan`, `tf_apply`, and `tf_destroy`.
- Default `tf.stacks[].agent_access` to `plan` for `tf/github` stacks (and unconfigured `tf/github` directories), rejecting any value other than `plan` or `apply` with a validator naming `tf.stacks[].agent_access`, `plan`, and `apply`.
- Read `get_github_rate_limiter().get_quota("core")` against `DEFAULT_TF_GITHUB_CORE_BUDGET_FLOOR` (250) without external network requests, so both real plans and `--dry-run` enforce the budget with zero API calls.
- Fail closed when `tf.github_reader.{app_id,installation_id,private_key}` is incomplete on non-dry-run agent plans (naming all three settings and never reading the writer/Service App key), while keeping `--dry-run` free of credential reads and token minting.

## Rejected alternatives

- Reusing `DEFAULT_GH_GRAPHQL_BUDGET_FLOOR`: rejected because GraphQL points differ from the REST core quota consumed by the GitHub OpenTofu provider.
- Calling live `GET /rate_limit` before each plan: rejected because it adds external requests and breaks zero-request dry-run behavior.
- Falling back to writer App credentials when `tf.github_reader` is unset: rejected by ADR 0026 (only a person applies; agents hold read-only reader access).
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Proposal: #5320

## Why

An agent session can call `devops tf apply` or `devops tf destroy` against `tf/github` stacks managing GitHub settings (ADR 0026, `#5311`), and provider calls bypass `run_gh` rate pacing. Only a person may apply GitHub settings changes, while agents need read-only `devops-settings-reader` plans gated by a pre-plan REST core quota floor and zero-request `--dry-run` behavior.

## What Changes

- Add `tf.stacks[].agent_access` (`plan` or `apply`, defaulting to `plan` on `tf/github` stacks) and `tf.github_reader.{app_id,installation_id,private_key}` settings (`DEVOPS_CLI_TF_GITHUB_READER_*` and OS keyring `tf_github_reader_private_key`).
- Add `src/devops_cli/tf/access.py` (`require_agent_access`, `check_github_core_budget`, `reader_github_env`, `prepare_tf_stack_run`) and invoke it at the top of `tf_plan`, `tf_apply`, and `tf_destroy` after `validate_dir` and before building or starting `tofu`.
- Add `DEFAULT_TF_GITHUB_CORE_BUDGET_FLOOR: int = 250` in `src/devops_cli/config/defaults.py` checked against `get_github_rate_limiter().get_quota("core")` before `tf/github` plans.
- Mint a read-only installation token for `devops-settings-reader` on non-dry-run agent `tf/github` plans, failing closed without falling back to writer App credentials.
- Document these controls under `## GitHub Settings` in `SECURITY.md` and regenerate CLI/config/error docs.

## Capabilities

- `tf-github-agent-guard`: Enforce `tf.stacks[].agent_access`, REST core budget floor, reader App credentials, and zero-request `--dry-run` for `tf/github` stacks.
- `security-controls-policy`: Document the GitHub settings guard policy in `SECURITY.md`.

## Impact

- `src/devops_cli/tf/access.py`, `src/devops_cli/commands/tf.py`, `src/devops_cli/config/{constants,defaults,env,settings}.py`, `src/devops_cli/exceptions/{__init__,tf}.py`, `src/devops_cli/lang/en/errors.py`, `SECURITY.md`, generated docs, and tests. State-command guards remain deferred to `#7061`.

## Feasibility

Verified against `src/devops_cli/commands/tf.py`, `src/devops_cli/infra/access.py`, `src/devops_cli/github/{app_installation,rate_limiter}.py`, `tests/security/test_config_declarations.py`, and `docs/adr/0026-github-settings-are-opentofu-code-the-owner-applies.md`. Note `tf_state`/`tf_import`/`tf_force_unlock` were moved out of `#5320` into `#7061` by refine (`2026-11-25`). Confirmed new tests fail cleanly before implementation.

## Existing Tool Considered

OpenTofu (`opentofu/opentofu`, MPL-2.0, maintained; `alternatives.verdict: in-use`) runs the `tf/github` stacks and provider calls but provides no `is_agent_caller()` refusal, local `run_gh` core budget floor, zero-request dry-run, or `devops-settings-reader` keyring token minting; wrapper guard cost ~0.5M tokens.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
## ADDED Requirements

### Requirement: GitHub Settings security controls section
`SECURITY.md` SHALL include a `## GitHub Settings` section stating the `tf.stacks[].agent_access` `plan` default and agent refusal for apply and destroy, the pre-plan `DEFAULT_TF_GITHUB_CORE_BUDGET_FLOOR` check, the fail-closed `tf.github_reader` reader App credentials, and zero-request `--dry-run` behavior.

#### Scenario: Docs check passes with GitHub Settings section
- **WHEN** `uv run devops ci --check --only docs` runs
- **THEN** the `docs` gate passes and `SECURITY.md` documents the GitHub settings controls
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
## ADDED Requirements

### Requirement: Agent access level for tf/github stacks
`tf.stacks[].agent_access` SHALL accept only `plan` or `apply` and SHALL default to `plan` for every `tf/github` stack. Invalid values (such as `full`) SHALL raise a configuration error naming `tf.stacks[].agent_access`, `plan`, and `apply`. When `is_agent_caller()` reports an agent caller, `devops tf apply` and `devops tf destroy` SHALL exit 1 before invoking `tofu` unless `agent_access` is `apply`, while person sessions remain permitted at `plan`.

#### Scenario: Agent apply refused at plan level and allowed at apply level
- **WHEN** an agent caller invokes `devops tf apply` or `devops tf destroy` on a `tf/github` stack with default (`plan`) access
- **THEN** the command exits 1 naming `tf.stacks[].agent_access` with zero `tofu` calls, whereas `apply` level allows the run and a person caller passes at `plan` level

### Requirement: Pre-plan core budget floor, reader App credentials, and zero-request dry run
`devops tf plan` on a `tf/github` stack SHALL check `get_github_rate_limiter().get_quota("core")` against `DEFAULT_TF_GITHUB_CORE_BUDGET_FLOOR` (250) and exit 1 naming the remaining budget and floor without calling `tofu` when valid quota is below 250. Non-dry-run agent plans SHALL mint an installation token for `devops-settings-reader` using `tf.github_reader.app_id`, `tf.github_reader.installation_id`, and `tf.github_reader.private_key` without reading writer App credentials, and SHALL exit 1 naming all three keys when incomplete. `--dry-run` checks access and budget without starting `tofu`, reading credentials, or sending requests.

#### Scenario: Budget floor and reader credentials protect agent plans
- **WHEN** core quota is 249 or `tf.github_reader` credentials are missing
- **THEN** `devops tf plan` exits 1 with zero `tofu` calls; when quota is sufficient and reader credentials exist, the `tofu` child receives the reader `GITHUB_TOKEN`
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Tasks

## 1. Implement `tf/github` agent guards, config, and reader credentials

Kind: python. Reads: `src/devops_cli/commands/tf.py`, `src/devops_cli/config/settings.py`, `src/devops_cli/infra/access.py`, `src/devops_cli/github/app_installation.py`. Changes: `src/devops_cli/tf/access.py`, `src/devops_cli/commands/tf.py`, `src/devops_cli/config/{constants,defaults,env,settings}.py`, `src/devops_cli/exceptions/{__init__,tf}.py`, `src/devops_cli/lang/en/errors.py`, `tests/`.

- [x] 1.1 Write failing unit, config-validation, and CLI tests for `tf.stacks[].agent_access`, core budget floor (249 vs 250), `tf.github_reader` token minting / missing credentials, zero-request `--dry-run`, and `SECURITY.md`
- Verification: `$ uv run pytest tests/tf/test_access.py tests/config/test_tf_stacks_setting.py tests/commands/test_tf.py`
- [x] 1.2 Implement `TFConfig` / `tf.github_reader` settings, `src/devops_cli/tf/access.py`, `tf_plan`/`tf_apply`/`tf_destroy` integration, `tests/security/test_config_declarations.py` declarations, and `SECURITY.md` `## GitHub Settings` section
- Verification: `$ uv run pytest tests/tf/test_access.py tests/config/test_tf_stacks_setting.py tests/commands/test_tf.py tests/security/test_config_declarations.py`

## 2. Regenerate docs and run full verification

Kind: python. Reads: `SECURITY.md`, `src/devops_cli/config/settings.py`. Changes: `docs/`.

- [x] 2.1 Regenerate docs (`devops docs generate`), archive OpenSpec, run `devops ci --check --skip test --force`, and run `devops ci fail-to-pass --base origin/main`
- Verification: `$ uv run devops ci --check --skip test --force && uv run devops ci fail-to-pass --base origin/main`
7 changes: 7 additions & 0 deletions openspec/specs/security-controls-policy/spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,10 @@ The changed documents SHALL pass the documentation check.
#### Scenario: Docs check passes
- **WHEN** a person runs `uv run devops docs check`.
- **THEN** the command exits 0.

### Requirement: GitHub Settings security controls section
`SECURITY.md` SHALL include a `## GitHub Settings` section stating the `tf.stacks[].agent_access` `plan` default and agent refusal for apply and destroy, the pre-plan `DEFAULT_TF_GITHUB_CORE_BUDGET_FLOOR` check, the fail-closed `tf.github_reader` reader App credentials, and zero-request `--dry-run` behavior.

#### Scenario: Docs check passes with GitHub Settings section
- **WHEN** `uv run devops ci --check --only docs` runs
- **THEN** the `docs` gate passes and `SECURITY.md` documents the GitHub settings controls
28 changes: 28 additions & 0 deletions openspec/specs/tf-github-agent-guard/spec.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# tf-github-agent-guard Specification

## Purpose

Enforce agent access levels, GitHub core rate-limit floors, and read-only reader App tokens for OpenTofu stacks under `tf/github`.

## Requirements

### Requirement: Agent access level for tf/github stacks
`tf.stacks[].agent_access` SHALL accept only `plan` or `apply` and SHALL default to `plan` for every `tf/github` stack. Invalid values (such as `full`) SHALL raise a configuration error naming `tf.stacks[].agent_access`, `plan`, and `apply`. When `is_agent_caller()` reports an agent caller, `devops tf apply` and `devops tf destroy` SHALL exit 1 before invoking `tofu` unless `agent_access` is `apply`, while person sessions remain permitted at `plan`.

#### Scenario: Agent apply refused at plan level and allowed at apply level
- **WHEN** an agent caller invokes `devops tf apply` or `devops tf destroy` on a `tf/github` stack with default (`plan`) access
- **THEN** the command exits 1 naming `tf.stacks[].agent_access` with zero `tofu` calls, whereas `apply` level allows the run and a person caller passes at `plan` level

### Requirement: Pre-plan core budget floor
`devops tf plan` on a `tf/github` stack SHALL check `get_github_rate_limiter().get_quota("core")` against `DEFAULT_TF_GITHUB_CORE_BUDGET_FLOOR` (250) and exit 1 naming the remaining budget and floor without invoking `tofu` when valid quota is below 250.

#### Scenario: Budget floor refuses low-quota plans
- **WHEN** the local core quota ledger reports 249 remaining requests
- **THEN** `devops tf plan` exits 1 naming both 249 and 250 without invoking `tofu`

### Requirement: Reader App token and zero-request dry run
Non-dry-run agent plans on `tf/github` SHALL mint a read-only `devops-settings-reader` token from `tf.github_reader.app_id`, `tf.github_reader.installation_id`, and `tf.github_reader.private_key` without reading writer App credentials, exiting 1 naming all three keys when incomplete. `--dry-run` checks access and budget without starting `tofu`, reading credentials, or sending requests.

#### Scenario: Reader credentials protect agent plans without fallback
- **WHEN** reader credentials are configured for an agent plan
- **THEN** `tofu plan` receives the reader `GITHUB_TOKEN`, while incomplete reader credentials exit 1 with zero `tofu` calls and `--dry-run` sends zero requests
Loading