blob: 76ef82e1cbb9c92577205d7dbae3b06b9b571933 [file] [view]
# AI Agent Project Profile: access-ipsubnet module
## Build and Test
This is a Gerrit plugin built with Bazel. It must be built from within the
Gerrit source tree where `//tools/bzl:plugin.bzl` and
`//tools/bzl:junit.bzl` are available (i.e., `plugins/access-ipsubnet/`
under the Gerrit repo root).
If `bazelisk` is installed, use `bazelisk` instead of `bazel` for all build
commands.
```bash
# Build the plugin jar
bazel build //plugins/access-ipsubnet
# Run tests
bazel test //plugins/access-ipsubnet:access-ipsubnet_tests
# Run a single test class
bazel test //plugins/access-ipsubnet:access-ipsubnet_tests --test_filter=CidrMatcherTest
```
Output artifact: `bazel-bin/plugins/access-ipsubnet/access-ipsubnet.jar`
## Architecture
This is a Gerrit `PermissionBackend` wrapper plugin that enforces
IP/CIDR-based project access control.
**Module entry point**: `IpSubnetAccessModule` binds `PermissionBackend` →
`IpSubnetPermissionBackend` as a singleton.
**Request flow**:
1. `IpSubnetPermissionBackend.user(...).project(project)` intercepts every
project permission check.
2. Resolves caller IP from `IdentifiedUser.getRemotePeer()` (covers
authenticated HTTP and SSH). Anonymous request users resolve the transport
peer through `IpSubnetHttpFilter` and a thread-local provider bridge;
`@RemotePeer` is only bound in the HTTP/SSH child injectors.
Configure `httpd.filterClass` for anonymous HTTP coverage. Absent-account checks
reuse the request user only for the same account; other subjects do not
borrow the request peer.
3. Evaluates global policy (`GlobalIpPolicyProvider`) then project policy
(`ProjectIpPolicyProvider`).
4. If denied, returns `ForDeniedProject` (mimics hidden project behavior);
otherwise delegates to the wrapped `DefaultPermissionBackend`.
**Policy evaluation order** (in `IpSubnetPolicyEvaluator.evaluate()`):
1. Global block list → deny
2. Global allow list non-match → deny
3. Project policy disabled → allow
4. Invalid project policy → deny (ENFORCE) or allow with log (LOG_ONLY)
5. Project block list → deny (or log in LOG_ONLY mode)
6. Project allow list non-match → deny (or log in LOG_ONLY mode)
7. Otherwise → allow
**Policy sources**:
- *Global* (`GlobalIpPolicyProvider`): reads `etc/access-ipsubnet.config`
(JGit `Config` format), cached by mtime+size, checked at most once every five seconds. Has a `[global]` section
(applies to all projects) and `[project "<name>"]` subsections
(admin-managed per-project additions). The effective global policy for a
project merges both.
- *Per-project* (`ProjectIpPolicyProvider`): reads
`plugin "access-ipsubnet"` subsection from
`refs/meta/config:project.config` via `PluginConfigFactory` with
inheritance. Relies on Gerrit's `ProjectCache` for eviction — no extra
listener needed.
**Key config fields** (read by `IpSubnetAccessConfig` from
`gerrit.config [plugin "access-ipsubnet"]`):
- `failClosedOnMissingRemotePeer` (default `false`) — deny if remote IP
cannot be resolved
- `logDecisions` (default `false`) — emit structured log per decision
- `failClosedOnGlobalPolicyError` (default `true`) — deny on global policy
parse/load failure
- `globalPolicyPath` (default `etc/access-ipsubnet.config`) — path
resolved relative to Gerrit site
- `globalMode` (default `ENFORCE`) — default enforcement mode for
**per-project** policy evaluation. When a project has `enabled = true`
in `refs/meta/config` but no explicit `mode` key, this value is used.
A project that sets its own `mode` always overrides this default.
**Important distinction**: `globalMode` only affects the per-project
policy path (steps 4–6 in the evaluation order above). The global CIDR
lists in `etc/access-ipsubnet.config` (`[global]` and `[project]`
sections) are always enforced regardless of `globalMode`. Setting
`globalMode = LOG_ONLY` will not make the global CIDR list log-only.
**CIDR matching** (`CidrMatcher`): pure-Java IPv4/IPv6 prefix matching
using byte-level masking. IPv4 and IPv6 CIDRs do not match each other
(different `getAddress()` lengths). Throws `IllegalArgumentException` on
invalid CIDR.
**Observability** (`IpDecisionLogger`): emits metrics
`ip_subnet_acl/decision_total{result,reason}`,
`ip_subnet_acl/policy_invalid_total`,
`ip_subnet_acl/missing_remote_peer_total`, plus structured log lines when
`logDecisions = true`.