-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5532 from oasisprotocol/peternose/trivial/refacto…
…r-km-acl go/worker/keymanager: Refactor access control
- Loading branch information
Showing
8 changed files
with
581 additions
and
351 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
package keymanager | ||
|
||
import ( | ||
"sync" | ||
|
||
"github.com/libp2p/go-libp2p/core" | ||
|
||
"github.com/oasisprotocol/oasis-core/go/common" | ||
"github.com/oasisprotocol/oasis-core/go/common/logging" | ||
"github.com/oasisprotocol/oasis-core/go/common/node" | ||
p2p "github.com/oasisprotocol/oasis-core/go/p2p/api" | ||
"github.com/oasisprotocol/oasis-core/go/worker/keymanager/api" | ||
) | ||
|
||
// RuntimeList is a concurrent-safe collection of unique runtime IDs. | ||
type RuntimeList struct { | ||
mu sync.RWMutex | ||
|
||
runtimes map[common.Namespace]struct{} // Guarded by mutex. | ||
} | ||
|
||
// NewRuntimeList constructs an empty runtime list. | ||
func NewRuntimeList() *RuntimeList { | ||
return &RuntimeList{ | ||
runtimes: make(map[common.Namespace]struct{}), | ||
} | ||
} | ||
|
||
// Contains returns true if and only if the list contains the given runtime. | ||
// | ||
// A nil runtime list is considered empty and will always return false. | ||
func (l *RuntimeList) Contains(runtimeID common.Namespace) bool { | ||
if l == nil { | ||
return false | ||
} | ||
|
||
l.mu.RLock() | ||
defer l.mu.RUnlock() | ||
|
||
_, ok := l.runtimes[runtimeID] | ||
return ok | ||
} | ||
|
||
// Add adds the given runtime to the list. | ||
func (l *RuntimeList) Add(runtimeID common.Namespace) { | ||
l.mu.Lock() | ||
defer l.mu.Unlock() | ||
|
||
l.runtimes[runtimeID] = struct{}{} | ||
} | ||
|
||
// Delete removes the given runtime from the list. | ||
// | ||
// If the runtime list is nil or there is no such element, this function is a no-op. | ||
func (l *RuntimeList) Delete(runtimeID common.Namespace) { | ||
if l == nil { | ||
return | ||
} | ||
|
||
l.mu.Lock() | ||
defer l.mu.Unlock() | ||
|
||
delete(l.runtimes, runtimeID) | ||
} | ||
|
||
// Empty returns true if and only if the list contains no elements. | ||
func (l *RuntimeList) Empty() bool { | ||
if l == nil { | ||
return true | ||
} | ||
|
||
l.mu.RLock() | ||
defer l.mu.RUnlock() | ||
|
||
return len(l.runtimes) == 0 | ||
} | ||
|
||
// AccessList is a concurrent-safe data structure for managing access permissions. | ||
type AccessList struct { | ||
mu sync.RWMutex | ||
|
||
accessList map[core.PeerID]*RuntimeList // Guarded by mutex. | ||
accessListByRuntime map[common.Namespace][]core.PeerID // Guarded by mutex. | ||
|
||
logger *logging.Logger | ||
} | ||
|
||
// NewAccessList constructs an empty access list. | ||
func NewAccessList() *AccessList { | ||
logger := logging.GetLogger("worker/keymanager/acl") | ||
|
||
return &AccessList{ | ||
accessList: make(map[core.PeerID]*RuntimeList), | ||
accessListByRuntime: make(map[common.Namespace][]core.PeerID), | ||
logger: logger, | ||
} | ||
} | ||
|
||
// Runtimes returns the IDs of runtimes in which the given peer participates. | ||
func (l *AccessList) Runtimes(peer core.PeerID) *RuntimeList { | ||
l.mu.RLock() | ||
defer l.mu.RUnlock() | ||
|
||
return l.accessList[peer] | ||
} | ||
|
||
// Update clears the access list for the specified runtime and adds the provided peers. | ||
func (l *AccessList) Update(runtimeID common.Namespace, peers []core.PeerID) { | ||
l.mu.Lock() | ||
defer l.mu.Unlock() | ||
|
||
// Clear any old nodes from the access list. | ||
for _, peerID := range l.accessListByRuntime[runtimeID] { | ||
rts := l.accessList[peerID] | ||
rts.Delete(runtimeID) | ||
if rts.Empty() { | ||
delete(l.accessList, peerID) | ||
} | ||
} | ||
|
||
// Update the access list. | ||
for _, peer := range peers { | ||
rts, ok := l.accessList[peer] | ||
if !ok { | ||
rts = NewRuntimeList() | ||
l.accessList[peer] = rts | ||
} | ||
|
||
rts.Add(runtimeID) | ||
} | ||
|
||
// To prevent race conditions when returning runtime access lists, it is essential to always | ||
// replace the peers array and refrain from making any modifications. | ||
l.accessListByRuntime[runtimeID] = peers | ||
} | ||
|
||
// UpdateNodes converts node public keys to peer IDs and updates the access list | ||
// for the specified runtime. | ||
func (l *AccessList) UpdateNodes(runtimeID common.Namespace, nodes []*node.Node) { | ||
var peers []core.PeerID | ||
for _, node := range nodes { | ||
peer, err := p2p.PublicKeyToPeerID(node.P2P.ID) | ||
if err != nil { | ||
l.logger.Warn("invalid node P2P ID", | ||
"err", err, | ||
"node_id", node.ID, | ||
) | ||
continue | ||
} | ||
peers = append(peers, peer) | ||
} | ||
|
||
l.Update(runtimeID, peers) | ||
|
||
l.logger.Debug("new client runtime access policy in effect", | ||
"runtime_id", runtimeID, | ||
"peers", peers, | ||
) | ||
} | ||
|
||
// RuntimeAccessLists returns a per-runtime list of allowed peers. | ||
func (l *AccessList) RuntimeAccessLists() []api.RuntimeAccessList { | ||
l.mu.RLock() | ||
defer l.mu.RUnlock() | ||
|
||
rals := make([]api.RuntimeAccessList, 0, len(l.accessListByRuntime)) | ||
for rt, ps := range l.accessListByRuntime { | ||
ral := api.RuntimeAccessList{ | ||
RuntimeID: rt, | ||
Peers: ps, | ||
} | ||
rals = append(rals, ral) | ||
} | ||
|
||
return rals | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.