Fabric的權限管理:Attribute-Based Access Control


之前稍微了解過Client Identity Chaincode Library,這幾天正好開始實際應用。

雖然了解過,還是發現了不少之前理解的不足,也踩了不少坑。

先列出官方介紹: https://github.com/hyperledger/fabric/blob/release-1.1/core/chaincode/lib/cid/README.md

 1,首先要給注冊的user添加attrs,但是在ca 數據庫中看不到,chaincode 層也查不到

查看CA的log,並沒有報錯,user也成功enroll,

chaincode層查找fabric 默認的attrs,則可以查到。然后意識到,需要在ca-server-config.yaml中添加需要的attrs。

2,在chaincode中 import http://github.com/hyperledger/fabric/core/chaincode/lib/cid,compile的時候總是說找不到 github.com/hyperledger/fabric/core/chaincode/lib/cid

錯誤消息說的很明確,但是由於對go語言及擴展知識理解不做,踩了不少坑。

shim包可以引入成功,但是並不知道shim包在哪里,也不知道應該怎么引入新包。於是系統中搜索shim,但是找不到結果。

上網查找,很確認這里是正解:https://stackoverflow.com/questions/49560104/cannot-find-package-cid-in-goroot-or-gopath

但是還是不是很明白,最后參考abac的例子和govendor的文檔,才搞定

 

下面是一些關鍵代碼:

ca-config.yaml

registry:
  # Maximum number of times a password/secret can be reused for enrollment
  # (default: -1, which means there is no limit)
  maxenrollments: -1

  # Contains identity information which is used when LDAP is disabled
  identities:
     - name: admin
       pass: adminpw
       type: client
       affiliation: ""
       attrs:
          hf.Registrar.Roles: "peer,orderer,client,user"
          hf.Registrar.DelegateRoles: "peer,orderer,client,user"
          hf.Revoker: true
          hf.IntermediateCA: true
          hf.GenCRL: true
          hf.Registrar.Attributes: "*"
          hf.AffiliationMgr: true
          permissions: "*"

  

node js

			let secret = await caClient.register({
				enrollmentID: username,
				affiliation: userOrg.toLowerCase() + '.department1',
				attrs:[{name:"hf.Registrar.Attributes",value:"query",ecert:true},
				{name:"permissions",value:"query",ecert:true}]
				//attrs:reg_attr
			}, adminUserObj);

  

 

chaincode

// Get the client ID object
	id, err := cid.New(stub)

	fmt.Println("client ID object:")
	fmt.Println(id)
	if err != nil {
		return shim.Error(err.Error())
	}
	mspid, err := id.GetMSPID()

	fmt.Println("mspid:")
	fmt.Println(mspid)
	if err != nil {
		return shim.Error(err.Error())
	}

	cert, err := cid.GetX509Certificate(stub)
	fmt.Println("cert:")
	fmt.Printf("%+v\n", cert)
	fmt.Println("cert.Extensions :")
	fmt.Printf("%+v\n", cert.Extensions)
	fmt.Println("cert.Subject.CommonName:")
	fmt.Println(cert.Subject.CommonName)

	val, ok, err := cid.GetAttributeValue(stub, "hf.Registrar.Attributes")
	if err != nil {
		return shim.Error(err.Error())
	}
	if !ok {
		return shim.Error("The client identity does not possess the attribute:hf.Registrar.Attributes")
	}
	fmt.Println("hf.Registrar.Attributes:")
	fmt.Println(val)

	val, ok, err = cid.GetAttributeValue(stub, "permissions")
	if err != nil {
		return shim.Error(err.Error())
	}
	if !ok {
		return shim.Error("The client identity does not possess the attribute:permissions")
	}
	fmt.Println("permissions:")
	fmt.Println(val)

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM