audit_test.go 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. "encoding/json"
  16. "fmt"
  17. "io/ioutil"
  18. "os"
  19. "strings"
  20. "testing"
  21. apiv1 "k8s.io/api/core/v1"
  22. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  23. "k8s.io/apimachinery/pkg/runtime/schema"
  24. "k8s.io/apimachinery/pkg/types"
  25. auditinternal "k8s.io/apiserver/pkg/apis/audit"
  26. auditv1 "k8s.io/apiserver/pkg/apis/audit/v1"
  27. auditv1beta1 "k8s.io/apiserver/pkg/apis/audit/v1beta1"
  28. "k8s.io/client-go/kubernetes"
  29. kubeapiservertesting "k8s.io/kubernetes/cmd/kube-apiserver/app/testing"
  30. "k8s.io/kubernetes/test/integration/framework"
  31. "k8s.io/kubernetes/test/utils"
  32. "github.com/evanphx/json-patch"
  33. )
  34. var (
  35. auditPolicyPattern = `
  36. apiVersion: {version}
  37. kind: Policy
  38. rules:
  39. - level: RequestResponse
  40. resources:
  41. - group: "" # core
  42. resources: ["configmaps"]
  43. `
  44. namespace = "default"
  45. watchTestTimeout int64 = 1
  46. watchOptions = metav1.ListOptions{TimeoutSeconds: &watchTestTimeout}
  47. patch, _ = json.Marshal(jsonpatch.Patch{})
  48. auditTestUser = "system:apiserver"
  49. versions = map[string]schema.GroupVersion{
  50. "audit.k8s.io/v1": auditv1.SchemeGroupVersion,
  51. "audit.k8s.io/v1beta1": auditv1beta1.SchemeGroupVersion,
  52. }
  53. expectedEvents = []utils.AuditEvent{
  54. {
  55. Level: auditinternal.LevelRequestResponse,
  56. Stage: auditinternal.StageResponseComplete,
  57. RequestURI: fmt.Sprintf("/api/v1/namespaces/%s/configmaps", namespace),
  58. Verb: "create",
  59. Code: 201,
  60. User: auditTestUser,
  61. Resource: "configmaps",
  62. Namespace: namespace,
  63. RequestObject: true,
  64. ResponseObject: true,
  65. AuthorizeDecision: "allow",
  66. }, {
  67. Level: auditinternal.LevelRequestResponse,
  68. Stage: auditinternal.StageResponseComplete,
  69. RequestURI: fmt.Sprintf("/api/v1/namespaces/%s/configmaps/audit-configmap", namespace),
  70. Verb: "get",
  71. Code: 200,
  72. User: auditTestUser,
  73. Resource: "configmaps",
  74. Namespace: namespace,
  75. RequestObject: false,
  76. ResponseObject: true,
  77. AuthorizeDecision: "allow",
  78. }, {
  79. Level: auditinternal.LevelRequestResponse,
  80. Stage: auditinternal.StageResponseComplete,
  81. RequestURI: fmt.Sprintf("/api/v1/namespaces/%s/configmaps", namespace),
  82. Verb: "list",
  83. Code: 200,
  84. User: auditTestUser,
  85. Resource: "configmaps",
  86. Namespace: namespace,
  87. RequestObject: false,
  88. ResponseObject: true,
  89. AuthorizeDecision: "allow",
  90. }, {
  91. Level: auditinternal.LevelRequestResponse,
  92. Stage: auditinternal.StageResponseStarted,
  93. RequestURI: fmt.Sprintf("/api/v1/namespaces/%s/configmaps?timeout=%ds&timeoutSeconds=%d&watch=true", namespace, watchTestTimeout, watchTestTimeout),
  94. Verb: "watch",
  95. Code: 200,
  96. User: auditTestUser,
  97. Resource: "configmaps",
  98. Namespace: namespace,
  99. RequestObject: false,
  100. ResponseObject: false,
  101. AuthorizeDecision: "allow",
  102. }, {
  103. Level: auditinternal.LevelRequestResponse,
  104. Stage: auditinternal.StageResponseComplete,
  105. RequestURI: fmt.Sprintf("/api/v1/namespaces/%s/configmaps?timeout=%ds&timeoutSeconds=%d&watch=true", namespace, watchTestTimeout, watchTestTimeout),
  106. Verb: "watch",
  107. Code: 200,
  108. User: auditTestUser,
  109. Resource: "configmaps",
  110. Namespace: namespace,
  111. RequestObject: false,
  112. ResponseObject: false,
  113. AuthorizeDecision: "allow",
  114. }, {
  115. Level: auditinternal.LevelRequestResponse,
  116. Stage: auditinternal.StageResponseComplete,
  117. RequestURI: fmt.Sprintf("/api/v1/namespaces/%s/configmaps/audit-configmap", namespace),
  118. Verb: "update",
  119. Code: 200,
  120. User: auditTestUser,
  121. Resource: "configmaps",
  122. Namespace: namespace,
  123. RequestObject: true,
  124. ResponseObject: true,
  125. AuthorizeDecision: "allow",
  126. }, {
  127. Level: auditinternal.LevelRequestResponse,
  128. Stage: auditinternal.StageResponseComplete,
  129. RequestURI: fmt.Sprintf("/api/v1/namespaces/%s/configmaps/audit-configmap", namespace),
  130. Verb: "patch",
  131. Code: 200,
  132. User: auditTestUser,
  133. Resource: "configmaps",
  134. Namespace: namespace,
  135. RequestObject: true,
  136. ResponseObject: true,
  137. AuthorizeDecision: "allow",
  138. }, {
  139. Level: auditinternal.LevelRequestResponse,
  140. Stage: auditinternal.StageResponseComplete,
  141. RequestURI: fmt.Sprintf("/api/v1/namespaces/%s/configmaps/audit-configmap", namespace),
  142. Verb: "delete",
  143. Code: 200,
  144. User: auditTestUser,
  145. Resource: "configmaps",
  146. Namespace: namespace,
  147. RequestObject: true,
  148. ResponseObject: true,
  149. AuthorizeDecision: "allow",
  150. },
  151. }
  152. )
  153. // TestAudit ensures that both v1beta1 and v1 version audit api could work.
  154. func TestAudit(t *testing.T) {
  155. for version := range versions {
  156. testAudit(t, version)
  157. }
  158. }
  159. func testAudit(t *testing.T, version string) {
  160. // prepare audit policy file
  161. auditPolicy := []byte(strings.Replace(auditPolicyPattern, "{version}", version, 1))
  162. policyFile, err := ioutil.TempFile("", "audit-policy.yaml")
  163. if err != nil {
  164. t.Fatalf("Failed to create audit policy file: %v", err)
  165. }
  166. defer os.Remove(policyFile.Name())
  167. if _, err := policyFile.Write(auditPolicy); err != nil {
  168. t.Fatalf("Failed to write audit policy file: %v", err)
  169. }
  170. if err := policyFile.Close(); err != nil {
  171. t.Fatalf("Failed to close audit policy file: %v", err)
  172. }
  173. // prepare audit log file
  174. logFile, err := ioutil.TempFile("", "audit.log")
  175. if err != nil {
  176. t.Fatalf("Failed to create audit log file: %v", err)
  177. }
  178. defer os.Remove(logFile.Name())
  179. // start api server
  180. result := kubeapiservertesting.StartTestServerOrDie(t, nil,
  181. []string{
  182. "--audit-policy-file", policyFile.Name(),
  183. "--audit-log-version", version,
  184. "--audit-log-mode", "blocking",
  185. "--audit-log-path", logFile.Name()},
  186. framework.SharedEtcd())
  187. defer result.TearDownFn()
  188. kubeclient, err := kubernetes.NewForConfig(result.ClientConfig)
  189. if err != nil {
  190. t.Fatalf("Unexpected error: %v", err)
  191. }
  192. // perform configmap operations
  193. configMapOperations(t, kubeclient)
  194. // check for corresponding audit logs
  195. stream, err := os.Open(logFile.Name())
  196. if err != nil {
  197. t.Fatalf("Unexpected error: %v", err)
  198. }
  199. defer stream.Close()
  200. missingReport, err := utils.CheckAuditLines(stream, expectedEvents, versions[version])
  201. if err != nil {
  202. t.Fatalf("Unexpected error: %v", err)
  203. }
  204. if len(missingReport.MissingEvents) > 0 {
  205. t.Errorf(missingReport.String())
  206. }
  207. }
  208. // configMapOperations is a set of known operations performed on the configmap type
  209. // which correspond to the expected events.
  210. // This is shared by the dynamic test
  211. func configMapOperations(t *testing.T, kubeclient kubernetes.Interface) {
  212. // create, get, watch, update, patch, list and delete configmap.
  213. configMap := &apiv1.ConfigMap{
  214. ObjectMeta: metav1.ObjectMeta{
  215. Name: "audit-configmap",
  216. },
  217. Data: map[string]string{
  218. "map-key": "map-value",
  219. },
  220. }
  221. _, err := kubeclient.CoreV1().ConfigMaps(namespace).Create(configMap)
  222. expectNoError(t, err, "failed to create audit-configmap")
  223. _, err = kubeclient.CoreV1().ConfigMaps(namespace).Get(configMap.Name, metav1.GetOptions{})
  224. expectNoError(t, err, "failed to get audit-configmap")
  225. configMapChan, err := kubeclient.CoreV1().ConfigMaps(namespace).Watch(watchOptions)
  226. expectNoError(t, err, "failed to create watch for config maps")
  227. for range configMapChan.ResultChan() {
  228. // Block until watchOptions.TimeoutSeconds expires.
  229. // If the test finishes before watchOptions.TimeoutSeconds expires, the watch audit
  230. // event at stage ResponseComplete will not be generated.
  231. }
  232. _, err = kubeclient.CoreV1().ConfigMaps(namespace).Update(configMap)
  233. expectNoError(t, err, "failed to update audit-configmap")
  234. _, err = kubeclient.CoreV1().ConfigMaps(namespace).Patch(configMap.Name, types.JSONPatchType, patch)
  235. expectNoError(t, err, "failed to patch configmap")
  236. _, err = kubeclient.CoreV1().ConfigMaps(namespace).List(metav1.ListOptions{})
  237. expectNoError(t, err, "failed to list config maps")
  238. err = kubeclient.CoreV1().ConfigMaps(namespace).Delete(configMap.Name, &metav1.DeleteOptions{})
  239. expectNoError(t, err, "failed to delete audit-configmap")
  240. }
  241. func expectNoError(t *testing.T, err error, msg string) {
  242. if err != nil {
  243. t.Fatalf("%s: %v", msg, err)
  244. }
  245. }