-
Notifications
You must be signed in to change notification settings - Fork 23
/
policies.go
236 lines (206 loc) · 6.63 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
package utils
import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
"strings"
tea "github.com/charmbracelet/bubbletea"
"github.com/ghodss/yaml"
"google.golang.org/protobuf/types/known/emptypb"
policylangv1 "github.com/fluxninja/aperture/api/v2/gen/proto/go/aperture/policy/language/v1"
"github.com/fluxninja/aperture/v2/cmd/aperturectl/cmd/tui"
policyv1alpha1 "github.com/fluxninja/aperture/v2/operator/api/policy/v1alpha1"
"github.com/fluxninja/aperture/v2/pkg/log"
)
// GetPoliciesTUIModel prepares the TUI model for selecting policies to apply from the given directory path.
func GetPoliciesTUIModel(policyDir string, selectAll bool) ([]string, *tui.CheckBoxModel, error) {
policies, err := GetPolicies(policyDir)
if err != nil {
return nil, nil, err
}
if len(policies) == 0 {
return nil, nil, errors.New("no policies found in the directory")
}
model := tui.InitialCheckboxModel(policies, "Which policies to apply?")
if !selectAll {
p := tea.NewProgram(model)
if _, err := p.Run(); err != nil {
return nil, nil, err
}
} else {
for i := range policies {
model.Selected[i] = struct{}{}
}
}
return policies, model, nil
}
// GetPolicies returns path of valid files having a valid Aperture Policy .
func GetPolicies(policyDir string) ([]string, error) {
policies := []string{}
policyMap := map[string]string{}
// walk the directory and apply all policies
return policies, filepath.Walk(policyDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if filepath.Ext(info.Name()) == ".yaml" || filepath.Ext(info.Name()) == ".yml" {
_, policyName, err := GetPolicy(path)
if err != nil {
log.Info().Str("file", path).Msg("Invalid policy found. Skipping...")
return nil
}
if _, ok := policyMap[policyName]; !ok {
policyMap[policyName] = path
policies = append(policies, path)
} else {
log.Info().Str("policy", policyName).Msg("Duplicate policy found. Skipping...")
}
}
return nil
})
}
// GetPolicy returns the policy from the policy file.
func GetPolicy(policyFile string) ([]byte, string, error) {
policyFileBase := filepath.Base(policyFile)
policyName := policyFileBase[:len(policyFileBase)-len(filepath.Ext(policyFileBase))]
var err error
policyBytes, err := os.ReadFile(policyFile)
if err != nil {
return nil, policyName, err
}
var policyCR *policyv1alpha1.Policy
policyCR, err = GetPolicyCR(policyBytes)
if err == nil {
policyBytes = policyCR.Spec.Raw
policyName = policyCR.Name
}
return policyBytes, policyName, nil
}
// GetPolicyCR returns the policy CR from the policy bytes.
func GetPolicyCR(policyBytes []byte) (*policyv1alpha1.Policy, error) {
policyCR := &policyv1alpha1.Policy{}
err := yaml.Unmarshal(policyBytes, policyCR)
if err != nil {
return nil, err
}
if policyCR.Name == "" {
return nil, fmt.Errorf("policy name is missing in the policy file")
}
return policyCR, nil
}
// UpdatePolicyUsingAPI updates the policy using the API.
func UpdatePolicyUsingAPI(client PolicyClient, listClient SelfHostedPolicyClient, name string, policyBytes []byte, force bool) error {
request := policylangv1.UpsertPolicyRequest{
PolicyName: name,
PolicyString: string(policyBytes),
}
if !force {
// If directly using controller API, we can call GetPolicies to
// verify that we're not accidentally overwriting the policy.
// Cloud API doesn't have this method and we always allow
// overwriting, even with force=false.
existingPolicies, err := listClient.ListPolicies(context.Background(), new(emptypb.Empty))
needsConfirmation := false
if err != nil {
return err
}
for policyName, policy := range existingPolicies.GetPolicies().GetPolicies() {
if policyName == name && policy.Status != policylangv1.GetPolicyResponse_STALE {
needsConfirmation = true
break
}
}
if needsConfirmation {
update, err := CheckForUpdate(name, force)
if err != nil {
return fmt.Errorf("failed to check for update: %w", err)
}
if !update {
log.Info().Str("policy", name).Str("namespace", controllerNs).Msg("Skipping update of Policy")
return errors.New("policy already exists")
}
}
}
_, err := client.UpsertPolicy(context.Background(), &request)
if err != nil {
return err
}
return nil
}
// CheckForUpdate checks if the user wants to update the policy.
func CheckForUpdate(name string, force bool) (bool, error) {
if force {
return true, nil
}
model := tui.InitialRadioButtonModel([]string{"Yes", "No"}, fmt.Sprintf("Policy '%s' already exists. Do you want to update it?", name))
p := tea.NewProgram(model)
if _, err := p.Run(); err != nil {
return false, err
}
return *model.Selected == 0, nil
}
// DeletePolicyUsingAPI deletes the policy using the API.
func DeletePolicyUsingAPI(client PolicyClient, policyName string) error {
policyRequest := policylangv1.DeletePolicyRequest{
Name: policyName,
}
_, err := client.DeletePolicy(context.Background(), &policyRequest)
if err != nil {
return fmt.Errorf("failed to delete policy '%s' using API: %w", policyName, err)
}
return nil
}
// ListPolicies lists the policies using the API.
func ListPolicies(client SelfHostedPolicyClient) error {
policies, err := client.ListPolicies(context.Background(), &emptypb.Empty{})
if err != nil {
return err
}
for name, body := range policies.GetPolicies().Policies {
fmt.Printf("%v:\n", name)
if body.GetStatus() != policylangv1.GetPolicyResponse_VALID {
fmt.Println("\tStatus:", body.GetStatus())
reason := strings.ReplaceAll(body.GetReason(), "\n", "\n\n\t\t")
reason = strings.ReplaceAll(reason, " Error", "\n\t\tError")
fmt.Printf("\tReason: %s\n", reason)
fmt.Println("\t\t---")
}
// Note: We try to print policy details also if status is not
// VALID, because statuses like like OUTDATED or STALE can contain
// policy details.
resources := body.GetPolicy().GetResources()
if ims := resources.GetInfraMeters(); len(ims) > 0 {
fmt.Println("\tInfra Meters:")
for im := range ims {
fmt.Printf("\t\t%v\n", im)
}
}
if fms := resources.GetFlowControl().GetFluxMeters(); len(fms) > 0 {
fmt.Println("\tFlux Meters:")
for fm := range fms {
fmt.Printf("\t\t%v\n", fm)
}
}
if cs := resources.GetFlowControl().GetClassifiers(); len(cs) > 0 {
fmt.Println("\tClassifiers:")
for _, c := range body.Policy.Resources.FlowControl.Classifiers {
if len(c.Selectors) > 0 {
fmt.Println("\t\tSelectors:")
for _, s := range c.Selectors {
fmt.Printf("\t\t\t%v\n", s)
}
}
if len(c.Rules) > 0 {
fmt.Println("\t\tRules:")
for r := range c.Rules {
fmt.Printf("\t\t\t%v\n", r)
}
}
}
fmt.Println("\t\t---")
}
}
return nil
}