说明:通过CA服务器构建联盟证书结构替代配置文件生成证书
$ go get -u github.com/hyperledger/fabric-ca
$ cd $GOPATH/src/github.com/hyperledger/fabric-ca
$ make fabric-ca-server
$ make fabric-ca-client
$ ls bin/
fabric-ca-client fabric-ca-server
fabric-ca-server start -b admin:pass
因为在创建CA服务的时候已经注册成功,这里只是做下载操作就可以
mkdir -p `pwd`/Org/admin
fabric-ca-client enroll -u http://admin:pass@localhost:7054 -H `pwd`/Org/admin
可以使用环境变量的方式执行client的工作目录,如果指定,生成的证书会放在此处,也可以用-H指定具体目录。
两者选其一,选二是傻逼
export FABRIC_CA_CLIENT_HOME=`pwd`/Org/admin
mkdir -p $FABRIC_CA_CLIENT_HOME
fabric-ca-client enroll -u http://admin:pass@localhost:7054
注:上面的方式会默认启动两个组织
$ fabric-ca-client -H `pwd`/Org/admin affiliation list
2018/05/07 02:36:46 [INFO] [::1]:56148 GET /affiliations 200 0 "OK"
affiliation: .
affiliation: org2
affiliation: org2.department1
affiliation: org1
affiliation: org1.department1
affiliation: org1.department2
为了查看信息的时候,看到的输出比较简洁,用下面的命令将其删除
fabric-ca-client -H `pwd`/Org/admin affiliation remove --force org1
fabric-ca-client -H `pwd`/Org/admin affiliation remove --force org2
执行下面命令创建联盟
fabric-ca-client -H `pwd`/Org/admin affiliation add com
fabric-ca-client -H `pwd`/Org/admin affiliation add com.example
fabric-ca-client -H `pwd`/Org/admin affiliation add com.example.org1
fabric-ca-client -H `pwd`/Org/admin affiliation add com.example.org2
注:联盟是有层级的
注:就是从Fabric-CA中,读取出用来签署用户的根证书,这一步的作用是获取根证书,是根证书,是根证书。
创建联盟目录,并且获取根证书
mkdir -p ./Org/example.com/msp
fabric-ca-client getcacert -M `pwd`/Org/example.com/msp //-M需要指定绝对路径
执行后,会在指定目录得到文件如下:
$ tree Org/example.com/msp/
example.com/msp/
|-- cacerts
| `-- localhost-7054.pem
|-- intermediatecerts
| `-- localhost-7054.pem
|-- keystore
`-- signcerts
同样的方式为旗下的组织获取根证书,以此模板,有几个组织获取几次
:然后这里要注意,就是还要创建两个目录,一个是 tlscacerts,一个是 admincerts,tlscacerts是为了tls通信用的,admincerts是用来存放组织管理员证书。这里简单说下他们的层级:
这里的getcacert是为了每个组织准备需要的CA文件,在生成创世快的时候要用。
tls 证书自己想办法 搞定 fabric-ca-client enroll -d --enrollment.profile tls
如果在你的环境中,各个组件域名的证书,是由第三方CA签署的,就将第三方CA的根证书添加到msp/tlscacerts目录中。
组织的msp目录中,包含都是CA根证书,分别是TLS加密的根证书,和用于身份验证的根证书。另外还需要admin用户的证书,后面的操作中会添加。
创建存放管理员的目录
mkdir -p ./Org/org3.example.com/admin
修改ca配置文件进行注册
id:
name: [email protected]
type: client
affiliation: com.example.org3
maxenrollments: 0
attributes:
- name: hf.Registrar.Roles
value: client,orderer,peer,user
- name: hf.Registrar.DelegateRoles
value: client,orderer,peer,user
- name: hf.Registrar.Attributes
value: "*"
- name: hf.GenCRL
value: true
- name: hf.Revoker
value: true
- name: hf.AffiliationMgr
value: true
- name: hf.IntermediateCA
value: true
- name: role
value: admin
ecert: true
注册:
fabric-ca-client register -H `pwd`/Org/admin --id.secret=password
从fabric-CA中读取凭证:
$ fabric-ca-client enroll -u http://[email protected]:password@localhost:7054 -H `pwd`/Org/org3.example.com/admin
$ ls ./Org/org3.example.com/admin
fabric-ca-client-config.yaml msp
Admin用户注册之后,还需要将它的证书添加到org3.example.com
将[email protected]的证书复制到org3.example.com的msp/admincerts中:
cp Org/org3.example.com/admin/msp/signcerts/cert.pem Org/org3.example.com/msp/admincerts/
这时候,就是组织的管理员注册完毕,然后用组织的管理员去注册peer节点用户
使用[email protected]账号来注册账号peer0.org3.example.com,用于org3的peer0
修改ca配置文件
id:
name: peer0.org3.example.com
type: peer
affiliation: com.example.org3
maxenrollments: 0
attributes:
- name: role
value: peer
ecert: true
注册:
fabric-ca-client register -H `pwd`/Org/org3.example.com/admin --id.secret=password
查看org3.example.com的所有账号
$ fabric-ca-client -H `pwd`/Org/org3.example.com/admin identity list
Name: [email protected], Type: client, Affiliation: com.example.org3, Max Enrollments: -1, Attributes: [{Name:hf.Registrar.Roles Value:client,orderer,peer,user ECert:false} {Name:hf.Registrar.DelegateRoles Value:client,orderer,peer,user ECert:false} {Name:hf.Registrar.Attributes Value:* ECert:false} {Name:hf.GenCRL Value:1 ECert:false} {Name:hf.Revoker Value:1 ECert:false} {Name:hf.AffiliationMgr Value:1 ECert:false} {Name:hf.IntermediateCA Value:1 ECert:false} {Name:role Value:admin ECert:true} {Name:hf.EnrollmentID Value:[email protected] ECert:true} {Name:hf.Type Value:client ECert:true} {Name:hf.Affiliation Value:com.example.org3 ECert:true}]
Name: peer0.org3.example.com, Type: peer, Affiliation: com.example.org3, Max Enrollments: -1, Attributes: [{Name:role Value:peer ECert:true} {Name:hf.EnrollmentID Value:peer0.org3.example.com ECert:true} {Name:hf.Type Value:peer ECert:true} {Name:hf.Affiliation Value:com.example.org3 ECert:true}]
从fabric-CA中读取peer0.org3.example.com的凭证:
mkdir ./Org/org3.example.com/peer0
fabric-ca-client enroll -u http://peer0.org3.example.com:password@localhost:7054 -H `pwd`/Org/org3.example.com/peer0