-
Notifications
You must be signed in to change notification settings - Fork 24
/
policy-service.go
379 lines (330 loc) · 12.6 KB
/
policy-service.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
package controlplane
import (
"context"
"fmt"
"path"
"strings"
clientv3 "go.etcd.io/etcd/client/v3"
"go.uber.org/fx"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/types/known/emptypb"
policylangv1 "github.com/fluxninja/aperture/v2/api/gen/proto/go/aperture/policy/language/v1"
policysyncv1 "github.com/fluxninja/aperture/v2/api/gen/proto/go/aperture/policy/sync/v1"
"github.com/fluxninja/aperture/v2/pkg/config"
etcdclient "github.com/fluxninja/aperture/v2/pkg/etcd/client"
"github.com/fluxninja/aperture/v2/pkg/log"
"github.com/fluxninja/aperture/v2/pkg/policies/paths"
"github.com/fluxninja/aperture/v2/pkg/utils"
)
// PolicyService is the implementation of policylangv1.PolicyService interface.
type PolicyService struct {
policylangv1.UnimplementedPolicyServiceServer
policyFactory *PolicyFactory
etcdClient *etcdclient.Client
}
// RegisterPolicyServiceIn bundles and annotates parameters.
type RegisterPolicyServiceIn struct {
fx.In
Server *grpc.Server `name:"default"`
PolicyFactory *PolicyFactory
ETCDClient *etcdclient.Client
Lifecycle fx.Lifecycle
}
// RegisterPolicyService registers a service for policy.
func RegisterPolicyService(in RegisterPolicyServiceIn) *PolicyService {
svc := &PolicyService{
policyFactory: in.PolicyFactory,
etcdClient: in.ETCDClient,
}
policylangv1.RegisterPolicyServiceServer(in.Server, svc)
return svc
}
// GetPolicies returns all the policies running (or supposed to be running) in the system.
func (s *PolicyService) GetPolicies(ctx context.Context, _ *emptypb.Empty) (*policylangv1.GetPoliciesResponse, error) {
localPolicies := s.policyFactory.GetPolicyWrappers()
remotePolicies, err := s.etcdClient.Client.KV.Get(ctx, paths.PoliciesAPIConfigPath, clientv3.WithPrefix())
if err != nil {
log.Warn().Err(err).Msg("GetPolicies: failed to fetch policies from etcd")
return nil, status.Error(codes.Internal, fmt.Sprintf("failed to fetch policies from etcd: %s", err))
}
resp := make(map[string]*policylangv1.GetPolicyResponse, len(remotePolicies.Kvs))
for _, kv := range remotePolicies.Kvs {
name, ok := strings.CutPrefix(string(kv.Key), fmt.Sprintf("%s/", paths.PoliciesAPIConfigPath))
if !ok {
log.Bug().Str("key", string(kv.Key)).Msg("failed to parse policy name from key")
continue
}
resp[name] = getPolicyResponse(kv.Value, localPolicies[name])
}
for name, policy := range localPolicies {
if _, ok := resp[name]; ok {
continue
}
resp[name] = getLocalOnlyPolicyResponse(policy)
}
return &policylangv1.GetPoliciesResponse{
Policies: &policylangv1.Policies{
Policies: resp,
},
}, nil
}
// GetPolicy returns the policy which matches the given name.
//
// Returns error if policy cannot be found in *neither* etcd nor locally.
func (s *PolicyService) GetPolicy(ctx context.Context, request *policylangv1.GetPolicyRequest) (*policylangv1.GetPolicyResponse, error) {
localPolicy := s.policyFactory.GetPolicyWrapper(request.Name)
remotePolicies, err := s.etcdClient.Client.KV.Get(ctx, path.Join(paths.PoliciesAPIConfigPath, request.Name))
if err != nil {
log.Warn().Err(err).Str("policy", request.Name).Msg("GetPolicy: failed to fetch policy from etcd")
return nil, status.Error(codes.Internal, fmt.Sprintf("failed to fetch policy from etcd: %s", err))
}
if len(remotePolicies.Kvs) == 0 {
if localPolicy == nil {
return nil, status.Error(codes.NotFound, "policy not found")
}
return getLocalOnlyPolicyResponse(localPolicy), nil
}
return getPolicyResponse(remotePolicies.Kvs[0].Value, localPolicy), nil
}
// getPolicyResponse combines info about the policy from etcd and local
// policyFactory to create a response for GetPolicy/GetPolicies
//
// localPolicy can be nil.
func getPolicyResponse(remoteBytes []byte, localPolicy *policysyncv1.PolicyWrapper) *policylangv1.GetPolicyResponse {
remotePolicy := &policylangv1.Policy{}
err := proto.Unmarshal(remoteBytes, remotePolicy)
if err != nil {
if localPolicy == nil {
return &policylangv1.GetPolicyResponse{
Status: policylangv1.GetPolicyResponse_INVALID,
Reason: fmt.Sprintf("parse error: %s", err),
}
}
return &policylangv1.GetPolicyResponse{
Policy: localPolicy.Policy,
Status: policylangv1.GetPolicyResponse_OUTDATED,
Reason: fmt.Sprintf("parse error: %s", err),
}
}
if localPolicy == nil {
return &policylangv1.GetPolicyResponse{
Policy: remotePolicy,
Status: policylangv1.GetPolicyResponse_NOT_LOADED,
Reason: "not loaded into controller",
}
}
remoteHash, err := HashPolicy(remotePolicy)
if err != nil {
return &policylangv1.GetPolicyResponse{
Policy: localPolicy.Policy,
Status: policylangv1.GetPolicyResponse_OUTDATED,
Reason: fmt.Sprintf("failed to compute remote policy hash: %s", err),
}
}
localHash := localPolicy.GetCommonAttributes().GetPolicyHash()
if remoteHash != localHash {
return &policylangv1.GetPolicyResponse{
Policy: localPolicy.Policy,
Status: policylangv1.GetPolicyResponse_OUTDATED,
Reason: fmt.Sprintf(
"policy mismatch, remote hash: %s, local hash %s",
remoteHash,
localHash,
),
}
}
return &policylangv1.GetPolicyResponse{
Policy: localPolicy.Policy,
Status: policylangv1.GetPolicyResponse_VALID,
}
}
func getLocalOnlyPolicyResponse(localPolicy *policysyncv1.PolicyWrapper) *policylangv1.GetPolicyResponse {
if localPolicy.Source == policysyncv1.PolicyWrapper_ETCD {
return &policylangv1.GetPolicyResponse{
Policy: localPolicy.Policy,
Status: policylangv1.GetPolicyResponse_STALE,
Reason: "deleted, but still running",
}
}
return &policylangv1.GetPolicyResponse{
// It's expected for policies from K8S CRs to not be present in /config/api/policies
Policy: localPolicy.Policy,
Status: policylangv1.GetPolicyResponse_VALID,
}
}
// UpsertPolicy creates/updates policy to the system.
func (s *PolicyService) UpsertPolicy(ctx context.Context, req *policylangv1.UpsertPolicyRequest) (*emptypb.Empty, error) {
if wrapper := s.policyFactory.GetPolicyWrapper(req.PolicyName); wrapper.GetSource() == policysyncv1.PolicyWrapper_K8S {
return nil, status.Error(codes.AlreadyExists, "policy already exists and is managed by K8S object")
}
etcdPolicy, err := s.etcdClient.Client.KV.Get(ctx, path.Join(paths.PoliciesAPIConfigPath, req.PolicyName))
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
oldPolicy := &policylangv1.Policy{}
if len(etcdPolicy.Kvs) > 0 {
uerr := config.UnmarshalJSON(etcdPolicy.Kvs[0].Value, oldPolicy)
if uerr != nil {
// Deprecated: v3.0.0. Older way of string policy on etcd.
// Remove this code in v3.0.0.
uerr = proto.Unmarshal(etcdPolicy.Kvs[0].Value, oldPolicy)
if uerr != nil {
return nil, status.Error(codes.FailedPrecondition, "cannot patch, existing policy is invalid")
}
}
}
newPolicy := &policylangv1.Policy{}
if req.PolicyString != "" {
err = config.UnmarshalYAML([]byte(req.PolicyString), newPolicy)
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "failed to unmarshal policy: %s", err)
}
} else if req.Policy != nil { // Deprecated: v2.20.0. Should stop accepting policy as proto message
newPolicy = proto.Clone(req.Policy).(*policylangv1.Policy)
} else {
return nil, status.Error(codes.InvalidArgument, "policy is empty")
}
if len(req.GetUpdateMask().GetPaths()) > 0 {
if len(etcdPolicy.Kvs) == 0 {
return nil, status.Error(codes.NotFound, "cannot patch, no such policy")
}
if len(req.UpdateMask.Paths) == 1 && req.UpdateMask.Paths[0] == "all" {
// We use update_mask: { paths: ["all"] } to convey that we want to
// update the whole Policy. This is non-standard usage of update
// masks, so we cannot call ApplyFieldMask here.
// FIXME Do we need field masks at all? Looks like they're only
// used to pass a "force" flag from aperturectl.
} else {
utils.ApplyFieldMask(oldPolicy, newPolicy, req.UpdateMask)
newPolicy = oldPolicy
}
} else if len(etcdPolicy.Kvs) > 0 {
// We want to prevent accidentally overwriting valid policy, that's why
// it's only possible when PATCH (update mask) is specified.
_, _, err = ValidateAndCompileProto(ctx, "dummy-name", oldPolicy)
if err == nil {
// Note: Older aperturectl versions and Aperture Cloud rely on
// exact string of error message!
return nil, status.Error(codes.AlreadyExists, "policy already exists. Use UpsertPolicy with PATCH call to update it.")
}
if ctx.Err() != nil {
return nil, err
}
// Otherwise, we know policy is invalid and thus we allow overwriting it.
}
_, _, err = ValidateAndCompileProto(ctx, req.PolicyName, newPolicy)
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "failed to compile policy: %s", err)
}
newPolicyString, err := newPolicy.MarshalJSON()
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to marshal policy: %s", err)
}
// FIXME compare original mod revision to make sure the policy we're patching hasn't changed meanwhile
_, err = s.etcdClient.KV.Put(ctx, path.Join(paths.PoliciesAPIConfigPath, req.PolicyName), string(newPolicyString))
if err != nil {
return nil, err
}
if len(etcdPolicy.Kvs) > 0 {
log.Info().Str("policy", req.PolicyName).Msg("Policy updated in etcd")
} else {
log.Info().Str("policy", req.PolicyName).Msg("Policy created in etcd")
}
return new(emptypb.Empty), nil
}
// PostDynamicConfig updates dynamic config to the system.
func (s *PolicyService) PostDynamicConfig(ctx context.Context, req *policylangv1.PostDynamicConfigRequest) (*emptypb.Empty, error) {
etcdPolicy, err := s.etcdClient.Client.KV.Get(
ctx,
path.Join(paths.PoliciesAPIConfigPath, req.PolicyName),
clientv3.WithKeysOnly(),
)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
if len(etcdPolicy.Kvs) == 0 && s.policyFactory.GetPolicyWrapper(req.PolicyName) == nil {
return nil, status.Error(codes.NotFound, "no such policy")
}
jsonDynamicConfig, err := req.DynamicConfig.MarshalJSON()
if err != nil {
return nil, fmt.Errorf("failed to marshal dynamic config '%s': '%s'", req.PolicyName, err)
}
// FIXME use a transaction so that it's not possible to post dynamic config
// to non-existing policy.
// Note: Right now we allow setting dynamic config for k8s-managed policy.
// Do we want to continue supporting that?
_, err = s.etcdClient.KV.Put(ctx, path.Join(paths.PoliciesAPIDynamicConfigPath, req.PolicyName), string(jsonDynamicConfig))
if err != nil {
return nil, fmt.Errorf("failed to write dynamic config '%s' to etcd: '%s'", req.PolicyName, err)
}
return new(emptypb.Empty), nil
}
// DeletePolicy deletes a policy from the system.
func (s *PolicyService) DeletePolicy(ctx context.Context, policy *policylangv1.DeletePolicyRequest) (*emptypb.Empty, error) {
resp, err := s.etcdClient.KV.Delete(ctx, path.Join(paths.PoliciesAPIConfigPath, policy.Name))
if err != nil {
return nil, err
}
if resp.Deleted > 0 {
log.Info().Str("policy", policy.Name).Msg("Policy removed from etcd")
}
// FIXME: If Deleted==0 we should return NotFound, but first we need to ensure
// that the delete activity in cloud handles such status correctly.
_, err = s.etcdClient.KV.Delete(ctx, path.Join(paths.PoliciesAPIDynamicConfigPath, policy.Name))
if err != nil {
return nil, err
}
return &emptypb.Empty{}, nil
}
// GetDecisions returns the decisions.
func (s *PolicyService) GetDecisions(ctx context.Context, req *policylangv1.GetDecisionsRequest) (*policylangv1.GetDecisionsResponse, error) {
decisionsPathPrefix := paths.DecisionsPrefix + "/"
decisionType := ""
all := true
if req != nil {
if req.DecisionType != "" {
all = false
decisionType = req.DecisionType
decisionsPathPrefix += decisionType + "/"
}
}
resp, err := s.etcdClient.Client.KV.Get(ctx, decisionsPathPrefix, clientv3.WithPrefix())
if err != nil {
return nil, err
}
decisions := map[string]string{}
for _, kv := range resp.Kvs {
decisionName, ok := strings.CutPrefix(string(kv.Key), decisionsPathPrefix)
if !ok {
continue
}
if all {
decisionType = strings.Split(decisionName, "/")[0]
}
var m protoreflect.ProtoMessage
switch decisionType {
case "load_scheduler":
m = &policysyncv1.LoadDecisionWrapper{}
case "rate_limiter", "quota_scheduler":
m = &policysyncv1.RateLimiterDecisionWrapper{}
case "pod_scaler":
m = &policysyncv1.ScaleDecisionWrapper{}
case "sampler":
m = &policysyncv1.SamplerDecisionWrapper{}
}
err := proto.Unmarshal(kv.Value, m)
if err != nil {
return nil, err
}
mjson := protojson.Format(m)
decisions[decisionName] = mjson
}
return &policylangv1.GetDecisionsResponse{
Decisions: decisions,
}, nil
}