當(dāng)前位置:首頁 > 物聯(lián)網(wǎng) > 區(qū)塊鏈
[導(dǎo)讀] 簡介 基于角色的訪問控制是軟件系統(tǒng)的安全需求,旨在為數(shù)百個用戶提供訪問。雖然這種需求通常在企業(yè)軟件和操作系統(tǒng)中實現(xiàn),但對以太坊區(qū)塊鏈的處理并不多。 當(dāng)將供應(yīng)鏈設(shè)計為有向無環(huán)圖時,

簡介

基于角色的訪問控制是軟件系統(tǒng)的安全需求,旨在為數(shù)百個用戶提供訪問。雖然這種需求通常在企業(yè)軟件和操作系統(tǒng)中實現(xiàn),但對以太坊區(qū)塊鏈的處理并不多。

當(dāng)將供應(yīng)鏈設(shè)計為有向無環(huán)圖時,我們意識到需要動態(tài)地確定誰可以向圖中的每個節(jié)點添加信息。從現(xiàn)實世界的角度來看,如果您擁有一家制造工廠,您可能希望裝配線上的所有操作員都能夠用他們自己的帳戶記錄他們已經(jīng)組裝了一個零件。

OpenZeppelin是我在Solidity開發(fā)中使用的金標準,它有一個roles.sol合同,用于在erc721.sol合同中實現(xiàn)諸如minter和burner等角色。不幸的是,這些實現(xiàn)不允許在運行時創(chuàng)建新角色,如果您想使用單獨的角色控制對每個單獨令牌的訪問,則需要創(chuàng)建新角色。

本文旨在展示如何為以太坊區(qū)塊鏈構(gòu)建基于角色的訪問控制系統(tǒng)。根據(jù)我們的要求從頭開始編寫RBAC合同,然后從OpenZeppelin中找到了相同想法的版本,它具有幾乎相同的方法。為了可重用性,我盡可能地重構(gòu)我的代碼以遵循它們的命名法。

在以下各節(jié)中,我將介紹:

1. 我們進入訪問系統(tǒng)的設(shè)計要求;

2. 智能合約的實施;

3. 測試案例;

4. 狀態(tài)變換法的gas利用;

5. 還有一些完善的想法。

概念設(shè)計

我對RBAC系統(tǒng)的想法很簡單。

1. 角色將由數(shù)字標識符標識,如Unix中的組。

2. 角色可以動態(tài)創(chuàng)建。

3. 每個角色存儲用戶的地址。

4. 每個角色都會有一個關(guān)聯(lián)的第二個角色,這是唯一允許添加或刪除用戶的角色。

如果您是使用OpenZeppelin中的Roles.sol和RBAC.sol合同,則需要注意Roles.sol僅實現(xiàn)在角色內(nèi)生效的操作,而在角色外部發(fā)生的操作在RBAC.sol或訪問中實現(xiàn)/roles/*Role.sol收縮,包括在創(chuàng)建角色時存儲角色的數(shù)據(jù)結(jié)構(gòu)。

在我的實現(xiàn)中,我根據(jù)我們的用例做了一些決策:

· 角色結(jié)構(gòu)中包含一個描述字符串,結(jié)構(gòu)本身存儲在一個數(shù)組中。數(shù)組中每個角色結(jié)構(gòu)的位置用作標識符。有一種使用映射來存儲角色,但我發(fā)現(xiàn)這里沒有必要。

· 每個角色在實例化時接收我們指定為其管理角色的另一個角色的標識符,并且在實例化之后不能修改該角色。此管理員角色是唯一可以為此角色添加和刪除承載者的角色。

出于安全性和一致性的原因,您可以從角色中刪除承載,但沒有方法可以從系統(tǒng)中完全刪除角色。

pragma solidity ^0.5.0;

/**

* @title RBAC

* @author Alberto Cuesta Canada

* @noTIce Implements runTIme configurable Role Based Access Control.

*/

