gunk-dev/the-valley

access.go

sourceddfdc45c1aa2main's published tip history
3.9 KiB
1// Package readaccess decides who may read a project.
2//
3// Read access is a grant to a principal, and it takes two consents. A
4// project's own policy/read (schema/read.cue) names the principals the
5// project lets read it: its Consent. The instance's identity registry
6// (schema/identity.cue) grants principals read at an edge read boundary,
7// each grant for a set of projects or for every project: its Grants. A
8// principal may read a project only where both say so. Anyone is the
9// principal for a reader who presents no credential, so a project is public
10// at a destination where both consents let Anyone read it.
11//
12// No principal is special. The operator reads a project as any other named
13// reader does: by its policy/read and an instance grant.
14//
15// The package has two halves. Consent, Scope and Grants hold what the two
16// consents say, and Grants' methods evaluate them, with no I/O. Reader
17// reads them out of git commits with cue (read.go).
18package readaccess
19
20import "slices"
21
22// Anyone is the reserved principal for a reader who presents no credential.
23const Anyone = "anyone"
24
25// Consent is a project's consent to read access: what its policy/read
26// declares at one commit.
27type Consent struct {
28 // Project is the project the policy is for, and Identity is its
29 // publication identity: 32 lowercase hex digits that an instance grant
30 // naming the project names too.
31 Project, Identity string
32
33 // Readers are the principals the project lets read it, sorted, each
34 // once. No reader lets no one read.
35 Readers []string
36}
37
38// Names reports whether the consent names the principal as a reader.
39func (c Consent) Names(principal string) bool {
40 _, found := slices.BinarySearch(c.Readers, principal)
41 return found
42}
43
44// Scope is the projects one read grant covers.
45type Scope struct {
46 // All covers every project, whatever its identity.
47 All bool
48
49 // Projects maps each project the grant covers to the publication
50 // identity the grant names for it. An empty map covers nothing.
51 Projects map[string]string
52}
53
54// Covers reports whether the scope covers the project with that
55// publication identity.
56func (s Scope) Covers(project, identity string) bool {
57 if s.All {
58 return true
59 }
60 id, ok := s.Projects[project]
61 return ok && id == identity
62}
63
64// Grants are the instance's read grants at one destination: the scopes each
65// principal holds there, by principal name, with the grants to a reader who
66// presents no credential under Anyone. A principal with no entry holds no
67// read grant there.
68type Grants map[string][]Scope
69
70// Covers reports whether a grant of the principal covers the project with
71// that publication identity: the instance's consent.
72func (g Grants) Covers(principal, project, identity string) bool {
73 for _, s := range g[principal] {
74 if s.Covers(project, identity) {
75 return true
76 }
77 }
78 return false
79}
80
81// Selects reports whether a grant of the principal could cover the project,
82// whatever identity its policy/read declares: the grant covers every
83// project, or names this one. A reader of consents need read the policy of
84// no project that no grant selects.
85func (g Grants) Selects(principal, project string) bool {
86 for _, s := range g[principal] {
87 if _, named := s.Projects[project]; s.All || named {
88 return true
89 }
90 }
91 return false
92}
93
94// May reports whether the principal may read the project the consent is
95// for: the consent names the principal, and a grant of the principal covers
96// the project with the consent's identity.
97func (g Grants) May(principal string, c Consent) bool {
98 return c.Names(principal) && g.Covers(principal, c.Project, c.Identity)
99}
100
101// Readers are the project's effective readers: the principals the consent
102// names that a grant covers the project for, sorted.
103func (g Grants) Readers(c Consent) []string {
104 var readers []string
105 for _, r := range c.Readers {
106 if g.Covers(r, c.Project, c.Identity) {
107 readers = append(readers, r)
108 }
109 }
110 return readers
111}