PageRenderTime 5340ms CodeModel.GetById 31ms RepoModel.GetById 2ms app.codeStats 0ms

/staging/src/k8s.io/apiserver/plugin/pkg/audit/log/backend_test.go

https://gitlab.com/unofficial-mirrors/kubernetes
Go | 158 lines | 135 code | 7 blank | 16 comment | 8 complexity | 8149fb998df1577a3dcf413773a78a31 MD5 | raw file
  1. /*
  2. Copyright 2017 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 log
  14. import (
  15. "bytes"
  16. "reflect"
  17. "regexp"
  18. "testing"
  19. "time"
  20. "github.com/pborman/uuid"
  21. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  22. "k8s.io/apimachinery/pkg/runtime"
  23. "k8s.io/apimachinery/pkg/types"
  24. auditinternal "k8s.io/apiserver/pkg/apis/audit"
  25. "k8s.io/apiserver/pkg/apis/audit/install"
  26. auditv1beta1 "k8s.io/apiserver/pkg/apis/audit/v1beta1"
  27. "k8s.io/apiserver/pkg/audit"
  28. )
  29. func init() {
  30. install.Install(audit.Scheme)
  31. }
  32. func TestLogEventsLegacy(t *testing.T) {
  33. for _, test := range []struct {
  34. event *auditinternal.Event
  35. expected string
  36. }{
  37. {
  38. &auditinternal.Event{
  39. AuditID: types.UID(uuid.NewRandom().String()),
  40. },
  41. `[\d\:\-\.\+TZ]+ AUDIT: id="[\w-]+" stage="" ip="<unknown>" method="" user="<none>" groups="<none>" as="<self>" asgroups="<lookup>" namespace="<none>" uri="" response="<deferred>"`,
  42. },
  43. {
  44. &auditinternal.Event{
  45. ResponseStatus: &metav1.Status{
  46. Code: 200,
  47. },
  48. RequestURI: "/apis/rbac.authorization.k8s.io/v1/roles",
  49. SourceIPs: []string{
  50. "127.0.0.1",
  51. },
  52. RequestReceivedTimestamp: metav1.NewMicroTime(time.Now()),
  53. AuditID: types.UID(uuid.NewRandom().String()),
  54. Stage: auditinternal.StageRequestReceived,
  55. Verb: "get",
  56. User: auditinternal.UserInfo{
  57. Username: "admin",
  58. Groups: []string{
  59. "system:masters",
  60. "system:authenticated",
  61. },
  62. },
  63. ObjectRef: &auditinternal.ObjectReference{
  64. Namespace: "default",
  65. },
  66. },
  67. `[\d\:\-\.\+TZ]+ AUDIT: id="[\w-]+" stage="RequestReceived" ip="127.0.0.1" method="get" user="admin" groups="\\"system:masters\\",\\"system:authenticated\\"" as="<self>" asgroups="<lookup>" namespace="default" uri="/apis/rbac.authorization.k8s.io/v1/roles" response="200"`,
  68. },
  69. {
  70. &auditinternal.Event{
  71. AuditID: types.UID(uuid.NewRandom().String()),
  72. Level: auditinternal.LevelMetadata,
  73. ObjectRef: &auditinternal.ObjectReference{
  74. Resource: "foo",
  75. APIVersion: "v1",
  76. Subresource: "bar",
  77. },
  78. },
  79. `[\d\:\-\.\+TZ]+ AUDIT: id="[\w-]+" stage="" ip="<unknown>" method="" user="<none>" groups="<none>" as="<self>" asgroups="<lookup>" namespace="<none>" uri="" response="<deferred>"`,
  80. },
  81. } {
  82. var buf bytes.Buffer
  83. backend := NewBackend(&buf, FormatLegacy, auditv1beta1.SchemeGroupVersion)
  84. backend.ProcessEvents(test.event)
  85. match, err := regexp.MatchString(test.expected, buf.String())
  86. if err != nil {
  87. t.Errorf("Unexpected error matching line %v", err)
  88. continue
  89. }
  90. if !match {
  91. t.Errorf("Unexpected line of audit: %s", buf.String())
  92. }
  93. }
  94. }
  95. func TestLogEventsJson(t *testing.T) {
  96. for _, event := range []*auditinternal.Event{
  97. {
  98. AuditID: types.UID(uuid.NewRandom().String()),
  99. },
  100. {
  101. ResponseStatus: &metav1.Status{
  102. Code: 200,
  103. },
  104. RequestURI: "/apis/rbac.authorization.k8s.io/v1/roles",
  105. SourceIPs: []string{
  106. "127.0.0.1",
  107. },
  108. RequestReceivedTimestamp: metav1.NewMicroTime(time.Now().Truncate(time.Microsecond)),
  109. StageTimestamp: metav1.NewMicroTime(time.Now().Truncate(time.Microsecond)),
  110. AuditID: types.UID(uuid.NewRandom().String()),
  111. Stage: auditinternal.StageRequestReceived,
  112. Verb: "get",
  113. User: auditinternal.UserInfo{
  114. Username: "admin",
  115. Groups: []string{
  116. "system:masters",
  117. "system:authenticated",
  118. },
  119. },
  120. ObjectRef: &auditinternal.ObjectReference{
  121. Namespace: "default",
  122. },
  123. },
  124. {
  125. AuditID: types.UID(uuid.NewRandom().String()),
  126. Level: auditinternal.LevelMetadata,
  127. ObjectRef: &auditinternal.ObjectReference{
  128. Resource: "foo",
  129. APIVersion: "v1",
  130. Subresource: "bar",
  131. },
  132. },
  133. } {
  134. var buf bytes.Buffer
  135. backend := NewBackend(&buf, FormatJson, auditv1beta1.SchemeGroupVersion)
  136. backend.ProcessEvents(event)
  137. // decode events back and compare with the original one.
  138. result := &auditinternal.Event{}
  139. decoder := audit.Codecs.UniversalDecoder(auditv1beta1.SchemeGroupVersion)
  140. if err := runtime.DecodeInto(decoder, buf.Bytes(), result); err != nil {
  141. t.Errorf("failed decoding buf: %s", buf.String())
  142. continue
  143. }
  144. if !reflect.DeepEqual(event, result) {
  145. t.Errorf("The result event should be the same with the original one, \noriginal: \n%#v\n result: \n%#v", event, result)
  146. }
  147. }
  148. }