zoukankan      html  css  js  c++  java
  • 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
    ", cert)
    	fmt.Println("cert.Extensions :")
    	fmt.Printf("%+v
    ", 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)
    

      

  • 相关阅读:
    [学习笔记] php设计模式总结 [转]
    [学习笔记] Windows下搭建PHP开发环境[转载]
    [学习笔记] PHP中this,self,parent的区别 [转载]
    [学习笔记] mysql连接数据库[转]
    [学习笔记] Memcache [转载]
    [学习笔记] PHPUnit 使用方法 [转载]
    [学习笔记] 正则表达式30分钟入门教程 [转]
    [学习笔记] Linux软连接和硬链接 [转]
    HLSL Matrix变量的存储方式
    Hieroglyph3 框架分析2
  • 原文地址:https://www.cnblogs.com/13579net/p/9116551.html
Copyright © 2011-2022 走看看