github.com/adecaro/fabric-ca@v2.0.0-alpha+incompatible/lib/clientconfig.go (about) 1 /* 2 Copyright IBM Corp. 2017 All Rights Reserved. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package lib 18 19 import ( 20 "fmt" 21 "net/url" 22 "path" 23 24 "github.com/cloudflare/cfssl/log" 25 "github.com/hyperledger/fabric-ca/api" 26 "github.com/hyperledger/fabric-ca/lib/tls" 27 "github.com/hyperledger/fabric-ca/util" 28 "github.com/hyperledger/fabric/bccsp/factory" 29 "github.com/pkg/errors" 30 ) 31 32 // ClientConfig is the fabric-ca client's config 33 type ClientConfig struct { 34 URL string `def:"http://localhost:7054" opt:"u" help:"URL of fabric-ca-server"` 35 MSPDir string `def:"msp" opt:"M" help:"Membership Service Provider directory"` 36 TLS tls.ClientTLSConfig 37 Enrollment api.EnrollmentRequest 38 CSR api.CSRInfo 39 ID api.RegistrationRequest 40 Revoke api.RevocationRequest 41 CAInfo api.GetCAInfoRequest 42 CAName string `help:"Name of CA"` 43 CSP *factory.FactoryOpts `mapstructure:"bccsp" hide:"true"` 44 Debug bool `opt:"d" help:"Enable debug level logging" hide:"true"` 45 LogLevel string `help:"Set logging level (info, warning, debug, error, fatal, critical)"` 46 } 47 48 // Enroll a client given the server's URL and the client's home directory. 49 // The URL may be of the form: http://user:pass@host:port where user and pass 50 // are the enrollment ID and secret, respectively. 51 func (c *ClientConfig) Enroll(rawurl, home string) (*EnrollmentResponse, error) { 52 purl, err := url.Parse(rawurl) 53 if err != nil { 54 return nil, err 55 } 56 if purl.User != nil { 57 name := purl.User.Username() 58 secret, _ := purl.User.Password() 59 c.Enrollment.Name = name 60 c.Enrollment.Secret = secret 61 purl.User = nil 62 } 63 if c.Enrollment.Name == "" { 64 expecting := fmt.Sprintf( 65 "%s://<enrollmentID>:<secret>@%s", 66 purl.Scheme, purl.Host) 67 return nil, errors.Errorf( 68 "The URL of the fabric CA server is missing the enrollment ID and secret;"+ 69 " found '%s' but expecting '%s'", rawurl, expecting) 70 } 71 c.Enrollment.CAName = c.CAName 72 c.URL = purl.String() 73 c.TLS.Enabled = purl.Scheme == "https" 74 c.Enrollment.CSR = &c.CSR 75 client := &Client{HomeDir: home, Config: c} 76 return client.Enroll(&c.Enrollment) 77 } 78 79 // GenCSR generates a certificate signing request and writes the CSR to a file. 80 func (c *ClientConfig) GenCSR(home string) error { 81 82 client := &Client{HomeDir: home, Config: c} 83 // Generate the CSR 84 85 err := client.Init() 86 if err != nil { 87 return err 88 } 89 90 if c.CSR.CN == "" { 91 return errors.Errorf("CSR common name not specified; use '--csr.cn' flag") 92 } 93 94 csrPEM, _, err := client.GenCSR(&c.CSR, c.CSR.CN) 95 if err != nil { 96 return err 97 } 98 99 csrFile := path.Join(client.Config.MSPDir, "signcerts", fmt.Sprintf("%s.csr", c.CSR.CN)) 100 err = util.WriteFile(csrFile, csrPEM, 0644) 101 if err != nil { 102 return errors.WithMessage(err, "Failed to store the CSR") 103 } 104 log.Infof("Stored CSR at %s", csrFile) 105 return nil 106 }