1#[derive(Clone, Copy)]
14pub struct ServicePolicyRule {
15 pub subject: &'static str,
16 pub domain: &'static str,
17 pub resource: &'static str,
18 pub action: &'static str,
19 pub effect: &'static str,
20}
21
22impl ServicePolicyRule {
23 pub fn to_vec(&self) -> Vec<String> {
25 vec![
26 self.subject.into(),
27 self.domain.into(),
28 self.resource.into(),
29 self.action.into(),
30 self.effect.into(),
31 ]
32 }
33
34 pub fn to_csv_line(&self) -> String {
36 format!(
37 "p, {}, {}, {}, {}, {}",
38 self.subject, self.domain, self.resource, self.action, self.effect
39 )
40 }
41}
42
43pub struct ServiceGrouping {
47 pub user: &'static str,
48 pub role: &'static str,
49}
50
51impl ServiceGrouping {
52 pub fn to_vec(&self) -> Vec<String> {
53 vec![self.user.into(), self.role.into()]
54 }
55
56 pub fn to_csv_line(&self) -> String {
57 format!("g, {}, {}", self.user, self.role)
58 }
59}
60
61pub const SERVICE_BASE_POLICIES: &[ServicePolicyRule] = &[
75 ServicePolicyRule {
77 subject: "service:policy",
78 domain: "*",
79 resource: "*",
80 action: "*",
81 effect: "allow",
82 },
83 ServicePolicyRule {
86 subject: "service:oauth",
87 domain: "*",
88 resource: "policy:IssueToken",
89 action: "manage",
90 effect: "allow",
91 },
92 ServicePolicyRule {
95 subject: "service:oauth",
96 domain: "*",
97 resource: "policy:RevokeCredential",
98 action: "manage",
99 effect: "allow",
100 },
101 ServicePolicyRule {
104 subject: "service:*",
105 domain: "*",
106 resource: "policy:CheckCredentialRevocation",
107 action: "query",
108 effect: "allow",
109 },
110 ServicePolicyRule {
113 subject: "service:oauth",
114 domain: "*",
115 resource: "policy:RegisterSession",
116 action: "manage",
117 effect: "allow",
118 },
119 ServicePolicyRule {
120 subject: "service:oauth",
121 domain: "*",
122 resource: "policy:RevokeSession",
123 action: "manage",
124 effect: "allow",
125 },
126 ServicePolicyRule {
129 subject: "service:*",
130 domain: "*",
131 resource: "policy:CheckSession",
132 action: "query",
133 effect: "allow",
134 },
135 ServicePolicyRule {
139 subject: "service:oauth",
140 domain: "*",
141 resource: "policy:PolicyCheck",
142 action: "check",
143 effect: "allow",
144 },
145 ServicePolicyRule {
146 subject: "service:registry",
147 domain: "*",
148 resource: "policy:*",
149 action: "check",
150 effect: "allow",
151 },
152 ServicePolicyRule {
153 subject: "service:model",
154 domain: "*",
155 resource: "policy:*",
156 action: "check",
157 effect: "allow",
158 },
159 ServicePolicyRule {
160 subject: "service:worker",
161 domain: "*",
162 resource: "policy:*",
163 action: "check",
164 effect: "allow",
165 },
166 ServicePolicyRule {
167 subject: "service:oai",
168 domain: "*",
169 resource: "policy:*",
170 action: "check",
171 effect: "allow",
172 },
173 ServicePolicyRule {
174 subject: "service:tui",
175 domain: "*",
176 resource: "policy:*",
177 action: "check",
178 effect: "allow",
179 },
180 ServicePolicyRule {
181 subject: "service:discovery",
182 domain: "*",
183 resource: "policy:*",
184 action: "check",
185 effect: "allow",
186 },
187 ServicePolicyRule {
188 subject: "service:notification",
189 domain: "*",
190 resource: "policy:*",
191 action: "check",
192 effect: "allow",
193 },
194 ServicePolicyRule {
195 subject: "service:metrics",
196 domain: "*",
197 resource: "policy:*",
198 action: "check",
199 effect: "allow",
200 },
201 ServicePolicyRule {
202 subject: "service:mcp",
203 domain: "*",
204 resource: "policy:*",
205 action: "*",
206 effect: "allow",
207 },
208 ServicePolicyRule {
209 subject: "service:flight",
210 domain: "*",
211 resource: "policy:*",
212 action: "check",
213 effect: "allow",
214 },
215 ServicePolicyRule {
217 subject: "service:oauth",
218 domain: "*",
219 resource: "policy:*",
220 action: "query",
221 effect: "allow",
222 },
223 ServicePolicyRule {
224 subject: "service:registry",
225 domain: "*",
226 resource: "policy:*",
227 action: "query",
228 effect: "allow",
229 },
230 ServicePolicyRule {
231 subject: "service:model",
232 domain: "*",
233 resource: "policy:*",
234 action: "query",
235 effect: "allow",
236 },
237 ServicePolicyRule {
238 subject: "service:worker",
239 domain: "*",
240 resource: "policy:*",
241 action: "query",
242 effect: "allow",
243 },
244 ServicePolicyRule {
245 subject: "service:oai",
246 domain: "*",
247 resource: "policy:*",
248 action: "query",
249 effect: "allow",
250 },
251 ServicePolicyRule {
252 subject: "service:tui",
253 domain: "*",
254 resource: "policy:*",
255 action: "query",
256 effect: "allow",
257 },
258 ServicePolicyRule {
259 subject: "service:discovery",
260 domain: "*",
261 resource: "policy:*",
262 action: "query",
263 effect: "allow",
264 },
265 ServicePolicyRule {
266 subject: "service:notification",
267 domain: "*",
268 resource: "policy:*",
269 action: "query",
270 effect: "allow",
271 },
272 ServicePolicyRule {
273 subject: "service:metrics",
274 domain: "*",
275 resource: "policy:*",
276 action: "query",
277 effect: "allow",
278 },
279 ServicePolicyRule {
281 subject: "service:model",
282 domain: "*",
283 resource: "registry:*",
284 action: "*",
285 effect: "allow",
286 },
287 ServicePolicyRule {
288 subject: "service:oai",
289 domain: "*",
290 resource: "registry:*",
291 action: "*",
292 effect: "allow",
293 },
294 ServicePolicyRule {
295 subject: "service:flight",
296 domain: "*",
297 resource: "registry:*",
298 action: "*",
299 effect: "allow",
300 },
301 ServicePolicyRule {
303 subject: "service:model",
304 domain: "*",
305 resource: "model:*",
306 action: "*",
307 effect: "allow",
308 },
309 ServicePolicyRule {
310 subject: "service:oai",
311 domain: "*",
312 resource: "model:*",
313 action: "*",
314 effect: "allow",
315 },
316 ServicePolicyRule {
318 subject: "service:discovery",
319 domain: "*",
320 resource: "discovery:*",
321 action: "*",
322 effect: "allow",
323 },
324 ServicePolicyRule {
325 subject: "service:model",
326 domain: "*",
327 resource: "discovery:*",
328 action: "*",
329 effect: "allow",
330 },
331 ServicePolicyRule {
332 subject: "service:oai",
333 domain: "*",
334 resource: "discovery:*",
335 action: "*",
336 effect: "allow",
337 },
338 ServicePolicyRule {
339 subject: "service:worker",
340 domain: "*",
341 resource: "discovery:*",
342 action: "*",
343 effect: "allow",
344 },
345 ServicePolicyRule {
354 subject: "service:mcp",
355 domain: "*",
356 resource: "discovery:Announce",
357 action: "write",
358 effect: "allow",
359 },
360 ServicePolicyRule {
362 subject: "service:mcp",
363 domain: "*",
364 resource: "discovery:model",
365 action: "query",
366 effect: "allow",
367 },
368 ServicePolicyRule {
369 subject: "service:metrics",
370 domain: "*",
371 resource: "discovery:Announce",
372 action: "write",
373 effect: "allow",
374 },
375 ServicePolicyRule {
376 subject: "service:notification",
377 domain: "*",
378 resource: "discovery:Announce",
379 action: "write",
380 effect: "allow",
381 },
382 ServicePolicyRule {
383 subject: "service:registry",
384 domain: "*",
385 resource: "discovery:Announce",
386 action: "write",
387 effect: "allow",
388 },
389 ServicePolicyRule {
390 subject: "service:tui",
391 domain: "*",
392 resource: "discovery:Announce",
393 action: "write",
394 effect: "allow",
395 },
396 ServicePolicyRule {
397 subject: "service:oauth",
398 domain: "*",
399 resource: "discovery:Announce",
400 action: "write",
401 effect: "allow",
402 },
403 ServicePolicyRule {
413 subject: "service:*",
414 domain: "*",
415 resource: "discovery:policy",
416 action: "query",
417 effect: "allow",
418 },
419 ServicePolicyRule {
423 subject: "service:oauth",
424 domain: "*",
425 resource: "discovery:registry",
426 action: "query",
427 effect: "allow",
428 },
429 ServicePolicyRule {
430 subject: "service:oauth",
431 domain: "*",
432 resource: "discovery:model",
433 action: "query",
434 effect: "allow",
435 },
436 ServicePolicyRule {
437 subject: "service:event",
438 domain: "*",
439 resource: "discovery:Announce",
440 action: "write",
441 effect: "allow",
442 },
443 ServicePolicyRule {
444 subject: "service:streams",
445 domain: "*",
446 resource: "discovery:Announce",
447 action: "write",
448 effect: "allow",
449 },
450 ServicePolicyRule {
451 subject: "service:flight",
452 domain: "*",
453 resource: "discovery:Announce",
454 action: "write",
455 effect: "allow",
456 },
457 ServicePolicyRule {
458 subject: "service:policy",
459 domain: "*",
460 resource: "discovery:Announce",
461 action: "write",
462 effect: "allow",
463 },
464 ServicePolicyRule {
484 subject: "service:oauth",
485 domain: "*",
486 resource: "oauth:*",
487 action: "*",
488 effect: "allow",
489 },
490 ServicePolicyRule {
491 subject: "service:streams",
492 domain: "*",
493 resource: "streams:*",
494 action: "*",
495 effect: "allow",
496 },
497 ServicePolicyRule {
498 subject: "service:event",
499 domain: "*",
500 resource: "event:*",
501 action: "*",
502 effect: "allow",
503 },
504];
505
506pub fn base_policies_to_vec() -> Vec<Vec<String>> {
508 SERVICE_BASE_POLICIES
509 .iter()
510 .map(ServicePolicyRule::to_vec)
511 .collect()
512}
513
514pub fn base_policies_to_csv() -> String {
516 SERVICE_BASE_POLICIES
517 .iter()
518 .map(ServicePolicyRule::to_csv_line)
519 .collect::<Vec<_>>()
520 .join("\n")
521}
522
523pub struct PolicyTemplate {
529 pub name: &'static str,
530 pub description: &'static str,
531 pub policies: Option<&'static [ServicePolicyRule]>,
533 pub groupings: Option<&'static [ServiceGrouping]>,
535}
536
537impl PolicyTemplate {
538 pub fn expanded_policies(&self) -> Vec<ServicePolicyRule> {
544 self.policies.map(<[_]>::to_vec).unwrap_or_default()
545 }
546
547 pub fn to_csv(&self) -> String {
552 let mut lines = Vec::new();
553 let policies = self.expanded_policies();
554 for p in &policies {
555 lines.push(p.to_csv_line());
556 }
557 if let Some(groupings) = self.groupings {
558 for g in groupings {
559 lines.push(g.to_csv_line());
560 }
561 }
562 lines.join("\n")
563 }
564}
565
566pub fn get_templates() -> &'static [PolicyTemplate] {
578 &[
579 PolicyTemplate {
580 name: "federation-open",
581 description: "Open federation: accept third-party apps and remote peer servers from any HTTPS origin (atproto-style, recommended for MCP/peer compatibility)",
582 policies: Some(&[
583 ServicePolicyRule {
589 subject: "*",
590 domain: "*",
591 resource: "federation:register:*",
592 action: "check",
593 effect: "allow",
594 },
595 ]),
596 groupings: None,
597 },
598 PolicyTemplate {
599 name: "cyberdione-staging-ui",
600 description: "Cyberdione staging only: allow OAuth CIMD and peer registration from https://www.staging.lab.hyprstream.com",
601 policies: Some(&[
602 ServicePolicyRule {
607 subject: "*",
608 domain: "*",
609 resource: "federation:register:https://www.staging.lab.hyprstream.com",
610 action: "check",
611 effect: "allow",
612 },
613 ]),
614 groupings: None,
615 },
616 PolicyTemplate {
617 name: "cyberdione-staging-e2e",
618 description: "One Federate staging test DID in the local tenant, limited to the pinned Registry and Model acceptance calls",
619 policies: Some(&[
620 ServicePolicyRule { subject: "did:plc:3f7ldey5bivhfjapwqi2sljy", domain: "local", resource: "registry:qwen2.5-0.5b-instruct", action: "query", effect: "allow" },
621 ServicePolicyRule { subject: "did:plc:3f7ldey5bivhfjapwqi2sljy", domain: "local", resource: "model:Load", action: "write", effect: "allow" },
622 ServicePolicyRule { subject: "did:plc:3f7ldey5bivhfjapwqi2sljy", domain: "local", resource: "model:Status", action: "query", effect: "allow" },
623 ServicePolicyRule { subject: "did:plc:3f7ldey5bivhfjapwqi2sljy", domain: "local", resource: "model:qwen2.5-0.5b-instruct:main", action: "infer", effect: "allow" },
624 ]),
625 groupings: None,
626 },
627 PolicyTemplate {
628 name: "cyberdione-staging-operator",
629 description: "One authorized staging operator DID in the local tenant, limited to the pinned Registry and Model acceptance calls",
630 policies: Some(&[
631 ServicePolicyRule { subject: "did:plc:rjqczurevrpoz4mcnax2kgw4", domain: "local", resource: "registry:qwen2.5-0.5b-instruct", action: "query", effect: "allow" },
632 ServicePolicyRule { subject: "did:plc:rjqczurevrpoz4mcnax2kgw4", domain: "local", resource: "model:Load", action: "write", effect: "allow" },
633 ServicePolicyRule { subject: "did:plc:rjqczurevrpoz4mcnax2kgw4", domain: "local", resource: "model:Status", action: "query", effect: "allow" },
634 ServicePolicyRule { subject: "did:plc:rjqczurevrpoz4mcnax2kgw4", domain: "local", resource: "model:qwen2.5-0.5b-instruct:main", action: "infer", effect: "allow" },
635 ]),
636 groupings: None,
637 },
638 PolicyTemplate {
639 name: "public-inference",
640 description: "Anonymous users can infer and query models",
641 policies: Some(&[
642 ServicePolicyRule { subject: "anonymous", domain: "*", resource: "model:*", action: "infer.generate", effect: "allow" },
643 ServicePolicyRule { subject: "anonymous", domain: "*", resource: "model:*", action: "query.status", effect: "allow" },
644 ]),
645 groupings: None,
646 },
647 PolicyTemplate {
648 name: "public-read",
649 description: "Anonymous users can query the registry (read-only)",
650 policies: Some(&[
651 ServicePolicyRule { subject: "anonymous", domain: "*", resource: "registry:*", action: "query.status", effect: "allow" },
652 ]),
653 groupings: None,
654 },
655 PolicyTemplate {
656 name: "ttt.user",
657 description: "TTT user: infer, query, persist on all models",
658 policies: Some(&[
659 ServicePolicyRule { subject: "ttt.user", domain: "*", resource: "model:*", action: "infer.generate", effect: "allow" },
660 ServicePolicyRule { subject: "ttt.user", domain: "*", resource: "model:*", action: "query.status", effect: "allow" },
661 ServicePolicyRule { subject: "ttt.user", domain: "*", resource: "model:*", action: "query.delta", effect: "allow" },
662 ServicePolicyRule { subject: "ttt.user", domain: "*", resource: "model:*", action: "persist.save", effect: "allow" },
663 ServicePolicyRule { subject: "ttt.user", domain: "*", resource: "model:*", action: "persist.export", effect: "allow" },
664 ServicePolicyRule { subject: "ttt.user", domain: "*", resource: "model:*", action: "persist.snapshot", effect: "allow" },
665 ]),
666 groupings: None,
667 },
668 PolicyTemplate {
669 name: "ttt.agent",
670 description: "TTT agent: alias for ttt.user",
671 policies: None,
672 groupings: Some(&[
673 ServiceGrouping { user: "ttt.agent", role: "ttt.user" },
674 ]),
675 },
676 PolicyTemplate {
713 name: "mesh-host",
714 description: "Inference mesh (#319): per-host pipeline RPC grants for tenant `acme` (worked example — substitute real host labels/tenant; authority is strictly non-wildcard per-host)",
715 policies: Some(&[
716 ServicePolicyRule { subject: "service:model", domain: "acme", resource: "mesh://acme/*", action: "infer.stage", effect: "allow" },
719 ServicePolicyRule { subject: "service:inference:host-1", domain: "acme", resource: "mesh://acme/*", action: "mesh.rpc", effect: "allow" },
721 ServicePolicyRule { subject: "service:inference:host-1", domain: "acme", resource: "mesh://acme/*", action: "infer.stage", effect: "allow" },
722 ServicePolicyRule { subject: "service:inference:host-1", domain: "acme", resource: "mesh://acme/*", action: "query.status", effect: "allow" },
723 ServicePolicyRule { subject: "service:inference:host-2", domain: "acme", resource: "mesh://acme/*", action: "mesh.rpc", effect: "allow" },
724 ServicePolicyRule { subject: "service:inference:host-2", domain: "acme", resource: "mesh://acme/*", action: "infer.stage", effect: "allow" },
725 ServicePolicyRule { subject: "service:inference:host-2", domain: "acme", resource: "mesh://acme/*", action: "query.status", effect: "allow" },
726 ServicePolicyRule { subject: "service:inference:host-1", domain: "acme", resource: "mesh://acme/*", action: "delta.submit", effect: "allow" },
728 ServicePolicyRule { subject: "service:inference:host-2", domain: "acme", resource: "mesh://acme/*", action: "delta.submit", effect: "allow" },
729 ]),
730 groupings: None,
731 },
732 PolicyTemplate {
739 name: "mesh-host-group",
740 description: "Inference mesh (#319): grant the read ability (query.status) to the `mesh-readers` host group for tenant `acme` (authority stays per-host via `mesh-host`)",
741 policies: Some(&[
742 ServicePolicyRule { subject: "mesh-readers", domain: "acme", resource: "mesh://acme/*", action: "query.status", effect: "allow" },
743 ]),
744 groupings: Some(&[
745 ServiceGrouping { user: "service:inference:host-1", role: "mesh-readers" },
746 ServiceGrouping { user: "service:inference:host-2", role: "mesh-readers" },
747 ]),
748 },
749 PolicyTemplate {
750 name: "ttt.privileged",
751 description: "TTT privileged: extends ttt.user with ttt.* operations",
752 policies: Some(&[
753 ServicePolicyRule { subject: "ttt.privileged", domain: "*", resource: "model:*", action: "ttt.train", effect: "allow" },
754 ServicePolicyRule { subject: "ttt.privileged", domain: "*", resource: "model:*", action: "ttt.writeback", effect: "allow" },
755 ServicePolicyRule { subject: "ttt.privileged", domain: "*", resource: "model:*", action: "ttt.evict", effect: "allow" },
756 ServicePolicyRule { subject: "ttt.privileged", domain: "*", resource: "model:*", action: "ttt.zero", effect: "allow" },
757 ]),
758 groupings: Some(&[
759 ServiceGrouping { user: "ttt.privileged", role: "ttt.user" },
760 ]),
761 },
762 ]
763}
764
765pub fn get_template(name: &str) -> Option<&'static PolicyTemplate> {
767 get_templates().iter().find(|t| t.name == name)
768}