audit_test.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. /*
  2. Copyright 2018 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package master
  14. import (
  15. "context"
  16. "encoding/json"
  17. "fmt"
  18. "io/ioutil"
  19. "net/http"
  20. "os"
  21. "strings"
  22. "testing"
  23. "time"
  24. "k8s.io/api/admission/v1beta1"
  25. admissionv1beta1 "k8s.io/api/admissionregistration/v1beta1"
  26. apiv1 "k8s.io/api/core/v1"
  27. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  28. "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
  29. "k8s.io/apimachinery/pkg/runtime/schema"
  30. "k8s.io/apimachinery/pkg/types"
  31. "k8s.io/apimachinery/pkg/util/wait"
  32. "k8s.io/apiserver/pkg/admission/plugin/webhook/mutating"
  33. auditinternal "k8s.io/apiserver/pkg/apis/audit"
  34. auditv1 "k8s.io/apiserver/pkg/apis/audit/v1"
  35. auditv1beta1 "k8s.io/apiserver/pkg/apis/audit/v1beta1"
  36. "k8s.io/client-go/kubernetes"
  37. clientset "k8s.io/client-go/kubernetes"
  38. kubeapiservertesting "k8s.io/kubernetes/cmd/kube-apiserver/app/testing"
  39. "k8s.io/kubernetes/test/integration/framework"
  40. "k8s.io/kubernetes/test/utils"
  41. jsonpatch "github.com/evanphx/json-patch"
  42. )
  43. const (
  44. testWebhookConfigurationName = "auditmutation.integration.test"
  45. testWebhookName = "auditmutation.integration.test"
  46. )
  47. var (
  48. auditPolicyPattern = `
  49. apiVersion: {version}
  50. kind: Policy
  51. rules:
  52. - level: {level}
  53. resources:
  54. - group: "" # core
  55. resources: ["configmaps"]
  56. `
  57. namespace = "default"
  58. watchTestTimeout int64 = 1
  59. watchOptions = metav1.ListOptions{TimeoutSeconds: &watchTestTimeout}
  60. patch, _ = json.Marshal(jsonpatch.Patch{})
  61. auditTestUser = "system:apiserver"
  62. versions = map[string]schema.GroupVersion{
  63. "audit.k8s.io/v1": auditv1.SchemeGroupVersion,
  64. "audit.k8s.io/v1beta1": auditv1beta1.SchemeGroupVersion,
  65. }
  66. expectedEvents = []utils.AuditEvent{
  67. {
  68. Level: auditinternal.LevelRequestResponse,
  69. Stage: auditinternal.StageResponseComplete,
  70. RequestURI: fmt.Sprintf("/api/v1/namespaces/%s/configmaps", namespace),
  71. Verb: "create",
  72. Code: 201,
  73. User: auditTestUser,
  74. Resource: "configmaps",
  75. Namespace: namespace,
  76. RequestObject: true,
  77. ResponseObject: true,
  78. AuthorizeDecision: "allow",
  79. }, {
  80. Level: auditinternal.LevelRequestResponse,
  81. Stage: auditinternal.StageResponseComplete,
  82. RequestURI: fmt.Sprintf("/api/v1/namespaces/%s/configmaps/audit-configmap", namespace),
  83. Verb: "get",
  84. Code: 200,
  85. User: auditTestUser,
  86. Resource: "configmaps",
  87. Namespace: namespace,
  88. RequestObject: false,
  89. ResponseObject: true,
  90. AuthorizeDecision: "allow",
  91. }, {
  92. Level: auditinternal.LevelRequestResponse,
  93. Stage: auditinternal.StageResponseComplete,
  94. RequestURI: fmt.Sprintf("/api/v1/namespaces/%s/configmaps", namespace),
  95. Verb: "list",
  96. Code: 200,
  97. User: auditTestUser,
  98. Resource: "configmaps",
  99. Namespace: namespace,
  100. RequestObject: false,
  101. ResponseObject: true,
  102. AuthorizeDecision: "allow",
  103. }, {
  104. Level: auditinternal.LevelRequestResponse,
  105. Stage: auditinternal.StageResponseStarted,
  106. RequestURI: fmt.Sprintf("/api/v1/namespaces/%s/configmaps?timeout=%ds&timeoutSeconds=%d&watch=true", namespace, watchTestTimeout, watchTestTimeout),
  107. Verb: "watch",
  108. Code: 200,
  109. User: auditTestUser,
  110. Resource: "configmaps",
  111. Namespace: namespace,
  112. RequestObject: false,
  113. ResponseObject: false,
  114. AuthorizeDecision: "allow",
  115. }, {
  116. Level: auditinternal.LevelRequestResponse,
  117. Stage: auditinternal.StageResponseComplete,
  118. RequestURI: fmt.Sprintf("/api/v1/namespaces/%s/configmaps?timeout=%ds&timeoutSeconds=%d&watch=true", namespace, watchTestTimeout, watchTestTimeout),
  119. Verb: "watch",
  120. Code: 200,
  121. User: auditTestUser,
  122. Resource: "configmaps",
  123. Namespace: namespace,
  124. RequestObject: false,
  125. ResponseObject: false,
  126. AuthorizeDecision: "allow",
  127. }, {
  128. Level: auditinternal.LevelRequestResponse,
  129. Stage: auditinternal.StageResponseComplete,
  130. RequestURI: fmt.Sprintf("/api/v1/namespaces/%s/configmaps/audit-configmap", namespace),
  131. Verb: "update",
  132. Code: 200,
  133. User: auditTestUser,
  134. Resource: "configmaps",
  135. Namespace: namespace,
  136. RequestObject: true,
  137. ResponseObject: true,
  138. AuthorizeDecision: "allow",
  139. }, {
  140. Level: auditinternal.LevelRequestResponse,
  141. Stage: auditinternal.StageResponseComplete,
  142. RequestURI: fmt.Sprintf("/api/v1/namespaces/%s/configmaps/audit-configmap", namespace),
  143. Verb: "patch",
  144. Code: 200,
  145. User: auditTestUser,
  146. Resource: "configmaps",
  147. Namespace: namespace,
  148. RequestObject: true,
  149. ResponseObject: true,
  150. AuthorizeDecision: "allow",
  151. }, {
  152. Level: auditinternal.LevelRequestResponse,
  153. Stage: auditinternal.StageResponseComplete,
  154. RequestURI: fmt.Sprintf("/api/v1/namespaces/%s/configmaps/audit-configmap", namespace),
  155. Verb: "delete",
  156. Code: 200,
  157. User: auditTestUser,
  158. Resource: "configmaps",
  159. Namespace: namespace,
  160. RequestObject: true,
  161. ResponseObject: true,
  162. AuthorizeDecision: "allow",
  163. },
  164. }
  165. )
  166. // TestAudit ensures that both v1beta1 and v1 version audit api could work.
  167. func TestAudit(t *testing.T) {
  168. tcs := []struct {
  169. auditLevel auditinternal.Level
  170. enableMutatingWebhook bool
  171. }{
  172. {
  173. auditLevel: auditinternal.LevelRequestResponse,
  174. enableMutatingWebhook: false,
  175. },
  176. {
  177. auditLevel: auditinternal.LevelMetadata,
  178. enableMutatingWebhook: true,
  179. },
  180. {
  181. auditLevel: auditinternal.LevelRequest,
  182. enableMutatingWebhook: true,
  183. },
  184. {
  185. auditLevel: auditinternal.LevelRequestResponse,
  186. enableMutatingWebhook: true,
  187. },
  188. }
  189. for version := range versions {
  190. for _, tc := range tcs {
  191. t.Run(fmt.Sprintf("%s.%s.%t", version, tc.auditLevel, tc.enableMutatingWebhook), func(t *testing.T) {
  192. testAudit(t, version, tc.auditLevel, tc.enableMutatingWebhook)
  193. })
  194. }
  195. }
  196. }
  197. func testAudit(t *testing.T, version string, level auditinternal.Level, enableMutatingWebhook bool) {
  198. var url string
  199. var err error
  200. closeFunc := func() {}
  201. if enableMutatingWebhook {
  202. webhookMux := http.NewServeMux()
  203. webhookMux.Handle("/mutation", utils.AdmissionWebhookHandler(t, admitFunc))
  204. url, closeFunc, err = utils.NewAdmissionWebhookServer(webhookMux)
  205. }
  206. defer closeFunc()
  207. if err != nil {
  208. t.Fatalf("%v", err)
  209. }
  210. // prepare audit policy file
  211. auditPolicy := strings.Replace(auditPolicyPattern, "{version}", version, 1)
  212. auditPolicy = strings.Replace(auditPolicy, "{level}", string(level), 1)
  213. policyFile, err := ioutil.TempFile("", "audit-policy.yaml")
  214. if err != nil {
  215. t.Fatalf("Failed to create audit policy file: %v", err)
  216. }
  217. defer os.Remove(policyFile.Name())
  218. if _, err := policyFile.Write([]byte(auditPolicy)); err != nil {
  219. t.Fatalf("Failed to write audit policy file: %v", err)
  220. }
  221. if err := policyFile.Close(); err != nil {
  222. t.Fatalf("Failed to close audit policy file: %v", err)
  223. }
  224. // prepare audit log file
  225. logFile, err := ioutil.TempFile("", "audit.log")
  226. if err != nil {
  227. t.Fatalf("Failed to create audit log file: %v", err)
  228. }
  229. defer os.Remove(logFile.Name())
  230. // start api server
  231. result := kubeapiservertesting.StartTestServerOrDie(t, nil,
  232. []string{
  233. "--audit-policy-file", policyFile.Name(),
  234. "--audit-log-version", version,
  235. "--audit-log-mode", "blocking",
  236. "--audit-log-path", logFile.Name()},
  237. framework.SharedEtcd())
  238. defer result.TearDownFn()
  239. kubeclient, err := kubernetes.NewForConfig(result.ClientConfig)
  240. if err != nil {
  241. t.Fatalf("Unexpected error: %v", err)
  242. }
  243. if enableMutatingWebhook {
  244. if err := createV1beta1MutationWebhook(kubeclient, url+"/mutation"); err != nil {
  245. t.Fatal(err)
  246. }
  247. }
  248. var lastMissingReport string
  249. if err := wait.Poll(500*time.Millisecond, wait.ForeverTestTimeout, func() (bool, error) {
  250. // perform configmap operations
  251. configMapOperations(t, kubeclient)
  252. // check for corresponding audit logs
  253. stream, err := os.Open(logFile.Name())
  254. if err != nil {
  255. return false, fmt.Errorf("unexpected error: %v", err)
  256. }
  257. defer stream.Close()
  258. missingReport, err := utils.CheckAuditLines(stream, getExpectedEvents(level, enableMutatingWebhook), versions[version])
  259. if err != nil {
  260. return false, fmt.Errorf("unexpected error: %v", err)
  261. }
  262. if len(missingReport.MissingEvents) > 0 {
  263. lastMissingReport = missingReport.String()
  264. return false, nil
  265. }
  266. return true, nil
  267. }); err != nil {
  268. t.Fatalf("failed to get expected events -- missingReport: %s, error: %v", lastMissingReport, err)
  269. }
  270. }
  271. func getExpectedEvents(level auditinternal.Level, enableMutatingWebhook bool) []utils.AuditEvent {
  272. if !enableMutatingWebhook {
  273. return expectedEvents
  274. }
  275. var webhookMutationAnnotations, webhookPatchAnnotations map[string]string
  276. var requestObject, responseObject bool
  277. if level.GreaterOrEqual(auditinternal.LevelMetadata) {
  278. // expect mutation existence annotation
  279. webhookMutationAnnotations = map[string]string{}
  280. webhookMutationAnnotations[mutating.MutationAuditAnnotationPrefix+"round_0_index_0"] = fmt.Sprintf(`{"configuration":"%s","webhook":"%s","mutated":%t}`, testWebhookConfigurationName, testWebhookName, true)
  281. }
  282. if level.GreaterOrEqual(auditinternal.LevelRequest) {
  283. // expect actual patch annotation
  284. webhookPatchAnnotations = map[string]string{}
  285. webhookPatchAnnotations[mutating.PatchAuditAnnotationPrefix+"round_0_index_0"] = strings.Replace(fmt.Sprintf(`{"configuration": "%s", "webhook": "%s", "patch": %s, "patchType": "JSONPatch"}`, testWebhookConfigurationName, testWebhookName, `[{"op":"add","path":"/data","value":{"test":"dummy"}}]`), " ", "", -1)
  286. // expect request object in audit log
  287. requestObject = true
  288. }
  289. if level.GreaterOrEqual(auditinternal.LevelRequestResponse) {
  290. // expect response obect in audit log
  291. responseObject = true
  292. }
  293. return []utils.AuditEvent{
  294. {
  295. // expect CREATE audit event with webhook in effect
  296. Level: level,
  297. Stage: auditinternal.StageResponseComplete,
  298. RequestURI: fmt.Sprintf("/api/v1/namespaces/%s/configmaps", namespace),
  299. Verb: "create",
  300. Code: 201,
  301. User: auditTestUser,
  302. Resource: "configmaps",
  303. Namespace: namespace,
  304. AuthorizeDecision: "allow",
  305. RequestObject: requestObject,
  306. ResponseObject: responseObject,
  307. AdmissionWebhookMutationAnnotations: webhookMutationAnnotations,
  308. AdmissionWebhookPatchAnnotations: webhookPatchAnnotations,
  309. }, {
  310. // expect UPDATE audit event with webhook in effect
  311. Level: level,
  312. Stage: auditinternal.StageResponseComplete,
  313. RequestURI: fmt.Sprintf("/api/v1/namespaces/%s/configmaps/audit-configmap", namespace),
  314. Verb: "update",
  315. Code: 200,
  316. User: auditTestUser,
  317. Resource: "configmaps",
  318. Namespace: namespace,
  319. AuthorizeDecision: "allow",
  320. RequestObject: requestObject,
  321. ResponseObject: responseObject,
  322. AdmissionWebhookMutationAnnotations: webhookMutationAnnotations,
  323. AdmissionWebhookPatchAnnotations: webhookPatchAnnotations,
  324. },
  325. }
  326. }
  327. // configMapOperations is a set of known operations performed on the configmap type
  328. // which correspond to the expected events.
  329. // This is shared by the dynamic test
  330. func configMapOperations(t *testing.T, kubeclient kubernetes.Interface) {
  331. // create, get, watch, update, patch, list and delete configmap.
  332. configMap := &apiv1.ConfigMap{
  333. ObjectMeta: metav1.ObjectMeta{
  334. Name: "audit-configmap",
  335. },
  336. Data: map[string]string{
  337. "map-key": "map-value",
  338. },
  339. }
  340. _, err := kubeclient.CoreV1().ConfigMaps(namespace).Create(context.TODO(), configMap, metav1.CreateOptions{})
  341. expectNoError(t, err, "failed to create audit-configmap")
  342. _, err = kubeclient.CoreV1().ConfigMaps(namespace).Get(context.TODO(), configMap.Name, metav1.GetOptions{})
  343. expectNoError(t, err, "failed to get audit-configmap")
  344. configMapChan, err := kubeclient.CoreV1().ConfigMaps(namespace).Watch(context.TODO(), watchOptions)
  345. expectNoError(t, err, "failed to create watch for config maps")
  346. for range configMapChan.ResultChan() {
  347. // Block until watchOptions.TimeoutSeconds expires.
  348. // If the test finishes before watchOptions.TimeoutSeconds expires, the watch audit
  349. // event at stage ResponseComplete will not be generated.
  350. }
  351. _, err = kubeclient.CoreV1().ConfigMaps(namespace).Update(context.TODO(), configMap, metav1.UpdateOptions{})
  352. expectNoError(t, err, "failed to update audit-configmap")
  353. _, err = kubeclient.CoreV1().ConfigMaps(namespace).Patch(context.TODO(), configMap.Name, types.JSONPatchType, patch, metav1.PatchOptions{})
  354. expectNoError(t, err, "failed to patch configmap")
  355. _, err = kubeclient.CoreV1().ConfigMaps(namespace).List(context.TODO(), metav1.ListOptions{})
  356. expectNoError(t, err, "failed to list config maps")
  357. err = kubeclient.CoreV1().ConfigMaps(namespace).Delete(context.TODO(), configMap.Name, &metav1.DeleteOptions{})
  358. expectNoError(t, err, "failed to delete audit-configmap")
  359. }
  360. func expectNoError(t *testing.T, err error, msg string) {
  361. if err != nil {
  362. t.Fatalf("%s: %v", msg, err)
  363. }
  364. }
  365. func admitFunc(review *v1beta1.AdmissionReview) error {
  366. gvk := schema.GroupVersionKind{Group: "admission.k8s.io", Version: "v1beta1", Kind: "AdmissionReview"}
  367. if review.GetObjectKind().GroupVersionKind() != gvk {
  368. return fmt.Errorf("invalid admission review kind: %#v", review.GetObjectKind().GroupVersionKind())
  369. }
  370. if len(review.Request.Object.Raw) > 0 {
  371. u := &unstructured.Unstructured{Object: map[string]interface{}{}}
  372. if err := json.Unmarshal(review.Request.Object.Raw, u); err != nil {
  373. return fmt.Errorf("failed to deserialize object: %s with error: %v", string(review.Request.Object.Raw), err)
  374. }
  375. review.Request.Object.Object = u
  376. }
  377. if len(review.Request.OldObject.Raw) > 0 {
  378. u := &unstructured.Unstructured{Object: map[string]interface{}{}}
  379. if err := json.Unmarshal(review.Request.OldObject.Raw, u); err != nil {
  380. return fmt.Errorf("failed to deserialize object: %s with error: %v", string(review.Request.OldObject.Raw), err)
  381. }
  382. review.Request.OldObject.Object = u
  383. }
  384. review.Response = &v1beta1.AdmissionResponse{
  385. Allowed: true,
  386. UID: review.Request.UID,
  387. Result: &metav1.Status{Message: "admitted"},
  388. }
  389. review.Response.Patch = []byte(`[{"op":"add","path":"/data","value":{"test":"dummy"}}]`)
  390. jsonPatch := v1beta1.PatchTypeJSONPatch
  391. review.Response.PatchType = &jsonPatch
  392. return nil
  393. }
  394. func createV1beta1MutationWebhook(client clientset.Interface, endpoint string) error {
  395. fail := admissionv1beta1.Fail
  396. // Attaching Mutation webhook to API server
  397. _, err := client.AdmissionregistrationV1beta1().MutatingWebhookConfigurations().Create(context.TODO(), &admissionv1beta1.MutatingWebhookConfiguration{
  398. ObjectMeta: metav1.ObjectMeta{Name: testWebhookConfigurationName},
  399. Webhooks: []admissionv1beta1.MutatingWebhook{{
  400. Name: testWebhookName,
  401. ClientConfig: admissionv1beta1.WebhookClientConfig{
  402. URL: &endpoint,
  403. CABundle: utils.LocalhostCert,
  404. },
  405. Rules: []admissionv1beta1.RuleWithOperations{{
  406. Operations: []admissionv1beta1.OperationType{admissionv1beta1.Create, admissionv1beta1.Update},
  407. Rule: admissionv1beta1.Rule{APIGroups: []string{"*"}, APIVersions: []string{"*"}, Resources: []string{"*/*"}},
  408. }},
  409. FailurePolicy: &fail,
  410. AdmissionReviewVersions: []string{"v1beta1"},
  411. }},
  412. }, metav1.CreateOptions{})
  413. return err
  414. }