-
Notifications
You must be signed in to change notification settings - Fork 172
/
get_identity.go
95 lines (78 loc) · 2.44 KB
/
get_identity.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package common
import (
"context"
"encoding/hex"
"fmt"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/onflow/flow-go/admin"
"github.com/onflow/flow-go/admin/commands"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/module"
)
var _ commands.AdminCommand = (*GetIdentityCommand)(nil)
type getIdentityRequestType int
const (
FlowID getIdentityRequestType = iota
PeerID
)
type getIdentityRequestData struct {
requestType getIdentityRequestType
flowID flow.Identifier
peerID peer.ID
}
type GetIdentityCommand struct {
idProvider module.IdentityProvider
}
func (r *GetIdentityCommand) Handler(ctx context.Context, req *admin.CommandRequest) (interface{}, error) {
data := req.ValidatorData.(*getIdentityRequestData)
if data.requestType == FlowID {
identity, ok := r.idProvider.ByNodeID(data.flowID)
if !ok {
return nil, fmt.Errorf("no identity found for flow ID: %s", data.flowID)
}
return commands.ConvertToMap(identity)
} else {
identity, ok := r.idProvider.ByPeerID(data.peerID)
if !ok {
return nil, fmt.Errorf("no identity found for peer ID: %s", data.peerID)
}
return commands.ConvertToMap(identity)
}
}
// Validator validates the request.
// Returns admin.InvalidAdminReqError for invalid/malformed requests.
func (r *GetIdentityCommand) Validator(req *admin.CommandRequest) error {
input, ok := req.Data.(map[string]interface{})
if !ok {
return admin.NewInvalidAdminReqFormatError("expected map[string]any")
}
data := &getIdentityRequestData{}
req.ValidatorData = data
if flowID, ok := input["flow_id"]; ok {
data.requestType = FlowID
if flowID, ok := flowID.(string); ok {
if len(flowID) == 2*flow.IdentifierLen {
if b, err := hex.DecodeString(flowID); err == nil {
data.flowID = flow.HashToID(b)
return nil
}
}
}
return admin.NewInvalidAdminReqParameterError("flow_id", "must be 64-char hex string", flowID)
} else if peerID, ok := input["peer_id"]; ok {
data.requestType = PeerID
if peerID, ok := peerID.(string); ok {
if pid, err := peer.Decode(peerID); err == nil {
data.peerID = pid
return nil
}
}
return admin.NewInvalidAdminReqParameterError("peer_id", "must be valid peer id string", peerID)
}
return admin.NewInvalidAdminReqErrorf("either \"flow_id\" or \"peer_id\" field is required")
}
func NewGetIdentityCommand(idProvider module.IdentityProvider) commands.AdminCommand {
return &GetIdentityCommand{
idProvider: idProvider,
}
}