-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
698 lines (614 loc) · 18.6 KB
/
server.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
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
/*
Copyright IBM Corp. 2017 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 (
"crypto/tls"
"crypto/x509"
"crypto/x509/pkix"
"errors"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
_ "net/http/pprof" // import to support profiling
"os"
"path/filepath"
"strconv"
"strings"
"sync"
"time"
"github.com/cloudflare/cfssl/log"
"github.com/cloudflare/cfssl/revoke"
stls "github.com/hyperledger/fabric-ca/lib/tls"
"github.com/hyperledger/fabric-ca/util"
"github.com/spf13/viper"
_ "github.com/go-sql-driver/mysql" // import to support MySQL
_ "github.com/lib/pq" // import to support Postgres
_ "github.com/mattn/go-sqlite3" // import to support SQLite3
)
const (
defaultClientAuth = "noclientcert"
fabricCAServerProfilePort = "FABRIC_CA_SERVER_PROFILE_PORT"
allRoles = "user,app,peer,orderer,client,validator,auditor"
)
// Attribute names
const (
attrRoles = "hf.Registrar.Roles"
attrDelegateRoles = "hf.Registrar.DelegateRoles"
attrRevoker = "hf.Revoker"
attrIntermediateCA = "hf.IntermediateCA"
)
// Server is the fabric-ca server
type Server struct {
// The home directory for the server
HomeDir string
// BlockingStart if true makes the Start function blocking;
// It is non-blocking by default.
BlockingStart bool
// The server's configuration
Config *ServerConfig
// The server mux
mux *http.ServeMux
// The current listener for this server
listener net.Listener
// An error which occurs when serving
serveError error
// Server's default CA
CA
// A map of CAs stored by CA name as key
caMap map[string]*CA
// A map of CA configs stored by CA file as key
caConfigMap map[string]*CAConfig
// channel for communication between http.serve and main threads.
wait chan bool
// Server mutex
mutex sync.Mutex
}
// Init initializes a fabric-ca server
func (s *Server) Init(renew bool) (err error) {
// Initialize the config
err = s.initConfig()
if err != nil {
return err
}
// Initialize the default CA last
err = s.initDefaultCA(renew)
if err != nil {
return err
}
// Successful initialization
return nil
}
// Start the fabric-ca server
func (s *Server) Start() (err error) {
log.Infof("Starting server in home directory: %s", s.HomeDir)
s.serveError = nil
if s.listener != nil {
return errors.New("server is already started")
}
// Initialize the server
err = s.Init(false)
if err != nil {
return err
}
// Register http handlers
s.registerHandlers()
log.Debugf("%d CA instance(s) running on server", len(s.caMap))
// Start listening and serving
return s.listenAndServe()
}
// Stop the server
// WARNING: This forcefully closes the listening socket and may cause
// requests in transit to fail, and so is only used for testing.
// A graceful shutdown will be supported with golang 1.8.
func (s *Server) Stop() error {
err := s.closeListener()
if err != nil {
return err
}
if s.wait == nil {
return nil
}
// Wait for message on wait channel from the http.serve thread. If message
// is not received in 10 seconds, return
port := s.Config.Port
for i := 0; i < 10; i++ {
select {
case <-s.wait:
log.Debugf("Stop: successful stop on port %d", port)
close(s.wait)
s.wait = nil
return nil
default:
log.Debugf("Stop: waiting for listener on port %d to stop", port)
time.Sleep(time.Second)
}
}
log.Debugf("Stop: timed out waiting for stop notification for port %d", port)
return nil
}
// RegisterBootstrapUser registers the bootstrap user with appropriate privileges
func (s *Server) RegisterBootstrapUser(user, pass, affiliation string) error {
// Initialize the config, setting defaults, etc
log.Debugf("Register bootstrap user: name=%s, affiliation=%s", user, affiliation)
if user == "" || pass == "" {
return errors.New("Empty identity name and/or pass not allowed")
}
id := CAConfigIdentity{
Name: user,
Pass: pass,
Type: "user",
Affiliation: affiliation,
MaxEnrollments: 0, // 0 means to use the server's max enrollment setting
Attrs: map[string]string{
attrRoles: allRoles,
attrDelegateRoles: allRoles,
attrRevoker: "true",
attrIntermediateCA: "true",
},
}
registry := &s.CA.Config.Registry
registry.Identities = append(registry.Identities, id)
log.Debugf("Registered bootstrap identity: %+v", id)
return nil
}
// initConfig initializes the configuration for the server
func (s *Server) initConfig() (err error) {
// Home directory is current working directory by default
if s.HomeDir == "" {
s.HomeDir, err = os.Getwd()
if err != nil {
return fmt.Errorf("Failed to get server's home directory: %s", err)
}
}
// Create config if not set
if s.Config == nil {
s.Config = new(ServerConfig)
}
cfg := s.Config
// Set log level if debug is true
if cfg.Debug {
log.Level = log.LevelDebug
}
// Set config defaults if not set
if cfg.Address == "" {
cfg.Address = DefaultServerAddr
}
if cfg.Port == 0 {
cfg.Port = DefaultServerPort
}
s.CA.server = s
s.CA.HomeDir = s.HomeDir
err = s.CA.initConfig()
if err != nil {
return err
}
err = s.initMultiCAConfig()
if err != nil {
return err
}
revoke.SetCRLFetcher(s.fetchCRL)
// Make file names absolute
s.makeFileNamesAbsolute()
return nil
}
// Initialize config related to multiple CAs
func (s *Server) initMultiCAConfig() (err error) {
cfg := s.Config
if cfg.CAcount != 0 && len(cfg.CAfiles) > 0 {
return fmt.Errorf("The --cacount and --cafiles options are mutually exclusive")
}
cfg.CAfiles, err = util.NormalizeFileList(cfg.CAfiles, s.HomeDir)
if err != nil {
return err
}
// Multi-CA related configuration initialization
s.caMap = make(map[string]*CA)
if cfg.CAcount >= 1 {
s.createDefaultCAConfigs(cfg.CAcount)
}
if len(cfg.CAfiles) != 0 {
log.Debugf("Default CA configuration, if necessary, will be used to replace missing values for additional CAs: %+v", s.Config.CAcfg)
log.Debugf("Additional CAs to be started: %s", cfg.CAfiles)
var caFiles []string
caFiles = util.NormalizeStringSlice(cfg.CAfiles)
for _, caFile := range caFiles {
err = s.loadCA(caFile, false)
if err != nil {
return err
}
}
}
return nil
}
func (s *Server) initDefaultCA(renew bool) error {
log.Debugf("Initializing default CA in directory %s", s.HomeDir)
ca := &s.CA
err := initCA(ca, s.HomeDir, s.CA.Config, s, renew)
if err != nil {
return err
}
err = s.addCA(ca)
if err != nil {
return err
}
log.Infof("Home directory for default CA: %s", ca.HomeDir)
return nil
}
// loadCAConfig loads up a CA's configuration from the specified
// CA configuration file
func (s *Server) loadCA(caFile string, renew bool) error {
log.Infof("Loading CA from %s", caFile)
var err error
if !util.FileExists(caFile) {
return fmt.Errorf("%s file does not exist", caFile)
}
// Creating new Viper instance, to prevent any server level environment variables or
// flags from overridding the configuration options specified in the
// CA config file
cfg := &CAConfig{}
caViper := viper.New()
err = UnmarshalConfig(cfg, caViper, caFile, false, true)
if err != nil {
return err
}
// Need to error if no CA name provided in config file, we cannot revert to using
// the name of default CA cause CA names must be unique
caName := cfg.CA.Name
if caName == "" {
return fmt.Errorf("No CA name provided in CA configuration file. CA name is required in %s", caFile)
}
// Replace missing values in CA configuration values with values from the
// defaut CA configuration
util.CopyMissingValues(s.CA.Config, cfg)
// Integers and boolean values are handled outside the util.CopyMissingValues
// because there is no way through reflect to detect if a value was explicitly
// set to 0 or false, or it is using the default value for its type. Viper is
// employed here to help detect.
if !caViper.IsSet("registry.maxenrollments") {
cfg.Registry.MaxEnrollments = s.CA.Config.Registry.MaxEnrollments
}
if !caViper.IsSet("db.tls.enabled") {
cfg.DB.TLS.Enabled = s.CA.Config.DB.TLS.Enabled
}
log.Debugf("CA configuration after checking for missing values: %+v", cfg)
ca, err := NewCA(caFile, cfg, s, renew)
if err != nil {
return err
}
return s.addCA(ca)
}
// DN is the distinguished name inside a certificate
type DN struct {
issuer string
subject string
}
// addCA adds a CA to the server if there are no conflicts
func (s *Server) addCA(ca *CA) error {
// check for conflicts
caName := ca.Config.CA.Name
for _, c := range s.caMap {
if c.Config.CA.Name == caName {
return fmt.Errorf("CA name '%s' is used in '%s' and '%s'",
caName, ca.ConfigFilePath, c.ConfigFilePath)
}
err := s.compareDN(c.Config.CA.Certfile, ca.Config.CA.Certfile)
if err != nil {
return err
}
}
// no conflicts, so add it
s.caMap[caName] = ca
return nil
}
// createDefaultCAConfigs creates specified number of default CA configuration files
func (s *Server) createDefaultCAConfigs(cacount int) error {
log.Debugf("Creating %d default CA configuration files", cacount)
cashome, err := util.MakeFileAbs("ca", s.HomeDir)
if err != nil {
return err
}
os.Mkdir(cashome, 0755)
for i := 1; i <= cacount; i++ {
cahome := fmt.Sprintf(cashome+"/ca%d", i)
cfgFileName := filepath.Join(cahome, "fabric-ca-config.yaml")
caName := fmt.Sprintf("ca%d", i)
cfg := strings.Replace(defaultCACfgTemplate, "<<<CANAME>>>", caName, 1)
cn := fmt.Sprintf("fabric-ca-server-ca%d", i)
cfg = strings.Replace(cfg, "<<<COMMONNAME>>>", cn, 1)
s.Config.CAfiles = append(s.Config.CAfiles, cfgFileName)
// Now write the file
err := os.MkdirAll(filepath.Dir(cfgFileName), 0755)
if err != nil {
return err
}
err = ioutil.WriteFile(cfgFileName, []byte(cfg), 0644)
if err != nil {
return err
}
}
return nil
}
// Register all endpoint handlers
func (s *Server) registerHandlers() {
s.mux = http.NewServeMux()
s.registerHandler("cainfo", newInfoHandler, noAuth)
s.registerHandler("register", newRegisterHandler, token)
s.registerHandler("enroll", newEnrollHandler, basic)
s.registerHandler("reenroll", newReenrollHandler, token)
s.registerHandler("revoke", newRevokeHandler, token)
s.registerHandler("tcert", newTCertHandler, token)
}
// Register an endpoint handler
func (s *Server) registerHandler(
path string,
getHandler func(server *Server) (http.Handler, error),
at authType) {
var handler http.Handler
handler, err := getHandler(s)
if err != nil {
log.Warningf("Endpoint '%s' is disabled: %s", path, err)
return
}
handler = &fcaAuthHandler{
server: s,
authType: at,
next: handler,
}
s.mux.Handle("/"+path, handler)
s.mux.Handle("/api/v1/"+path, handler)
}
// Starting listening and serving
func (s *Server) listenAndServe() (err error) {
var listener net.Listener
var clientAuth tls.ClientAuthType
var ok bool
c := s.Config
// Set default listening address and port
if c.Address == "" {
c.Address = DefaultServerAddr
}
if c.Port == 0 {
c.Port = DefaultServerPort
}
addr := net.JoinHostPort(c.Address, strconv.Itoa(c.Port))
var addrStr string
if c.TLS.Enabled {
log.Debug("TLS is enabled")
addrStr = fmt.Sprintf("https://%s", addr)
cer, err := util.LoadX509KeyPair(c.TLS.CertFile, c.TLS.KeyFile, s.csp)
if err != nil {
return err
}
if c.TLS.ClientAuth.Type == "" {
c.TLS.ClientAuth.Type = defaultClientAuth
}
log.Debugf("Client authentication type requested: %s", c.TLS.ClientAuth.Type)
authType := strings.ToLower(c.TLS.ClientAuth.Type)
if clientAuth, ok = clientAuthTypes[authType]; !ok {
return errors.New("Invalid client auth type provided")
}
var certPool *x509.CertPool
if authType != defaultClientAuth {
certPool, err = LoadPEMCertPool(c.TLS.ClientAuth.CertFiles)
if err != nil {
return err
}
}
config := &tls.Config{
Certificates: []tls.Certificate{*cer},
ClientAuth: clientAuth,
ClientCAs: certPool,
MinVersion: tls.VersionTLS12,
MaxVersion: tls.VersionTLS12,
}
listener, err = tls.Listen("tcp", addr, config)
if err != nil {
return fmt.Errorf("TLS listen failed for %s: %s", addrStr, err)
}
} else {
addrStr = fmt.Sprintf("http://%s", addr)
listener, err = net.Listen("tcp", addr)
if err != nil {
return fmt.Errorf("TCP listen failed for %s: %s", addrStr, err)
}
}
s.listener = listener
log.Infof("Listening on %s", addrStr)
err = s.checkAndEnableProfiling()
if err != nil {
s.closeListener()
return fmt.Errorf("TCP listen for profiling failed: %s", err)
}
// Start serving requests, either blocking or non-blocking
if s.BlockingStart {
return s.serve()
}
s.wait = make(chan bool)
go s.serve()
return nil
}
func (s *Server) serve() error {
listener := s.listener
if listener == nil {
// This can happen as follows:
// 1) listenAndServe above is called with s.BlockingStart set to false
// and returns to the caller
// 2) the caller immediately calls s.Stop, which sets s.listener to nil
// 3) the go routine runs and calls this function
// So this prevents the panic which was reported in
// in https://jira.hyperledger.org/browse/FAB-3100.
return nil
}
s.serveError = http.Serve(listener, s.mux)
log.Errorf("Server has stopped serving: %s", s.serveError)
s.closeListener()
if s.wait != nil {
s.wait <- true
}
return s.serveError
}
// checkAndEnableProfiling checks for FABRIC_CA_SERVER_PROFILE_PORT env variable
// if it is set, starts listening for profiling requests at the port specified
// by the environment variable
func (s *Server) checkAndEnableProfiling() error {
// Start listening for profile requests
pport := os.Getenv(fabricCAServerProfilePort)
if pport != "" {
iport, err := strconv.Atoi(pport)
if err != nil || iport < 0 {
log.Warningf("Profile port specified by the %s environment variable is not a valid port, not enabling profiling",
fabricCAServerProfilePort)
} else {
addr := net.JoinHostPort(s.Config.Address, pport)
listener, err1 := net.Listen("tcp", addr)
log.Infof("Profiling enabled; listening for profile requests on port %s", pport)
if err1 != nil {
return err1
}
go func() {
log.Debugf("Profiling enabled; waiting for profile requests on port %s", pport)
err := http.Serve(listener, nil)
log.Errorf("Stopped serving for profiling requests on port %s: %s", pport, err)
}()
}
}
return nil
}
// Make all file names in the config absolute
func (s *Server) makeFileNamesAbsolute() error {
log.Debug("Making server filenames absolute")
err := stls.AbsTLSServer(&s.Config.TLS, s.HomeDir)
if err != nil {
return err
}
return nil
}
// closeListener closes the listening endpoint
func (s *Server) closeListener() error {
s.mutex.Lock()
defer s.mutex.Unlock()
port := s.Config.Port
if s.listener == nil {
msg := fmt.Sprintf("Stop: listener was already closed on port %d", port)
log.Debugf(msg)
return fmt.Errorf(msg)
}
err := s.listener.Close()
s.listener = nil
if err != nil {
log.Debugf("Stop: failed to close listener on port %d: %s", port, err)
return err
}
log.Debugf("Stop: successfully closed listener on port %d", port)
return nil
}
func (s *Server) compareDN(existingCACertFile, newCACertFile string) error {
log.Debugf("Comparing DNs from certificates: %s and %s", existingCACertFile, newCACertFile)
existingDN, err := s.loadDNFromCertFile(existingCACertFile)
if err != nil {
return err
}
newDN, err := s.loadDNFromCertFile(newCACertFile)
if err != nil {
return err
}
err = existingDN.equal(newDN)
if err != nil {
return fmt.Errorf("Please modify CSR in %s and try adding CA again: %s", newCACertFile, err)
}
return nil
}
// Read the CRL from body of http response
func (s *Server) fetchCRL(r io.Reader) ([]byte, error) {
crlSizeLimit := s.Config.CRLSizeLimit
log.Debugf("CRL size limit is %d bytes", crlSizeLimit)
crl := make([]byte, crlSizeLimit)
crl, err := util.Read(r, crl)
if err != nil {
return nil, fmt.Errorf("Error reading CRL with max buffer size of %d: %s", crlSizeLimit, err)
}
return crl, nil
}
func (s *Server) loadDNFromCertFile(certFile string) (*DN, error) {
log.Debugf("Loading DNs from certificate %s", certFile)
cert, err := util.GetX509CertificateFromPEMFile(certFile)
if err != nil {
return nil, err
}
issuerDN, err := s.getDNFromCert(cert.Issuer, "/")
if err != nil {
return nil, err
}
subjectDN, err := s.getDNFromCert(cert.Subject, "/")
if err != nil {
return nil, err
}
distinguishedName := &DN{
issuer: issuerDN,
subject: subjectDN,
}
return distinguishedName, nil
}
func (dn *DN) equal(checkDN *DN) error {
log.Debugf("Check to see if two DNs are equal - %+v and %+v", dn, checkDN)
if dn.issuer == checkDN.issuer {
log.Debug("Issuer distinguished name already in use, checking for unique subject distinguished name")
if dn.subject == checkDN.subject {
return errors.New("Both issuer and subject distinguished name are already in use")
}
}
return nil
}
func (s *Server) getDNFromCert(namespace pkix.Name, sep string) (string, error) {
subject := []string{}
for _, s := range namespace.ToRDNSequence() {
for _, i := range s {
if v, ok := i.Value.(string); ok {
if name, ok := oid[i.Type.String()]; ok {
// <oid name>=<value>
subject = append(subject, fmt.Sprintf("%s=%s", name, v))
} else {
// <oid>=<value> if no <oid name> is found
subject = append(subject, fmt.Sprintf("%s=%s", i.Type.String(), v))
}
} else {
// <oid>=<value in default format> if value is not string
subject = append(subject, fmt.Sprintf("%s=%v", i.Type.String(), v))
}
}
}
return sep + strings.Join(subject, sep), nil
}
var oid = map[string]string{
"2.5.4.3": "CN",
"2.5.4.4": "SN",
"2.5.4.5": "serialNumber",
"2.5.4.6": "C",
"2.5.4.7": "L",
"2.5.4.8": "ST",
"2.5.4.9": "streetAddress",
"2.5.4.10": "O",
"2.5.4.11": "OU",
"2.5.4.12": "title",
"2.5.4.17": "postalCode",
"2.5.4.42": "GN",
"2.5.4.43": "initials",
"2.5.4.44": "generationQualifier",
"2.5.4.46": "dnQualifier",
"2.5.4.65": "pseudonym",
"0.9.2342.19200300.100.1.25": "DC",
"1.2.840.113549.1.9.1": "emailAddress",
"0.9.2342.19200300.100.1.1": "userid",
}