github.com/silveraid/fabric-ca@v1.1.0-preview.0.20180127000700-71974f53ab08/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 Debug bool `def:"false" opt:"d" help:"Enable debug level logging"` 35 URL string `def:"http://localhost:7054" opt:"u" help:"URL of fabric-ca-server"` 36 MSPDir string `def:"msp" opt:"M" help:"Membership Service Provider directory"` 37 TLS tls.ClientTLSConfig 38 Enrollment api.EnrollmentRequest 39 CSR api.CSRInfo 40 ID api.RegistrationRequest 41 Revoke api.RevocationRequest 42 CAInfo api.GetCAInfoRequest 43 CAName string `help:"Name of CA"` 44 CSP *factory.FactoryOpts `mapstructure:"bccsp"` 45 } 46 47 // Enroll a client given the server's URL and the client's home directory. 48 // The URL may be of the form: http://user:pass@host:port where user and pass 49 // are the enrollment ID and secret, respectively. 50 func (c *ClientConfig) Enroll(rawurl, home string) (*EnrollmentResponse, error) { 51 purl, err := url.Parse(rawurl) 52 if err != nil { 53 return nil, err 54 } 55 if purl.User != nil { 56 name := purl.User.Username() 57 secret, _ := purl.User.Password() 58 c.Enrollment.Name = name 59 c.Enrollment.Secret = secret 60 purl.User = nil 61 } 62 if c.Enrollment.Name == "" { 63 expecting := fmt.Sprintf( 64 "%s://<enrollmentID>:<secret>@%s", 65 purl.Scheme, purl.Host) 66 return nil, errors.Errorf( 67 "The URL of the fabric CA server is missing the enrollment ID and secret;"+ 68 " found '%s' but expecting '%s'", rawurl, expecting) 69 } 70 c.Enrollment.CAName = c.CAName 71 c.URL = purl.String() 72 c.TLS.Enabled = purl.Scheme == "https" 73 c.Enrollment.CSR = &c.CSR 74 client := &Client{HomeDir: home, Config: c} 75 return client.Enroll(&c.Enrollment) 76 } 77 78 // GenCSR generates a certificate signing request and writes the CSR to a file. 79 func (c *ClientConfig) GenCSR(home string) error { 80 81 client := &Client{HomeDir: home, Config: c} 82 // Generate the CSR 83 84 err := client.Init() 85 if err != nil { 86 return err 87 } 88 89 if c.CSR.CN == "" { 90 return errors.Errorf("CSR common name not specified; use '--csr.cn' flag") 91 } 92 93 csrPEM, _, err := client.GenCSR(&c.CSR, c.CSR.CN) 94 if err != nil { 95 return err 96 } 97 98 csrFile := path.Join(client.Config.MSPDir, "signcerts", fmt.Sprintf("%s.csr", c.CSR.CN)) 99 err = util.WriteFile(csrFile, csrPEM, 0644) 100 if err != nil { 101 return errors.WithMessage(err, "Failed to store the CSR") 102 } 103 log.Infof("Stored CSR at %s", csrFile) 104 return nil 105 }