|
| 1 | +package apiserver |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | + |
| 8 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 9 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 10 | + "k8s.io/client-go/tools/cache" |
| 11 | + |
| 12 | + configv1 "github.com/openshift/api/config/v1" |
| 13 | + configlistersv1 "github.com/openshift/client-go/config/listers/config/v1" |
| 14 | + "github.com/openshift/library-go/pkg/operator/events" |
| 15 | +) |
| 16 | + |
| 17 | +var ( |
| 18 | + auditPolicyFilePath = []string{"apiServerArguments", "audit-policy-file"} |
| 19 | +) |
| 20 | + |
| 21 | +func TestAuditObserver(t *testing.T) { |
| 22 | + tests := []struct { |
| 23 | + name string |
| 24 | + existingConfig map[string]interface{} |
| 25 | + desiredProfile *string |
| 26 | + expectedPath string |
| 27 | + errExpected bool |
| 28 | + }{ |
| 29 | + { |
| 30 | + name: "WithCurrentAndDesiredBothEmpty", |
| 31 | + existingConfig: map[string]interface{}{}, |
| 32 | + desiredProfile: stringPointer(""), |
| 33 | + expectedPath: "", |
| 34 | + }, |
| 35 | + { |
| 36 | + name: "WithCurrentSetAndDesiredEmpty", |
| 37 | + existingConfig: map[string]interface{}{ |
| 38 | + "apiServerArguments": map[string]interface{}{ |
| 39 | + "audit-policy-file": []interface{}{ |
| 40 | + path("AllRequestBodies"), |
| 41 | + }, |
| 42 | + }, |
| 43 | + }, |
| 44 | + desiredProfile: stringPointer(""), |
| 45 | + expectedPath: "", |
| 46 | + }, |
| 47 | + { |
| 48 | + name: "WithCurrentEmpty", |
| 49 | + existingConfig: map[string]interface{}{}, |
| 50 | + desiredProfile: stringPointer("WriteRequestBodies"), |
| 51 | + expectedPath: path("WriteRequestBodies"), |
| 52 | + }, |
| 53 | + { |
| 54 | + name: "WithCurrentAndDesiredAtDifferentValues", |
| 55 | + existingConfig: map[string]interface{}{ |
| 56 | + "apiServerArguments": map[string]interface{}{ |
| 57 | + "audit-policy-file": []interface{}{ |
| 58 | + path("AllRequestBodies"), |
| 59 | + }, |
| 60 | + }, |
| 61 | + }, |
| 62 | + desiredProfile: stringPointer("WriteRequestBodies"), |
| 63 | + expectedPath: path("WriteRequestBodies"), |
| 64 | + }, |
| 65 | + { |
| 66 | + // we expect the function to return just the keys it is responsible for. |
| 67 | + name: "WithOtherKeysDropped", |
| 68 | + existingConfig: map[string]interface{}{ |
| 69 | + "apiServerArguments": map[string]interface{}{ |
| 70 | + "audit-policy-file": []interface{}{ |
| 71 | + path("AllRequestBodies"), |
| 72 | + }, |
| 73 | + }, |
| 74 | + "foo": []interface{}{ |
| 75 | + "should not be returned", |
| 76 | + }, |
| 77 | + }, |
| 78 | + desiredProfile: stringPointer("WriteRequestBodies"), |
| 79 | + expectedPath: path("WriteRequestBodies"), |
| 80 | + }, |
| 81 | + { |
| 82 | + // if the user specifies an invalid audit profile we expect the current config to be set. |
| 83 | + name: "WithCurrentSetAndDesiredInvalid", |
| 84 | + existingConfig: map[string]interface{}{ |
| 85 | + "apiServerArguments": map[string]interface{}{ |
| 86 | + "audit-policy-file": []interface{}{ |
| 87 | + path("AllRequestBodies"), |
| 88 | + }, |
| 89 | + }, |
| 90 | + }, |
| 91 | + desiredProfile: stringPointer("NotExist"), |
| 92 | + expectedPath: path("AllRequestBodies"), |
| 93 | + errExpected: true, |
| 94 | + }, |
| 95 | + { |
| 96 | + name: "WithCurrentEmptyAndDesiredInvalid", |
| 97 | + existingConfig: map[string]interface{}{}, |
| 98 | + desiredProfile: stringPointer("NotExist"), |
| 99 | + expectedPath: "", |
| 100 | + errExpected: true, |
| 101 | + }, |
| 102 | + { |
| 103 | + name: "WithCurrentAndDesiredBothSame", |
| 104 | + existingConfig: map[string]interface{}{ |
| 105 | + "apiServerArguments": map[string]interface{}{ |
| 106 | + "audit-policy-file": []interface{}{ |
| 107 | + path("AllRequestBodies"), |
| 108 | + }, |
| 109 | + }, |
| 110 | + }, |
| 111 | + desiredProfile: stringPointer("AllRequestBodies"), |
| 112 | + expectedPath: path("AllRequestBodies"), |
| 113 | + }, |
| 114 | + { |
| 115 | + name: "WithCurrentSetAndAPIServerResourceMissing", |
| 116 | + existingConfig: map[string]interface{}{ |
| 117 | + "apiServerArguments": map[string]interface{}{ |
| 118 | + "audit-policy-file": []interface{}{ |
| 119 | + path("AllRequestBodies"), |
| 120 | + }, |
| 121 | + }, |
| 122 | + }, |
| 123 | + expectedPath: path("AllRequestBodies"), |
| 124 | + }, |
| 125 | + { |
| 126 | + name: "WithCurrentNotSetAndAPIServerResourceMissing", |
| 127 | + existingConfig: map[string]interface{}{}, |
| 128 | + expectedPath: "", |
| 129 | + }, |
| 130 | + } |
| 131 | + |
| 132 | + for _, test := range tests { |
| 133 | + t.Run(test.name, func(t *testing.T) { |
| 134 | + indexer := cache.NewIndexer(cache.MetaNamespaceKeyFunc, cache.Indexers{}) |
| 135 | + if test.desiredProfile != nil { |
| 136 | + if err := indexer.Add(&configv1.APIServer{ |
| 137 | + ObjectMeta: metav1.ObjectMeta{ |
| 138 | + Name: "cluster", |
| 139 | + }, |
| 140 | + Spec: configv1.APIServerSpec{ |
| 141 | + Audit: configv1.Audit{ |
| 142 | + Profile: configv1.AuditProfileType(*test.desiredProfile), |
| 143 | + }, |
| 144 | + }, |
| 145 | + }); err != nil { |
| 146 | + t.Fatal(err) |
| 147 | + } |
| 148 | + } |
| 149 | + listers := testLister{ |
| 150 | + apiLister: configlistersv1.NewAPIServerLister(indexer), |
| 151 | + } |
| 152 | + |
| 153 | + observer := NewAuditObserver(getter) |
| 154 | + recorder := events.NewInMemoryRecorder(t.Name()) |
| 155 | + for i := 1; i <= 2; i++ { |
| 156 | + gotConfig, errs := observer(listers, recorder, test.existingConfig) |
| 157 | + |
| 158 | + if test.errExpected && len(errs) == 0 { |
| 159 | + t.Errorf("expected errors, got %v", errs) |
| 160 | + } |
| 161 | + if !test.errExpected && len(errs) > 0 { |
| 162 | + t.Errorf("expected no errors, got %v", errs) |
| 163 | + } |
| 164 | + |
| 165 | + gotPath := read(t, gotConfig) |
| 166 | + if test.expectedPath != gotPath { |
| 167 | + t.Errorf("audit path expected=%s got=%s", test.expectedPath, gotPath) |
| 168 | + } |
| 169 | + |
| 170 | + // put the observed config back into existingConfig. |
| 171 | + if err := unstructured.SetNestedStringSlice(test.existingConfig, []string{gotPath}, auditPolicyFilePath...); err != nil { |
| 172 | + t.Errorf("failed to put the observed config into the current conig -%s", err) |
| 173 | + } |
| 174 | + } |
| 175 | + }) |
| 176 | + } |
| 177 | +} |
| 178 | + |
| 179 | +func path(profile string) string { |
| 180 | + return fmt.Sprintf("%s/%s", "/etc/kubernetes/static-pod-resources/configmaps/kube-apiserver-audit-policies", |
| 181 | + strings.ToLower(profile)) |
| 182 | +} |
| 183 | + |
| 184 | +func getter(profile string) (string, error) { |
| 185 | + if profile == "NotExist" { |
| 186 | + return "", fmt.Errorf("invalid profile - name=%s", profile) |
| 187 | + } |
| 188 | + |
| 189 | + path := path(profile) |
| 190 | + return path, nil |
| 191 | +} |
| 192 | + |
| 193 | +func stringPointer(s string) *string { |
| 194 | + p := &s |
| 195 | + return p |
| 196 | +} |
| 197 | + |
| 198 | +func read(t *testing.T, config map[string]interface{}) string { |
| 199 | + // we expect only one key returned in the observed config. |
| 200 | + if len(config) > 1 { |
| 201 | + t.Fatal("expected observed config to have a single key 'apiServerArguments'") |
| 202 | + } |
| 203 | + |
| 204 | + current, found, err := unstructured.NestedStringSlice(config, auditPolicyFilePath...) |
| 205 | + if err != nil { |
| 206 | + t.Fatal(err) |
| 207 | + } |
| 208 | + |
| 209 | + if !found { |
| 210 | + return "" |
| 211 | + } |
| 212 | + |
| 213 | + if len(current) != 1 { |
| 214 | + t.Fatal("expected config to have only audit policy path defined") |
| 215 | + } |
| 216 | + |
| 217 | + return current[0] |
| 218 | +} |
0 commit comments