contract RBAC {

event RoleCreated(uint256 role);

event BearerAdded(address account, uint256 role);

event BearerRemoved(address account, uint256 role);

uint256 constant NO_ROLE = 0;

/**

* @noTIce A role, which will be used to group users.

* @dev The role id is its posiTIon in the roles array.

* @param description A description for the role.

* @param admin The only role that can add or remove bearers from

* this role. To have the role bearers to be also the role admins

* you should pass roles.length as the admin role.

* @param bearers Addresses belonging to this role.

*/

struct Role {

string description;

uint256 admin;

mapping (address =》 bool) bearers;

}

/**

* @notice All roles ever created.

*/

Role[] public roles;

/**

* @notice The contract constructor, empty as of now.

*/

constructor() public {

addRootRole(“NO_ROLE”);

}

/**

* @notice Create a new role that has itself as an admin.

* msg.sender is added as a bearer.

* @param _roleDescription The description of the role created.

* @return The role id.

*/

function addRootRole(string memory _roleDescription)

public

returns(uint256)

{

uint256 role = addRole(_roleDescription, roles.length);

roles[role].bearers[msg.sender] = true;

emit BearerAdded(msg.sender, role);

}

/**

* @notice Create a new role.

* @param _roleDescription The description of the role created.

* @param _admin The role that is allowed to add and remove

* bearers from the role being created.

* @return The role id.

*/

function addRole(string memory _roleDescription, uint256 _admin)

public

returns(uint256)

{

require(_admin 《= roles.length, “Admin role doesn‘t exist.”);

uint256 role = roles.push(

Role({

description: _roleDescription,

admin: _admin

})

) - 1;

emit RoleCreated(role);

return role;

}

/**

* @notice Retrieve the number of roles in the contract.

* @dev The zero position in the roles array is reserved for

* NO_ROLE and doesn’t count towards this total.

*/

function totalRoles()

public

view

returns(uint256)

{

return roles.length - 1;

}

/**

* @notice Verify whether an account is a bearer of a role

* @param _account The account to verify.

* @param _role The role to look into.

* @return Whether the account is a bearer of the role.

*/

function hasRole(address _account, uint256 _role)

public

view

returns(bool)

{

return _role 《 roles.length && roles[_role].bearers[_account];

}

/**

* @notice A method to add a bearer to a role

* @param _account The account to add as a bearer.

* @param _role The role to add the bearer to.

*/

function addBearer(address _account, uint256 _role)

public

{

require(

_role 《 roles.length,

“Role doesn‘t exist.”

);

require(

hasRole(msg.sender, roles[_role].admin),

“User can’t add bearers.”

);

require(

!hasRole(_account, _role),

“Account is bearer of role.”

);

roles[_role].bearers[_account] = true;

emit BearerAdded(_account, _role);

}

/**

* @notice A method to remove a bearer from a role

* @param _account The account to remove as a bearer.

* @param _role The role to remove the bearer from.

*/

function removeBearer(address _account, uint256 _role)

public

{

require(

_role 《 roles.length,

“Role doesn‘t exist.”

);

require(

hasRole(msg.sender, roles[_role].admin),

“User can’t remove bearers.”

);

require(

hasRole(_account, _role),

“Account is not bearer of role.”

);

delete roles[_role].bearers[_account];

emit BearerRemoved(_account, _role);

}

}

測試

我喜歡公開測試智能合約,既展示了操作案例,又能對代碼的可靠性提供了一些信心。

Contract: RBAC

RBAC

? addRootRole creates a role.

? hasRole returns false for non existing roles.

? hasRole returns false for non existing bearerships.

? addRootRole adds msg.sender as bearer.

? addRole doesn’t add msg.sender with admin role.

? addBearer reverts on non existing roles.

? addBearer reverts on non authorized users.

? addBearer reverts if the bearer belongs to the role.

? addBearer adds a bearer to a role.

? removeBearer reverts on non existing roles.

? removeBearer reverts on non authorized users.

? removeBearer reverts if the bearer doesn‘t belong to the role.

? removeBearer removes a bearer from a role.

為了回應(yīng)之前的反饋,我現(xiàn)在還使用eth-gas-reporter的gas使用報告。

結(jié)論

本文描述了一個基于智能合約角色的訪問控制系統(tǒng)的實現(xiàn),它具有以下屬性:

1. 允許在系統(tǒng)運行時創(chuàng)建新角色。

2. 包括角色管理員的概念,允許添加和刪除角色的成員。

3. 允許輕松確定所有現(xiàn)有角色及其承載。

4. 基于角色的訪問控制實現(xiàn)起來并不一定復(fù)雜,但正如本文所示,需要考慮許多權(quán)衡和設(shè)計決策,這些決策與您的用戶及其允許的操作密切相關(guān) 去設(shè)計。如果您決定復(fù)用RBAC系統(tǒng)的這種實現(xiàn),我會很高興,但我也鼓勵您尋找并考慮其他選擇。

