|
1 | 1 | /*
|
2 |
| -Copyright IBM Corp. 2016 All Rights Reserved. |
| 2 | +Copyright IBM Corp. All Rights Reserved. |
3 | 3 |
|
4 |
| -Licensed under the Apache License, Version 2.0 (the "License"); |
5 |
| -you may not use this file except in compliance with the License. |
6 |
| -You may obtain a copy of the License at |
7 |
| -
|
8 |
| - http://www.apache.org/licenses/LICENSE-2.0 |
9 |
| -
|
10 |
| -Unless required by applicable law or agreed to in writing, software |
11 |
| -distributed under the License is distributed on an "AS IS" BASIS, |
12 |
| -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 |
| -See the License for the specific language governing permissions and |
14 |
| -limitations under the License. |
| 4 | +SPDX-License-Identifier: Apache-2.0 |
15 | 5 | */
|
16 | 6 |
|
17 | 7 | package core
|
18 | 8 |
|
19 | 9 | import (
|
20 | 10 | "github.com/golang/protobuf/ptypes/empty"
|
21 | 11 | "github.com/hyperledger/fabric/common/flogging"
|
| 12 | + "github.com/hyperledger/fabric/protos/common" |
22 | 13 | pb "github.com/hyperledger/fabric/protos/peer"
|
| 14 | + "github.com/pkg/errors" |
23 | 15 | "golang.org/x/net/context"
|
24 | 16 | )
|
25 | 17 |
|
26 | 18 | var logger = flogging.MustGetLogger("server")
|
27 | 19 |
|
| 20 | +type requestValidator interface { |
| 21 | + validate(ctx context.Context, env *common.Envelope) (*pb.AdminOperation, error) |
| 22 | +} |
| 23 | + |
| 24 | +// AccessControlEvaluator evaluates whether the creator of the given SignedData |
| 25 | +// is eligible of using the admin service |
| 26 | +type AccessControlEvaluator interface { |
| 27 | + // Evaluate evaluates the eligibility of the creator of the given SignedData |
| 28 | + // for being serviced by the admin service |
| 29 | + Evaluate(signatureSet []*common.SignedData) error |
| 30 | +} |
| 31 | + |
28 | 32 | // NewAdminServer creates and returns a Admin service instance.
|
29 |
| -func NewAdminServer() *ServerAdmin { |
30 |
| - s := new(ServerAdmin) |
| 33 | +func NewAdminServer(ace AccessControlEvaluator) *ServerAdmin { |
| 34 | + s := &ServerAdmin{ |
| 35 | + v: &validator{ |
| 36 | + ace: ace, |
| 37 | + }, |
| 38 | + } |
31 | 39 | return s
|
32 | 40 | }
|
33 | 41 |
|
34 | 42 | // ServerAdmin implementation of the Admin service for the Peer
|
35 | 43 | type ServerAdmin struct {
|
| 44 | + v requestValidator |
36 | 45 | }
|
37 | 46 |
|
38 |
| -// GetStatus reports the status of the server |
39 |
| -func (*ServerAdmin) GetStatus(context.Context, *empty.Empty) (*pb.ServerStatus, error) { |
| 47 | +func (s *ServerAdmin) GetStatus(ctx context.Context, env *common.Envelope) (*pb.ServerStatus, error) { |
| 48 | + if _, err := s.v.validate(ctx, env); err != nil { |
| 49 | + return nil, err |
| 50 | + } |
40 | 51 | status := &pb.ServerStatus{Status: pb.ServerStatus_STARTED}
|
41 | 52 | logger.Debugf("returning status: %s", status)
|
42 | 53 | return status, nil
|
43 | 54 | }
|
44 | 55 |
|
45 |
| -// StartServer starts the server |
46 |
| -func (*ServerAdmin) StartServer(context.Context, *empty.Empty) (*pb.ServerStatus, error) { |
| 56 | +func (s *ServerAdmin) StartServer(ctx context.Context, env *common.Envelope) (*pb.ServerStatus, error) { |
| 57 | + if _, err := s.v.validate(ctx, env); err != nil { |
| 58 | + return nil, err |
| 59 | + } |
47 | 60 | status := &pb.ServerStatus{Status: pb.ServerStatus_STARTED}
|
48 | 61 | logger.Debugf("returning status: %s", status)
|
49 | 62 | return status, nil
|
50 | 63 | }
|
51 | 64 |
|
52 |
| -// GetModuleLogLevel gets the current logging level for the specified module |
53 |
| -// TODO Modify the signature so as to remove the error return - it's always been nil |
54 |
| -func (*ServerAdmin) GetModuleLogLevel(ctx context.Context, request *pb.LogLevelRequest) (*pb.LogLevelResponse, error) { |
| 65 | +func (s *ServerAdmin) GetModuleLogLevel(ctx context.Context, env *common.Envelope) (*pb.LogLevelResponse, error) { |
| 66 | + op, err := s.v.validate(ctx, env) |
| 67 | + if err != nil { |
| 68 | + return nil, err |
| 69 | + } |
| 70 | + request := op.GetLogReq() |
| 71 | + if request == nil { |
| 72 | + return nil, errors.New("request is nil") |
| 73 | + } |
55 | 74 | logLevelString := flogging.GetModuleLevel(request.LogModule)
|
56 | 75 | logResponse := &pb.LogLevelResponse{LogModule: request.LogModule, LogLevel: logLevelString}
|
57 | 76 | return logResponse, nil
|
58 | 77 | }
|
59 | 78 |
|
60 |
| -// SetModuleLogLevel sets the logging level for the specified module |
61 |
| -func (*ServerAdmin) SetModuleLogLevel(ctx context.Context, request *pb.LogLevelRequest) (*pb.LogLevelResponse, error) { |
| 79 | +func (s *ServerAdmin) SetModuleLogLevel(ctx context.Context, env *common.Envelope) (*pb.LogLevelResponse, error) { |
| 80 | + op, err := s.v.validate(ctx, env) |
| 81 | + if err != nil { |
| 82 | + return nil, err |
| 83 | + } |
| 84 | + request := op.GetLogReq() |
| 85 | + if request == nil { |
| 86 | + return nil, errors.New("request is nil") |
| 87 | + } |
62 | 88 | logLevelString, err := flogging.SetModuleLevel(request.LogModule, request.LogLevel)
|
63 | 89 | logResponse := &pb.LogLevelResponse{LogModule: request.LogModule, LogLevel: logLevelString}
|
64 | 90 | return logResponse, err
|
65 | 91 | }
|
66 | 92 |
|
67 |
| -// RevertLogLevels reverts the logger levels for all modules to the level |
68 |
| -// defined at the end of peer startup. |
69 |
| -func (*ServerAdmin) RevertLogLevels(context.Context, *empty.Empty) (*empty.Empty, error) { |
| 93 | +func (s *ServerAdmin) RevertLogLevels(ctx context.Context, env *common.Envelope) (*empty.Empty, error) { |
| 94 | + if _, err := s.v.validate(ctx, env); err != nil { |
| 95 | + return nil, err |
| 96 | + } |
70 | 97 | err := flogging.RevertToPeerStartupLevels()
|
71 |
| - |
72 | 98 | return &empty.Empty{}, err
|
73 | 99 | }
|
0 commit comments