超级账本HyperLedger的fabricCA的用法讲解

说明

概览

编译安装

fabric-ca-server初始化

生成fabric-ca管理员的凭证

查看联盟成员

注册新用户

用户的属性

查看用户详情

用户的权利范围

参考

说明

超级账本HyperLedger视频教程演示汇总:HyperLedger Fabric的视频讲解–“主页”中可领优惠券

超级账本HyperLedger的Fabric-CA的使用演示(两个组织一个Orderer三个Peer)

Hyperledger Fabric CA是Hyperledger Fabric的证书授权中心,支持:

添加用户、或者对接LDAP

签发注册证书

证书的更新和撤销

Welcome to Hyperledger Fabric CA

fabric-ca codes

概览

如下图所示,fabric-ca可以对接ldap,或者mysql、pgsql数据库。

通过fabric-ca client或者fabric-ca的sdk获得证书后,就可以用这些证书访问Peer。

编译安装

$ 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初始化

mkdir -p /opt/app/fabric-ca/client

cp $GOPATH/src/github.com/hyperledger/fabric-ca/bin/fabric-ca-client /opt/app/fabric-ca/client/

ln -s /opt/app/fabric-ca/client/fabric-ca-client  /usr/bin/fabric-ca-client

mkdir -p /opt/app/fabric-ca/server

cp $GOPATH/src/github.com/hyperledger/fabric-ca/bin/fabric-ca-server /opt/app/fabric-ca/server/

cd /opt/app/fabric-ca/server

初始化,可以使用ldap、mysql、postgresql,或者本地sqlite,默认sqlite:

./fabric-ca-server init -b admin:pass

如果启动的是中间CA,用-u指定上级CA:

./fabric-ca-server start -b admin:adminpw -u http://:@:

如果直接作为RootCA,后面两个参数表示允许删除联盟和用户:

./fabric-ca-server start -b admin:pass --cfg.affiliations.allowremove  --cfg.identities.allowremove

初始化后,会在当前目录生成以下文件:

ca-cert.pem  fabric-ca-server-config.yaml  fabric-ca-server.db  msp/

fabric-ca-server-config.yaml是fabric-ca-server的配置文件。

其中ca-cert.pem是一个自签署的证书:

$ openssl verify -CAfile ca-cert.pem ca-cert.pem

ca-cert.pem: OK

这个证书是根据fabric-ca-server-config.yaml中的csr生成的:

csr:

  cn: fabric-ca-server

  names:

      - C: US

        ST: "North Carolina"

        L:

        O: Hyperledger

        OU: Fabric

  hosts:

    - 10-39-0-121

    - localhost

  ca:

      expiry: 131400h

      pathlength: 1

可以用下面的命令查看:

openssl x509 -in ca-cert.pem  -text

如果要生成被其它CA签署的证书,使用-u指定上一级CA:

-u

URL格式:

    ://:@:

如果要签署已经准备好的key,需要用ca.certfile和ca.keyfile指定:

ca:

  # Name of this CA

  name:

  # Key file (is only used to import a private key into BCCSP)

  keyfile:

  # Certificate file (default: ca-cert.pem)

  certfile:

  # Chain file

  chainfile:

msp/keystore中存放的BCCSP (BlockChain Crypto Service Provider)中用到的key:

bccsp:

    default: SW

    sw:

        hash: SHA2

        security: 256

        filekeystore:

            # The directory used for the software file-based keystore

            keystore: msp/keystore

还可以通过指定-cacount生成多个CA:

fabric-ca-server start -b admin:adminpw --cacount 2

以及指定多个cafiles:

fabric-ca-server start -b admin:adminpw --cafiles ca/ca1/fabric-ca-config.yaml --cafiles ca/ca2/fabric-ca-config.yaml

每个cafiles格式如下:

ca:

# Name of this CA

name:

csr:

  cn:

目录结构可以如下:

--

  |--ca

    |--ca1

      |-- fabric-ca-config.yaml

    |--ca2

      |-- fabric-ca-config.yaml

生成fabric-ca管理员的凭证

生成第一个用户的凭证:

export FABRIC_CA_CLIENT_HOME=/opt/app/fabric-ca/clients/admin

mkdir -p $FABRIC_CA_CLIENT_HOME

fabric-ca-client enroll -u http://admin:pass@localhost:7054

或者:

fabric-ca-client enroll -u http://admin:pass@localhost:7054 -H `pwd`

在/opt/app/fabric-ca/clients/admin可以看到生成了以下文件:

fabric-ca-client-config.yaml  msp/

其中fabric-ca-client-config.yaml是一个默认的配置文件,msp/中存放的是相关的证书。

msp/

|-- cacerts

|  -- localhost-7054.pem

|-- intermediatecerts

|  -- localhost-7054.pem

|-- keystore

|  -- a17c01f53b037cbf51ad76f2a95684e6b2a4b8371e0b247949805af8035bf50e_sk

-- signcerts

    -- cert.pem

之后就可以通过msp目录中的证书,访问fabric-ca,不需要在输入密码。

