-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
go/worker/keymanager: Actually allow replication to maybe work
Access control forbidding replication may be more secure, but is not all that useful.
- Loading branch information
Showing
4 changed files
with
145 additions
and
23 deletions.
There are no files selected for viewing
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,4 @@ | ||
go/worker/keymanager: Actually allow replication to maybe work | ||
|
||
Access control forbidding replication may be more secure, but is not all | ||
that useful. |
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,115 @@ | ||
package keymanager | ||
|
||
import ( | ||
"github.com/oasislabs/oasis-core/go/common/accessctl" | ||
"github.com/oasislabs/oasis-core/go/common/crypto/signature" | ||
"github.com/oasislabs/oasis-core/go/common/node" | ||
registry "github.com/oasislabs/oasis-core/go/registry/api" | ||
"github.com/oasislabs/oasis-core/go/runtime/committee" | ||
) | ||
|
||
type kmNodeWatcher struct { | ||
w *Worker | ||
registry registry.Backend | ||
} | ||
|
||
func newKmNodeWatcher(w *Worker) *kmNodeWatcher { | ||
return &kmNodeWatcher{ | ||
w: w, | ||
registry: w.commonWorker.Consensus.Registry(), | ||
} | ||
} | ||
|
||
func (knw *kmNodeWatcher) watchNodes() { | ||
nodesCh, nodesSub, err := knw.registry.WatchNodeList(knw.w.ctx) | ||
if err != nil { | ||
knw.w.logger.Error("worker/keymanager: failed to watch node list", | ||
"err", err, | ||
) | ||
return | ||
} | ||
defer nodesSub.Close() | ||
|
||
watcher, err := committee.NewNodeDescriptorWatcher(knw.w.ctx, knw.registry) | ||
if err != nil { | ||
knw.w.logger.Error("worker/keymanager: failed to create node desc watcher", | ||
"err", err, | ||
) | ||
return | ||
} | ||
watcherCh, watcherSub, err := watcher.WatchNodeUpdates() | ||
if err != nil { | ||
knw.w.logger.Error("worker/keymanager: failed to watch node updates", | ||
"err", err, | ||
) | ||
return | ||
} | ||
defer watcherSub.Close() | ||
|
||
var activeNodes map[signature.PublicKey]bool | ||
for { | ||
select { | ||
case nodeList := <-nodesCh: | ||
watcher.Reset() | ||
activeNodes = knw.rebuildActiveNodeIDs(nodeList.Nodes) | ||
for id := range activeNodes { | ||
if _, err := watcher.WatchNode(knw.w.ctx, id); err != nil { | ||
knw.w.logger.Error("worker/keymanager: failed to watch node", | ||
"err", err, | ||
"id", id, | ||
) | ||
} | ||
} | ||
case watcherEv := <-watcherCh: | ||
if watcherEv.Update == nil { | ||
continue | ||
} | ||
if !activeNodes[watcherEv.Update.ID] { | ||
continue | ||
} | ||
case <-knw.w.stopCh: | ||
return | ||
} | ||
|
||
// Rebuild the access policy, something has changed. | ||
policy := accessctl.NewPolicy() | ||
|
||
sentryCerts := knw.w.commonWorker.GetConfig().SentryCertificates | ||
for _, cert := range sentryCerts { | ||
sentryNodesPolicy.AddCertPolicy(&policy, cert) | ||
} | ||
|
||
var nodes []*node.Node | ||
for id := range activeNodes { | ||
n := watcher.Lookup(id) | ||
if n == nil { | ||
continue | ||
} | ||
nodes = append(nodes, n) | ||
} | ||
|
||
kmNodesPolicy.AddRulesForNodeRoles(&policy, nodes, node.RoleKeyManager) | ||
knw.w.grpcPolicy.SetAccessPolicy(policy, knw.w.runtime.ID()) | ||
knw.w.logger.Debug("worker/keymanager: new km runtime access policy in effect", | ||
"policy", policy, | ||
) | ||
} | ||
} | ||
|
||
func (knw *kmNodeWatcher) rebuildActiveNodeIDs(nodeList []*node.Node) map[signature.PublicKey]bool { | ||
m := make(map[signature.PublicKey]bool) | ||
id := knw.w.runtime.ID() | ||
for _, n := range nodeList { | ||
if !n.HasRoles(node.RoleKeyManager) { | ||
continue | ||
} | ||
for _, rt := range n.Runtimes { | ||
if rt.ID.Equal(&id) { | ||
m[n.ID] = true | ||
break | ||
} | ||
} | ||
} | ||
|
||
return m | ||
} |
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