-
Notifications
You must be signed in to change notification settings - Fork 8.8k
/
admin.go
142 lines (123 loc) · 4 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package admin
import (
"context"
"fmt"
"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"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
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,
},
specAtStartup: flogging.Global.Spec(),
}
return s
}
// ServerAdmin implementation of the Admin service for the Peer
type ServerAdmin struct {
v requestValidator
specAtStartup string
}
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.GetLoggerLevel(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")
}
spec := fmt.Sprintf("%s:%s=%s", flogging.Global.Spec(), request.LogModule, request.LogLevel)
err = flogging.Global.ActivateSpec(spec)
if err != nil {
err = status.Errorf(codes.InvalidArgument, "error setting log spec to '%s': %s", spec, err.Error())
return nil, err
}
logResponse := &pb.LogLevelResponse{LogModule: request.LogModule, LogLevel: strings.ToUpper(request.LogLevel)}
return logResponse, nil
}
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.ActivateSpec(s.specAtStartup)
return &empty.Empty{}, nil
}
func (s *ServerAdmin) GetLogSpec(ctx context.Context, env *common.Envelope) (*pb.LogSpecResponse, error) {
if _, err := s.v.validate(ctx, env); err != nil {
return nil, err
}
logSpec := flogging.Global.Spec()
logResponse := &pb.LogSpecResponse{LogSpec: logSpec}
return logResponse, nil
}
func (s *ServerAdmin) SetLogSpec(ctx context.Context, env *common.Envelope) (*pb.LogSpecResponse, error) {
op, err := s.v.validate(ctx, env)
if err != nil {
return nil, err
}
request := op.GetLogSpecReq()
if request == nil {
return nil, errors.New("request is nil")
}
err = flogging.Global.ActivateSpec(request.LogSpec)
logResponse := &pb.LogSpecResponse{
LogSpec: request.LogSpec,
}
if err != nil {
logResponse.Error = err.Error()
}
return logResponse, nil
}