quorum/core/types/permissions_cache.go

123 lines
2.5 KiB
Go
Raw Normal View History

package types
import (
"github.com/ethereum/go-ethereum/common"
"github.com/hashicorp/golang-lru"
2018-11-12 00:16:56 -08:00
"sync"
)
type AccessType uint8
const (
ReadOnly AccessType = iota
Transact
ContractDeploy
2019-02-11 17:54:13 -08:00
FullAccess
)
type PermStruct struct {
AcctId common.Address
roleId string
}
2018-09-27 04:24:38 -07:00
type OrgStruct struct {
OrgId string
Keys []string
2018-09-27 04:24:38 -07:00
}
2018-11-14 02:24:28 -08:00
// permission config for bootstrapping
type PermissionConfig struct {
UpgrdAddress string
InterfAddress string
ImplAddress string
NodeAddress string
AccountAddress string
NwAdminOrg string
NwAdminRole string
OrgAdminRole string
Accounts []string //initial list of account that need full access
}
2018-11-13 00:15:28 -08:00
var DefaultAccess = FullAccess
2018-11-12 00:16:56 -08:00
const acctMapLimit = 100
const orgKeyMapLimit = 100
2018-09-27 04:24:38 -07:00
2018-11-12 00:16:56 -08:00
var AcctMap, _ = lru.New(acctMapLimit)
var OrgKeyMap, _ = lru.New(orgKeyMapLimit)
var orgKeyLock sync.Mutex
2019-03-13 23:26:17 -07:00
func (pc *PermissionConfig) IsEmpty() bool {
return pc.InterfAddress == "" || pc.NodeAddress == "" || pc.AccountAddress == ""
2019-03-13 23:26:17 -07:00
}
// sets default access to ReadOnly
2018-11-13 00:15:28 -08:00
func SetDefaultAccess() {
DefaultAccess = FullAccess
2018-11-13 00:15:28 -08:00
}
// Adds account access to the cache
func AddAccountAccess(acctId common.Address, roleId string) {
AcctMap.Add(acctId, &PermStruct{AcctId: acctId, roleId: roleId})
}
// Returns the access type for an account. If not found returns
// default access
func GetAcctAccess(acctId common.Address) AccessType {
if AcctMap.Len() != 0 {
if _, ok := AcctMap.Get(acctId); ok {
// val.(*PermStruct)
return DefaultAccess
}
}
return DefaultAccess
}
2018-09-27 04:24:38 -07:00
// Adds org key details to cache
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()
defer orgKeyLock.Unlock()
2018-10-02 01:28:55 -07:00
// Org record exists. Append the key only
vo := val.(*OrgStruct)
vo.Keys = append(vo.Keys, key)
2018-10-02 01:28:55 -07:00
return
}
}
OrgKeyMap.Add(orgId, &OrgStruct{OrgId: orgId, Keys: []string{key}})
2018-10-02 01:28:55 -07:00
}
// deletes org key details from cache
func DeleteOrgKey(orgId string, key string) {
if val, ok := OrgKeyMap.Get(orgId); ok {
2018-11-12 00:16:56 -08:00
orgKeyLock.Lock()
defer orgKeyLock.Unlock()
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
}
// Givens a orgid returns the linked keys for the org
func ResolvePrivateForKeys(orgId string) []string {
2018-09-27 04:24:38 -07:00
var keys []string
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
}
return keys
2018-09-27 04:24:38 -07:00
}
keys = append(keys, orgId)
return keys
}