|
| 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 | + "io/ioutil" |
| 11 | + |
| 12 | + "github.com/pkg/errors" |
| 13 | + yaml "gopkg.in/yaml.v2" |
| 14 | +) |
| 15 | + |
| 16 | +// NetworkConfig provides a static definition of a Hyperledger Fabric network |
| 17 | +type NetworkConfig struct { |
| 18 | + Name string `yaml:"name"` |
| 19 | + Xtype string `yaml:"x-type"` |
| 20 | + Description string `yaml:"description"` |
| 21 | + Version string `yaml:"version"` |
| 22 | + Channels map[string]ChannelNetworkConfig `yaml:"channels"` |
| 23 | + Organizations map[string]OrganizationConfig `yaml:"organizations"` |
| 24 | + Peers map[string]PeerConfig `yaml:"peers"` |
| 25 | + Client ClientConfig `yaml:"client"` |
| 26 | + Orderers map[string]OrdererConfig `yaml:"orderers"` |
| 27 | + CertificateAuthorities map[string]CAConfig `yaml:"certificateAuthorities"` |
| 28 | +} |
| 29 | + |
| 30 | +// ClientConfig - not currently used by CLI |
| 31 | +type ClientConfig struct { |
| 32 | + Organization string `yaml:"organization"` |
| 33 | + Logging LoggingType `yaml:"logging"` |
| 34 | + CryptoConfig CCType `yaml:"cryptoconfig"` |
| 35 | + TLS TLSType `yaml:"tls"` |
| 36 | + CredentialStore CredentialStoreType `yaml:"credentialStore"` |
| 37 | +} |
| 38 | + |
| 39 | +// LoggingType not currently used by CLI |
| 40 | +type LoggingType struct { |
| 41 | + Level string `yaml:"level"` |
| 42 | +} |
| 43 | + |
| 44 | +// CCType - not currently used by CLI |
| 45 | +type CCType struct { |
| 46 | + Path string `yaml:"path"` |
| 47 | +} |
| 48 | + |
| 49 | +// TLSType - not currently used by CLI |
| 50 | +type TLSType struct { |
| 51 | + Enabled bool `yaml:"enabled"` |
| 52 | +} |
| 53 | + |
| 54 | +// CredentialStoreType - not currently used by CLI |
| 55 | +type CredentialStoreType struct { |
| 56 | + Path string `yaml:"path"` |
| 57 | + CryptoStore struct { |
| 58 | + Path string `yaml:"path"` |
| 59 | + } |
| 60 | + Wallet string `yaml:"wallet"` |
| 61 | +} |
| 62 | + |
| 63 | +// ChannelNetworkConfig provides the definition of channels for the network |
| 64 | +type ChannelNetworkConfig struct { |
| 65 | + // Orderers list of ordering service nodes |
| 66 | + Orderers []string `yaml:"orderers"` |
| 67 | + // Peers a list of peer-channels that are part of this organization |
| 68 | + // to get the real Peer config object, use the Name field and fetch NetworkConfig.Peers[Name] |
| 69 | + Peers map[string]PeerChannelConfig `yaml:"peers"` |
| 70 | + // Chaincodes list of services |
| 71 | + Chaincodes []string `yaml:"chaincodes"` |
| 72 | +} |
| 73 | + |
| 74 | +// PeerChannelConfig defines the peer capabilities |
| 75 | +type PeerChannelConfig struct { |
| 76 | + EndorsingPeer bool `yaml:"endorsingPeer"` |
| 77 | + ChaincodeQuery bool `yaml:"chaincodeQuery"` |
| 78 | + LedgerQuery bool `yaml:"ledgerQuery"` |
| 79 | + EventSource bool `yaml:"eventSource"` |
| 80 | +} |
| 81 | + |
| 82 | +// OrganizationConfig provides the definition of an organization in the network |
| 83 | +// not currently used by CLI |
| 84 | +type OrganizationConfig struct { |
| 85 | + MspID string `yaml:"mspid"` |
| 86 | + Peers []string `yaml:"peers"` |
| 87 | + CryptoPath string `yaml:"cryptoPath"` |
| 88 | + CertificateAuthorities []string `yaml:"certificateAuthorities"` |
| 89 | + AdminPrivateKey TLSConfig `yaml:"adminPrivateKey"` |
| 90 | + SignedCert TLSConfig `yaml:"signedCert"` |
| 91 | +} |
| 92 | + |
| 93 | +// OrdererConfig defines an orderer configuration |
| 94 | +// not currently used by CLI |
| 95 | +type OrdererConfig struct { |
| 96 | + URL string `yaml:"url"` |
| 97 | + GrpcOptions map[string]interface{} `yaml:"grpcOptions"` |
| 98 | + TLSCACerts TLSConfig `yaml:"tlsCACerts"` |
| 99 | +} |
| 100 | + |
| 101 | +// PeerConfig defines a peer configuration |
| 102 | +type PeerConfig struct { |
| 103 | + URL string `yaml:"url"` |
| 104 | + EventURL string `yaml:"eventUrl"` |
| 105 | + GRPCOptions map[string]interface{} `yaml:"grpcOptions"` |
| 106 | + TLSCACerts TLSConfig `yaml:"tlsCACerts"` |
| 107 | +} |
| 108 | + |
| 109 | +// CAConfig defines a CA configuration |
| 110 | +// not currently used by CLI |
| 111 | +type CAConfig struct { |
| 112 | + URL string `yaml:"url"` |
| 113 | + HTTPOptions map[string]interface{} `yaml:"httpOptions"` |
| 114 | + TLSCACerts MutualTLSConfig `yaml:"tlsCACerts"` |
| 115 | + Registrar EnrollCredentials `yaml:"registrar"` |
| 116 | + CaName string `yaml:"caName"` |
| 117 | +} |
| 118 | + |
| 119 | +// EnrollCredentials holds credentials used for enrollment |
| 120 | +// not currently used by CLI |
| 121 | +type EnrollCredentials struct { |
| 122 | + EnrollID string `yaml:"enrollId"` |
| 123 | + EnrollSecret string `yaml:"enrollSecret"` |
| 124 | +} |
| 125 | + |
| 126 | +// TLSConfig TLS configurations |
| 127 | +type TLSConfig struct { |
| 128 | + // the following two fields are interchangeable. |
| 129 | + // If Path is available, then it will be used to load the cert |
| 130 | + // if Pem is available, then it has the raw data of the cert it will be used as-is |
| 131 | + // Certificate root certificate path |
| 132 | + Path string `yaml:"path"` |
| 133 | + // Certificate actual content |
| 134 | + Pem string `yaml:"pem"` |
| 135 | +} |
| 136 | + |
| 137 | +// MutualTLSConfig Mutual TLS configurations |
| 138 | +// not currently used by CLI |
| 139 | +type MutualTLSConfig struct { |
| 140 | + Pem []string `yaml:"pem"` |
| 141 | + |
| 142 | + // Certfiles root certificates for TLS validation (Comma separated path list) |
| 143 | + Path string `yaml:"path"` |
| 144 | + |
| 145 | + //Client TLS information |
| 146 | + Client TLSKeyPair `yaml:"client"` |
| 147 | +} |
| 148 | + |
| 149 | +// TLSKeyPair contains the private key and certificate for TLS encryption |
| 150 | +// not currently used by CLI |
| 151 | +type TLSKeyPair struct { |
| 152 | + Key TLSConfig `yaml:"key"` |
| 153 | + Cert TLSConfig `yaml:"cert"` |
| 154 | +} |
| 155 | + |
| 156 | +// GetConfig unmarshals the provided connection profile into a network |
| 157 | +// configuration struct |
| 158 | +func GetConfig(fileName string) (*NetworkConfig, error) { |
| 159 | + if fileName == "" { |
| 160 | + return nil, errors.New("filename cannot be empty") |
| 161 | + } |
| 162 | + |
| 163 | + data, err := ioutil.ReadFile(fileName) |
| 164 | + if err != nil { |
| 165 | + return nil, errors.Wrap(err, "error reading connection profile") |
| 166 | + } |
| 167 | + |
| 168 | + configData := string(data) |
| 169 | + config := &NetworkConfig{} |
| 170 | + err = yaml.Unmarshal([]byte(configData), &config) |
| 171 | + if err != nil { |
| 172 | + return nil, errors.Wrap(err, "error unmarshaling YAML") |
| 173 | + } |
| 174 | + |
| 175 | + return config, nil |
| 176 | +} |
0 commit comments