-
Notifications
You must be signed in to change notification settings - Fork 0
/
identity.go
513 lines (452 loc) · 14.8 KB
/
identity.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
/*
Copyright IBM Corp. 2016 All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package lib
import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"github.com/pkg/errors"
"github.com/cloudflare/cfssl/log"
"github.com/hyperledger/fabric-ca/api"
"github.com/hyperledger/fabric-ca/util"
"github.com/hyperledger/fabric/bccsp"
)
func newIdentity(client *Client, name string, key bccsp.Key, cert []byte) *Identity {
id := new(Identity)
id.name = name
id.ecert = newSigner(key, cert, id)
id.client = client
if client != nil {
id.CSP = client.csp
} else {
id.CSP = util.GetDefaultBCCSP()
}
return id
}
// Identity is fabric-ca's implementation of an identity
type Identity struct {
name string
ecert *Signer
client *Client
CSP bccsp.BCCSP
}
// GetName returns the identity name
func (i *Identity) GetName() string {
return i.name
}
// GetClient returns the client associated with this identity
func (i *Identity) GetClient() *Client {
return i.client
}
// GetECert returns the enrollment certificate signer for this identity
func (i *Identity) GetECert() *Signer {
return i.ecert
}
// GetTCertBatch returns a batch of TCerts for this identity
func (i *Identity) GetTCertBatch(req *api.GetTCertBatchRequest) ([]*Signer, error) {
reqBody, err := util.Marshal(req, "GetTCertBatchRequest")
if err != nil {
return nil, err
}
err = i.Post("tcert", reqBody, nil, nil)
if err != nil {
return nil, err
}
// Ignore the contents of the response for now. They will be processed in the future when we need to
// support the Go SDK. We currently have Node and Java SDKs which process this and they are the
// priority.
return nil, nil
}
// Register registers a new identity
// @param req The registration request
func (i *Identity) Register(req *api.RegistrationRequest) (rr *api.RegistrationResponse, err error) {
log.Debugf("Register %+v", req)
if req.Name == "" {
return nil, errors.New("Register was called without a Name set")
}
reqBody, err := util.Marshal(req, "RegistrationRequest")
if err != nil {
return nil, err
}
// Send a post to the "register" endpoint with req as body
resp := &api.RegistrationResponse{}
err = i.Post("register", reqBody, resp, nil)
if err != nil {
return nil, err
}
log.Debug("The register request completed successfully")
return resp, nil
}
// RegisterAndEnroll registers and enrolls an identity and returns the identity
func (i *Identity) RegisterAndEnroll(req *api.RegistrationRequest) (*Identity, error) {
if i.client == nil {
return nil, errors.New("No client is associated with this identity")
}
rresp, err := i.Register(req)
if err != nil {
return nil, errors.WithMessage(err, fmt.Sprintf("Failed to register %s", req.Name))
}
eresp, err := i.client.Enroll(&api.EnrollmentRequest{
Name: req.Name,
Secret: rresp.Secret,
})
if err != nil {
return nil, errors.WithMessage(err, fmt.Sprintf("Failed to enroll %s", req.Name))
}
return eresp.Identity, nil
}
// Reenroll reenrolls an existing Identity and returns a new Identity
// @param req The reenrollment request
func (i *Identity) Reenroll(req *api.ReenrollmentRequest) (*EnrollmentResponse, error) {
log.Debugf("Reenrolling %s", util.StructToString(req))
csrPEM, key, err := i.client.GenCSR(req.CSR, i.GetName())
if err != nil {
return nil, err
}
reqNet := &api.ReenrollmentRequestNet{
CAName: req.CAName,
AttrReqs: req.AttrReqs,
}
// Get the body of the request
if req.CSR != nil {
reqNet.SignRequest.Hosts = req.CSR.Hosts
}
reqNet.SignRequest.Request = string(csrPEM)
reqNet.SignRequest.Profile = req.Profile
reqNet.SignRequest.Label = req.Label
body, err := util.Marshal(reqNet, "SignRequest")
if err != nil {
return nil, err
}
var result enrollmentResponseNet
err = i.Post("reenroll", body, &result, nil)
if err != nil {
return nil, err
}
return i.client.newEnrollmentResponse(&result, i.GetName(), key)
}
// Revoke the identity associated with 'id'
func (i *Identity) Revoke(req *api.RevocationRequest) (*api.RevocationResponse, error) {
log.Debugf("Entering identity.Revoke %+v", req)
reqBody, err := util.Marshal(req, "RevocationRequest")
if err != nil {
return nil, err
}
var result revocationResponseNet
err = i.Post("revoke", reqBody, &result, nil)
if err != nil {
return nil, err
}
log.Debugf("Successfully revoked certificates: %+v", req)
crl, err := util.B64Decode(result.CRL)
if err != nil {
return nil, err
}
return &api.RevocationResponse{RevokedCerts: result.RevokedCerts, CRL: crl}, nil
}
// RevokeSelf revokes the current identity and all certificates
func (i *Identity) RevokeSelf() (*api.RevocationResponse, error) {
name := i.GetName()
log.Debugf("RevokeSelf %s", name)
req := &api.RevocationRequest{
Name: name,
}
return i.Revoke(req)
}
// GenCRL generates CRL
func (i *Identity) GenCRL(req *api.GenCRLRequest) (*api.GenCRLResponse, error) {
log.Debugf("Entering identity.GenCRL %+v", req)
reqBody, err := util.Marshal(req, "GenCRLRequest")
if err != nil {
return nil, err
}
var result genCRLResponseNet
err = i.Post("gencrl", reqBody, &result, nil)
if err != nil {
return nil, err
}
log.Debugf("Successfully generated CRL: %+v", req)
crl, err := util.B64Decode(result.CRL)
if err != nil {
return nil, err
}
return &api.GenCRLResponse{CRL: crl}, nil
}
// GetIdentity returns information about the requested identity
func (i *Identity) GetIdentity(id, caname string) (*api.GetIDResponse, error) {
log.Debugf("Entering identity.GetIdentity %s", id)
result := &api.GetIDResponse{}
err := i.Get(fmt.Sprintf("identities/%s", id), caname, result)
if err != nil {
return nil, err
}
log.Debugf("Successfully retrieved identity: %+v", result)
return result, nil
}
// GetAllIdentities returns all identities that the caller is authorized to see
func (i *Identity) GetAllIdentities(caname string, cb func(*json.Decoder) error) error {
log.Debugf("Entering identity.GetAllIdentities")
err := i.GetStreamResponse("identities", caname, "result.identities", cb)
if err != nil {
return err
}
log.Debugf("Successfully retrieved identities")
return nil
}
// AddIdentity adds a new identity to the server
func (i *Identity) AddIdentity(req *api.AddIdentityRequest) (*api.IdentityResponse, error) {
log.Debugf("Entering identity.AddIdentity with request: %+v", req)
if req.ID == "" {
return nil, errors.New("Adding identity with no 'ID' set")
}
reqBody, err := util.Marshal(req, "addIdentity")
if err != nil {
return nil, err
}
// Send a post to the "identities" endpoint with req as body
result := &api.IdentityResponse{}
err = i.Post("identities", reqBody, result, nil)
if err != nil {
return nil, err
}
log.Debugf("Successfully added new identity '%s'", result.ID)
return result, nil
}
// ModifyIdentity modifies an existing identity on the server
func (i *Identity) ModifyIdentity(req *api.ModifyIdentityRequest) (*api.IdentityResponse, error) {
log.Debugf("Entering identity.ModifyIdentity with request: %+v", req)
if req.ID == "" {
return nil, errors.New("Name of identity to be modified not specified")
}
reqBody, err := util.Marshal(req, "modifyIdentity")
if err != nil {
return nil, err
}
// Send a put to the "identities" endpoint with req as body
result := &api.IdentityResponse{}
err = i.Put(fmt.Sprintf("identities/%s", req.ID), reqBody, nil, result)
if err != nil {
return nil, err
}
log.Debugf("Successfully modified identity '%s'", result.ID)
return result, nil
}
// RemoveIdentity removes a new identity from the server
func (i *Identity) RemoveIdentity(req *api.RemoveIdentityRequest) (*api.IdentityResponse, error) {
log.Debugf("Entering identity.RemoveIdentity with request: %+v", req)
id := req.ID
if id == "" {
return nil, errors.New("Name of the identity to removed is required")
}
// Send a delete to the "identities" endpoint id as a path parameter
result := &api.IdentityResponse{}
queryParam := make(map[string]string)
queryParam["force"] = strconv.FormatBool(req.Force)
queryParam["ca"] = req.CAName
err := i.Delete(fmt.Sprintf("identities/%s", id), result, queryParam)
if err != nil {
return nil, err
}
log.Debugf("Successfully removed identity: %s", id)
return result, nil
}
// GetAffiliation returns information about the requested affiliation
func (i *Identity) GetAffiliation(affiliation, caname string) (*api.AffiliationResponse, error) {
log.Debugf("Entering identity.GetAffiliation %+v", affiliation)
result := &api.AffiliationResponse{}
err := i.Get(fmt.Sprintf("affiliations/%s", affiliation), caname, result)
if err != nil {
return nil, err
}
log.Debugf("Successfully retrieved affiliation: %+v", result)
return result, nil
}
// GetAllAffiliations returns all affiliations that the caller is authorized to see
func (i *Identity) GetAllAffiliations(caname string) (*api.AffiliationResponse, error) {
log.Debugf("Entering identity.GetAllAffiliations")
result := &api.AffiliationResponse{}
err := i.Get("affiliations", caname, result)
if err != nil {
return nil, err
}
log.Debug("Successfully retrieved affiliations")
return result, nil
}
// AddAffiliation adds a new affiliation to the server
func (i *Identity) AddAffiliation(req *api.AddAffiliationRequest) (*api.AffiliationResponse, error) {
log.Debugf("Entering identity.AddAffiliation with request: %+v", req)
if req.Name == "" {
return nil, errors.New("Affiliation to add was not specified")
}
reqBody, err := util.Marshal(req, "addAffiliation")
if err != nil {
return nil, err
}
// Send a post to the "affiliations" endpoint with req as body
result := &api.AffiliationResponse{}
queryParam := make(map[string]string)
queryParam["force"] = strconv.FormatBool(req.Force)
err = i.Post("affiliations", reqBody, result, queryParam)
if err != nil {
return nil, err
}
log.Debugf("Successfully added new affiliation")
return result, nil
}
// ModifyAffiliation renames an existing affiliation on the server
func (i *Identity) ModifyAffiliation(req *api.ModifyAffiliationRequest) (*api.AffiliationResponse, error) {
log.Debugf("Entering identity.ModifyAffiliation with request: %+v", req)
modifyAff := req.Name
if modifyAff == "" {
return nil, errors.New("Affiliation to modify was not specified")
}
if req.NewName == "" {
return nil, errors.New("New affiliation not specified")
}
reqBody, err := util.Marshal(req, "modifyIdentity")
if err != nil {
return nil, err
}
// Send a put to the "affiliations" endpoint with req as body
result := &api.AffiliationResponse{}
queryParam := make(map[string]string)
queryParam["force"] = strconv.FormatBool(req.Force)
err = i.Put(fmt.Sprintf("affiliations/%s", modifyAff), reqBody, queryParam, result)
if err != nil {
return nil, err
}
log.Debugf("Successfully modified affiliation")
return result, nil
}
// RemoveAffiliation removes an existing affiliation from the server
func (i *Identity) RemoveAffiliation(req *api.RemoveAffiliationRequest) (*api.AffiliationResponse, error) {
log.Debugf("Entering identity.RemoveAffiliation with request: %+v", req)
removeAff := req.Name
if removeAff == "" {
return nil, errors.New("Affiliation to remove was not specified")
}
// Send a delete to the "affiliations" endpoint with the affiliation as a path parameter
result := &api.AffiliationResponse{}
queryParam := make(map[string]string)
queryParam["force"] = strconv.FormatBool(req.Force)
queryParam["ca"] = req.CAName
err := i.Delete(fmt.Sprintf("affiliations/%s", removeAff), result, queryParam)
if err != nil {
return nil, err
}
log.Debugf("Successfully removed affiliation")
return result, nil
}
// Store writes my identity info to disk
func (i *Identity) Store() error {
if i.client == nil {
return errors.New("An identity with no client may not be stored")
}
return i.client.StoreMyIdentity(i.ecert.cert)
}
// Get sends a get request to an endpoint
func (i *Identity) Get(endpoint, caname string, result interface{}) error {
req, err := i.client.newGet(endpoint)
if err != nil {
return err
}
if caname != "" {
addQueryParm(req, "ca", caname)
}
err = i.addTokenAuthHdr(req, nil)
if err != nil {
return err
}
return i.client.SendReq(req, result)
}
// GetStreamResponse sends a request to an endpoint and streams the response
func (i *Identity) GetStreamResponse(endpoint, caname, stream string, cb func(*json.Decoder) error) error {
req, err := i.client.newGet(endpoint)
if err != nil {
return err
}
if caname != "" {
addQueryParm(req, "ca", caname)
}
err = i.addTokenAuthHdr(req, nil)
if err != nil {
return err
}
return i.client.StreamResponse(req, stream, cb)
}
// Put sends a put request to an endpoint
func (i *Identity) Put(endpoint string, reqBody []byte, queryParam map[string]string, result interface{}) error {
req, err := i.client.newPut(endpoint, reqBody)
if err != nil {
return err
}
if queryParam != nil {
for key, value := range queryParam {
addQueryParm(req, key, value)
}
}
err = i.addTokenAuthHdr(req, reqBody)
if err != nil {
return err
}
return i.client.SendReq(req, result)
}
// Delete sends a delete request to an endpoint
func (i *Identity) Delete(endpoint string, result interface{}, queryParam map[string]string) error {
req, err := i.client.newDelete(endpoint)
if err != nil {
return err
}
if queryParam != nil {
for key, value := range queryParam {
addQueryParm(req, key, value)
}
}
err = i.addTokenAuthHdr(req, nil)
if err != nil {
return err
}
return i.client.SendReq(req, result)
}
// Post sends arbitrary request body (reqBody) to an endpoint.
// This adds an authorization header which contains the signature
// of this identity over the body and non-signature part of the authorization header.
// The return value is the body of the response.
func (i *Identity) Post(endpoint string, reqBody []byte, result interface{}, queryParam map[string]string) error {
req, err := i.client.newPost(endpoint, reqBody)
if err != nil {
return err
}
if queryParam != nil {
for key, value := range queryParam {
addQueryParm(req, key, value)
}
}
err = i.addTokenAuthHdr(req, reqBody)
if err != nil {
return err
}
return i.client.SendReq(req, result)
}
func (i *Identity) addTokenAuthHdr(req *http.Request, body []byte) error {
log.Debug("Adding token-based authorization header")
cert := i.ecert.cert
key := i.ecert.key
token, err := util.CreateToken(i.CSP, cert, key, body)
if err != nil {
return errors.WithMessage(err, "Failed to add token authorization header")
}
req.Header.Set("authorization", token)
return nil
}