6 Star 46 Fork 28

Hyperledger/fabric

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
config.go 1.79 KB
一键复制 编辑 原始数据 按行查看 历史
yacovm 提交于 2018-05-30 01:22 . [FAB-10458] Common CLI infrastructure
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package comm
import (
"io/ioutil"
"time"
"github.com/hyperledger/fabric/common/crypto/tlsgen"
"github.com/hyperledger/fabric/core/comm"
"github.com/pkg/errors"
)
type genTLSCertFunc func() (*tlsgen.CertKeyPair, error)
// Config defines configuration of a Client
type Config struct {
CertPath string
KeyPath string
PeerCACertPath string
Timeout time.Duration
}
// ToSecureOptions converts this Config to SecureOptions.
// The given function generates a self signed client TLS certificate if
// the TLS certificate and key aren't present at the config
func (conf Config) ToSecureOptions(newSelfSignedTLSCert genTLSCertFunc) (*comm.SecureOptions, error) {
if conf.PeerCACertPath == "" {
return &comm.SecureOptions{}, nil
}
caBytes, err := loadFile(conf.PeerCACertPath)
if err != nil {
return nil, errors.WithStack(err)
}
var keyBytes, certBytes []byte
// If TLS key and certificate aren't given, generate a self signed one on the fly
if conf.KeyPath == "" && conf.CertPath == "" {
tlsCert, err := newSelfSignedTLSCert()
if err != nil {
return nil, err
}
keyBytes, certBytes = tlsCert.Key, tlsCert.Cert
} else {
keyBytes, err = loadFile(conf.KeyPath)
if err != nil {
return nil, errors.WithStack(err)
}
certBytes, err = loadFile(conf.CertPath)
if err != nil {
return nil, errors.WithStack(err)
}
}
return &comm.SecureOptions{
Key: keyBytes,
Certificate: certBytes,
UseTLS: true,
ServerRootCAs: [][]byte{caBytes},
RequireClientCert: true,
}, nil
}
func loadFile(path string) ([]byte, error) {
b, err := ioutil.ReadFile(path)
if err != nil {
return nil, errors.Errorf("Failed opening file %s: %v", path, err)
}
return b, nil
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/hyperledger/fabric.git
git@gitee.com:hyperledger/fabric.git
hyperledger
fabric
fabric
v1.4.9

搜索帮助