查看联盟成员

fabric-ca默认注册了几个联盟,可以用affiliation list查看:

$ fabric-ca-client affiliation list

2018/04/28 14:59:55 [INFO] 127.0.0.1:38094 GET /affiliations 200 0 "OK"

affiliation: .

  affiliation: org1

      affiliation: org1.department1

      affiliation: org1.department2

  affiliation: org2

      affiliation: org2.department1

添加/删除联盟成员,也是通过这个命令进行:

fabric-ca-client affiliation add another

fabric-ca-client affiliation add another.sub

新增加的联盟成员如下:

[root@10-39-0-121 admin]# fabric-ca-client affiliation list

2018/04/28 15:03:42 [INFO] 127.0.0.1:38104 GET /affiliations 201 0 "OK"

affiliation: .

  affiliation: org1

      affiliation: org1.department1

      affiliation: org1.department2

  affiliation: org2

      affiliation: org2.department1

  affiliation: another

      affiliation: another.sub1

默认是禁止删除联盟,如果需要开启,需要在启动fabric-ca-server时传入参数--cfg.affiliations.allowremove:

./fabric-ca-server start -b  admin:pass  --cfg.affiliations.allowremove

然后可以删除:

fabric-ca-client affiliation remove another.sub1

如果联盟下还有联盟,可以加上参数-force级联删除。

注册新用户

例如注册一个名为admin2的用户,先指定要使用的用户的client目录:

$ export FABRIC_CA_CLIENT_HOME=/opt/app/fabric-ca/clients/admin

也可以用-H参数指定:

$ fabric-ca-client -H /opt/app/fabric-ca/clients/admin  <其它参数...>

注册新用户的命令如下:

$ fabric-ca-client register --id.name admin2 --id.affiliation org1.department1 --id.attrs 'hf.Revoker=true,admin=true:ecert'

2018/04/28 10:20:42 [INFO] Configuration file location: /opt/app/fabric-ca/clients/admin/fabric-ca-client-config.yaml

2018/04/28 10:20:42 [INFO] 127.0.0.1:38036 POST /register 201 0 "OK"

Password: wvkpvMzLsjPz

新用户注册后,会自动为新用户生成一个密码,新用户需要使用这个密码,生成自己的凭证,就像第一个用户那样。

为新注册的用户admin2准备一个目录,注意要使用一个新的目录,防止与其它用户混淆:

export FABRIC_CA_CLIENT_HOME=/opt/app/fabric-ca/clients/admin2

mkdir -p $FABRIC_CA_CLIENT_HOME

然后生成凭证:

fabric-ca-client enroll -u http://admin2:wvkpvMzLsjPz@localhost:7054

之后可以看到新的目录中也生成了fabric-ca-client-config.yaml和msp/,这是admin2用户的凭证:

$ tree admin2/

admin2/

|-- fabric-ca-client-config.yaml

-- msp

    |-- cacerts

    |  -- localhost-7054.pem

    |-- intermediatecerts

    |  -- localhost-7054.pem

    |-- keystore

    |  -- 6732d8d3802384c8935aa7174930975ba6141b6d4bc419e6ccbe5f4d76db58f0_sk

    -- signcerts

        -- cert.pem

用户的属性

在注册admin2的时候,指定了这样一些属性--id.attrs 'hf.Revoker=true,admin=true:ecert':

fabric-ca-client register --id.name admin2 --id.affiliation org1.department1 --id.attrs 'hf.Revoker=true,admin=true:ecert'

这些属性是一定要了解的,以hf.开头的属性,是fabric-ca的内置属性:

hf.Registrar.Roles            List        List of roles that the registrar is allowed to manage

hf.Registrar.DelegateRoles    List        List of roles that the registrar is allowed to give to a registree for its ‘hf.Registrar.Roles’ attribute

hf.Registrar.Attributes      List        List of attributes that registrar is allowed to register

hf.GenCRL                    Boolean    Identity is able to generate CRL if attribute value is true

hf.Revoker                    Boolean    Identity is able to revoke a user and/or certificates if attribute value is true

hf.AffiliationMgr            Boolean    Identity is able to manage affiliations if attribute value is true

hf.IntermediateCA            Boolean    Identity is able to enroll as an intermediate CA if attribute value is true

这些属性表示的是用户是否拥有相关的权限:

hf.Registrar.Roles该用户可以增加的新用户类型,用户类型都有:client、orderer、peer、user。

hf.Registrar.DelegateRoles该用户可以设置的新用户的hf.Registrar.Roles属性。

hf.Registrar.Attributes该用户可以为新用户设置的保留属性和自定义属性。

hf.GenCRL该用户是否可以获取CRL列表,已经撤销的证书列表。

hf.Revoker该用户是否能够撤销其它用户。

hf.AffiliationMgr该用户是否可以管理联盟。

hf.IntermediateCA该用户是否可以作为中间CA。

除了这些以hf.开头的属性外,还可以自定义属性,例如下面的admin=true:

