-
Notifications
You must be signed in to change notification settings - Fork 0
/
admin.go
105 lines (90 loc) · 3.03 KB
/
admin.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
96
97
98
99
100
101
102
103
104
105
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package admin
import (
"context"
"strings"
"github.com/golang/protobuf/ptypes/empty"
"github.com/hyperledger/fabric/common/flogging"
"github.com/hyperledger/fabric/protos/common"
pb "github.com/hyperledger/fabric/protos/peer"
"github.com/pkg/errors"
"go.uber.org/zap/zapcore"
)
var logger = flogging.MustGetLogger("server")
type requestValidator interface {
validate(ctx context.Context, env *common.Envelope) (*pb.AdminOperation, error)
}
// AccessControlEvaluator evaluates whether the creator of the given SignedData
// is eligible of using the admin service
type AccessControlEvaluator interface {
// Evaluate evaluates the eligibility of the creator of the given SignedData
// for being serviced by the admin service
Evaluate(signatureSet []*common.SignedData) error
}
// NewAdminServer creates and returns a Admin service instance.
func NewAdminServer(ace AccessControlEvaluator) *ServerAdmin {
s := &ServerAdmin{
v: &validator{
ace: ace,
},
levelsAtStartup: flogging.GetModuleLevels(),
}
return s
}
// ServerAdmin implementation of the Admin service for the Peer
type ServerAdmin struct {
v requestValidator
levelsAtStartup map[string]zapcore.Level
}
func (s *ServerAdmin) GetStatus(ctx context.Context, env *common.Envelope) (*pb.ServerStatus, error) {
if _, err := s.v.validate(ctx, env); err != nil {
return nil, err
}
status := &pb.ServerStatus{Status: pb.ServerStatus_STARTED}
logger.Debugf("returning status: %s", status)
return status, nil
}
func (s *ServerAdmin) StartServer(ctx context.Context, env *common.Envelope) (*pb.ServerStatus, error) {
if _, err := s.v.validate(ctx, env); err != nil {
return nil, err
}
status := &pb.ServerStatus{Status: pb.ServerStatus_STARTED}
logger.Debugf("returning status: %s", status)
return status, nil
}
func (s *ServerAdmin) GetModuleLogLevel(ctx context.Context, env *common.Envelope) (*pb.LogLevelResponse, error) {
op, err := s.v.validate(ctx, env)
if err != nil {
return nil, err
}
request := op.GetLogReq()
if request == nil {
return nil, errors.New("request is nil")
}
logLevelString := flogging.GetModuleLevel(request.LogModule)
logResponse := &pb.LogLevelResponse{LogModule: request.LogModule, LogLevel: logLevelString}
return logResponse, nil
}
func (s *ServerAdmin) SetModuleLogLevel(ctx context.Context, env *common.Envelope) (*pb.LogLevelResponse, error) {
op, err := s.v.validate(ctx, env)
if err != nil {
return nil, err
}
request := op.GetLogReq()
if request == nil {
return nil, errors.New("request is nil")
}
err = flogging.SetModuleLevels(request.LogModule, request.LogLevel)
logResponse := &pb.LogLevelResponse{LogModule: request.LogModule, LogLevel: strings.ToUpper(request.LogLevel)}
return logResponse, err
}
func (s *ServerAdmin) RevertLogLevels(ctx context.Context, env *common.Envelope) (*empty.Empty, error) {
if _, err := s.v.validate(ctx, env); err != nil {
return nil, err
}
flogging.RestoreLevels(s.levelsAtStartup)
return &empty.Empty{}, nil
}