forked from hyperledger/fabric-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
identity.go
443 lines (391 loc) · 12.9 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
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
/*
Notice: This file has been modified for Hyperledger Fabric SDK Go usage.
Please review third_party pinning scripts and patches for more details.
*/
package lib
import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"github.com/akorosmezey/fabric-sdk-go/internal/github.com/hyperledger/fabric-ca/lib/client/credential"
"github.com/akorosmezey/fabric-sdk-go/internal/github.com/hyperledger/fabric-ca/lib/client/credential/x509"
"github.com/akorosmezey/fabric-sdk-go/internal/github.com/hyperledger/fabric-ca/sdkinternal/pkg/api"
"github.com/akorosmezey/fabric-sdk-go/internal/github.com/hyperledger/fabric-ca/sdkinternal/pkg/util"
log "github.com/akorosmezey/fabric-sdk-go/internal/github.com/hyperledger/fabric-ca/sdkpatch/logbridge"
"github.com/pkg/errors"
)
// Identity is fabric-ca's implementation of an identity
type Identity struct {
name string
client *Client
creds []credential.Credential
}
// NewIdentity is the constructor for identity
func NewIdentity(client *Client, name string, creds []credential.Credential) *Identity {
id := new(Identity)
id.name = name
id.client = client
id.creds = creds
return id
}
// GetName returns the identity name
func (i *Identity) GetName() string {
return i.name
}
// GetECert returns the enrollment certificate signer for this identity
// Returns nil if the identity does not have a X509 credential
func (i *Identity) GetECert() *x509.Signer {
for _, cred := range i.creds {
if cred.Type() == x509.CredType {
v, _ := cred.Val()
if v != nil {
s, _ := v.(*x509.Signer)
return s
}
}
}
return 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
}
// 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 api.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
}
// 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")
queryParam := make(map[string]string)
queryParam["ca"] = caname
err := i.GetStreamResponse("identities", queryParam, "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
}
// 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 string, queryParam map[string]string, stream string, cb func(*json.Decoder) error) error {
req, err := i.client.newGet(endpoint)
if err != nil {
return err
}
if queryParam != nil {
for key, value := range queryParam {
if value != "" {
addQueryParm(req, key, value)
}
}
}
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")
var token string
var err error
for _, cred := range i.creds {
token, err = cred.CreateToken(req, body)
if err != nil {
return errors.WithMessage(err, "Failed to add token authorization header")
}
break
}
req.Header.Set("authorization", token)
return nil
}