|
| 1 | +/* |
| 2 | +Copyright IBM Corp. All Rights Reserved. |
| 3 | +
|
| 4 | +SPDX-License-Identifier: Apache-2.0 |
| 5 | +*/ |
| 6 | + |
| 7 | +package common |
| 8 | + |
| 9 | +import ( |
| 10 | + "fmt" |
| 11 | + "os" |
| 12 | + "path/filepath" |
| 13 | + |
| 14 | + "io" |
| 15 | + |
| 16 | + "github.com/hyperledger/fabric/cmd/common/comm" |
| 17 | + "github.com/hyperledger/fabric/cmd/common/signer" |
| 18 | + "gopkg.in/alecthomas/kingpin.v2" |
| 19 | +) |
| 20 | + |
| 21 | +const ( |
| 22 | + saveConfigCommand = "saveConfig" |
| 23 | +) |
| 24 | + |
| 25 | +var ( |
| 26 | + // Function used to terminate the CLI |
| 27 | + terminate = os.Exit |
| 28 | + // Function used to obtain the stdout |
| 29 | + outWriter io.Writer = os.Stdout |
| 30 | + |
| 31 | + // CLI arguments |
| 32 | + mspID *string |
| 33 | + tlsCA, tlsCert, tlsKey, userKey, userCert **os.File |
| 34 | + configFile *string |
| 35 | +) |
| 36 | + |
| 37 | +// CLICommand defines a command that is added to the CLI |
| 38 | +// via an external consumer. |
| 39 | +type CLICommand func(Config) error |
| 40 | + |
| 41 | +// CLI defines a command line interpreter |
| 42 | +type CLI struct { |
| 43 | + app *kingpin.Application |
| 44 | + dispatchers map[string]CLICommand |
| 45 | +} |
| 46 | + |
| 47 | +// NewCLI creates a new CLI with the given name and help message |
| 48 | +func NewCLI(name, help string) *CLI { |
| 49 | + return &CLI{ |
| 50 | + app: kingpin.New(name, help), |
| 51 | + dispatchers: make(map[string]CLICommand), |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +// Command adds a new top-level command to the CLI |
| 56 | +func (cli *CLI) Command(name, help string, onCommand CLICommand) *kingpin.CmdClause { |
| 57 | + cmd := cli.app.Command(name, help) |
| 58 | + cli.dispatchers[name] = onCommand |
| 59 | + return cmd |
| 60 | +} |
| 61 | + |
| 62 | +// Run makes the CLI process the arguments and executes the command(s) with the flag(s) |
| 63 | +func (cli *CLI) Run(args []string) { |
| 64 | + configFile = cli.app.Flag("configFile", "Specifies the config file to load the configuration from").String() |
| 65 | + persist := cli.app.Command(saveConfigCommand, fmt.Sprintf("Save the config passed by flags into the file specified by --configFile")) |
| 66 | + configureFlags(cli.app) |
| 67 | + |
| 68 | + command := kingpin.MustParse(cli.app.Parse(args)) |
| 69 | + if command == persist.FullCommand() { |
| 70 | + if *configFile == "" { |
| 71 | + out("--configFile must be used to specify the configuration file") |
| 72 | + return |
| 73 | + } |
| 74 | + persistConfig(parseFlagsToConfig(), *configFile) |
| 75 | + return |
| 76 | + } |
| 77 | + |
| 78 | + var conf Config |
| 79 | + if *configFile == "" { |
| 80 | + conf = parseFlagsToConfig() |
| 81 | + } else { |
| 82 | + conf = loadConfig(*configFile) |
| 83 | + } |
| 84 | + |
| 85 | + f, exists := cli.dispatchers[command] |
| 86 | + if !exists { |
| 87 | + out("Unknown command:", command) |
| 88 | + terminate(1) |
| 89 | + return |
| 90 | + } |
| 91 | + err := f(conf) |
| 92 | + if err != nil { |
| 93 | + out(err) |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +func configureFlags(persistCommand *kingpin.Application) { |
| 98 | + // TLS flags |
| 99 | + tlsCA = persistCommand.Flag("peerTLSCA", "Sets the TLS CA certificate file path that verifies the TLS peer's certificate").File() |
| 100 | + tlsCert = persistCommand.Flag("tlsCert", "(Optional) Sets the client TLS certificate file path that is used when the peer enforces client authentication").File() |
| 101 | + tlsKey = persistCommand.Flag("tlsKey", "(Optional) Sets the client TLS key file path that is used when the peer enforces client authentication").File() |
| 102 | + // Enrollment flags |
| 103 | + userKey = persistCommand.Flag("userKey", "Sets the user's key file path that is used to sign messages sent to the peer").File() |
| 104 | + userCert = persistCommand.Flag("userCert", "Sets the user's certificate file path that is used to authenticate the messages sent to the peer").File() |
| 105 | + mspID = persistCommand.Flag("MSP", "Sets the MSP ID of the user, which represents the CA(s) that issued its user certificate").String() |
| 106 | +} |
| 107 | + |
| 108 | +func persistConfig(conf Config, file string) { |
| 109 | + if err := conf.ToFile(file); err != nil { |
| 110 | + out("Failed persisting configuration:", err) |
| 111 | + terminate(1) |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +func loadConfig(file string) Config { |
| 116 | + conf, err := ConfigFromFile(file) |
| 117 | + if err != nil { |
| 118 | + out("Failed loading config", err) |
| 119 | + terminate(1) |
| 120 | + return Config{} |
| 121 | + } |
| 122 | + return conf |
| 123 | +} |
| 124 | + |
| 125 | +func parseFlagsToConfig() Config { |
| 126 | + conf := Config{ |
| 127 | + SignerConfig: signer.Config{ |
| 128 | + MSPID: *mspID, |
| 129 | + IdentityPath: evaluateFileFlag(userCert), |
| 130 | + KeyPath: evaluateFileFlag(userKey), |
| 131 | + }, |
| 132 | + TLSConfig: comm.Config{ |
| 133 | + KeyPath: evaluateFileFlag(tlsKey), |
| 134 | + CertPath: evaluateFileFlag(tlsCert), |
| 135 | + PeerCACertPath: evaluateFileFlag(tlsCA), |
| 136 | + }, |
| 137 | + } |
| 138 | + return conf |
| 139 | +} |
| 140 | + |
| 141 | +func evaluateFileFlag(f **os.File) string { |
| 142 | + if f == nil { |
| 143 | + return "" |
| 144 | + } |
| 145 | + if *f == nil { |
| 146 | + return "" |
| 147 | + } |
| 148 | + path, err := filepath.Abs((*f).Name()) |
| 149 | + if err != nil { |
| 150 | + out("Failed listing", (*f).Name(), ":", err) |
| 151 | + terminate(1) |
| 152 | + } |
| 153 | + return path |
| 154 | +} |
| 155 | +func out(a ...interface{}) { |
| 156 | + fmt.Fprintln(outWriter, a) |
| 157 | +} |
0 commit comments