/
admin.go
98 lines (88 loc) · 3.45 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
// Copyright 2018 The LUCI Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package apiservers
import (
"context"
"github.com/golang/protobuf/proto"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"go.chromium.org/luci/common/logging"
"go.chromium.org/luci/common/proto/google"
"go.chromium.org/luci/grpc/grpcutil"
schedulerpb "go.chromium.org/luci/scheduler/api/scheduler/v1"
"go.chromium.org/luci/scheduler/appengine/catalog"
"go.chromium.org/luci/scheduler/appengine/engine"
"go.chromium.org/luci/scheduler/appengine/internal"
"go.chromium.org/luci/server/auth"
)
// AdminServerWithACL returns AdminServer implementation that checks all callers
// are in the given administrator group.
func AdminServerWithACL(e engine.EngineInternal, c catalog.Catalog, adminGroup string) internal.AdminServer {
return &internal.DecoratedAdmin{
Service: &adminServer{
Engine: e,
Catalog: c,
},
Prelude: func(c context.Context, methodName string, req proto.Message) (context.Context, error) {
caller := auth.CurrentIdentity(c)
logging.Warningf(c, "Admin call %q by %q", methodName, caller)
switch yes, err := auth.IsMember(c, adminGroup); {
case err != nil:
return nil, status.Errorf(codes.Internal, "failed to check ACL")
case !yes:
return nil, status.Errorf(codes.PermissionDenied, "not an administrator")
default:
return c, nil
}
},
Postlude: func(c context.Context, methodName string, rsp proto.Message, err error) error {
return grpcutil.GRPCifyAndLogErr(c, err)
},
}
}
// adminServer implements internal.admin.Admin API without ACL check.
//
// It also returns regular errors, NOT gRPC errors. AdminServerWithACL takes
// care of authorization and conversion of errors to grpc ones.
type adminServer struct {
Engine engine.EngineInternal
Catalog catalog.Catalog
}
// GetDebugJobState implements the corresponding RPC method.
func (s *adminServer) GetDebugJobState(c context.Context, r *schedulerpb.JobRef) (resp *internal.DebugJobState, err error) {
switch state, err := s.Engine.GetDebugJobState(c, r.Project+"/"+r.Job); {
case err == engine.ErrNoSuchJob:
return nil, status.Errorf(codes.NotFound, "no such job")
case err != nil:
return nil, err
default:
return &internal.DebugJobState{
Enabled: state.Job.Enabled,
Paused: state.Job.Paused,
LastTriage: google.NewTimestamp(state.Job.LastTriage),
CronState: &internal.DebugJobState_CronState{
Enabled: state.Job.Cron.Enabled,
Generation: state.Job.Cron.Generation,
LastRewind: google.NewTimestamp(state.Job.Cron.LastRewind),
LastTickWhen: google.NewTimestamp(state.Job.Cron.LastTick.When),
LastTickNonce: state.Job.Cron.LastTick.TickNonce,
},
ManagerState: state.ManagerState,
ActiveInvocations: state.Job.ActiveInvocations,
FinishedInvocations: state.FinishedInvocations,
RecentlyFinishedSet: state.RecentlyFinishedSet,
PendingTriggersSet: state.PendingTriggersSet,
}, nil
}
}