forked from weaveworks/weave-gitops
-
Notifications
You must be signed in to change notification settings - Fork 0
/
policies.go
240 lines (210 loc) · 6.86 KB
/
policies.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
package server
import (
"context"
"encoding/json"
"errors"
"fmt"
"strconv"
"strings"
"time"
"github.com/golang/protobuf/ptypes/any"
"github.com/hashicorp/go-multierror"
pacv2beta2 "github.com/weaveworks/policy-agent/api/v2beta2"
"github.com/weaveworks/weave-gitops/core/clustersmngr"
pb "github.com/weaveworks/weave-gitops/pkg/api/core"
"github.com/weaveworks/weave-gitops/pkg/server/auth"
"google.golang.org/protobuf/types/known/anypb"
"google.golang.org/protobuf/types/known/wrapperspb"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
)
const (
DefaultCluster = "Default"
)
func getPolicyParamValue(param pacv2beta2.PolicyParameters, policyID string) (*anypb.Any, error) {
if param.Value == nil {
return nil, nil
}
var anyValue *any.Any
var err error
switch param.Type {
case "string":
var strValue string
// attempt to clean up extra quotes if not successful show as is
unquotedValue, UnquoteErr := strconv.Unquote(string(param.Value.Raw))
if UnquoteErr != nil {
strValue = string(param.Value.Raw)
} else {
strValue = unquotedValue
}
value := wrapperspb.String(strValue)
anyValue, err = anypb.New(value)
case "integer":
intValue, convErr := strconv.Atoi(string(param.Value.Raw))
if convErr != nil {
err = convErr
break
}
value := wrapperspb.Int32(int32(intValue))
anyValue, err = anypb.New(value)
case "boolean":
boolValue, convErr := strconv.ParseBool(string(param.Value.Raw))
if convErr != nil {
err = convErr
break
}
value := wrapperspb.Bool(boolValue)
anyValue, err = anypb.New(value)
case "array":
var arrayValue []string
convErr := json.Unmarshal(param.Value.Raw, &arrayValue)
if convErr != nil {
err = convErr
break
}
value := &pb.PolicyParamRepeatedString{Value: arrayValue}
anyValue, err = anypb.New(value)
default:
return nil, fmt.Errorf("found unsupported policy parameter type %s in policy %s", param.Type, policyID)
}
if err != nil {
return nil, fmt.Errorf("failed to serialize parameter value %s in policy %s: %w", param.Name, policyID, err)
}
return anyValue, nil
}
func policyToPolicyRespone(policyCRD pacv2beta2.Policy, clusterName string) (*pb.PolicyObj, error) {
policySpec := policyCRD.Spec
policy := &pb.PolicyObj{
Name: policySpec.Name,
Id: policySpec.ID,
Category: policySpec.Category,
Tags: policySpec.Tags,
Severity: policySpec.Severity,
CreatedAt: policyCRD.CreationTimestamp.Format(time.RFC3339),
Tenant: policyCRD.GetLabels()["toolkit.fluxcd.io/tenant"],
Modes: policyCRD.Status.Modes,
ClusterName: clusterName,
Code: policySpec.Code,
Description: policySpec.Description,
HowToSolve: policySpec.HowToSolve,
}
var policyLabels []*pb.PolicyTargetLabel
for i := range policySpec.Targets.Labels {
policyLabels = append(policyLabels, &pb.PolicyTargetLabel{
Values: policySpec.Targets.Labels[i],
})
}
var policyParams []*pb.PolicyParam
for _, param := range policySpec.Parameters {
policyParam := &pb.PolicyParam{
Name: param.Name,
Required: param.Required,
Type: param.Type,
}
value, err := getPolicyParamValue(param, policySpec.ID)
if err != nil {
return nil, err
}
policyParam.Value = value
policyParams = append(policyParams, policyParam)
}
var policyStandards []*pb.PolicyStandard
for _, standard := range policySpec.Standards {
policyStandards = append(policyStandards, &pb.PolicyStandard{
Id: standard.ID,
Controls: standard.Controls,
})
}
policy.Targets = &pb.PolicyTargets{
Kinds: policySpec.Targets.Kinds,
Namespaces: policySpec.Targets.Namespaces,
Labels: policyLabels,
}
policy.Parameters = policyParams
policy.Standards = policyStandards
return policy, nil
}
func (cs *coreServer) ListPolicies(ctx context.Context, m *pb.ListPoliciesRequest) (*pb.ListPoliciesResponse, error) {
respErrors := []*pb.ListError{}
var clustersClient clustersmngr.Client
var err error
clustersClient, err = cs.clustersManager.GetImpersonatedClient(ctx, auth.Principal(ctx))
if err != nil {
if merr, ok := err.(*multierror.Error); ok {
for _, err := range merr.Errors {
if cerr, ok := err.(*clustersmngr.ClientError); ok {
respErrors = append(respErrors, &pb.ListError{ClusterName: cerr.ClusterName, Message: cerr.Error()})
}
}
} else {
return nil, fmt.Errorf("unexpected error while getting clusters client, error: %w", err)
}
}
opts := []client.ListOption{}
if m.Pagination != nil {
opts = append(opts, client.Limit(m.Pagination.PageSize))
opts = append(opts, client.Continue(m.Pagination.PageToken))
}
var continueToken string
var lists map[string][]client.ObjectList
clist := clustersmngr.NewClusteredList(func() client.ObjectList {
return &pacv2beta2.PolicyList{}
})
var errsV2beta2 clustersmngr.ClusteredListError
if err := clustersClient.ClusteredList(ctx, clist, false, opts...); err != nil {
if !errors.As(err, &errsV2beta2) {
return nil, fmt.Errorf("error while listing v2beta2 policies: %w", err)
}
}
for _, e := range errsV2beta2.Errors {
if !strings.Contains(e.Err.Error(), "no matches for kind \"Policy\"") {
respErrors = append(respErrors, &pb.ListError{ClusterName: e.Cluster, Message: e.Err.Error()})
}
}
continueToken = clist.GetContinue()
lists = clist.Lists()
var policies []*pb.PolicyObj
for clusterName, lists := range lists {
for _, l := range lists {
list, ok := l.(*pacv2beta2.PolicyList)
if !ok {
respErrors = append(respErrors, &pb.ListError{ClusterName: clusterName, Message: fmt.Sprintf("unexpected list type %T", l)})
continue
}
for i := range list.Items {
policy, err := policyToPolicyRespone(list.Items[i], clusterName)
if err != nil {
return nil, fmt.Errorf("error while converting policy %s to response: %w", list.Items[i].Name, err)
}
policies = append(policies, policy)
}
}
}
return &pb.ListPoliciesResponse{
Policies: policies,
Total: int32(len(policies)),
NextPageToken: continueToken,
Errors: respErrors,
}, nil
}
func (cs *coreServer) GetPolicy(ctx context.Context, m *pb.GetPolicyRequest) (*pb.GetPolicyResponse, error) {
var clustersClient clustersmngr.Client
var err error
if m.ClusterName == "" {
m.ClusterName = DefaultCluster
}
clustersClient, err = cs.clustersManager.GetImpersonatedClientForCluster(ctx, auth.Principal(ctx), m.ClusterName)
if err != nil {
return nil, fmt.Errorf("error getting impersonating client: %w", err)
}
policyCR := pacv2beta2.Policy{}
if err := clustersClient.Get(ctx, m.ClusterName, types.NamespacedName{Name: m.PolicyName}, &policyCR); err != nil {
return nil, fmt.Errorf("error while getting policy %s from cluster %s: %w", m.PolicyName, m.ClusterName, err)
}
var policy *pb.PolicyObj
policy, err = policyToPolicyRespone(policyCR, m.ClusterName)
if err != nil {
return nil, err
}
return &pb.GetPolicyResponse{Policy: policy}, nil
}