/permission/api.go

https://github.com/jpmorganchase/quorum · Go · 1101 lines · 898 code · 132 blank · 71 comment · 438 complexity · f589c495d790cfee4fc19cf4849cb9db MD5 · raw file

  1. package permission
  2. import (
  3. "errors"
  4. "fmt"
  5. "math/big"
  6. "regexp"
  7. "github.com/ethereum/go-ethereum/accounts"
  8. "github.com/ethereum/go-ethereum/accounts/abi/bind"
  9. "github.com/ethereum/go-ethereum/common"
  10. "github.com/ethereum/go-ethereum/core/types"
  11. "github.com/ethereum/go-ethereum/internal/ethapi"
  12. "github.com/ethereum/go-ethereum/log"
  13. "github.com/ethereum/go-ethereum/p2p/enode"
  14. pbind "github.com/ethereum/go-ethereum/permission/bind"
  15. )
  16. var isStringAlphaNumeric = regexp.MustCompile(`^[a-zA-Z0-9_-]*$`).MatchString
  17. //default gas limit to use if not passed in sendTxArgs
  18. var defaultGasLimit = uint64(4712384)
  19. //default gas price to use if not passed in sendTxArgs
  20. var defaultGasPrice = big.NewInt(0)
  21. // PermAction represents actions in permission contract
  22. type PermAction int
  23. const (
  24. AddOrg PermAction = iota
  25. ApproveOrg
  26. AddSubOrg
  27. UpdateOrgStatus
  28. ApproveOrgStatus
  29. AddNode
  30. UpdateNodeStatus
  31. AssignAdminRole
  32. ApproveAdminRole
  33. AddNewRole
  34. RemoveRole
  35. AddAccountToOrg
  36. ChangeAccountRole
  37. UpdateAccountStatus
  38. InitiateNodeRecovery
  39. InitiateAccountRecovery
  40. ApproveNodeRecovery
  41. ApproveAccountRecovery
  42. )
  43. type AccountUpdateAction int
  44. const (
  45. SuspendAccount AccountUpdateAction = iota + 1
  46. ActivateSuspendedAccount
  47. BlacklistAccount
  48. RecoverBlacklistedAccount
  49. ApproveBlacklistedAccountRecovery
  50. )
  51. type NodeUpdateAction int
  52. const (
  53. SuspendNode NodeUpdateAction = iota + 1
  54. ActivateSuspendedNode
  55. BlacklistNode
  56. RecoverBlacklistedNode
  57. ApproveBlacklistedNodeRecovery
  58. )
  59. type OrgUpdateAction int
  60. const (
  61. SuspendOrg OrgUpdateAction = iota + 1
  62. ActivateSuspendedOrg
  63. )
  64. // QuorumControlsAPI provides an API to access Quorum's node permission and org key management related services
  65. type QuorumControlsAPI struct {
  66. permCtrl *PermissionCtrl
  67. }
  68. // txArgs holds arguments required for execute functions
  69. type txArgs struct {
  70. orgId string
  71. porgId string
  72. url string
  73. roleId string
  74. isVoter bool
  75. isAdmin bool
  76. acctId common.Address
  77. accessType uint8
  78. action uint8
  79. voter common.Address
  80. morgId string
  81. tmKey string
  82. txa ethapi.SendTxArgs
  83. }
  84. type PendingOpInfo struct {
  85. PendingKey string `json:"pendingKey"`
  86. PendingOp string `json:"pendingOp"`
  87. }
  88. var actionSuccess = "Action completed successfully"
  89. // NewQuorumControlsAPI creates a new QuorumControlsAPI to access quorum services
  90. func NewQuorumControlsAPI(p *PermissionCtrl) *QuorumControlsAPI {
  91. return &QuorumControlsAPI{p}
  92. }
  93. func (q *QuorumControlsAPI) OrgList() []types.OrgInfo {
  94. return types.OrgInfoMap.GetOrgList()
  95. }
  96. func (q *QuorumControlsAPI) NodeList() []types.NodeInfo {
  97. return types.NodeInfoMap.GetNodeList()
  98. }
  99. func (q *QuorumControlsAPI) RoleList() []types.RoleInfo {
  100. return types.RoleInfoMap.GetRoleList()
  101. }
  102. func (q *QuorumControlsAPI) AcctList() []types.AccountInfo {
  103. return types.AcctInfoMap.GetAcctList()
  104. }
  105. func (q *QuorumControlsAPI) GetOrgDetails(orgId string) (types.OrgDetailInfo, error) {
  106. o, err := types.OrgInfoMap.GetOrg(orgId)
  107. if err != nil {
  108. return types.OrgDetailInfo{}, err
  109. }
  110. if o == nil {
  111. return types.OrgDetailInfo{}, errors.New("org does not exist")
  112. }
  113. var acctList []types.AccountInfo
  114. var roleList []types.RoleInfo
  115. var nodeList []types.NodeInfo
  116. for _, a := range q.AcctList() {
  117. if a.OrgId == orgId {
  118. acctList = append(acctList, a)
  119. }
  120. }
  121. for _, a := range q.RoleList() {
  122. if a.OrgId == orgId {
  123. roleList = append(roleList, a)
  124. }
  125. }
  126. for _, a := range q.NodeList() {
  127. if a.OrgId == orgId {
  128. nodeList = append(nodeList, a)
  129. }
  130. }
  131. orgRec, err := types.OrgInfoMap.GetOrg(orgId)
  132. if err != nil {
  133. return types.OrgDetailInfo{}, err
  134. }
  135. if orgRec == nil {
  136. return types.OrgDetailInfo{NodeList: nodeList, RoleList: roleList, AcctList: acctList}, nil
  137. }
  138. return types.OrgDetailInfo{NodeList: nodeList, RoleList: roleList, AcctList: acctList, SubOrgList: orgRec.SubOrgList}, nil
  139. }
  140. func (q *QuorumControlsAPI) initOp(txa ethapi.SendTxArgs) (*pbind.PermInterfaceSession, error) {
  141. var err error
  142. var w accounts.Wallet
  143. w, err = q.validateAccount(txa.From)
  144. if err != nil {
  145. return nil, types.ErrInvalidAccount
  146. }
  147. pinterf := q.newPermInterfaceSession(w, txa)
  148. return pinterf, nil
  149. }
  150. func reportExecError(action PermAction, err error) (string, error) {
  151. log.Error("Failed to execute permission action", "action", action, "err", err)
  152. msg := fmt.Sprintf("failed to execute permissions action: %v", err)
  153. return "", errors.New(msg)
  154. }
  155. func (q *QuorumControlsAPI) AddOrg(orgId string, url string, acct common.Address, txa ethapi.SendTxArgs) (string, error) {
  156. pinterf, err := q.initOp(txa)
  157. if err != nil {
  158. return "", err
  159. }
  160. args := txArgs{orgId: orgId, url: url, acctId: acct, txa: txa}
  161. if err := q.valAddOrg(args, pinterf); err != nil {
  162. return "", err
  163. }
  164. tx, err := pinterf.AddOrg(args.orgId, args.url, args.acctId)
  165. if err != nil {
  166. return reportExecError(AddOrg, err)
  167. }
  168. log.Debug("executed permission action", "action", AddOrg, "tx", tx)
  169. return actionSuccess, nil
  170. }
  171. func (q *QuorumControlsAPI) AddSubOrg(porgId, orgId string, url string, txa ethapi.SendTxArgs) (string, error) {
  172. pinterf, err := q.initOp(txa)
  173. if err != nil {
  174. return "", err
  175. }
  176. args := txArgs{porgId: porgId, orgId: orgId, url: url, txa: txa}
  177. if err := q.valAddSubOrg(args, pinterf); err != nil {
  178. return "", err
  179. }
  180. tx, err := pinterf.AddSubOrg(args.porgId, args.orgId, args.url)
  181. if err != nil {
  182. return reportExecError(AddSubOrg, err)
  183. }
  184. log.Debug("executed permission action", "action", AddSubOrg, "tx", tx)
  185. return actionSuccess, nil
  186. }
  187. func (q *QuorumControlsAPI) ApproveOrg(orgId string, url string, acct common.Address, txa ethapi.SendTxArgs) (string, error) {
  188. pinterf, err := q.initOp(txa)
  189. if err != nil {
  190. return "", err
  191. }
  192. args := txArgs{orgId: orgId, url: url, acctId: acct, txa: txa}
  193. if err := q.valApproveOrg(args, pinterf); err != nil {
  194. return "", err
  195. }
  196. tx, err := pinterf.ApproveOrg(args.orgId, args.url, args.acctId)
  197. if err != nil {
  198. return reportExecError(ApproveOrg, err)
  199. }
  200. log.Debug("executed permission action", "action", ApproveOrg, "tx", tx)
  201. return actionSuccess, nil
  202. }
  203. func (q *QuorumControlsAPI) UpdateOrgStatus(orgId string, status uint8, txa ethapi.SendTxArgs) (string, error) {
  204. pinterf, err := q.initOp(txa)
  205. if err != nil {
  206. return "", err
  207. }
  208. args := txArgs{orgId: orgId, action: status, txa: txa}
  209. if err := q.valUpdateOrgStatus(args, pinterf); err != nil {
  210. return "", err
  211. }
  212. // and in suspended state for suspension revoke
  213. tx, err := pinterf.UpdateOrgStatus(args.orgId, big.NewInt(int64(args.action)))
  214. if err != nil {
  215. return reportExecError(UpdateOrgStatus, err)
  216. }
  217. log.Debug("executed permission action", "action", UpdateOrgStatus, "tx", tx)
  218. return actionSuccess, nil
  219. }
  220. func (q *QuorumControlsAPI) AddNode(orgId string, url string, txa ethapi.SendTxArgs) (string, error) {
  221. pinterf, err := q.initOp(txa)
  222. if err != nil {
  223. return "", err
  224. }
  225. args := txArgs{orgId: orgId, url: url, txa: txa}
  226. if err := q.valAddNode(args, pinterf); err != nil {
  227. return "", err
  228. }
  229. // check if node is already there
  230. tx, err := pinterf.AddNode(args.orgId, args.url)
  231. if err != nil {
  232. return reportExecError(AddNode, err)
  233. }
  234. log.Debug("executed permission action", "action", AddNode, "tx", tx)
  235. return actionSuccess, nil
  236. }
  237. func (q *QuorumControlsAPI) UpdateNodeStatus(orgId string, url string, action uint8, txa ethapi.SendTxArgs) (string, error) {
  238. pinterf, err := q.initOp(txa)
  239. if err != nil {
  240. return "", err
  241. }
  242. args := txArgs{orgId: orgId, url: url, action: action, txa: txa}
  243. if err := q.valUpdateNodeStatus(args, UpdateNodeStatus, pinterf); err != nil {
  244. return "", err
  245. }
  246. // check node status for operation
  247. tx, err := pinterf.UpdateNodeStatus(args.orgId, args.url, big.NewInt(int64(args.action)))
  248. if err != nil {
  249. return reportExecError(UpdateNodeStatus, err)
  250. }
  251. log.Debug("executed permission action", "action", UpdateNodeStatus, "tx", tx)
  252. return actionSuccess, nil
  253. }
  254. func (q *QuorumControlsAPI) ApproveOrgStatus(orgId string, status uint8, txa ethapi.SendTxArgs) (string, error) {
  255. pinterf, err := q.initOp(txa)
  256. if err != nil {
  257. return "", err
  258. }
  259. args := txArgs{orgId: orgId, action: status, txa: txa}
  260. if err := q.valApproveOrgStatus(args, pinterf); err != nil {
  261. return "", err
  262. }
  263. // validate that status change is pending approval
  264. tx, err := pinterf.ApproveOrgStatus(args.orgId, big.NewInt(int64(args.action)))
  265. if err != nil {
  266. return reportExecError(ApproveOrgStatus, err)
  267. }
  268. log.Debug("executed permission action", "action", ApproveOrgStatus, "tx", tx)
  269. return actionSuccess, nil
  270. }
  271. func (q *QuorumControlsAPI) AssignAdminRole(orgId string, acct common.Address, roleId string, txa ethapi.SendTxArgs) (string, error) {
  272. pinterf, err := q.initOp(txa)
  273. if err != nil {
  274. return "", err
  275. }
  276. args := txArgs{orgId: orgId, acctId: acct, roleId: roleId, txa: txa}
  277. if err := q.valAssignAdminRole(args, pinterf); err != nil {
  278. return "", err
  279. }
  280. // check if account is already in use in another org
  281. tx, err := pinterf.AssignAdminRole(args.orgId, args.acctId, args.roleId)
  282. if err != nil {
  283. return reportExecError(AssignAdminRole, err)
  284. }
  285. log.Debug("executed permission action", "action", AssignAdminRole, "tx", tx)
  286. return actionSuccess, nil
  287. }
  288. func (q *QuorumControlsAPI) ApproveAdminRole(orgId string, acct common.Address, txa ethapi.SendTxArgs) (string, error) {
  289. pinterf, err := q.initOp(txa)
  290. if err != nil {
  291. return "", err
  292. }
  293. args := txArgs{orgId: orgId, acctId: acct, txa: txa}
  294. if err := q.valApproveAdminRole(args, pinterf); err != nil {
  295. return "", err
  296. }
  297. // check if anything is pending approval
  298. tx, err := pinterf.ApproveAdminRole(args.orgId, args.acctId)
  299. if err != nil {
  300. return reportExecError(ApproveAdminRole, err)
  301. }
  302. log.Debug("executed permission action", "action", ApproveAdminRole, "tx", tx)
  303. return actionSuccess, nil
  304. }
  305. func (q *QuorumControlsAPI) AddNewRole(orgId string, roleId string, access uint8, isVoter bool, isAdmin bool, txa ethapi.SendTxArgs) (string, error) {
  306. pinterf, err := q.initOp(txa)
  307. if err != nil {
  308. return "", err
  309. }
  310. args := txArgs{orgId: orgId, roleId: roleId, accessType: access, isVoter: isVoter, isAdmin: isAdmin, txa: txa}
  311. if err := q.valAddNewRole(args, pinterf); err != nil {
  312. return "", err
  313. }
  314. // check if role is already there in the org
  315. tx, err := pinterf.AddNewRole(args.roleId, args.orgId, big.NewInt(int64(args.accessType)), args.isVoter, args.isAdmin)
  316. if err != nil {
  317. return reportExecError(AddNewRole, err)
  318. }
  319. log.Debug("executed permission action", "action", AddNewRole, "tx", tx)
  320. return actionSuccess, nil
  321. }
  322. func (q *QuorumControlsAPI) RemoveRole(orgId string, roleId string, txa ethapi.SendTxArgs) (string, error) {
  323. pinterf, err := q.initOp(txa)
  324. if err != nil {
  325. return "", err
  326. }
  327. args := txArgs{orgId: orgId, roleId: roleId, txa: txa}
  328. if err := q.valRemoveRole(args, pinterf); err != nil {
  329. return "", err
  330. }
  331. tx, err := pinterf.RemoveRole(args.roleId, args.orgId)
  332. if err != nil {
  333. return reportExecError(RemoveRole, err)
  334. }
  335. log.Debug("executed permission action", "action", RemoveRole, "tx", tx)
  336. return actionSuccess, nil
  337. }
  338. func (q *QuorumControlsAPI) AddAccountToOrg(acct common.Address, orgId string, roleId string, txa ethapi.SendTxArgs) (string, error) {
  339. pinterf, err := q.initOp(txa)
  340. if err != nil {
  341. return "", err
  342. }
  343. args := txArgs{orgId: orgId, roleId: roleId, acctId: acct, txa: txa}
  344. if err := q.valAssignRole(args, pinterf); err != nil {
  345. return "", err
  346. }
  347. tx, err := pinterf.AssignAccountRole(args.acctId, args.orgId, args.roleId)
  348. if err != nil {
  349. return reportExecError(AddAccountToOrg, err)
  350. }
  351. log.Debug("executed permission action", "action", AddAccountToOrg, "tx", tx)
  352. return actionSuccess, nil
  353. }
  354. func (q *QuorumControlsAPI) ChangeAccountRole(acct common.Address, orgId string, roleId string, txa ethapi.SendTxArgs) (string, error) {
  355. pinterf, err := q.initOp(txa)
  356. if err != nil {
  357. return "", err
  358. }
  359. args := txArgs{orgId: orgId, roleId: roleId, acctId: acct, txa: txa}
  360. if err := q.valAssignRole(args, pinterf); err != nil {
  361. return "", err
  362. }
  363. tx, err := pinterf.AssignAccountRole(args.acctId, args.orgId, args.roleId)
  364. if err != nil {
  365. return reportExecError(ChangeAccountRole, err)
  366. }
  367. log.Debug("executed permission action", "action", ChangeAccountRole, "tx", tx)
  368. return actionSuccess, nil
  369. }
  370. func (q *QuorumControlsAPI) UpdateAccountStatus(orgId string, acct common.Address, status uint8, txa ethapi.SendTxArgs) (string, error) {
  371. pinterf, err := q.initOp(txa)
  372. if err != nil {
  373. return "", err
  374. }
  375. args := txArgs{orgId: orgId, acctId: acct, action: status, txa: txa}
  376. if err := q.valUpdateAccountStatus(args, UpdateAccountStatus, pinterf); err != nil {
  377. return "", err
  378. }
  379. tx, err := pinterf.UpdateAccountStatus(args.orgId, args.acctId, big.NewInt(int64(args.action)))
  380. if err != nil {
  381. return reportExecError(UpdateAccountStatus, err)
  382. }
  383. log.Debug("executed permission action", "action", UpdateAccountStatus, "tx", tx)
  384. return actionSuccess, nil
  385. }
  386. func (q *QuorumControlsAPI) RecoverBlackListedNode(orgId string, enodeId string, txa ethapi.SendTxArgs) (string, error) {
  387. pinterf, err := q.initOp(txa)
  388. if err != nil {
  389. return "", err
  390. }
  391. args := txArgs{orgId: orgId, url: enodeId, txa: txa}
  392. if err := q.valRecoverNode(args, pinterf, InitiateNodeRecovery); err != nil {
  393. return "", err
  394. }
  395. tx, err := pinterf.StartBlacklistedNodeRecovery(args.orgId, args.url)
  396. if err != nil {
  397. return reportExecError(InitiateNodeRecovery, err)
  398. }
  399. log.Debug("executed permission action", "action", InitiateNodeRecovery, "tx", tx)
  400. return actionSuccess, nil
  401. }
  402. func (q *QuorumControlsAPI) ApproveBlackListedNodeRecovery(orgId string, enodeId string, txa ethapi.SendTxArgs) (string, error) {
  403. pinterf, err := q.initOp(txa)
  404. if err != nil {
  405. return "", err
  406. }
  407. args := txArgs{orgId: orgId, url: enodeId, txa: txa}
  408. if err := q.valRecoverNode(args, pinterf, ApproveNodeRecovery); err != nil {
  409. return "", err
  410. }
  411. tx, err := pinterf.ApproveBlacklistedNodeRecovery(args.orgId, args.url)
  412. if err != nil {
  413. return reportExecError(ApproveNodeRecovery, err)
  414. }
  415. log.Debug("executed permission action", "action", ApproveNodeRecovery, "tx", tx)
  416. return actionSuccess, nil
  417. }
  418. func (q *QuorumControlsAPI) RecoverBlackListedAccount(orgId string, acctId common.Address, txa ethapi.SendTxArgs) (string, error) {
  419. pinterf, err := q.initOp(txa)
  420. if err != nil {
  421. return "", err
  422. }
  423. args := txArgs{orgId: orgId, acctId: acctId, txa: txa}
  424. if err := q.valRecoverAccount(args, pinterf, InitiateAccountRecovery); err != nil {
  425. return "", err
  426. }
  427. tx, err := pinterf.StartBlacklistedAccountRecovery(args.orgId, args.acctId)
  428. if err != nil {
  429. return reportExecError(InitiateAccountRecovery, err)
  430. }
  431. log.Debug("executed permission action", "action", InitiateAccountRecovery, "tx", tx)
  432. return actionSuccess, nil
  433. }
  434. func (q *QuorumControlsAPI) ApproveBlackListedAccountRecovery(orgId string, acctId common.Address, txa ethapi.SendTxArgs) (string, error) {
  435. pinterf, err := q.initOp(txa)
  436. if err != nil {
  437. return "", err
  438. }
  439. args := txArgs{orgId: orgId, acctId: acctId, txa: txa}
  440. if err := q.valRecoverAccount(args, pinterf, ApproveAccountRecovery); err != nil {
  441. return "", err
  442. }
  443. tx, err := pinterf.ApproveBlacklistedAccountRecovery(args.orgId, args.acctId)
  444. if err != nil {
  445. return reportExecError(ApproveAccountRecovery, err)
  446. }
  447. log.Debug("executed permission action", "action", ApproveAccountRecovery, "tx", tx)
  448. return actionSuccess, nil
  449. }
  450. // check if the account is network admin
  451. func (q *QuorumControlsAPI) isNetworkAdmin(account common.Address) bool {
  452. ac, _ := types.AcctInfoMap.GetAccount(account)
  453. return ac != nil && ac.RoleId == q.permCtrl.permConfig.NwAdminRole
  454. }
  455. func (q *QuorumControlsAPI) isOrgAdmin(account common.Address, orgId string) error {
  456. org, err := types.OrgInfoMap.GetOrg(orgId)
  457. if err != nil {
  458. return err
  459. }
  460. if org == nil {
  461. return types.ErrOrgDoesNotExists
  462. }
  463. ac, _ := types.AcctInfoMap.GetAccount(account)
  464. if ac == nil {
  465. return types.ErrNotOrgAdmin
  466. }
  467. // check if the account is network admin
  468. if !(ac.IsOrgAdmin && (ac.OrgId == orgId || ac.OrgId == org.UltimateParent)) {
  469. return types.ErrNotOrgAdmin
  470. }
  471. return nil
  472. }
  473. func (q *QuorumControlsAPI) validateOrg(orgId, pOrgId string) error {
  474. // validate Parent org id
  475. if pOrgId != "" {
  476. if _, err := types.OrgInfoMap.GetOrg(pOrgId); err != nil {
  477. return types.ErrInvalidParentOrg
  478. }
  479. locOrgId := pOrgId + "." + orgId
  480. if lorgRec, _ := types.OrgInfoMap.GetOrg(locOrgId); lorgRec != nil {
  481. return types.ErrOrgExists
  482. }
  483. } else if orgRec, _ := types.OrgInfoMap.GetOrg(orgId); orgRec != nil {
  484. return types.ErrOrgExists
  485. }
  486. return nil
  487. }
  488. func (q *QuorumControlsAPI) validatePendingOp(authOrg, orgId, url string, account common.Address, pendingOp int64, pinterf *pbind.PermInterfaceSession) bool {
  489. pOrg, pUrl, pAcct, op, err := pinterf.GetPendingOp(authOrg)
  490. return err == nil && (op.Int64() == pendingOp && pOrg == orgId && pUrl == url && pAcct == account)
  491. }
  492. func (q *QuorumControlsAPI) checkPendingOp(orgId string, pinterf *pbind.PermInterfaceSession) bool {
  493. _, _, _, op, err := pinterf.GetPendingOp(orgId)
  494. return err == nil && op.Int64() != 0
  495. }
  496. func (q *QuorumControlsAPI) checkOrgStatus(orgId string, op uint8) error {
  497. org, _ := types.OrgInfoMap.GetOrg(orgId)
  498. if org == nil {
  499. return types.ErrOrgDoesNotExists
  500. }
  501. // check if its a master org. operation is allowed only if its a master org
  502. if org.Level.Cmp(big.NewInt(1)) != 0 {
  503. return types.ErrNotMasterOrg
  504. }
  505. if !((op == 1 && org.Status == types.OrgApproved) || (op == 2 && org.Status == types.OrgSuspended)) {
  506. return types.ErrOpNotAllowed
  507. }
  508. return nil
  509. }
  510. func (q *QuorumControlsAPI) valNodeStatusChange(orgId, url string, op NodeUpdateAction, permAction PermAction) error {
  511. // validates if the enode is linked the passed organization
  512. // validate node id and
  513. if len(url) == 0 {
  514. return types.ErrInvalidNode
  515. }
  516. if err := q.valNodeDetails(url); err != nil && err.Error() != types.ErrNodePresent.Error() {
  517. return err
  518. }
  519. node, err := types.NodeInfoMap.GetNodeByUrl(url)
  520. if err != nil {
  521. return err
  522. }
  523. if node.OrgId != orgId {
  524. return types.ErrNodeOrgMismatch
  525. }
  526. if node.Status == types.NodeBlackListed && op != RecoverBlacklistedNode {
  527. return types.ErrBlacklistedNode
  528. }
  529. // validate the op and node status and check if the op can be performed
  530. if (permAction == UpdateNodeStatus && (op != SuspendNode && op != ActivateSuspendedNode && op != BlacklistNode)) ||
  531. (permAction == InitiateNodeRecovery && op != RecoverBlacklistedNode) ||
  532. (permAction == ApproveNodeRecovery && op != ApproveBlacklistedNodeRecovery) {
  533. return types.ErrOpNotAllowed
  534. }
  535. if (op == SuspendNode && node.Status != types.NodeApproved) ||
  536. (op == ActivateSuspendedNode && node.Status != types.NodeDeactivated) ||
  537. (op == BlacklistNode && node.Status == types.NodeRecoveryInitiated) ||
  538. (op == RecoverBlacklistedNode && node.Status != types.NodeBlackListed) ||
  539. (op == ApproveBlacklistedNodeRecovery && node.Status != types.NodeRecoveryInitiated) {
  540. return types.ErrOpNotAllowed
  541. }
  542. return nil
  543. }
  544. func (q *QuorumControlsAPI) validateRole(orgId, roleId string) bool {
  545. var r *types.RoleInfo
  546. r, err := types.RoleInfoMap.GetRole(orgId, roleId)
  547. if err != nil {
  548. return false
  549. }
  550. orgRec, err := types.OrgInfoMap.GetOrg(orgId)
  551. if err != nil {
  552. return false
  553. }
  554. r, err = types.RoleInfoMap.GetRole(orgRec.UltimateParent, roleId)
  555. if err != nil {
  556. return false
  557. }
  558. return r != nil && r.Active
  559. }
  560. func (q *QuorumControlsAPI) valAccountStatusChange(orgId string, account common.Address, permAction PermAction, op AccountUpdateAction) error {
  561. // validates if the enode is linked the passed organization
  562. ac, err := types.AcctInfoMap.GetAccount(account)
  563. if err != nil {
  564. return err
  565. }
  566. if ac.IsOrgAdmin && (ac.RoleId == q.permCtrl.permConfig.NwAdminRole || ac.RoleId == q.permCtrl.permConfig.OrgAdminRole) && (op == 1 || op == 3) {
  567. return types.ErrOpNotAllowed
  568. }
  569. if ac.OrgId != orgId {
  570. return types.ErrOrgNotOwner
  571. }
  572. if (permAction == UpdateAccountStatus && (op != SuspendAccount && op != ActivateSuspendedAccount && op != BlacklistAccount)) ||
  573. (permAction == InitiateAccountRecovery && op != RecoverBlacklistedAccount) ||
  574. (permAction == ApproveAccountRecovery && op != ApproveBlacklistedAccountRecovery) {
  575. return types.ErrOpNotAllowed
  576. }
  577. if ac.Status == types.AcctBlacklisted && op != RecoverBlacklistedAccount {
  578. return types.ErrBlacklistedAccount
  579. }
  580. if (op == SuspendAccount && ac.Status != types.AcctActive) ||
  581. (op == ActivateSuspendedAccount && ac.Status != types.AcctSuspended) ||
  582. (op == BlacklistAccount && ac.Status == types.AcctRecoveryInitiated) ||
  583. (op == RecoverBlacklistedAccount && ac.Status != types.AcctBlacklisted) ||
  584. (op == ApproveBlacklistedAccountRecovery && ac.Status != types.AcctRecoveryInitiated) {
  585. return types.ErrOpNotAllowed
  586. }
  587. return nil
  588. }
  589. func (q *QuorumControlsAPI) checkOrgAdminExists(orgId, roleId string, account common.Address) error {
  590. if ac, _ := types.AcctInfoMap.GetAccount(account); ac != nil {
  591. if ac.OrgId != orgId {
  592. return types.ErrAccountInUse
  593. }
  594. if roleId != "" && roleId == q.permCtrl.permConfig.OrgAdminRole && ac.IsOrgAdmin {
  595. return types.ErrAccountOrgAdmin
  596. }
  597. }
  598. return nil
  599. }
  600. func (q *QuorumControlsAPI) valSubOrgBreadthDepth(porgId string) error {
  601. org, err := types.OrgInfoMap.GetOrg(porgId)
  602. if err != nil {
  603. return types.ErrOpNotAllowed
  604. }
  605. if q.permCtrl.permConfig.SubOrgDepth.Cmp(org.Level) == 0 {
  606. return types.ErrMaxDepth
  607. }
  608. if q.permCtrl.permConfig.SubOrgBreadth.Cmp(big.NewInt(int64(len(org.SubOrgList)))) == 0 {
  609. return types.ErrMaxBreadth
  610. }
  611. return nil
  612. }
  613. func (q *QuorumControlsAPI) checkNodeExists(url, enodeId string) bool {
  614. node, _ := types.NodeInfoMap.GetNodeByUrl(url)
  615. if node != nil {
  616. return true
  617. }
  618. // check if the same nodeid is in use with different port numbers
  619. nodeList := types.NodeInfoMap.GetNodeList()
  620. for _, n := range nodeList {
  621. if enodeDet, er := enode.ParseV4(n.Url); er == nil {
  622. if enodeDet.EnodeID() == enodeId {
  623. return true
  624. }
  625. }
  626. }
  627. return false
  628. }
  629. func (q *QuorumControlsAPI) valNodeDetails(url string) error {
  630. // validate node id and
  631. if len(url) != 0 {
  632. enodeDet, err := enode.ParseV4(url)
  633. if err != nil {
  634. return types.ErrInvalidNode
  635. }
  636. // check if node already there
  637. if q.checkNodeExists(url, enodeDet.EnodeID()) {
  638. return types.ErrNodePresent
  639. }
  640. }
  641. return nil
  642. }
  643. // all validations for add org operation
  644. func (q *QuorumControlsAPI) valAddOrg(args txArgs, pinterf *pbind.PermInterfaceSession) error {
  645. // check if the org id contains "."
  646. if args.orgId == "" || args.url == "" || args.acctId == (common.Address{0}) {
  647. return types.ErrInvalidInput
  648. }
  649. if !isStringAlphaNumeric(args.orgId) {
  650. return types.ErrInvalidOrgName
  651. }
  652. // check if caller is network admin
  653. if !q.isNetworkAdmin(args.txa.From) {
  654. return types.ErrNotNetworkAdmin
  655. }
  656. // check if any previous op is pending approval for network admin
  657. if q.checkPendingOp(q.permCtrl.permConfig.NwAdminOrg, pinterf) {
  658. return types.ErrPendingApprovals
  659. }
  660. // check if org already exists
  661. if er := q.validateOrg(args.orgId, ""); er != nil {
  662. return er
  663. }
  664. // validate node id and
  665. if er := q.valNodeDetails(args.url); er != nil {
  666. return er
  667. }
  668. // check if account is already part of another org
  669. if er := q.checkOrgAdminExists(args.orgId, "", args.acctId); er != nil {
  670. return er
  671. }
  672. return nil
  673. }
  674. func (q *QuorumControlsAPI) valApproveOrg(args txArgs, pinterf *pbind.PermInterfaceSession) error {
  675. // check caller is network admin
  676. if !q.isNetworkAdmin(args.txa.From) {
  677. return types.ErrNotNetworkAdmin
  678. }
  679. // check if anything pending approval
  680. if !q.validatePendingOp(q.permCtrl.permConfig.NwAdminOrg, args.orgId, args.url, args.acctId, 1, pinterf) {
  681. return types.ErrNothingToApprove
  682. }
  683. return nil
  684. }
  685. func (q *QuorumControlsAPI) valAddSubOrg(args txArgs, pinterf *pbind.PermInterfaceSession) error {
  686. // check if the org id contains "."
  687. if args.orgId == "" {
  688. return types.ErrInvalidInput
  689. }
  690. if !isStringAlphaNumeric(args.orgId) {
  691. return types.ErrInvalidOrgName
  692. }
  693. // check if caller is network admin
  694. if er := q.isOrgAdmin(args.txa.From, args.porgId); er != nil {
  695. return er
  696. }
  697. // check if org already exists
  698. if er := q.validateOrg(args.orgId, args.porgId); er != nil {
  699. return er
  700. }
  701. if er := q.valSubOrgBreadthDepth(args.porgId); er != nil {
  702. return er
  703. }
  704. if er := q.valNodeDetails(args.url); er != nil {
  705. return er
  706. }
  707. return nil
  708. }
  709. func (q *QuorumControlsAPI) valUpdateOrgStatus(args txArgs, pinterf *pbind.PermInterfaceSession) error {
  710. // check if called is network admin
  711. if !q.isNetworkAdmin(args.txa.From) {
  712. return types.ErrNotNetworkAdmin
  713. }
  714. if OrgUpdateAction(args.action) != SuspendOrg &&
  715. OrgUpdateAction(args.action) != ActivateSuspendedOrg {
  716. return types.ErrOpNotAllowed
  717. }
  718. //check if passed org id is network admin org. update should not be allowed
  719. if args.orgId == q.permCtrl.permConfig.NwAdminOrg {
  720. return types.ErrOpNotAllowed
  721. }
  722. // check if status update can be performed. Org should be approved for suspension
  723. if er := q.checkOrgStatus(args.orgId, args.action); er != nil {
  724. return er
  725. }
  726. return nil
  727. }
  728. func (q *QuorumControlsAPI) valApproveOrgStatus(args txArgs, pinterf *pbind.PermInterfaceSession) error {
  729. // check if called is network admin
  730. if !q.isNetworkAdmin(args.txa.From) {
  731. return types.ErrNotNetworkAdmin
  732. }
  733. // check if anything is pending approval
  734. var pendingOp int64
  735. if args.action == 1 {
  736. pendingOp = 2
  737. } else if args.action == 2 {
  738. pendingOp = 3
  739. } else {
  740. return types.ErrOpNotAllowed
  741. }
  742. if !q.validatePendingOp(q.permCtrl.permConfig.NwAdminOrg, args.orgId, "", common.Address{}, pendingOp, pinterf) {
  743. return types.ErrNothingToApprove
  744. }
  745. return nil
  746. }
  747. func (q *QuorumControlsAPI) valAddNode(args txArgs, pinterf *pbind.PermInterfaceSession) error {
  748. if args.url == "" {
  749. return types.ErrInvalidInput
  750. }
  751. // check if caller is network admin
  752. if er := q.isOrgAdmin(args.txa.From, args.orgId); er != nil {
  753. return er
  754. }
  755. if er := q.valNodeDetails(args.url); er != nil {
  756. return er
  757. }
  758. return nil
  759. }
  760. func (q *QuorumControlsAPI) valUpdateNodeStatus(args txArgs, permAction PermAction, pinterf *pbind.PermInterfaceSession) error {
  761. // check if org admin
  762. // check if caller is network admin
  763. if er := q.isOrgAdmin(args.txa.From, args.orgId); er != nil {
  764. return er
  765. }
  766. // validation status change is with in allowed set
  767. if er := q.valNodeStatusChange(args.orgId, args.url, NodeUpdateAction(args.action), permAction); er != nil {
  768. return er
  769. }
  770. return nil
  771. }
  772. func (q *QuorumControlsAPI) valAssignAdminRole(args txArgs, pinterf *pbind.PermInterfaceSession) error {
  773. if args.acctId == (common.Address{0}) {
  774. return types.ErrInvalidInput
  775. }
  776. // check if caller is network admin
  777. if args.roleId != q.permCtrl.permConfig.OrgAdminRole && args.roleId != q.permCtrl.permConfig.NwAdminRole {
  778. return types.ErrOpNotAllowed
  779. }
  780. if !q.isNetworkAdmin(args.txa.From) {
  781. return types.ErrNotNetworkAdmin
  782. }
  783. if err := q.validateOrg(args.orgId, ""); err == nil {
  784. return types.ErrOrgDoesNotExists
  785. }
  786. // check if account is already part of another org
  787. if er := q.checkOrgAdminExists(args.orgId, args.roleId, args.acctId); er != nil && er.Error() != types.ErrOrgAdminExists.Error() {
  788. return er
  789. }
  790. return nil
  791. }
  792. func (q *QuorumControlsAPI) valApproveAdminRole(args txArgs, pinterf *pbind.PermInterfaceSession) error {
  793. // check if caller is network admin
  794. if !q.isNetworkAdmin(args.txa.From) {
  795. return types.ErrNotNetworkAdmin
  796. }
  797. // check if the org exists
  798. // check if account is valid
  799. ac, _ := types.AcctInfoMap.GetAccount(args.acctId)
  800. if ac == nil {
  801. return types.ErrInvalidAccount
  802. }
  803. // validate pending op
  804. if !q.validatePendingOp(q.permCtrl.permConfig.NwAdminOrg, ac.OrgId, "", args.acctId, 4, pinterf) {
  805. return types.ErrNothingToApprove
  806. }
  807. return nil
  808. }
  809. func (q *QuorumControlsAPI) valAddNewRole(args txArgs, pinterf *pbind.PermInterfaceSession) error {
  810. if args.roleId == "" {
  811. return types.ErrInvalidInput
  812. }
  813. // check if caller is network admin
  814. if er := q.isOrgAdmin(args.txa.From, args.orgId); er != nil {
  815. return er
  816. }
  817. // validate if role is already present
  818. if roleRec, _ := types.RoleInfoMap.GetRole(args.orgId, args.roleId); roleRec != nil {
  819. return types.ErrRoleExists
  820. }
  821. return nil
  822. }
  823. func (q *QuorumControlsAPI) valRemoveRole(args txArgs, pinterf *pbind.PermInterfaceSession) error {
  824. // check if caller is network admin
  825. if er := q.isOrgAdmin(args.txa.From, args.orgId); er != nil {
  826. return er
  827. }
  828. // admin roles cannot be removed
  829. if args.roleId == q.permCtrl.permConfig.OrgAdminRole || args.roleId == q.permCtrl.permConfig.NwAdminRole {
  830. return types.ErrAdminRoles
  831. }
  832. // check if role is alraedy inactive
  833. r, _ := types.RoleInfoMap.GetRole(args.orgId, args.roleId)
  834. if r == nil {
  835. return types.ErrInvalidRole
  836. } else if !r.Active {
  837. return types.ErrInactiveRole
  838. }
  839. // check if the role has active accounts. if yes operations should not be allowed
  840. if len(types.AcctInfoMap.GetAcctListRole(args.orgId, args.roleId)) != 0 {
  841. return types.ErrRoleActive
  842. }
  843. return nil
  844. }
  845. func (q *QuorumControlsAPI) valAssignRole(args txArgs, pinterf *pbind.PermInterfaceSession) error {
  846. if args.acctId == (common.Address{0}) {
  847. return types.ErrInvalidInput
  848. }
  849. if args.roleId == q.permCtrl.permConfig.OrgAdminRole || args.roleId == q.permCtrl.permConfig.NwAdminRole {
  850. return types.ErrInvalidRole
  851. }
  852. // check if caller is network admin
  853. if er := q.isOrgAdmin(args.txa.From, args.orgId); er != nil {
  854. return er
  855. }
  856. // check if the role is valid
  857. if !q.validateRole(args.orgId, args.roleId) {
  858. return types.ErrInvalidRole
  859. }
  860. // check if the account is part of another org
  861. if ac, _ := types.AcctInfoMap.GetAccount(args.acctId); ac != nil {
  862. if ac != nil && ac.OrgId != args.orgId {
  863. return types.ErrAccountInUse
  864. }
  865. }
  866. return nil
  867. }
  868. func (q *QuorumControlsAPI) valUpdateAccountStatus(args txArgs, permAction PermAction, pinterf *pbind.PermInterfaceSession) error {
  869. // check if the caller is org admin
  870. if er := q.isOrgAdmin(args.txa.From, args.orgId); er != nil {
  871. return er
  872. }
  873. // validation status change is with in allowed set
  874. if er := q.valAccountStatusChange(args.orgId, args.acctId, permAction, AccountUpdateAction(args.action)); er != nil {
  875. return er
  876. }
  877. return nil
  878. }
  879. func (q *QuorumControlsAPI) valRecoverNode(args txArgs, pinterf *pbind.PermInterfaceSession, action PermAction) error {
  880. // check if the caller is org admin
  881. if !q.isNetworkAdmin(args.txa.From) {
  882. return types.ErrNotNetworkAdmin
  883. }
  884. // validate inputs - org id is valid, node is valid and in blacklisted state
  885. if err := q.validateOrg(args.orgId, ""); err.Error() != types.ErrOrgExists.Error() {
  886. return types.ErrInvalidOrgName
  887. }
  888. if action == InitiateNodeRecovery {
  889. if err := q.valNodeStatusChange(args.orgId, args.url, 4, InitiateAccountRecovery); err != nil {
  890. return err
  891. }
  892. // check no pending approval items
  893. if q.checkPendingOp(q.permCtrl.permConfig.NwAdminOrg, pinterf) {
  894. return types.ErrPendingApprovals
  895. }
  896. } else {
  897. // validate inputs - org id is valid, node is valid pending recovery state
  898. if err := q.valNodeStatusChange(args.orgId, args.url, 5, ApproveNodeRecovery); err != nil {
  899. return err
  900. }
  901. // check that there is a pending approval item for node recovery
  902. if !q.validatePendingOp(q.permCtrl.permConfig.NwAdminOrg, args.orgId, args.url, common.Address{}, 5, pinterf) {
  903. return types.ErrNothingToApprove
  904. }
  905. }
  906. // if it is approval ensure that
  907. return nil
  908. }
  909. func (q *QuorumControlsAPI) valRecoverAccount(args txArgs, pinterf *pbind.PermInterfaceSession, action PermAction) error {
  910. // check if the caller is org admin
  911. if !q.isNetworkAdmin(args.txa.From) {
  912. return types.ErrNotNetworkAdmin
  913. }
  914. var opAction AccountUpdateAction
  915. if action == InitiateAccountRecovery {
  916. opAction = RecoverBlacklistedAccount
  917. } else {
  918. opAction = ApproveBlacklistedAccountRecovery
  919. }
  920. if err := q.valAccountStatusChange(args.orgId, args.acctId, action, opAction); err != nil {
  921. return err
  922. }
  923. if action == InitiateAccountRecovery && q.checkPendingOp(q.permCtrl.permConfig.NwAdminOrg, pinterf) {
  924. return types.ErrPendingApprovals
  925. }
  926. if action == ApproveAccountRecovery && !q.validatePendingOp(q.permCtrl.permConfig.NwAdminOrg, args.orgId, "", args.acctId, 6, pinterf) {
  927. return types.ErrNothingToApprove
  928. }
  929. return nil
  930. }
  931. // validateAccount validates the account and returns the wallet associated with that for signing the transaction
  932. func (q *QuorumControlsAPI) validateAccount(from common.Address) (accounts.Wallet, error) {
  933. acct := accounts.Account{Address: from}
  934. w, err := q.permCtrl.eth.AccountManager().Find(acct)
  935. if err != nil {
  936. return nil, err
  937. }
  938. return w, nil
  939. }
  940. func (q *QuorumControlsAPI) newPermInterfaceSession(w accounts.Wallet, txa ethapi.SendTxArgs) *pbind.PermInterfaceSession {
  941. frmAcct, transactOpts, gasLimit, gasPrice := q.getTxParams(txa, w)
  942. ps := &pbind.PermInterfaceSession{
  943. Contract: q.permCtrl.permInterf,
  944. CallOpts: bind.CallOpts{
  945. Pending: true,
  946. },
  947. TransactOpts: bind.TransactOpts{
  948. From: frmAcct.Address,
  949. GasLimit: gasLimit,
  950. GasPrice: gasPrice,
  951. Signer: transactOpts.Signer,
  952. },
  953. }
  954. return ps
  955. }
  956. // getTxParams extracts the transaction related parameters
  957. func (q *QuorumControlsAPI) getTxParams(txa ethapi.SendTxArgs, w accounts.Wallet) (accounts.Account, *bind.TransactOpts, uint64, *big.Int) {
  958. fromAcct := accounts.Account{Address: txa.From}
  959. transactOpts := bind.NewWalletTransactor(w, fromAcct)
  960. gasLimit := defaultGasLimit
  961. gasPrice := defaultGasPrice
  962. if txa.GasPrice != nil {
  963. gasPrice = txa.GasPrice.ToInt()
  964. }
  965. if txa.Gas != nil {
  966. gasLimit = uint64(*txa.Gas)
  967. }
  968. return fromAcct, transactOpts, gasLimit, gasPrice
  969. }