-
Notifications
You must be signed in to change notification settings - Fork 32
/
primitive.go
534 lines (463 loc) · 14.6 KB
/
primitive.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
// Package primitive contains definitions of the primitive types used
// in ag.
package primitive
import (
"encoding/json"
"strings"
"time"
"github.com/arigatomachine/cli/base64"
"github.com/arigatomachine/cli/identity"
"github.com/arigatomachine/cli/pathexp"
)
// v1Schema embeds in other structs to indicate their schema version is 1.
type v1Schema struct{}
// Version returns the schema version of structs that embed this type.
func (v1Schema) Version() int {
return 1
}
// Embedding the immutable or mutable struct in a struct denotes if it is
// immutable and should be signed, or a mutable type.
type immutable struct{}
func (immutable) Immutable() {}
type mutable struct{}
func (mutable) Mutable() {}
// v2Schema embeds in other structs to indicate their schema version is 2.
type v2Schema struct{}
// Version returns the schema version of structs that embed this type.
func (v2Schema) Version() int {
return 2
}
// User is the body of a user object
type User struct { // type: 0x01
v1Schema
mutable
Username string `json:"username"`
Name string `json:"name"`
Email string `json:"email"`
State string `json:"state"`
Password *UserPassword `json:"password"`
Master *UserMaster `json:"master"`
}
// UserMaster is the body.master object for a user
type UserMaster struct {
Value *base64.Value `json:"value"`
Alg string `json:"alg"`
}
// UserPassword is the body.password object for a user
type UserPassword struct {
Salt string `json:"salt"`
Value *base64.Value `json:"value"`
Alg string `json:"alg"`
}
// Signature is an immutable object, but not technically a payload.
// If PublicKeyID is nil, the signature is self-signed.
type Signature struct {
Algorithm string `json:"alg"`
PublicKeyID *identity.ID `json:"public_key_id"`
Value *base64.Value `json:"value"`
}
// PrivateKeyValue holds the encrypted value of the PrivateKey.
type PrivateKeyValue struct {
Algorithm string `json:"alg"`
Value *base64.Value `json:"value"`
}
// PrivateKey is the private portion of an asymetric key.
type PrivateKey struct { // type: 0x07
v1Schema
immutable
Key PrivateKeyValue `json:"key"`
OrgID *identity.ID `json:"org_id"`
OwnerID *identity.ID `json:"owner_id"`
PNonce *base64.Value `json:"pnonce"`
PublicKeyID *identity.ID `json:"public_key_id"`
}
// PublicKeyValue is the actual value of a PublicKey.
type PublicKeyValue struct {
Value *base64.Value `json:"value"`
}
// PublicKey is the public portion of an asymetric key.
type PublicKey struct { // type: 0x06
v1Schema
immutable
Algorithm string `json:"alg"`
Created time.Time `json:"created_at"`
Expires time.Time `json:"expires_at"`
Key PublicKeyValue `json:"key"`
OrgID *identity.ID `json:"org_id"`
OwnerID *identity.ID `json:"owner_id"`
KeyType string `json:"type"`
}
// Types of claims that can be made against public keys.
const (
SignatureClaimType = "signature"
RevocationClaimType = "revocation"
)
// Claim is a signature or revocation claim against a public key.
type Claim struct { // type: 0x08
v1Schema
immutable
Created time.Time `json:"created_at"`
OrgID *identity.ID `json:"org_id"`
OwnerID *identity.ID `json:"owner_id"`
Previous *identity.ID `json:"previous"`
PublicKeyID *identity.ID `json:"public_key_id"`
KeyType string `json:"type"`
}
// NewClaim returns a new Claim, with the created time set to now
func NewClaim(orgID, ownerID, previous, pubKeyID *identity.ID,
keyType string) *Claim {
return &Claim{
OrgID: orgID,
OwnerID: ownerID,
Previous: previous,
PublicKeyID: pubKeyID,
KeyType: keyType,
Created: time.Now().UTC(),
}
}
// Credential is a secret value shared between a group of services based
// on users identity, operating environment, project, and organization
type Credential struct { // type: 0x0b
v2Schema
immutable
BaseCredential
State *string `json:"state"`
}
// CredentialV1 is a secret value shared between a group of services based
// on users identity, operating environment, project, and organization
type CredentialV1 struct { // type: 0x0b
v1Schema
immutable
BaseCredential
}
// BaseCredential is a secret value shared between a group of services based
// on users identity, operating environment, project, and organization
type BaseCredential struct {
Credential *CredentialValue `json:"credential"`
KeyringID *identity.ID `json:"keyring_id"`
Name string `json:"name"`
Nonce *base64.Value `json:"nonce"`
OrgID *identity.ID `json:"org_id"`
PathExp *pathexp.PathExp `json:"pathexp"`
Previous *identity.ID `json:"previous"`
ProjectID *identity.ID `json:"project_id"`
CredentialVersion int `json:"version"`
}
// CredentialValue is the secretbox encrypted value of the containing
// Credential.
type CredentialValue struct {
Algorithm string `json:"alg"`
Nonce *base64.Value `json:"nonce"`
Value *base64.Value `json:"value"`
}
// BaseKeyring is the shared structure between keyring schema versions.
type BaseKeyring struct {
immutable
Created time.Time `json:"created_at"`
OrgID *identity.ID `json:"org_id"`
PathExp *pathexp.PathExp `json:"pathexp"`
Previous *identity.ID `json:"previous"`
ProjectID *identity.ID `json:"project_id"`
KeyringVersion int `json:"version"`
}
// KeyringV1 is the old keyring format, without claims or mekshares.
type KeyringV1 struct { // type: 0x09
v1Schema
BaseKeyring
}
// Keyring is a mechanism for sharing a shared secret between many different
// users and machines at a position in the credential path.
//
// Credentials belong to Keyrings
type Keyring struct { // type: 0x09
v2Schema
BaseKeyring
}
// NewKeyring returns a new v2 Keyring, with the created time set to now
func NewKeyring(orgID, projectID *identity.ID, pathExp *pathexp.PathExp) *Keyring {
return &Keyring{
BaseKeyring: BaseKeyring{
Created: time.Now().UTC(),
OrgID: orgID,
PathExp: pathExp,
ProjectID: projectID,
// This is the first instance of the keyring, so version is 1,
// and there is no previous instance.
Previous: nil,
KeyringVersion: 1,
},
}
}
// KeyringMemberV1 is a record of sharing a master secret key with a user or
// machine.
//
// KeyringMember belongs to a Keyring
type KeyringMemberV1 struct { // type: 0x0a
v1Schema
immutable
Created time.Time `json:"created_at"`
EncryptingKeyID *identity.ID `json:"encrypting_key_id"`
Key *KeyringMemberKey `json:"key"`
KeyringID *identity.ID `json:"keyring_id"`
OrgID *identity.ID `json:"org_id"`
OwnerID *identity.ID `json:"owner_id"`
ProjectID *identity.ID `json:"project_id"`
PublicKeyID *identity.ID `json:"public_key_id"`
}
// KeyringMember is a record of sharing a master secret key with a user or
// machine.
//
// This is the v2 schema version, which has a detached mekshare so it can be
// revoked.
//
// KeyringMember belongs to a Keyring
type KeyringMember struct { // type: 0x0a
v2Schema
immutable
Created time.Time `json:"created_at"`
EncryptingKeyID *identity.ID `json:"encrypting_key_id"`
KeyringID *identity.ID `json:"keyring_id"`
OrgID *identity.ID `json:"org_id"`
OwnerID *identity.ID `json:"owner_id"`
PublicKeyID *identity.ID `json:"public_key_id"`
}
// KeyringMemberKey is the keyring master encryption key, encrypted for the
// owner of a KeyringMember/MEKShare
type KeyringMemberKey struct {
Algorithm string `json:"alg"`
Nonce *base64.Value `json:"nonce"`
Value *base64.Value `json:"value"`
}
// MEKShare is a V2 KeyringMember's share of the keyring master encryption key.
type MEKShare struct { // type: 0x16
v1Schema
immutable
Created time.Time `json:"created_at"`
OrgID *identity.ID `json:"org_id"`
OwnerID *identity.ID `json:"owner_id"`
KeyringID *identity.ID `json:"keyring_id"`
KeyringMemberID *identity.ID `json:"keyring_member_id"`
Key *KeyringMemberKey `json:"key"`
}
// KeyringMemberClaim is a claim for a keyring member. Only revocation is
// supported as a claim type.
type KeyringMemberClaim struct { // type: 0x15
v1Schema
immutable
OrgID *identity.ID `json:"org_id"`
KeyringID *identity.ID `json:"keyring_id"`
KeyringMemberID *identity.ID `json:"keyring_member_id"`
OwnerID *identity.ID `json:"owner_id"`
Previous *identity.ID `json:"previous"`
ClaimType string `json:"type"`
Created time.Time `json:"created_at"`
}
// Org is a grouping of users that collaborate with each other
type Org struct { // type: 0x0d
v1Schema
mutable
Name string `json:"name"`
}
// Org Invitations exist in four states: pending, associated,
// accepted, and approved.
const (
OrgInvitePendingState = "pending"
OrgInviteAssociatedState = "associated"
OrgInviteAcceptedState = "accepted"
OrgInviteApprovedState = "approved"
)
// OrgInvite is an invitation for an individual to join an organization
type OrgInvite struct { // type: 0x13
v1Schema
mutable
OrgID *identity.ID `json:"org_id"`
Email string `json:"email"`
InviterID *identity.ID `json:"inviter_id"`
InviteeID *identity.ID `json:"invitee_id"`
ApproverID *identity.ID `json:"approver_id"`
State string `json:"state"`
Code *struct {
Alg string `json:"alg"`
Salt *base64.Value `json:"salt"`
Value *base64.Value `json:"value"`
} `json:"code"`
PendingTeams []identity.ID `json:"pending_teams"`
Created *time.Time `json:"created_at"`
Accepted *time.Time `json:"accepted_at"`
Approved *time.Time `json:"approved_at"`
}
// Project is an entity that represents a group of services
type Project struct { // type: 0x04
v1Schema
mutable
Name string `json:"name"`
OrgID *identity.ID `json:"org_id"`
}
// Policy is an entity that represents a group of statements for acl
type Policy struct { // type: 0x11
v1Schema
mutable
PolicyType string `json:"type"`
Previous *identity.ID `json:"previous"`
OrgID *identity.ID `json:"org_id"`
Policy struct {
Name string `json:"name"`
Description string `json:"description"`
Statements []PolicyStatement `json:"statements"`
} `json:"policy"`
}
// PolicyStatement is an acl statement on a policy object
type PolicyStatement struct {
Effect PolicyEffect `json:"effect"`
Action PolicyAction `json:"action"`
Resource string `json:"resource"`
}
// PolicyEffect is the effect type of the statement (allow or deny)
type PolicyEffect bool
// These are the two policy effect types
const (
PolicyEffectAllow = true
PolicyEffectDeny = false
)
// MarshalText implements the encoding.TextMarshaler interface, used for JSON
// marshaling.
func (pe *PolicyEffect) MarshalText() ([]byte, error) {
if *pe {
return []byte("allow"), nil
}
return []byte("deny"), nil
}
// UnmarshalText implements the encoding.TextUnmarshaler interface, used for
// JSON unmarshaling.
func (pe *PolicyEffect) UnmarshalText(b []byte) error {
*pe = string(b) == "allow"
return nil
}
// String returns a string representation of the PolicyEffect (allow or deny)
func (pe *PolicyEffect) String() string {
b, _ := pe.MarshalText()
return string(b)
}
// PolicyAction represents the user actions that are covered by a statement.
type PolicyAction byte
// These are all the possible PolicyActions
const (
PolicyActionCreate = 1 << iota
PolicyActionRead
PolicyActionUpdate
PolicyActionDelete
PolicyActionList
)
var policyActionStrings = []string{
"create",
"read",
"update",
"delete",
"list",
}
// MarshalJSON implements the json.Marshaler interface. A PolicyAction is
// encoded in JSON either the string representations of its actions in a list,
// or a single string when there is only one action.
func (pa *PolicyAction) MarshalJSON() ([]byte, error) {
out := []string{}
for i, v := range policyActionStrings {
if (1<<uint(i))&byte(*pa) > 0 {
out = append(out, v)
}
}
if len(out) == 1 {
return json.Marshal(out[0])
}
return json.Marshal(out)
}
// UnmarshalJSON implements the json.Unmarshaler interface.
func (pa *PolicyAction) UnmarshalJSON(b []byte) error {
raw := make([]string, 1)
var err error
if len(b) > 0 && b[0] == '"' {
err = json.Unmarshal(b, &raw[0])
} else {
err = json.Unmarshal(b, &raw)
}
if err != nil {
return err
}
for _, a := range raw {
for i, v := range policyActionStrings {
if a == v {
*pa |= 1 << uint(i)
continue
}
}
}
return nil
}
func (pa *PolicyAction) String() string {
out := []string{}
for i, v := range policyActionStrings {
if (1<<uint(i))&byte(*pa) > 0 {
out = append(out, v)
}
}
return strings.Join(out, ", ")
}
// ShortString displays a single character representation of each of the
// policy's actions.
func (pa *PolicyAction) ShortString() string {
out := []byte{}
for i, v := range policyActionStrings {
if (1<<uint(i))&byte(*pa) > 0 {
out = append(out, v[0])
} else {
out = append(out, '-')
}
}
return string(out)
}
// PolicyAttachment is an entity that represents the link between policies and teams
type PolicyAttachment struct { // type: 0x12
v1Schema
mutable
OwnerID *identity.ID `json:"owner_id"`
PolicyID *identity.ID `json:"policy_id"`
OrgID *identity.ID `json:"org_id"`
}
// Service is an entity that represents a group of processes
type Service struct { // type: 0x03
v1Schema
mutable
Name string `json:"name"`
OrgID *identity.ID `json:"org_id"`
ProjectID *identity.ID `json:"project_id"`
}
// Environment is an entity that represents a group of processes
type Environment struct { // type: 0x05
v1Schema
mutable
Name string `json:"name"`
OrgID *identity.ID `json:"org_id"`
ProjectID *identity.ID `json:"project_id"`
}
// There are two types of teams: system and user. System teams are
// managed by the Torus registry.
const (
SystemTeam = "system"
UserTeam = "user"
)
// Team is an entity that represents a group of users
type Team struct { // type: 0x0f
v1Schema
mutable
Name string `json:"name"`
OrgID *identity.ID `json:"org_id"`
TeamType string `json:"type"`
}
// Membership is an entity that represents whether a user or
// machine is a part of a team in an organization.
type Membership struct { // type: 0x0e
v1Schema
mutable
OrgID *identity.ID `json:"org_id"`
OwnerID *identity.ID `json:"owner_id"`
TeamID *identity.ID `json:"team_id"`
}