-
Notifications
You must be signed in to change notification settings - Fork 0
/
trust.go
226 lines (201 loc) · 6.66 KB
/
trust.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
package local
import (
"github.com/gravitational/teleport/lib/backend"
"github.com/gravitational/teleport/lib/services"
"github.com/gravitational/trace"
)
// CA is local implementation of Trust service that
// is using local backend
type CA struct {
backend.Backend
}
// NewCAService returns new instance of CAService
func NewCAService(b backend.Backend) *CA {
return &CA{
Backend: b,
}
}
// DeleteAllCertAuthorities deletes all certificate authorities of a certain type
func (s *CA) DeleteAllCertAuthorities(caType services.CertAuthType) error {
return s.DeleteBucket([]string{"authorities"}, string(caType))
}
// CreateCertAuthority updates or inserts a new certificate authority
func (s *CA) CreateCertAuthority(ca services.CertAuthority) error {
if err := ca.Check(); err != nil {
return trace.Wrap(err)
}
data, err := services.GetCertAuthorityMarshaler().MarshalCertAuthority(ca)
if err != nil {
return trace.Wrap(err)
}
ttl := backend.TTL(s.Clock(), ca.Expiry())
err = s.CreateVal([]string{"authorities", string(ca.GetType())}, ca.GetName(), data, ttl)
if err != nil {
if trace.IsAlreadyExists(err) {
return trace.AlreadyExists("cluster %q already exists", ca.GetName())
}
return trace.Wrap(err)
}
return nil
}
// UpsertCertAuthority updates or inserts a new certificate authority
func (s *CA) UpsertCertAuthority(ca services.CertAuthority) error {
if err := ca.Check(); err != nil {
return trace.Wrap(err)
}
data, err := services.GetCertAuthorityMarshaler().MarshalCertAuthority(ca)
if err != nil {
return trace.Wrap(err)
}
ttl := backend.TTL(s.Clock(), ca.Expiry())
err = s.UpsertVal([]string{"authorities", string(ca.GetType())}, ca.GetName(), data, ttl)
if err != nil {
return trace.Wrap(err)
}
return nil
}
// CompareAndSwapCertAuthority updates the cert authority value
// if the existing value matches existing parameter, returns nil if succeeds,
// trace.CompareFailed otherwise.
func (s *CA) CompareAndSwapCertAuthority(new, existing services.CertAuthority) error {
if err := new.Check(); err != nil {
return trace.Wrap(err)
}
newData, err := services.GetCertAuthorityMarshaler().MarshalCertAuthority(new)
if err != nil {
return trace.Wrap(err)
}
existingData, err := services.GetCertAuthorityMarshaler().MarshalCertAuthority(existing)
if err != nil {
return trace.Wrap(err)
}
ttl := backend.TTL(s.Clock(), new.Expiry())
err = s.CompareAndSwapVal([]string{"authorities", string(new.GetType())}, new.GetName(), newData, existingData, ttl)
if err != nil {
if trace.IsCompareFailed(err) {
return trace.CompareFailed("cluster %v settings have been updated, try again", new.GetName())
}
return trace.Wrap(err)
}
return nil
}
// DeleteCertAuthority deletes particular certificate authority
func (s *CA) DeleteCertAuthority(id services.CertAuthID) error {
if err := id.Check(); err != nil {
return trace.Wrap(err)
}
// when removing a services.CertAuthority also remove any deactivated
// services.CertAuthority as well if they exist.
err := s.DeleteKey([]string{"authorities", "deactivated", string(id.Type)}, id.DomainName)
if err != nil {
if !trace.IsNotFound(err) {
return trace.Wrap(err)
}
}
err = s.DeleteKey([]string{"authorities", string(id.Type)}, id.DomainName)
if err != nil {
return trace.Wrap(err)
}
return nil
}
// ActivateCertAuthority moves a CertAuthority from the deactivated list to
// the normal list.
func (s *CA) ActivateCertAuthority(id services.CertAuthID) error {
data, err := s.GetVal([]string{"authorities", "deactivated", string(id.Type)}, id.DomainName)
if err != nil {
return trace.BadParameter("can not activate CertAuthority which has not been deactivated: %v: %v", id, err)
}
certAuthority, err := services.GetCertAuthorityMarshaler().UnmarshalCertAuthority(data)
if err != nil {
return trace.Wrap(err)
}
err = s.UpsertCertAuthority(certAuthority)
if err != nil {
return trace.Wrap(err)
}
err = s.DeleteKey([]string{"authorities", "deactivated", string(id.Type)}, id.DomainName)
if err != nil {
return trace.Wrap(err)
}
return nil
}
// DeactivateCertAuthority moves a CertAuthority from the normal list to
// the deactivated list.
func (s *CA) DeactivateCertAuthority(id services.CertAuthID) error {
certAuthority, err := s.GetCertAuthority(id, true)
if err != nil {
return trace.NotFound("can not deactivate CertAuthority which does not exist: %v", err)
}
err = s.DeleteCertAuthority(id)
if err != nil {
return trace.Wrap(err)
}
data, err := services.GetCertAuthorityMarshaler().MarshalCertAuthority(certAuthority)
if err != nil {
return trace.Wrap(err)
}
ttl := backend.TTL(s.Clock(), certAuthority.Expiry())
err = s.UpsertVal([]string{"authorities", "deactivated", string(id.Type)}, id.DomainName, data, ttl)
if err != nil {
return trace.Wrap(err)
}
return nil
}
// GetCertAuthority returns certificate authority by given id. Parameter loadSigningKeys
// controls if signing keys are loaded
func (s *CA) GetCertAuthority(id services.CertAuthID, loadSigningKeys bool, opts ...services.MarshalOption) (services.CertAuthority, error) {
if err := id.Check(); err != nil {
return nil, trace.Wrap(err)
}
data, err := s.GetVal([]string{"authorities", string(id.Type)}, id.DomainName)
if err != nil {
return nil, trace.Wrap(err)
}
ca, err := services.GetCertAuthorityMarshaler().UnmarshalCertAuthority(data, opts...)
if err != nil {
return nil, trace.Wrap(err)
}
if err := ca.Check(); err != nil {
return nil, trace.Wrap(err)
}
setSigningKeys(ca, loadSigningKeys)
return ca, nil
}
func setSigningKeys(ca services.CertAuthority, loadSigningKeys bool) {
if loadSigningKeys {
return
}
ca.SetSigningKeys(nil)
keyPairs := ca.GetTLSKeyPairs()
for i := range keyPairs {
keyPairs[i].Key = nil
}
ca.SetTLSKeyPairs(keyPairs)
}
// GetCertAuthorities returns a list of authorities of a given type
// loadSigningKeys controls whether signing keys should be loaded or not
func (s *CA) GetCertAuthorities(caType services.CertAuthType, loadSigningKeys bool, opts ...services.MarshalOption) ([]services.CertAuthority, error) {
if err := caType.Check(); err != nil {
return nil, trace.Wrap(err)
}
// Get all items in the bucket.
bucket := []string{"authorities", string(caType)}
items, err := s.GetItems(bucket)
if err != nil {
return nil, trace.Wrap(err)
}
// Marshal values into a []services.CertAuthority slice.
cas := make([]services.CertAuthority, len(items))
for i, item := range items {
ca, err := services.GetCertAuthorityMarshaler().UnmarshalCertAuthority(item.Value, opts...)
if err != nil {
return nil, trace.Wrap(err)
}
if err := ca.Check(); err != nil {
return nil, trace.Wrap(err)
}
setSigningKeys(ca, loadSigningKeys)
cas[i] = ca
}
return cas, nil
}