fabric-ca-client register --id.name admin2 --id.affiliation org1.department1 \

--id.attrs 'hf.Revoker=true,admin=true:ecert'

用户自定义的属性可以写入到用户的凭证中,在合约中可以获取发起请求的用户属性,根据用户属性决定用户是否由操作权限。

参考HyplerLedger FabricCA ABACHyperLedger Fabric Chaincode ABAC给出一个使用ABAC的chaincode:

...

"github.com/hyperledger/fabric/core/chaincode/lib/cid"

...

err := cid.AssertAttributeValue(stub, "abac.init", "true")

if err != nil {

    return shim.Error(err.Error())

}

...

cid的使用说明见: HyperLedger Fabric: Client Identity Chaincode Library

cid(Client Identity)中提供下面的方法:

+AssertAttributeValue(stub ChaincodeStubInterface, attrName, attrValue string) : error

+GetAttributeValue(stub ChaincodeStubInterface, attrName string) : string, bool, error

+GetID(stub ChaincodeStubInterface) : string, error

+GetMSPID(stub ChaincodeStubInterface) : string, error

+GetX509Certificate(stub ChaincodeStubInterface) : *x509.Certificate, error

+New(stub ChaincodeStubInterface) : ClientIdentity, error

▼+ClientIdentity : interface

    [methods]

  +AssertAttributeValue(attrName, attrValue string) : error

  +GetAttributeValue(attrName string) : string, bool, error

  +GetID() : string, error

  +GetMSPID() : string, error

  +GetX509Certificate() : *x509.Certificate, error

“admin=true:ecert”中的ecert的意思是,该属性会被自动写入到用户凭证中(enrollment certificate)。

对于没有注明ecert的属性,可以在生成用户凭证的时候,例如注册了这样一个用户的时候:

fabric-ca-client register --id.name user1 --id.secret user1pw --id.type user \

--id.affiliation org1 --id.attrs 'app1Admin=true:ecert,email=user1@gmail.com'

它的属性email=user1@gmail.com没有注明是ecert,在生成凭证的时候,可以指定下面的属性:

fabric-ca-client enroll -u http://user1:user1pw@localhost:7054 --enrollment.attrs "email,phone:opt"

app1Admin将被自动包含在凭证中;email是被明确指定的,它必须是存在的,否则报错;phone:opt也是明确指定的,opt表示phone属性可选的,如果没有phone属性不会报错。

以hf.开头的下面三个属性,也会被自动包含在登陆凭证中:

hf.EnrollmentID    The enrollment ID of the identity

hf.Type            The type of the identity

hf.Affiliation    The affiliation of the identity

查看用户详情

可以用fabric-ca-client identity list查看有权限查看的用户的详情:

$ fabric-ca-client identity list

Name: admin, Type: client, Affiliation: , Max Enrollments: -1, Attributes: [{Name:hf.GenCRL Value:1 ECert:false} {Name:hf.Registrar.Attributes Value:* ECert:false} {Name:hf.AffiliationMgr Value:1 ECert:false} {Name:hf.Registrar.Roles Value:peer,orderer,client,user ECert:false} {Name:hf.Registrar.DelegateRoles Value:peer,orderer,client,user ECert:false} {Name:hf.Revoker Value:1 ECert:false} {Name:hf.IntermediateCA Value:1 ECert:false}]

Name: admin2, Type: client, Affiliation: org1.department1, Max Enrollments: -1, Attributes: [{Name:hf.Revoker Value:true ECert:false} {Name:admin Value:true ECert:true} {Name:hf.EnrollmentID Value:admin2 ECert:true} {Name:hf.Type Value:client ECert:true} {Name:hf.Affiliation Value:org1.department1 ECert:true}]

用户的权利范围

用户的权利范围是用联盟属性hf.Affiliation来控制的。

hf.Affiliation    The affiliation of the identity

联盟属性一个类似”a.b.c”样式的字符串,最顶层用户的该属性为”.”。

一个用户只能管理平级以及下级的用户。

例如如果一个用户的hf.Affiliation是”a.b.*“,那么它能管理的用户的联盟属性必须以”a.b”开头。

联盟属性可以配置多个,并且支持通配符,例如:

hf.Registrar.Attributes = a.b.*, x.y.z

另外:

一个用户管理它的`hf.Registrar.Roles`属性中列出用户类型。

一个用户能够管理的其它用户的属性,只能是它的`hf.Registrar.Attributes`属性列出的属性。

    其中保留属性,还要求必须是当前用户拥有的属性。

后续的很多操作都受到权利范围的约束。

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 161,780评论 4 369
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 68,424评论 1 305
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 111,397评论 0 254
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,576评论 0 218
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,997评论 3 295
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,945评论 1 224
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 32,107评论 2 317
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,850评论 0 208
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,625评论 1 250
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,804评论 2 253
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,285评论 1 265
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,613评论 3 261
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,291评论 3 242
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,164评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,963评论 0 201
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 36,096评论 2 285
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,886评论 2 278

推荐阅读更多精彩内容