-
Notifications
You must be signed in to change notification settings - Fork 402
/
identities.go
144 lines (119 loc) · 4.76 KB
/
identities.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
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information
package testidentity
import (
"errors"
"storj.io/storj/pkg/identity"
"storj.io/storj/pkg/storj"
)
//go:generate go run gen_identities.go -version 0 -count 150 -out V0_identities_table.go
//go:generate go run gen_identities.go -signed -version 0 -count 150 -out V0_signed_identities_table.go
var (
// IdentityVersions holds pregenerated identities for each/ identity version.
IdentityVersions = VersionedIdentitiesMap{
storj.V0: pregeneratedV0Identities,
}
// SignedIdentityVersions holds pregenerated, signed identities for each.
// identity version
SignedIdentityVersions = VersionedIdentitiesMap{
storj.V0: pregeneratedV0SignedIdentities,
}
// SignerVersions holds certificate authorities for each identity version.
SignerVersions = VersionedCertificateAuthorityMap{
storj.V0: pregeneratedV0Signer,
}
)
// VersionedIdentitiesMap maps a `storj.IDVersionNumber` to a set of
// pregenerated identities with the corresponding version.
type VersionedIdentitiesMap map[storj.IDVersionNumber]*Identities
// VersionedCertificateAuthorityMap maps a `storj.IDVersionNumber` to a set of
// pregenerated certificate authorities used for signing the corresponding
// version of signed identities.
type VersionedCertificateAuthorityMap map[storj.IDVersionNumber]*identity.FullCertificateAuthority
// Identities is a pregenerated full identity table.
type Identities struct {
list []*identity.FullIdentity
next int
}
// NewIdentities creates a new table from provided identities.
func NewIdentities(list ...*identity.FullIdentity) *Identities {
return &Identities{
list: list,
next: 0,
}
}
// MustPregeneratedIdentity returns a pregenerated identity or panics
func MustPregeneratedIdentity(index int, version storj.IDVersion) *identity.FullIdentity {
identity, err := PregeneratedIdentity(index, version)
if err != nil {
panic(err)
}
return identity
}
// MustPregeneratedSignedIdentity returns a pregenerated identity or panics
func MustPregeneratedSignedIdentity(index int, version storj.IDVersion) *identity.FullIdentity {
identity, err := PregeneratedSignedIdentity(index, version)
if err != nil {
panic(err)
}
return identity
}
// PregeneratedIdentity returns a pregenerated identity from a list
func PregeneratedIdentity(index int, version storj.IDVersion) (*identity.FullIdentity, error) {
pregeneratedIdentities := IdentityVersions[version.Number]
if pregeneratedIdentities.next >= len(pregeneratedIdentities.list) {
return nil, errors.New("out of pregenerated identities")
}
return pregeneratedIdentities.list[index], nil
}
// PregeneratedSignedIdentity returns a signed pregenerated identity from a list
func PregeneratedSignedIdentity(index int, version storj.IDVersion) (*identity.FullIdentity, error) {
pregeneratedSignedIdentities := SignedIdentityVersions[version.Number]
if pregeneratedSignedIdentities.next >= len(pregeneratedSignedIdentities.list) {
return nil, errors.New("out of signed pregenerated identities")
}
return pregeneratedSignedIdentities.list[index], nil
}
// NewPregeneratedIdentities retruns a new table from provided identities.
func NewPregeneratedIdentities(version storj.IDVersion) *Identities {
return IdentityVersions[version.Number].Clone()
}
// NewPregeneratedSignedIdentities retruns a new table from provided signed identities.
func NewPregeneratedSignedIdentities(version storj.IDVersion) *Identities {
return SignedIdentityVersions[version.Number].Clone()
}
// NewPregeneratedSigner returns the signer for all pregenerated, signed identities
func NewPregeneratedSigner(version storj.IDVersion) *identity.FullCertificateAuthority {
return SignerVersions[version.Number]
}
// Clone creates a shallow clone of the table.
func (identities *Identities) Clone() *Identities {
return NewIdentities(identities.list...)
}
// NewIdentity gets a new identity from the list.
func (identities *Identities) NewIdentity() (*identity.FullIdentity, error) {
if identities.next >= len(identities.list) {
return nil, errors.New("out of pregenerated identities")
}
id := identities.list[identities.next]
identities.next++
return id, nil
}
// mustParseIdentityPEM parses pem encoded identity chain and key strings.
func mustParseIdentityPEM(chain, key string) *identity.FullIdentity {
// TODO: add whitelist handling somehow
fi, err := identity.FullIdentityFromPEM([]byte(chain), []byte(key))
if err != nil {
panic(err)
}
return fi
}
// mustParseCertificateAuthorityPEM parses pem encoded certificate authority chain and key strings.
func mustParseCertificateAuthorityPEM(chain, key string) *identity.FullCertificateAuthority {
// TODO: add whitelist handling somehow
fi, err := identity.FullCertificateAuthorityFromPEM([]byte(chain), []byte(key))
if err != nil {
panic(err)
}
return fi
}