2018-08-06 01:18:54 -07:00
|
|
|
package types
|
|
|
|
|
2018-11-07 04:36:42 -08:00
|
|
|
import (
|
2018-08-06 01:18:54 -07:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2019-03-27 04:07:01 -07:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2018-11-07 04:36:42 -08:00
|
|
|
"github.com/hashicorp/golang-lru"
|
2018-11-12 00:16:56 -08:00
|
|
|
"sync"
|
2018-08-06 01:18:54 -07:00
|
|
|
)
|
|
|
|
|
2018-08-12 19:00:35 -07:00
|
|
|
type AccessType uint8
|
|
|
|
|
|
|
|
const (
|
2019-01-28 22:06:24 -08:00
|
|
|
ReadOnly AccessType = iota
|
2018-08-12 19:00:35 -07:00
|
|
|
Transact
|
|
|
|
ContractDeploy
|
2019-02-11 17:54:13 -08:00
|
|
|
FullAccess
|
2018-08-12 19:00:35 -07:00
|
|
|
)
|
|
|
|
|
2019-03-27 04:07:01 -07:00
|
|
|
type OrgStatus uint8
|
|
|
|
|
|
|
|
const (
|
|
|
|
Proposed OrgStatus = 1
|
|
|
|
Approved
|
|
|
|
PendingSuspension
|
|
|
|
Suspended
|
|
|
|
RevokeSuspension
|
|
|
|
)
|
|
|
|
|
|
|
|
type OrgInfo struct {
|
|
|
|
OrgId string
|
|
|
|
Status int
|
|
|
|
}
|
|
|
|
|
|
|
|
type NodeStatus uint8
|
|
|
|
|
|
|
|
const (
|
|
|
|
PendingApproval NodeStatus = 1
|
|
|
|
NodeApproved
|
|
|
|
PendingDeactivation
|
|
|
|
Deactivated
|
|
|
|
PendingActivation
|
|
|
|
PendingBlacklisting
|
|
|
|
Blacklisted
|
|
|
|
)
|
|
|
|
|
|
|
|
type NodeInfo struct {
|
|
|
|
OrgId string
|
|
|
|
Url string
|
|
|
|
Status int
|
|
|
|
}
|
|
|
|
|
|
|
|
type RoleInfo struct {
|
|
|
|
OrgId string
|
|
|
|
RoleId string
|
|
|
|
IsVoter bool
|
|
|
|
Access int
|
|
|
|
Active bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type AccountInfo struct {
|
|
|
|
OrgId string
|
|
|
|
RoleId string
|
|
|
|
AcctId common.Address
|
|
|
|
IsOrgAdmin bool
|
|
|
|
Status int
|
|
|
|
}
|
|
|
|
|
2018-08-06 01:18:54 -07:00
|
|
|
type PermStruct struct {
|
2019-03-22 04:34:15 -07:00
|
|
|
AcctId common.Address
|
|
|
|
roleId string
|
2018-08-06 01:18:54 -07:00
|
|
|
}
|
2019-03-27 04:07:01 -07:00
|
|
|
|
2018-09-27 04:24:38 -07:00
|
|
|
type OrgStruct struct {
|
|
|
|
OrgId string
|
2018-11-07 04:36:42 -08:00
|
|
|
Keys []string
|
2018-09-27 04:24:38 -07:00
|
|
|
}
|
2018-11-14 02:24:28 -08:00
|
|
|
|
2019-03-11 20:18:20 -07:00
|
|
|
// permission config for bootstrapping
|
|
|
|
type PermissionConfig struct {
|
2019-03-22 04:34:15 -07:00
|
|
|
UpgrdAddress string
|
|
|
|
InterfAddress string
|
|
|
|
ImplAddress string
|
|
|
|
NodeAddress string
|
|
|
|
AccountAddress string
|
2019-03-26 08:20:06 -07:00
|
|
|
RoleAddress string
|
|
|
|
VoterAddress string
|
|
|
|
OrgAddress string
|
2019-03-22 04:34:15 -07:00
|
|
|
NwAdminOrg string
|
|
|
|
NwAdminRole string
|
|
|
|
OrgAdminRole string
|
|
|
|
|
|
|
|
Accounts []string //initial list of account that need full access
|
2019-03-11 20:18:20 -07:00
|
|
|
}
|
|
|
|
|
2019-03-27 04:07:01 -07:00
|
|
|
type OrgKey struct {
|
|
|
|
OrgId string
|
|
|
|
}
|
|
|
|
|
|
|
|
type NodeKey struct {
|
|
|
|
OrgId string
|
|
|
|
Url string
|
|
|
|
}
|
|
|
|
|
|
|
|
type RoleKey struct {
|
|
|
|
OrgId string
|
|
|
|
RoleId string
|
|
|
|
}
|
|
|
|
|
|
|
|
type AccountKey struct {
|
|
|
|
OrgId string
|
|
|
|
RoleId string
|
|
|
|
AcctId common.Address
|
|
|
|
}
|
|
|
|
|
|
|
|
type OrgCache struct {
|
|
|
|
c *lru.Cache
|
|
|
|
mux sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
type NodeCache struct {
|
|
|
|
c *lru.Cache
|
|
|
|
mux sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
type RoleCache struct {
|
|
|
|
c *lru.Cache
|
|
|
|
mux sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
type AcctCache struct {
|
|
|
|
c *lru.Cache
|
|
|
|
mux sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewOrgCache() *OrgCache {
|
|
|
|
c, _ := lru.New(defaultMapLimit)
|
|
|
|
return &OrgCache{c, sync.Mutex{}}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewNodeCache() *NodeCache {
|
|
|
|
c, _ := lru.New(defaultMapLimit)
|
|
|
|
return &NodeCache{c, sync.Mutex{}}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewRoleCache() *RoleCache {
|
|
|
|
c, _ := lru.New(defaultMapLimit)
|
|
|
|
return &RoleCache{c, sync.Mutex{}}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewAcctCache() *AcctCache {
|
|
|
|
c, _ := lru.New(defaultMapLimit)
|
|
|
|
return &AcctCache{c, sync.Mutex{}}
|
|
|
|
}
|
|
|
|
|
2018-11-13 00:15:28 -08:00
|
|
|
var DefaultAccess = FullAccess
|
2018-08-06 01:18:54 -07:00
|
|
|
|
2018-11-12 00:16:56 -08:00
|
|
|
const acctMapLimit = 100
|
|
|
|
const orgKeyMapLimit = 100
|
2018-09-27 04:24:38 -07:00
|
|
|
|
2019-03-27 04:07:01 -07:00
|
|
|
const defaultMapLimit = 100
|
2018-11-12 00:16:56 -08:00
|
|
|
|
2019-03-27 04:07:01 -07:00
|
|
|
var AcctMap, _ = lru.New(acctMapLimit)
|
2018-11-12 00:16:56 -08:00
|
|
|
var OrgKeyMap, _ = lru.New(orgKeyMapLimit)
|
|
|
|
|
2019-03-27 04:07:01 -07:00
|
|
|
var OrgInfoMap = NewOrgCache()
|
|
|
|
var NodeInfoMap = NewNodeCache()
|
|
|
|
var RoleInfoMap = NewRoleCache()
|
|
|
|
var AcctInfoMap = NewAcctCache()
|
|
|
|
|
2018-11-12 00:16:56 -08:00
|
|
|
var orgKeyLock sync.Mutex
|
2018-08-06 01:18:54 -07:00
|
|
|
|
2019-03-13 23:26:17 -07:00
|
|
|
func (pc *PermissionConfig) IsEmpty() bool {
|
2019-03-22 04:34:15 -07:00
|
|
|
return pc.InterfAddress == "" || pc.NodeAddress == "" || pc.AccountAddress == ""
|
2019-03-13 23:26:17 -07:00
|
|
|
}
|
|
|
|
|
2019-02-12 17:57:11 -08:00
|
|
|
// sets default access to ReadOnly
|
2018-11-13 00:15:28 -08:00
|
|
|
func SetDefaultAccess() {
|
2019-03-22 04:34:15 -07:00
|
|
|
DefaultAccess = FullAccess
|
2018-11-13 00:15:28 -08:00
|
|
|
}
|
2019-02-12 17:57:11 -08:00
|
|
|
|
2019-03-27 04:07:01 -07:00
|
|
|
func (o *OrgCache) UpsertOrg(orgId string, status int) {
|
|
|
|
defer o.mux.Lock()
|
|
|
|
key := OrgKey{OrgId: orgId}
|
|
|
|
if _, ok := o.c.Get(key); ok {
|
|
|
|
log.Info("AJ-OrgId already exists. update it", "orgId", orgId)
|
|
|
|
o.c.Add(key, &OrgInfo{orgId, status})
|
|
|
|
} else {
|
|
|
|
log.Info("AJ-OrgId does not exist. add it", "orgId", orgId)
|
|
|
|
o.c.Add(key, &OrgInfo{orgId, status})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *OrgCache) GetOrg(orgId string) *OrgInfo {
|
|
|
|
defer o.mux.Lock()
|
|
|
|
key := OrgKey{OrgId: orgId}
|
|
|
|
if ent, ok := o.c.Get(key); ok {
|
|
|
|
log.Info("AJ-OrgFound", "orgId", orgId)
|
|
|
|
return ent.(*OrgInfo)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *OrgCache) Show() {
|
|
|
|
for i, k := range o.c.Keys() {
|
|
|
|
v, _ := o.c.Get(k)
|
|
|
|
log.Info("AJ-Org", "i", i, "key", k, "value", v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *NodeCache) UpsertNode(orgId string, url string, status int) {
|
|
|
|
defer n.mux.Lock()
|
|
|
|
key := NodeKey{OrgId: orgId, Url: url}
|
|
|
|
if _, ok := n.c.Get(key); ok {
|
|
|
|
log.Info("AJ-Node already exists. update it", "orgId", orgId, "url", url)
|
|
|
|
n.c.Add(key, &NodeInfo{orgId, url, status})
|
|
|
|
} else {
|
|
|
|
log.Info("AJ-Node does not exist. add it", "orgId", orgId, "url", url)
|
|
|
|
n.c.Add(key, &NodeInfo{orgId, url, status})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *NodeCache) GetNodeByUrl(url string) *NodeInfo {
|
|
|
|
defer n.mux.Lock()
|
|
|
|
var key NodeKey
|
|
|
|
var found = false
|
|
|
|
for _, k := range n.c.Keys() {
|
|
|
|
ent := k.(NodeKey)
|
|
|
|
if ent.Url == url {
|
|
|
|
key = ent
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if found {
|
|
|
|
v, _ := n.c.Get(key)
|
|
|
|
ent := v.(*NodeInfo)
|
|
|
|
log.Info("AJ-NodeFound", "url", ent.Url, "orgId", ent.OrgId)
|
|
|
|
return ent
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *NodeCache) Show() {
|
|
|
|
for i, k := range o.c.Keys() {
|
|
|
|
v, _ := o.c.Get(k)
|
|
|
|
log.Info("AJ-Node", "i", i, "key", k, "value", v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AcctCache) UpsertAccount(orgId string, role string, acct common.Address, orgAdmin bool, status int) {
|
|
|
|
defer a.mux.Lock()
|
|
|
|
key := AccountKey{orgId, role, acct}
|
|
|
|
if _, ok := a.c.Get(key); ok {
|
|
|
|
log.Info("AJ-account already exists. update it", "orgId", orgId, "role", role, "acct", acct)
|
|
|
|
a.c.Add(key, &AccountInfo{orgId, role, acct, orgAdmin, status})
|
|
|
|
} else {
|
|
|
|
log.Info("AJ-account does not exist. add it", "orgId", orgId, "role", role, "acct", acct)
|
|
|
|
a.c.Add(key, &AccountInfo{orgId, role, acct, orgAdmin, status})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *AcctCache) GetAccountByAccount(acct common.Address) *AccountInfo {
|
|
|
|
defer n.mux.Lock()
|
|
|
|
var key AccountKey
|
|
|
|
var found = false
|
|
|
|
for _, k := range n.c.Keys() {
|
|
|
|
ent := k.(AccountKey)
|
|
|
|
if ent.AcctId == acct {
|
|
|
|
key = ent
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if found {
|
|
|
|
v, _ := n.c.Get(key)
|
|
|
|
ent := v.(*AccountInfo)
|
|
|
|
log.Info("AJ-AccountFound", "org", ent.OrgId, "role", ent.RoleId, "acct", ent.AcctId)
|
|
|
|
return ent
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *AcctCache) Show() {
|
|
|
|
for i, k := range o.c.Keys() {
|
|
|
|
v, _ := o.c.Get(k)
|
|
|
|
log.Info("AJ-Accounts", "i", i, "key", k, "value", v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RoleCache) UpsertRole(orgId string, role string, voter bool, access int, active bool) {
|
|
|
|
defer r.mux.Lock()
|
|
|
|
key := RoleKey{orgId, role}
|
|
|
|
if _, ok := r.c.Get(key); ok {
|
|
|
|
log.Info("AJ-role already exists. update it", "orgId", orgId, "role", role, "access", access, "voter", voter, "active", active)
|
|
|
|
r.c.Add(key, &RoleInfo{orgId, role, voter, access, active})
|
|
|
|
} else {
|
|
|
|
log.Info("AJ-role does not exist. add it", "orgId", orgId, "role", role, "access", access, "voter", voter, "active", active)
|
|
|
|
r.c.Add(key, &RoleInfo{orgId, role, voter, access, active})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RoleCache) GetRole(orgId string, roleId string) *RoleInfo {
|
|
|
|
defer r.mux.Lock()
|
|
|
|
key := RoleKey{OrgId: orgId, RoleId: roleId}
|
|
|
|
if ent, ok := r.c.Get(key); ok {
|
|
|
|
log.Info("AJ-RoleFound", "orgId", orgId, "roleId", roleId)
|
|
|
|
return ent.(*RoleInfo)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *RoleCache) Show() {
|
|
|
|
for i, k := range o.c.Keys() {
|
|
|
|
v, _ := o.c.Get(k)
|
|
|
|
log.Info("AJ-Role", "i", i, "key", k, "value", v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-12 17:57:11 -08:00
|
|
|
// Adds account access to the cache
|
2019-03-22 04:34:15 -07:00
|
|
|
func AddAccountAccess(acctId common.Address, roleId string) {
|
|
|
|
AcctMap.Add(acctId, &PermStruct{AcctId: acctId, roleId: roleId})
|
2018-08-06 01:18:54 -07:00
|
|
|
}
|
|
|
|
|
2019-02-12 17:57:11 -08:00
|
|
|
// Returns the access type for an account. If not found returns
|
|
|
|
// default access
|
2018-08-12 19:00:35 -07:00
|
|
|
func GetAcctAccess(acctId common.Address) AccessType {
|
2018-11-07 04:36:42 -08:00
|
|
|
if AcctMap.Len() != 0 {
|
2019-03-22 04:34:15 -07:00
|
|
|
if _, ok := AcctMap.Get(acctId); ok {
|
|
|
|
// val.(*PermStruct)
|
|
|
|
return DefaultAccess
|
2018-08-06 01:18:54 -07:00
|
|
|
}
|
|
|
|
}
|
2019-02-06 20:45:29 -08:00
|
|
|
return DefaultAccess
|
2018-08-06 01:18:54 -07:00
|
|
|
}
|
2018-09-27 04:24:38 -07:00
|
|
|
|
2019-02-12 17:57:11 -08:00
|
|
|
// Adds org key details to cache
|
2018-11-07 04:36:42 -08:00
|
|
|
func AddOrgKey(orgId string, key string) {
|
|
|
|
if OrgKeyMap.Len() != 0 {
|
|
|
|
if val, ok := OrgKeyMap.Get(orgId); ok {
|
2018-11-12 00:16:56 -08:00
|
|
|
orgKeyLock.Lock()
|
2019-02-12 17:57:11 -08:00
|
|
|
defer orgKeyLock.Unlock()
|
2018-10-02 01:28:55 -07:00
|
|
|
// Org record exists. Append the key only
|
2018-11-07 04:36:42 -08:00
|
|
|
vo := val.(*OrgStruct)
|
|
|
|
vo.Keys = append(vo.Keys, key)
|
2018-10-02 01:28:55 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2018-11-07 04:36:42 -08:00
|
|
|
OrgKeyMap.Add(orgId, &OrgStruct{OrgId: orgId, Keys: []string{key}})
|
2018-10-02 01:28:55 -07:00
|
|
|
}
|
|
|
|
|
2019-02-12 17:57:11 -08:00
|
|
|
// deletes org key details from cache
|
2018-11-07 04:36:42 -08:00
|
|
|
func DeleteOrgKey(orgId string, key string) {
|
|
|
|
if val, ok := OrgKeyMap.Get(orgId); ok {
|
2018-11-12 00:16:56 -08:00
|
|
|
orgKeyLock.Lock()
|
2019-02-12 17:57:11 -08:00
|
|
|
defer orgKeyLock.Unlock()
|
2018-11-07 04:36:42 -08:00
|
|
|
vo := val.(*OrgStruct)
|
|
|
|
for i, keyVal := range vo.Keys {
|
|
|
|
if keyVal == key {
|
|
|
|
vo.Keys = append(vo.Keys[:i], vo.Keys[i+1:]...)
|
|
|
|
break
|
2018-10-02 01:28:55 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-09-27 04:24:38 -07:00
|
|
|
}
|
|
|
|
|
2019-02-12 17:57:11 -08:00
|
|
|
// Givens a orgid returns the linked keys for the org
|
2018-11-07 04:36:42 -08:00
|
|
|
func ResolvePrivateForKeys(orgId string) []string {
|
2018-09-27 04:24:38 -07:00
|
|
|
var keys []string
|
2018-11-07 04:36:42 -08:00
|
|
|
if val, ok := OrgKeyMap.Get(orgId); ok {
|
|
|
|
vo := val.(*OrgStruct)
|
|
|
|
if len(vo.Keys) > 0 {
|
|
|
|
keys = vo.Keys
|
|
|
|
} else {
|
|
|
|
keys = append(keys, orgId)
|
2018-09-27 04:24:38 -07:00
|
|
|
}
|
2018-11-07 04:36:42 -08:00
|
|
|
return keys
|
2018-09-27 04:24:38 -07:00
|
|
|
}
|
|
|
|
keys = append(keys, orgId)
|
|
|
|
return keys
|
|
|
|
}
|