github.com/silveraid/fabric-ca@v1.1.0-preview.0.20180127000700-71974f53ab08/cmd/fabric-ca-client/enroll.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 main 18 19 import ( 20 "io/ioutil" 21 "path/filepath" 22 "strings" 23 24 "github.com/pkg/errors" 25 26 "github.com/cloudflare/cfssl/log" 27 "github.com/spf13/cobra" 28 ) 29 30 func (c *ClientCmd) newEnrollCommand() *cobra.Command { 31 enrollCmd := &cobra.Command{ 32 Use: "enroll -u http://user:userpw@serverAddr:serverPort", 33 Short: "Enroll an identity", 34 Long: "Enroll identity with Fabric CA server", 35 // PreRunE block for this command will check to make sure username 36 // and secret provided for the enroll command before creating and/or 37 // reading configuration file 38 PreRunE: func(cmd *cobra.Command, args []string) error { 39 if len(args) > 0 { 40 return errors.Errorf(extraArgsError, args, cmd.UsageString()) 41 } 42 43 err := c.configInit() 44 if err != nil { 45 return err 46 } 47 48 log.Debugf("Client configuration settings: %+v", c.clientCfg) 49 50 return nil 51 }, 52 53 RunE: func(cmd *cobra.Command, args []string) error { 54 err := c.runEnroll(cmd) 55 if err != nil { 56 return err 57 } 58 return nil 59 }, 60 } 61 return enrollCmd 62 } 63 64 // The client enroll main logic 65 func (c *ClientCmd) runEnroll(cmd *cobra.Command) error { 66 log.Debug("Entered runEnroll") 67 resp, err := c.clientCfg.Enroll(c.clientCfg.URL, filepath.Dir(c.cfgFileName)) 68 if err != nil { 69 return err 70 } 71 72 ID := resp.Identity 73 74 cfgFile, err := ioutil.ReadFile(c.cfgFileName) 75 if err != nil { 76 return errors.Wrapf(err, "Failed to read file at '%s'", c.cfgFileName) 77 } 78 79 cfg := strings.Replace(string(cfgFile), "<<<ENROLLMENT_ID>>>", ID.GetName(), 1) 80 81 err = ioutil.WriteFile(c.cfgFileName, []byte(cfg), 0644) 82 if err != nil { 83 return errors.Wrapf(err, "Failed to write file at '%s'", c.cfgFileName) 84 } 85 86 err = ID.Store() 87 if err != nil { 88 return errors.WithMessage(err, "Failed to store enrollment information") 89 } 90 91 err = storeCAChain(c.clientCfg, &resp.ServerInfo) 92 if err != nil { 93 return err 94 } 95 96 return nil 97 }