claims_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 serviceaccount
  14. import (
  15. "encoding/json"
  16. "fmt"
  17. "testing"
  18. "time"
  19. "gopkg.in/square/go-jose.v2/jwt"
  20. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  21. "k8s.io/kubernetes/pkg/apis/core"
  22. )
  23. func init() {
  24. now = func() time.Time {
  25. // epoch time: 1514764800
  26. return time.Date(2018, time.January, 1, 0, 0, 0, 0, time.UTC)
  27. }
  28. }
  29. func TestClaims(t *testing.T) {
  30. sa := core.ServiceAccount{
  31. ObjectMeta: metav1.ObjectMeta{
  32. Namespace: "myns",
  33. Name: "mysvcacct",
  34. UID: "mysvcacct-uid",
  35. },
  36. }
  37. pod := &core.Pod{
  38. ObjectMeta: metav1.ObjectMeta{
  39. Namespace: "myns",
  40. Name: "mypod",
  41. UID: "mypod-uid",
  42. },
  43. }
  44. sec := &core.Secret{
  45. ObjectMeta: metav1.ObjectMeta{
  46. Namespace: "myns",
  47. Name: "mysecret",
  48. UID: "mysecret-uid",
  49. },
  50. }
  51. cs := []struct {
  52. // input
  53. sa core.ServiceAccount
  54. pod *core.Pod
  55. sec *core.Secret
  56. exp int64
  57. aud []string
  58. // desired
  59. sc *jwt.Claims
  60. pc *privateClaims
  61. }{
  62. {
  63. // pod and secret
  64. sa: sa,
  65. pod: pod,
  66. sec: sec,
  67. // really fast
  68. exp: 0,
  69. // nil audience
  70. aud: nil,
  71. sc: &jwt.Claims{
  72. Subject: "system:serviceaccount:myns:mysvcacct",
  73. IssuedAt: jwt.NumericDate(1514764800),
  74. NotBefore: jwt.NumericDate(1514764800),
  75. Expiry: jwt.NumericDate(1514764800),
  76. },
  77. pc: &privateClaims{
  78. Kubernetes: kubernetes{
  79. Namespace: "myns",
  80. Svcacct: ref{Name: "mysvcacct", UID: "mysvcacct-uid"},
  81. Pod: &ref{Name: "mypod", UID: "mypod-uid"},
  82. },
  83. },
  84. },
  85. {
  86. // pod
  87. sa: sa,
  88. pod: pod,
  89. // empty audience
  90. aud: []string{},
  91. exp: 100,
  92. sc: &jwt.Claims{
  93. Subject: "system:serviceaccount:myns:mysvcacct",
  94. IssuedAt: jwt.NumericDate(1514764800),
  95. NotBefore: jwt.NumericDate(1514764800),
  96. Expiry: jwt.NumericDate(1514764800 + 100),
  97. },
  98. pc: &privateClaims{
  99. Kubernetes: kubernetes{
  100. Namespace: "myns",
  101. Svcacct: ref{Name: "mysvcacct", UID: "mysvcacct-uid"},
  102. Pod: &ref{Name: "mypod", UID: "mypod-uid"},
  103. },
  104. },
  105. },
  106. {
  107. // secret
  108. sa: sa,
  109. sec: sec,
  110. exp: 100,
  111. // single member audience
  112. aud: []string{"1"},
  113. sc: &jwt.Claims{
  114. Subject: "system:serviceaccount:myns:mysvcacct",
  115. Audience: []string{"1"},
  116. IssuedAt: jwt.NumericDate(1514764800),
  117. NotBefore: jwt.NumericDate(1514764800),
  118. Expiry: jwt.NumericDate(1514764800 + 100),
  119. },
  120. pc: &privateClaims{
  121. Kubernetes: kubernetes{
  122. Namespace: "myns",
  123. Svcacct: ref{Name: "mysvcacct", UID: "mysvcacct-uid"},
  124. Secret: &ref{Name: "mysecret", UID: "mysecret-uid"},
  125. },
  126. },
  127. },
  128. {
  129. // no obj binding
  130. sa: sa,
  131. exp: 100,
  132. // multimember audience
  133. aud: []string{"1", "2"},
  134. sc: &jwt.Claims{
  135. Subject: "system:serviceaccount:myns:mysvcacct",
  136. Audience: []string{"1", "2"},
  137. IssuedAt: jwt.NumericDate(1514764800),
  138. NotBefore: jwt.NumericDate(1514764800),
  139. Expiry: jwt.NumericDate(1514764800 + 100),
  140. },
  141. pc: &privateClaims{
  142. Kubernetes: kubernetes{
  143. Namespace: "myns",
  144. Svcacct: ref{Name: "mysvcacct", UID: "mysvcacct-uid"},
  145. },
  146. },
  147. },
  148. }
  149. for i, c := range cs {
  150. t.Run(fmt.Sprintf("case %d", i), func(t *testing.T) {
  151. // comparing json spews has the benefit over
  152. // reflect.DeepEqual that we are also asserting that
  153. // claims structs are json serializable
  154. spew := func(obj interface{}) string {
  155. b, err := json.Marshal(obj)
  156. if err != nil {
  157. t.Fatalf("err, couldn't marshal claims: %v", err)
  158. }
  159. return string(b)
  160. }
  161. sc, pc := Claims(c.sa, c.pod, c.sec, c.exp, c.aud)
  162. if spew(sc) != spew(c.sc) {
  163. t.Errorf("standard claims differed\n\tsaw:\t%s\n\twant:\t%s", spew(sc), spew(c.sc))
  164. }
  165. if spew(pc) != spew(c.pc) {
  166. t.Errorf("private claims differed\n\tsaw: %s\n\twant: %s", spew(pc), spew(c.pc))
  167. }
  168. })
  169. }
  170. }