Skip to main content

hyprstream_core/cli/commands/
policy.rs

1//! Policy management CLI commands
2//!
3//! Cisco-inspired transactional UX for managing access control policies.
4//!
5//! Policy states:
6//! - **Running** = Current active (HEAD of .registry/policies/)
7//! - **Draft** = Uncommitted changes
8//! - **History** = Previous versions (HEAD~n)
9
10use clap::Subcommand;
11
12/// Policy management subcommands
13#[derive(Subcommand)]
14pub enum PolicyCommand {
15    /// Display the running policy (current active configuration)
16    Show {
17        /// Show policy in raw CSV format instead of formatted table
18        #[arg(long)]
19        raw: bool,
20    },
21
22    /// Show policy commit history
23    History {
24        /// Number of commits to show
25        #[arg(short = 'n', long, default_value = "10")]
26        count: usize,
27        /// Show one commit per line
28        #[arg(long)]
29        oneline: bool,
30    },
31
32    /// Open policy in $VISUAL/$EDITOR for editing
33    Edit,
34
35    /// Show diff between draft (uncommitted changes) and running policy
36    Diff {
37        /// Compare against specific commit/ref instead of HEAD
38        #[arg(long)]
39        against: Option<String>,
40    },
41
42    /// Commit draft changes to running policy
43    Apply {
44        /// Preview changes without committing
45        #[arg(long)]
46        dry_run: bool,
47        /// Commit message (auto-generated if not provided)
48        #[arg(short = 'm', long)]
49        message: Option<String>,
50    },
51
52    /// Revert to a previous policy version
53    Rollback {
54        /// Git ref to rollback to (default: HEAD~1)
55        #[arg(default_value = "HEAD~1")]
56        git_ref: String,
57        /// Preview changes without applying
58        #[arg(long)]
59        dry_run: bool,
60    },
61
62    /// Test if a user has permission for an action on a resource
63    Check {
64        /// User to check permissions for
65        user: String,
66        /// Resource to check (e.g., "model:qwen3-small")
67        resource: String,
68        /// Action to check (infer, train, query, write, serve, manage)
69        action: String,
70    },
71
72    /// API token management (OpenAI-compatible Bearer authentication)
73    Token {
74        #[command(subcommand)]
75        command: TokenCommand,
76    },
77
78    /// Apply a built-in policy template
79    ApplyTemplate {
80        /// Template name (run list-templates to see options)
81        template: String,
82        /// Preview changes without applying
83        #[arg(long)]
84        dry_run: bool,
85    },
86
87    /// List available policy templates
88    ListTemplates,
89
90    /// Role assignment subcommands
91    Role {
92        #[command(subcommand)]
93        command: RoleCommand,
94    },
95}
96
97/// Role assignment subcommands
98#[derive(Subcommand, Debug)]
99pub enum RoleCommand {
100    /// Assign a role to a user
101    Add {
102        /// User subject (e.g., "alice" or "https://node.example.com:alice")
103        user: String,
104        /// Role to assign (e.g., "ttt.user", "ttt.privileged")
105        role: String,
106        /// Preview without making changes
107        #[arg(long)]
108        dry_run: bool,
109    },
110    /// Remove a role from a user
111    Remove {
112        /// User subject
113        user: String,
114        /// Role to remove
115        role: String,
116        /// Skip confirmation prompt
117        #[arg(short = 'f', long)]
118        force: bool,
119    },
120    /// List role assignments (g-lines from policy)
121    List {
122        /// Filter by user
123        #[arg(short = 'u', long)]
124        user: Option<String>,
125        /// Filter by role
126        #[arg(short = 'r', long)]
127        role: Option<String>,
128    },
129}
130
131/// Token management subcommands
132#[derive(Subcommand)]
133pub enum TokenCommand {
134    /// Create a new API token for a user
135    Create {
136        /// User the token authenticates as
137        user: String,
138
139        /// Human-readable name for the token (e.g., "dev-laptop", "ci-pipeline")
140        #[arg(long, short = 'n')]
141        name: Option<String>,
142
143        /// Token expiration (e.g., "30d", "90d", "1y", "never")
144        #[arg(long, short = 'e', default_value = "1y")]
145        expires: String,
146
147        /// Limit token to specific resources (repeatable, e.g., --scope model:qwen3-small)
148        #[arg(long, short = 's')]
149        scope: Vec<String>,
150    },
151
152    /// List all API tokens
153    List {
154        /// Filter by user
155        #[arg(long, short = 'u')]
156        user: Option<String>,
157    },
158
159    /// Revoke an API token
160    Revoke {
161        /// Token to revoke (full token or prefix)
162        token: Option<String>,
163
164        /// Revoke by user and name instead of token
165        #[arg(long, short = 'n', requires = "revoke_user")]
166        name: Option<String>,
167
168        /// User whose token to revoke (use with --name)
169        #[arg(long, short = 'u')]
170        revoke_user: Option<String>,
171
172        /// Skip confirmation prompt
173        #[arg(long, short = 'f')]
174        force: bool,
175    },
176}