-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
359 lines (322 loc) · 10 KB
/
main.go
File metadata and controls
359 lines (322 loc) · 10 KB
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
package main
import (
"context"
"errors"
"fmt"
"maps"
"slices"
"strings"
"github.com/compliance-framework/plugin-k8s/auth"
"github.com/compliance-framework/plugin-k8s/auth/eks"
"github.com/compliance-framework/plugin-k8s/auth/kubeconfig"
policyManager "github.com/compliance-framework/agent/policy-manager"
"github.com/compliance-framework/agent/runner"
"github.com/compliance-framework/agent/runner/proto"
"github.com/hashicorp/go-hclog"
goplugin "github.com/hashicorp/go-plugin"
"github.com/mitchellh/mapstructure"
)
const (
schemaVersionV1 = "v1"
sourcePluginK8s = "plugin-kubernetes"
)
// PolicyEvaluator wraps OPA policy execution so eval loop behavior can be tested with mocks.
type PolicyEvaluator interface {
Generate(
ctx context.Context,
policyPath string,
labels map[string]string,
subjects []*proto.Subject,
components []*proto.Component,
inventory []*proto.InventoryItem,
actors []*proto.OriginActor,
activities []*proto.Activity,
data interface{},
) ([]*proto.Evidence, error)
}
// DefaultPolicyEvaluator uses the agent SDK policy processor.
type DefaultPolicyEvaluator struct {
Logger hclog.Logger
}
func (e *DefaultPolicyEvaluator) Generate(
ctx context.Context,
policyPath string,
labels map[string]string,
subjects []*proto.Subject,
components []*proto.Component,
inventory []*proto.InventoryItem,
actors []*proto.OriginActor,
activities []*proto.Activity,
data interface{},
) ([]*proto.Evidence, error) {
e.Logger.Debug("Evaluating OPA policy", "policy_path", policyPath)
processor := policyManager.NewPolicyProcessor(
e.Logger,
labels,
subjects,
components,
inventory,
actors,
activities,
)
e.Logger.Debug("policy data", "type", fmt.Sprintf("%T", data), "data", data)
return processor.GenerateResults(ctx, policyPath, data)
}
// Plugin is the CCF plugin for Kubernetes cluster data collection and policy evaluation.
type Plugin struct {
Logger hclog.Logger
config *PluginConfig
parsedConfig *ParsedConfig
collector ClusterCollector
evaluator PolicyEvaluator
}
func (p *Plugin) Configure(req *proto.ConfigureRequest) (*proto.ConfigureResponse, error) {
p.Logger.Debug("Received raw plugin configuration", "config_keys", sortedKeys(req.Config))
config := &PluginConfig{}
if err := mapstructure.Decode(req.Config, config); err != nil {
p.Logger.Error("Error decoding config", "error", err)
return nil, err
}
parsed, err := config.Parse()
if err != nil {
p.Logger.Error("Error parsing config", "error", err)
return nil, err
}
p.config = config
p.parsedConfig = parsed
if p.collector == nil {
p.collector = &DynamicClusterCollector{
AuthProvider: &auth.RegistryAuthProvider{
Providers: map[string]auth.AuthProvider{
auth.ProviderEKS: &eks.Provider{},
auth.ProviderKubeconfig: &kubeconfig.Provider{},
},
},
}
}
if p.evaluator == nil {
p.evaluator = &DefaultPolicyEvaluator{Logger: p.Logger.Named("policy-evaluator")}
}
p.Logger.Info("Kubernetes Plugin configured",
"clusters", len(parsed.Clusters),
"resources", parsed.Resources,
)
return &proto.ConfigureResponse{}, nil
}
func (p *Plugin) Eval(req *proto.EvalRequest, apiHelper runner.ApiHelper) (*proto.EvalResponse, error) {
ctx := context.Background()
if p.parsedConfig == nil {
return &proto.EvalResponse{Status: proto.ExecutionStatus_FAILURE}, errors.New("plugin not configured")
}
if len(req.GetPolicyPaths()) == 0 {
return &proto.EvalResponse{Status: proto.ExecutionStatus_FAILURE}, errors.New("no policy paths provided")
}
// Collect resources from all clusters concurrently.
clusterData, err := CollectAll(
ctx,
p.collector,
p.parsedConfig.Clusters,
p.parsedConfig.Resources,
p.parsedConfig.NamespaceInclude,
p.parsedConfig.NamespaceExclude,
)
if err != nil {
p.Logger.Error("Collection failed", "error", err)
return &proto.EvalResponse{Status: proto.ExecutionStatus_FAILURE}, err
}
// Build the Rego input document.
regoInput := buildRegoInput(clusterData, p.parsedConfig.PolicyInput)
// Build evidence metadata.
labels := map[string]string{}
maps.Copy(labels, p.parsedConfig.PolicyLabels)
labels["source"] = sourcePluginK8s
labels["tool"] = sourcePluginK8s
if _, exists := labels["provider"]; !exists {
labels["provider"] = inferProvider(p.parsedConfig.Clusters)
}
actors := []*proto.OriginActor{
{
Title: "The Continuous Compliance Framework",
Type: "assessment-platform",
Links: []*proto.Link{
{
Href: "https://compliance-framework.github.io/docs/",
Rel: policyManager.Pointer("reference"),
Text: policyManager.Pointer("The Continuous Compliance Framework"),
},
},
},
{
Title: "Continuous Compliance Framework - Kubernetes Plugin",
Type: "tool",
Links: []*proto.Link{
{
Href: "https://github.com/compliance-framework/plugin-kubernetes",
Rel: policyManager.Pointer("reference"),
Text: policyManager.Pointer("The Continuous Compliance Framework Kubernetes Plugin"),
},
},
},
}
var clusterComponents []*proto.Component
var clusterInventory []*proto.InventoryItem
var subjects []*proto.Subject
for _, cl := range p.parsedConfig.Clusters {
clusterID := fmt.Sprintf("k8s-cluster/%s", sanitizeIdentifier(cl.Name))
clusterComponents = append(clusterComponents, &proto.Component{
Identifier: clusterID,
Type: "service",
Title: fmt.Sprintf("Kubernetes Cluster: %s", cl.Name),
Description: fmt.Sprintf("Kubernetes cluster %q in region %s", cl.ClusterName, cl.Region),
Purpose: "Kubernetes cluster providing resource data for compliance evaluation.",
})
clusterInventory = append(clusterInventory, &proto.InventoryItem{
Identifier: clusterID,
Type: "k8s-cluster",
Title: fmt.Sprintf("Kubernetes Cluster %s", cl.Name),
ImplementedComponents: []*proto.InventoryItemImplementedComponent{
{Identifier: clusterID},
},
})
subjects = append(subjects, &proto.Subject{
Type: proto.SubjectType_SUBJECT_TYPE_COMPONENT,
Identifier: clusterID,
})
}
activities := []*proto.Activity{
{
Title: "Collect Kubernetes Cluster Resources",
Steps: []*proto.Step{
{Title: "Authenticate", Description: "Authenticate to each Kubernetes cluster using the configured provider."},
{Title: "Discover Resources", Description: "Resolve resource names to GVRs via the Kubernetes discovery API."},
{Title: "List Resources", Description: "List resources using the Kubernetes dynamic client with namespace filtering."},
},
},
{
Title: "Evaluate OPA Policy Bundles",
Steps: []*proto.Step{
{Title: "Build Rego Input", Description: "Combine cluster data with user-provided policy input."},
{Title: "Evaluate Policies", Description: "Run policy bundles against the combined Rego input document."},
},
},
}
// Evaluate each policy path.
allEvidences := make([]*proto.Evidence, 0)
var accumulatedErrors error
successfulRuns := 0
for _, policyPath := range req.GetPolicyPaths() {
evidences, evalErr := p.evaluator.Generate(
ctx,
policyPath,
labels,
subjects,
clusterComponents,
clusterInventory,
actors,
activities,
regoInput,
)
allEvidences = append(allEvidences, evidences...)
if evalErr != nil {
p.Logger.Warn("Policy evaluation failed", "policy_path", policyPath, "error", evalErr)
accumulatedErrors = errors.Join(accumulatedErrors, fmt.Errorf("policy %s: %w", policyPath, evalErr))
continue
}
successfulRuns++
}
if len(allEvidences) > 0 {
if err := apiHelper.CreateEvidence(ctx, allEvidences); err != nil {
p.Logger.Error("Error creating evidence", "error", err)
return &proto.EvalResponse{Status: proto.ExecutionStatus_FAILURE}, err
}
}
if successfulRuns == 0 && len(allEvidences) == 0 {
if accumulatedErrors == nil {
accumulatedErrors = errors.New("policy evaluation failed for all paths")
}
return &proto.EvalResponse{Status: proto.ExecutionStatus_FAILURE}, accumulatedErrors
}
return &proto.EvalResponse{Status: proto.ExecutionStatus_SUCCESS}, nil
}
// buildRegoInput constructs the Rego input document from cluster data and user-provided policy input.
func buildRegoInput(clusters map[string]*ClusterResources, policyInput map[string]interface{}) map[string]interface{} {
input := map[string]interface{}{
"schema_version": schemaVersionV1,
"source": sourcePluginK8s,
"clusters": clusters,
}
// Merge user-provided policy_input keys (reserved keys are already rejected at config time).
for k, v := range policyInput {
input[k] = v
}
return input
}
// inferProvider returns the provider label based on cluster configs.
// If all clusters use the same provider, return that. Otherwise "multi".
func inferProvider(clusters []auth.ClusterConfig) string {
if len(clusters) == 0 {
return "kubernetes"
}
first := clusters[0].EffectiveProvider()
for _, cl := range clusters[1:] {
if cl.EffectiveProvider() != first {
return "multi"
}
}
switch first {
case auth.ProviderEKS:
return "aws"
case auth.ProviderKubeconfig:
return "kubernetes"
default:
return first
}
}
func sanitizeIdentifier(in string) string {
trimmed := strings.TrimSpace(strings.ToLower(in))
if trimmed == "" {
return "unknown"
}
builder := strings.Builder{}
prevDash := false
for _, r := range trimmed {
isAlphaNum := (r >= 'a' && r <= 'z') || (r >= '0' && r <= '9')
if isAlphaNum {
builder.WriteRune(r)
prevDash = false
continue
}
if !prevDash {
builder.WriteRune('-')
prevDash = true
}
}
out := strings.Trim(builder.String(), "-")
if out == "" {
return "unknown"
}
return out
}
func sortedKeys(input map[string]string) []string {
keys := make([]string, 0, len(input))
for k := range input {
keys = append(keys, k)
}
slices.Sort(keys)
return keys
}
func main() {
logger := hclog.New(&hclog.LoggerOptions{
Level: hclog.Trace,
JSONFormat: true,
})
plugin := &Plugin{Logger: logger}
logger.Info("Starting Kubernetes Plugin")
goplugin.Serve(&goplugin.ServeConfig{
HandshakeConfig: runner.HandshakeConfig,
Plugins: map[string]goplugin.Plugin{
"runner": &runner.RunnerGRPCPlugin{Impl: plugin},
},
GRPCServer: goplugin.DefaultGRPCServer,
})
}