-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
go: Add key manager worker status to node control
- Loading branch information
Showing
5 changed files
with
195 additions
and
0 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
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,100 @@ | ||
package api | ||
|
||
import ( | ||
"fmt" | ||
|
||
core "github.com/libp2p/go-libp2p-core" | ||
|
||
"github.com/oasisprotocol/oasis-core/go/common" | ||
) | ||
|
||
// StatusState is the concise status state of the key manager worker. | ||
type StatusState uint8 | ||
|
||
const ( | ||
// StatusStateReady is the ready status state. | ||
StatusStateReady StatusState = iota | ||
// StatusStateStarting is the starting status state. | ||
StatusStateStarting | ||
// StatusStateStopped is the stopped status state. | ||
StatusStateStopped | ||
// StatusStateDisabled is the disabled status state. | ||
StatusStateDisabled | ||
) | ||
|
||
// String returns a string representation of a status state. | ||
func (s StatusState) String() string { | ||
switch s { | ||
case StatusStateReady: | ||
return "ready" | ||
case StatusStateStarting: | ||
return "starting" | ||
case StatusStateStopped: | ||
return "stopped" | ||
case StatusStateDisabled: | ||
return "disabled" | ||
default: | ||
return "[invalid status state]" | ||
} | ||
} | ||
|
||
// MarshalText encodes a StatusState into text form. | ||
func (s StatusState) MarshalText() ([]byte, error) { | ||
switch s { | ||
case StatusStateReady: | ||
return []byte(StatusStateReady.String()), nil | ||
case StatusStateStarting: | ||
return []byte(StatusStateStarting.String()), nil | ||
case StatusStateStopped: | ||
return []byte(StatusStateStopped.String()), nil | ||
case StatusStateDisabled: | ||
return []byte(StatusStateDisabled.String()), nil | ||
default: | ||
return nil, fmt.Errorf("invalid StatusState: %d", s) | ||
} | ||
} | ||
|
||
// UnmarshalText decodes a text slice into a StatusState. | ||
func (s *StatusState) UnmarshalText(text []byte) error { | ||
switch string(text) { | ||
case StatusStateReady.String(): | ||
*s = StatusStateReady | ||
case StatusStateStarting.String(): | ||
*s = StatusStateStarting | ||
case StatusStateStopped.String(): | ||
*s = StatusStateStopped | ||
case StatusStateDisabled.String(): | ||
*s = StatusStateDisabled | ||
default: | ||
return fmt.Errorf("invalid StatusState: %s", string(text)) | ||
} | ||
return nil | ||
} | ||
|
||
// RuntimeAccessList is an access control lists for a runtime. | ||
type RuntimeAccessList struct { | ||
// RuntimeID is the runtime ID of the runtime this access list is for. | ||
RuntimeID common.Namespace `json:"runtime_id"` | ||
|
||
// Peers is a list of peers that are allowed to call protected methods. | ||
Peers []core.PeerID `json:"peers"` | ||
} | ||
|
||
// Status is the key manager worker status. | ||
type Status struct { | ||
// Status is a concise status of the key manager worker. | ||
Status StatusState `json:"status"` | ||
|
||
// MayGenerate returns whether the enclave can generate a master secret. | ||
MayGenerate bool `json:"may_generate"` | ||
|
||
// RuntimeID is the runtime ID of the key manager. | ||
RuntimeID *common.Namespace `json:"runtime_id"` | ||
// ClientRuntimes is a list of compute runtimes that use this key manager. | ||
ClientRuntimes []common.Namespace `json:"client_runtimes"` | ||
|
||
// AccessList is per-runtime list of peers that are allowed to call protected methods. | ||
AccessList []RuntimeAccessList `json:"access_list"` | ||
// PrivatePeers is a list of peers that are always allowed to call protected methods. | ||
PrivatePeers []core.PeerID `json:"private_peers"` | ||
} |
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,73 @@ | ||
package keymanager | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/libp2p/go-libp2p-core/peer" | ||
|
||
"github.com/oasisprotocol/oasis-core/go/common" | ||
"github.com/oasisprotocol/oasis-core/go/worker/keymanager/api" | ||
) | ||
|
||
// GetStatus returns the key manager worker status. | ||
func (w *Worker) GetStatus(ctx context.Context) (*api.Status, error) { | ||
var initialized, stopped bool | ||
select { | ||
case <-w.Initialized(): | ||
initialized = true | ||
default: | ||
} | ||
select { | ||
case <-w.Quit(): | ||
stopped = true | ||
default: | ||
} | ||
|
||
var ss api.StatusState | ||
switch { | ||
case !w.enabled: | ||
ss = api.StatusStateDisabled | ||
case stopped: | ||
ss = api.StatusStateStopped | ||
case initialized: | ||
ss = api.StatusStateReady | ||
default: | ||
ss = api.StatusStateStarting | ||
} | ||
|
||
var rid *common.Namespace | ||
if w.runtime != nil { | ||
id := w.runtime.ID() | ||
rid = &id | ||
} | ||
|
||
rts := make([]common.Namespace, 0, len(w.clientRuntimes)) | ||
for rt := range w.clientRuntimes { | ||
rts = append(rts, rt) | ||
} | ||
|
||
ps := make([]peer.ID, 0, len(w.privatePeers)) | ||
for p := range w.privatePeers { | ||
ps = append(ps, p) | ||
} | ||
|
||
w.RLock() | ||
al := make([]api.RuntimeAccessList, 0, len(w.accessListByRuntime)) | ||
for rt, ps := range w.accessListByRuntime { | ||
ral := api.RuntimeAccessList{ | ||
RuntimeID: rt, | ||
Peers: ps, | ||
} | ||
al = append(al, ral) | ||
} | ||
w.RUnlock() | ||
|
||
return &api.Status{ | ||
Status: ss, | ||
MayGenerate: w.mayGenerate, | ||
RuntimeID: rid, | ||
ClientRuntimes: rts, | ||
AccessList: al, | ||
PrivatePeers: ps, | ||
}, nil | ||
} |