-
Notifications
You must be signed in to change notification settings - Fork 3
/
caveat.go
183 lines (158 loc) · 4.63 KB
/
caveat.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package macaroon
import (
"reflect"
"strconv"
)
// A numeric identifier for caveat types. Values less than
// CavMinUserRegisterable (0x100000000) are reserved for use by fly.io. Users
// may request a globally-recognized caveat type via pull requests to this
// repository. Implementations that don't need to integrate with fly.io itself
// can pick from the user-defined range (>0x1000000000000).
type CaveatType uint64
const (
CavFlyioOrganization CaveatType = iota
_ // deprecated
CavFlyioVolumes
CavFlyioApps
CavValidityWindow
CavFlyioFeatureSet
CavFlyioMutations
CavFlyioMachines
CavAuthConfineUser
CavAuthConfineOrganization
CavFlyioIsUser
Cav3P
CavBindToParentToken
CavIfPresent
CavFlyioMachineFeatureSet
CavFlyioFromMachineSource
CavFlyioClusters
_ // fly.io reserved
_ // fly.io reserved
CavAuthConfineGoogleHD
CavAuthConfineGitHubOrg
CavAuthMaxValidity
CavFlyioIsMember
AttestationAuthFlyioUserID
AttestationAuthGitHubUserID
AttestationAuthGoogleUserID
CavAction
CavFlyioCommands
CavFlyioAppFeatureSet
CavFlyioStorageObjects
CavAllowedRoles
// allocate internal blocks of size 255 here
block255Min CaveatType = 1 << 16
BlockPetsemMin = block255Min
BlockPetsemMax = BlockPetsemMin + 0xff
block255Max CaveatType = 1<<17 - 1
// Globally-recognized user-registerable caveat types may be requested via
// pull requests to this repository. Add a meaningful name of the caveat
// type (e.g. CavAcmeCorpWidgetID) on the line prior to
// CavMaxUserRegisterable.
CavMinUserRegisterable CaveatType = 1 << 32
CavMaxUserRegisterable CaveatType = 1<<48 - 1
CavMinUserDefined CaveatType = 1 << 48
CavMaxUserDefined CaveatType = 1<<64 - 2
CavUnregistered CaveatType = 1<<64 - 1
)
// Caveat is the interface implemented by all caveats.
type Caveat interface {
// The numeric caveat type identifier.
CaveatType() CaveatType
// The string name of the caveat. Used for JSON encoding.
Name() string
// Callback for checking if the authorization check is blocked by this
// caveat. Implementors must take care to return appropriate error types,
// as they have bearing on the evaluation of IfPresent caveats.
// Specifically, returning ErrResourceUnspecified indicates that caveat
// constrains access to a resource type that isn't specified by the Access.
Prohibits(f Access) error
}
// Attestations make a positive assertion rather than constraining access to a
// resource. Most caveats are not attestations. Attestations may only be
// included in Proofs (macaroons whose signature is finalized and cannot have
// more caveats appended by the user).
type Attestation interface {
Caveat
// Whether or not this caveat type is an attestation.
IsAttestation() bool
}
func IsAttestation(c Caveat) bool {
a, ok := c.(Attestation)
return ok && a.IsAttestation()
}
// WrapperCaveat should be implemented by caveats that wrap other caveats (eg.
// resset.IfPresent).
type WrapperCaveat interface {
Unwrap() *CaveatSet
}
var (
t2c = map[CaveatType]Caveat{}
s2t = map[string]CaveatType{}
t2s = map[CaveatType]string{}
)
// Register a caveat type for use with this library.
func RegisterCaveatType(zeroValue Caveat) {
typ := zeroValue.CaveatType()
name := zeroValue.Name()
if _, dup := t2c[typ]; dup {
panic("duplicate caveat type")
}
if _, dup := t2s[typ]; dup {
panic("duplicate caveat type")
}
if _, dup := s2t[name]; dup {
panic("duplicate caveat type")
}
t2c[typ] = zeroValue
t2s[typ] = name
s2t[name] = typ
}
func unregisterCaveatType(zeroValue Caveat) {
typ := zeroValue.CaveatType()
name := zeroValue.Name()
delete(t2c, typ)
delete(t2s, typ)
delete(s2t, name)
}
// Register an alternate name for this caveat type that will be recognized when
// decoding JSON.
func RegisterCaveatJSONAlias(typ CaveatType, alias string) {
if _, dup := s2t[alias]; dup {
panic("duplicate caveat type")
}
if _, exist := t2s[typ]; !exist {
panic("unregistered caveat type")
}
s2t[alias] = typ
}
func unegisterCaveatJSONAlias(alias string) {
delete(s2t, alias)
}
func typeToCaveat(t CaveatType) Caveat {
cav, ok := t2c[t]
if !ok {
return &UnregisteredCaveat{Type: t}
}
ct := reflect.TypeOf(cav)
if ct.Kind() == reflect.Pointer {
return reflect.New(ct.Elem()).Interface().(Caveat)
}
return reflect.Zero(ct).Interface().(Caveat)
}
func caveatTypeFromString(s string) CaveatType {
if t, ok := s2t[s]; ok {
return t
}
if t, err := strconv.ParseUint(s, 10, 64); err == nil {
return CaveatType(t)
}
return CavUnregistered
}
func caveatTypeToString(t CaveatType) string {
if s, ok := t2s[t]; ok && t < CavMinUserDefined {
return s
}
return strconv.FormatUint(uint64(t), 10)
}