mirrored from https://chromium.googlesource.com/infra/luci/luci-go
/
config.go
211 lines (187 loc) · 6.34 KB
/
config.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// 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 rpc
import (
"context"
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes/empty"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"go.chromium.org/luci/common/clock"
"go.chromium.org/luci/common/errors"
"go.chromium.org/luci/common/logging"
"go.chromium.org/luci/common/proto/paged"
"go.chromium.org/luci/gae/service/datastore"
"go.chromium.org/luci/server/auth"
"go.chromium.org/luci/gce/api/config/v1"
"go.chromium.org/luci/gce/appengine/model"
)
// Config implements config.ConfigurationServer.
type Config struct {
}
// Ensure Config implements config.ConfigurationServer.
var _ config.ConfigurationServer = &Config{}
// Delete handles a request to delete a config.
// For app-internal use only.
func (*Config) Delete(c context.Context, req *config.DeleteRequest) (*empty.Empty, error) {
if req.GetId() == "" {
return nil, status.Errorf(codes.InvalidArgument, "ID is required")
}
if err := datastore.Delete(c, &model.Config{ID: req.Id}); err != nil {
return nil, errors.Annotate(err, "failed to delete config").Err()
}
return &empty.Empty{}, nil
}
// Ensure handles a request to create or update a config.
// For app-internal use only.
func (*Config) Ensure(c context.Context, req *config.EnsureRequest) (*config.Config, error) {
if req.GetId() == "" {
return nil, status.Errorf(codes.InvalidArgument, "ID is required")
}
cfg := &model.Config{
ID: req.Id,
}
if err := datastore.RunInTransaction(c, func(c context.Context) error {
var priorAmount int32
switch err := datastore.Get(c, cfg); {
case err == nil:
// Don't forget potentially custom amount set via Update RPC.
priorAmount = cfg.Config.CurrentAmount
case err == datastore.ErrNoSuchEntity:
priorAmount = 0
default:
return errors.Annotate(err, "failed to fetch config").Err()
}
cfg.Config = *req.Config
cfg.Config.CurrentAmount = priorAmount
if err := datastore.Put(c, cfg); err != nil {
return errors.Annotate(err, "failed to store config").Err()
}
return nil
}, nil); err != nil {
return nil, err
}
return &cfg.Config, nil
}
// Get handles a request to get a config.
func (*Config) Get(c context.Context, req *config.GetRequest) (*config.Config, error) {
if req.GetId() == "" {
return nil, status.Errorf(codes.InvalidArgument, "ID is required")
}
cfg, err := getConfigByID(c, req.Id)
if err != nil {
return nil, err
}
return &cfg.Config, nil
}
// List handles a request to list all configs.
func (*Config) List(c context.Context, req *config.ListRequest) (*config.ListResponse, error) {
rsp := &config.ListResponse{}
if err := paged.Query(c, req.GetPageSize(), req.GetPageToken(), rsp, datastore.NewQuery(model.ConfigKind), func(cfg *model.Config) error {
rsp.Configs = append(rsp.Configs, &cfg.Config)
return nil
}); err != nil {
return nil, err
}
return rsp, nil
}
// Update handles a request to update a config.
func (*Config) Update(c context.Context, req *config.UpdateRequest) (*config.Config, error) {
switch {
case req.GetId() == "":
return nil, status.Errorf(codes.InvalidArgument, "ID is required")
case len(req.UpdateMask.GetPaths()) == 0:
return nil, status.Errorf(codes.InvalidArgument, "update mask is required")
}
for _, p := range req.UpdateMask.Paths {
if p != "config.current_amount" {
return nil, status.Errorf(codes.InvalidArgument, "field %q is invalid or immutable", p)
}
}
var ret *config.Config
if err := datastore.RunInTransaction(c, func(c context.Context) error {
cfg, err := getConfigByID(c, req.Id)
if err != nil {
return err
}
ret = &cfg.Config
amt, err := cfg.Config.ComputeAmount(req.Config.GetCurrentAmount(), clock.Now(c))
switch {
case err != nil:
return errors.Annotate(err, "failed to parse amount").Err()
case amt == cfg.Config.CurrentAmount:
return nil
default:
cfg.Config.CurrentAmount = amt
if err := datastore.Put(c, cfg); err != nil {
return errors.Annotate(err, "failed to store config").Err()
}
return nil
}
}, nil); err != nil {
return nil, err
}
return ret, nil
}
// configPrelude ensures the user is authorized to use the config API.
func configPrelude(c context.Context, methodName string, req proto.Message) (context.Context, error) {
if methodName == "Update" || methodName == "Get" {
// Update performs its own authorization checks, so allow all callers through.
logging.Debugf(c, "%s called %q:\n%s", auth.CurrentIdentity(c), methodName, req)
return c, nil
}
if !isReadOnly(methodName) {
return c, status.Errorf(codes.PermissionDenied, "unauthorized user")
}
switch is, err := auth.IsMember(c, admins, writers, readers); {
case err != nil:
return c, err
case is:
logging.Debugf(c, "%s called %q:\n%s", auth.CurrentIdentity(c), methodName, req)
return c, nil
}
return c, status.Errorf(codes.PermissionDenied, "unauthorized user")
}
// NewConfigurationServer returns a new configuration server.
func NewConfigurationServer() config.ConfigurationServer {
return &config.DecoratedConfiguration{
Prelude: configPrelude,
Service: &Config{},
Postlude: gRPCifyAndLogErr,
}
}
func getConfigByID(c context.Context, id string) (*model.Config, error) {
cfg := &model.Config{
ID: id,
}
switch err := datastore.Get(c, cfg); err {
case nil:
case datastore.ErrNoSuchEntity:
return nil, notFoundErr(id)
default:
return nil, errors.Annotate(err, "failed to fetch config").Err()
}
switch is, err := auth.IsMember(c, cfg.Config.GetOwner()...); {
case err != nil:
return nil, err
case !is:
return nil, notFoundErr(id)
}
return cfg, nil
}
func notFoundErr(id string) error {
// To avoid revealing information about config existence to unauthorized users,
// not found and permission denied responses should be ambiguous.
return status.Errorf(codes.NotFound, "no config found with ID %q or unauthorized user", id)
}