本站聲明: 本文章由作者或相關(guān)機構(gòu)授權(quán)發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點,本站亦不保證或承諾內(nèi)容真實性等。需要轉(zhuǎn)載請聯(lián)系該專欄作者,如若文章內(nèi)容侵犯您的權(quán)益,請及時聯(lián)系本站刪除。
換一批
延伸閱讀

9月2日消息,不造車的華為或?qū)⒋呱龈蟮莫毥谦F公司,隨著阿維塔和賽力斯的入局,華為引望愈發(fā)顯得引人矚目。

關(guān)鍵字: 阿維塔 塞力斯 華為

加利福尼亞州圣克拉拉縣2024年8月30日 /美通社/ -- 數(shù)字化轉(zhuǎn)型技術(shù)解決方案公司Trianz今天宣布,該公司與Amazon Web Services (AWS)簽訂了...

關(guān)鍵字: AWS AN BSP 數(shù)字化

倫敦2024年8月29日 /美通社/ -- 英國汽車技術(shù)公司SODA.Auto推出其旗艦產(chǎn)品SODA V,這是全球首款涵蓋汽車工程師從創(chuàng)意到認證的所有需求的工具,可用于創(chuàng)建軟件定義汽車。 SODA V工具的開發(fā)耗時1.5...

關(guān)鍵字: 汽車 人工智能 智能驅(qū)動 BSP

北京2024年8月28日 /美通社/ -- 越來越多用戶希望企業(yè)業(yè)務(wù)能7×24不間斷運行,同時企業(yè)卻面臨越來越多業(yè)務(wù)中斷的風(fēng)險,如企業(yè)系統(tǒng)復(fù)雜性的增加,頻繁的功能更新和發(fā)布等。如何確保業(yè)務(wù)連續(xù)性,提升韌性,成...

關(guān)鍵字: 亞馬遜 解密 控制平面 BSP

8月30日消息,據(jù)媒體報道,騰訊和網(wǎng)易近期正在縮減他們對日本游戲市場的投資。

關(guān)鍵字: 騰訊 編碼器 CPU

8月28日消息,今天上午,2024中國國際大數(shù)據(jù)產(chǎn)業(yè)博覽會開幕式在貴陽舉行,華為董事、質(zhì)量流程IT總裁陶景文發(fā)表了演講。

關(guān)鍵字: 華為 12nm EDA 半導(dǎo)體

8月28日消息,在2024中國國際大數(shù)據(jù)產(chǎn)業(yè)博覽會上,華為常務(wù)董事、華為云CEO張平安發(fā)表演講稱,數(shù)字世界的話語權(quán)最終是由生態(tài)的繁榮決定的。

關(guān)鍵字: 華為 12nm 手機 衛(wèi)星通信

要點: 有效應(yīng)對環(huán)境變化,經(jīng)營業(yè)績穩(wěn)中有升 落實提質(zhì)增效舉措,毛利潤率延續(xù)升勢 戰(zhàn)略布局成效顯著,戰(zhàn)新業(yè)務(wù)引領(lǐng)增長 以科技創(chuàng)新為引領(lǐng),提升企業(yè)核心競爭力 堅持高質(zhì)量發(fā)展策略,塑強核心競爭優(yōu)勢...

關(guān)鍵字: 通信 BSP 電信運營商 數(shù)字經(jīng)濟

北京2024年8月27日 /美通社/ -- 8月21日,由中央廣播電視總臺與中國電影電視技術(shù)學(xué)會聯(lián)合牽頭組建的NVI技術(shù)創(chuàng)新聯(lián)盟在BIRTV2024超高清全產(chǎn)業(yè)鏈發(fā)展研討會上宣布正式成立。 活動現(xiàn)場 NVI技術(shù)創(chuàng)新聯(lián)...

關(guān)鍵字: VI 傳輸協(xié)議 音頻 BSP

北京2024年8月27日 /美通社/ -- 在8月23日舉辦的2024年長三角生態(tài)綠色一體化發(fā)展示范區(qū)聯(lián)合招商會上,軟通動力信息技術(shù)(集團)股份有限公司(以下簡稱"軟通動力")與長三角投資(上海)有限...

關(guān)鍵字: BSP 信息技術(shù)
關(guān)閉
關(guān)閉