# P4Sudo — Project Handoff to Claude Code This document captures the full context of the P4Sudo design project so that Claude Code (the `claude` CLI) can continue the work, including setting up version control and driving the implementation forward. --- ## 1. Project Summary **P4Sudo** is a site-level extension for the Perforce P4 Server Deployment Package (SDP) environments that gives non-superuser P4 accounts the ability to run specific privileged operations — or entirely site-defined commands — through a controlled, audited mechanism. It is modeled on Unix `sudo`: a configuration file enumerates what is permitted, every attempt is logged, and the user runs only what they are explicitly authorized for. The implementation uses the **P4Broker (p4broker)** to intercept the non-native command `p4 sudo` and route it to a runtime dispatcher. From the user's perspective it behaves like a native `p4` command. It is only accessible through the `p4` CLI connected to the broker port — not through GUI clients (P4V, etc.) or the P4 API. The project will be deployed in **SDP (Server Deployment Package)** environments, which provide a predictable directory structure. All P4Sudo files live under `/p4/common/site/`. --- ## 2. Original Requirements (verbatim from the requester) > I'd like to start implement a system that allows non-super P4 users to run > certain commands that may require super access. Like the `sudo` command in > UNIX/Linux, it will be controlled by a configuration file. I'd like the > implementation to involve a p4broker, so that we can take advantage of its > ability to create a new `p4 sudo` command without requiring server changes. > A p4broker can intercept and optionally rewrite p4 commands inbound from a > client and on their way to a P4 Server. > > A configuration file would define what commands users are allowed to run with > elevated access. The p4broker also has a builtin `checkauth` feature we'll > leverage to ensure the users connecting to the broker are authenticated. One > of the features of the p4broker to use is the REWRITE feature. That REWRITE > feature is not formally documented (and technically unsupported), but it works > perfectly well and has for over a decade. New commands created this way, with > a p4broker, are not part of the API and can only be accessed with the 'p4' > command line interface. > > The 'p4 sudo' will not be accessible by GUI clients like P4V. So, a component > of the P4Sudo will be a web interface of some kind. Perhaps we can do that > with a NodeJS app or perhaps a regular Apache web server? > > The broker filter scripts can be in any language. My intent is to use bash for > ease of maintenance. The degree of complexity is at an appropriate level for > handling in bash. ### System Components and Features (from requester) - A `p4 sudo` command implemented using a p4broker configuration file with supplemental scripts. - The `p4 sudo` command will use the builtin `checkauth` feature to ensure users are authenticated. - The `p4 sudo` command will have logic that validates, based on a configuration file, whether the authenticated user is authorized to perform whatever it is they are trying to do. - The `p4 sudo` command will accept commands and arguments/options, and vet them against its configuration file. - The p4broker will intercept the `p4 help sudo` command, and display detailed help documentation (as `p4 help edit` or `p4 help `). - The P4Sudo tool will be deployed in SDP environments. Files will be deployed under the `/p4/common/site` directory tree, mostly within `/p4/common/site/p4sudo`, with a config file at `/p4/common/site/config/p4sudo.cfg`. - SDP allows for `/p4/common/site/bin` for utilities in the PATH, but P4Sudo scripts are called by p4broker with fully qualified paths (no PATH requirement). A CLI convenience wrapper `p4sudo` may live in `/p4/common/site/bin`. - The `p4sudo.cfg` file format needs to be defined — inspired by OS sudo but not required to follow that format exactly. - User-defined commands will be supported, e.g. `p4 sudo mkproj ` where `mkproj` is a site-defined command backed by a script. ### Additional Technical Notes from Requester - The p4broker REWRITE feature is undocumented but stable and proven over 10+ years. Working code examples exist and can be provided. - A web interface component is planned for GUI users who can't use `p4 sudo` via CLI. Technology choice (Node.js or Apache) is TBD. - A specific first use case (not yet described in detail) will be provided to sharpen the design. It should be incorporated once shared. --- ## 3. Architecture ``` p4 client (CLI) │ │ p4 sudo ▼ ┌─────────────┐ │ p4broker │ ◄──── broker config + filter scripts │ │ /p4/common/site/p4sudo/ └──────┬──────┘ │ │ REWRITE or FILTER action │ ├─── [checkauth] Verify requesting user has valid ticket │ ├─── [validation] Read p4sudo.cfg; match user/group against rules │ ├─── [dispatch] Route to: │ A) Site-defined command script, OR │ B) Native p4 command re-executed as service acct │ └─── [audit log] Record outcome (allow/deny) with timestamp ``` ### Key Components | Component | Location | Purpose | |---|---|---| | p4broker binary | System PATH (SDP-managed) | Intercepts `p4 sudo` | | Broker config | `/p4/N/broker/p4broker.conf` (SDP convention) | Declares the `sudo` filter/rewrite rule | | P4Sudo runtime script | `/p4/common/site/p4sudo/p4sudo.sh` | Core dispatch logic | | Configuration file | `/p4/common/site/config/p4sudo.cfg` | Access control rules and command definitions | | Command scripts | `/p4/common/site/p4sudo/commands/` | Site-defined subcommand implementations | | Logs | `/p4/common/site/p4sudo/logs/` | Operational and audit logs | | CLI wrapper (optional) | `/p4/common/site/bin/p4sudo` | Convenience alias for admin use | | Web interface | TBD (Node.js or Apache) | Browser-based access for GUI users | --- ## 4. Directory Layout ``` /p4/common/site/ ├── bin/ │ └── p4sudo # Optional CLI convenience wrapper ├── config/ │ └── p4sudo.cfg # Access control configuration └── p4sudo/ ├── p4sudo.sh # Core runtime (called by broker) ├── commands/ # Site-defined command scripts │ ├── mkproj.sh │ └── archive.sh └── logs/ ├── p4sudo.log # Operational log └── audit.log # Audit trail ``` --- ## 5. Design Artifacts Produced So Far The following three artifacts were drafted in the initial design session. They should be committed to version control as the starting point. --- ### 5.1 — `p4sudo.cfg` (Configuration File Format) The format is INI-style with three sections: `[settings]`, `[commands]`, and `[rules]`. Inline comments explain every field. **Key design decisions:** - INI-style was chosen because it is easy to parse in bash with `awk` (section-aware parsing is a well-established pattern). - `[commands]` and `[rules]` are separate sections so that *what commands exist* is decoupled from *who can run them*. - The `[rules]` section uses a whitespace-delimited column format within the INI structure — readable, easy to parse left-to-right. - Native p4 commands (e.g. `protect`) do NOT require a `[commands]` entry; they can be referenced directly in `[rules]` by their bare name. A `[commands]` entry for a native command is optional and only needed for custom help text. - Explicit `ALLOW` / `DENY` action verbs are used rather than implicit "allow by presence", making DENY overrides readable and unambiguous. - Arg-pattern matching uses shell-glob patterns matched against the normalized argument string. This is an area for further discussion — it's flexible but requires care to avoid overly loose patterns. ```ini ############################################################################## # p4sudo.cfg — P4Sudo Configuration File # Location: /p4/common/site/config/p4sudo.cfg # # Format version: 1 ############################################################################## [settings] command_dir = /p4/common/site/p4sudo/commands log = /p4/common/site/p4sudo/logs/p4sudo.log audit_log = /p4/common/site/p4sudo/logs/audit.log p4port = ssl:perforce:1666 p4sudo_user = p4sudo-svc max_args = 20 script_timeout = 300 debug = false [commands] mkproj.type = script mkproj.script = /p4/common/site/p4sudo/commands/mkproj.sh mkproj.description = Create a new project depot, mainline stream, and default group permissions. mkproj.usage = p4 sudo mkproj [--template