bootstrap_test.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. Copyright 2016 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 bootstrap
  14. import (
  15. "fmt"
  16. "io/ioutil"
  17. "os"
  18. "reflect"
  19. "testing"
  20. certificates "k8s.io/api/certificates/v1beta1"
  21. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  22. "k8s.io/apimachinery/pkg/util/diff"
  23. "k8s.io/apimachinery/pkg/watch"
  24. certificatesclient "k8s.io/client-go/kubernetes/typed/certificates/v1beta1"
  25. restclient "k8s.io/client-go/rest"
  26. "k8s.io/client-go/util/keyutil"
  27. )
  28. func TestLoadRESTClientConfig(t *testing.T) {
  29. testData := []byte(`
  30. apiVersion: v1
  31. kind: Config
  32. clusters:
  33. - cluster:
  34. certificate-authority: ca-a.crt
  35. server: https://cluster-a.com
  36. name: cluster-a
  37. - cluster:
  38. certificate-authority-data: VGVzdA==
  39. server: https://cluster-b.com
  40. name: cluster-b
  41. contexts:
  42. - context:
  43. cluster: cluster-a
  44. namespace: ns-a
  45. user: user-a
  46. name: context-a
  47. - context:
  48. cluster: cluster-b
  49. namespace: ns-b
  50. user: user-b
  51. name: context-b
  52. current-context: context-b
  53. users:
  54. - name: user-a
  55. user:
  56. token: mytoken-a
  57. - name: user-b
  58. user:
  59. token: mytoken-b
  60. `)
  61. f, err := ioutil.TempFile("", "kubeconfig")
  62. if err != nil {
  63. t.Fatal(err)
  64. }
  65. defer os.Remove(f.Name())
  66. ioutil.WriteFile(f.Name(), testData, os.FileMode(0755))
  67. config, err := loadRESTClientConfig(f.Name())
  68. if err != nil {
  69. t.Fatal(err)
  70. }
  71. expectedConfig := &restclient.Config{
  72. Host: "https://cluster-b.com",
  73. TLSClientConfig: restclient.TLSClientConfig{
  74. CAData: []byte(`Test`),
  75. },
  76. BearerToken: "mytoken-b",
  77. }
  78. if !reflect.DeepEqual(config, expectedConfig) {
  79. t.Errorf("Unexpected config: %s", diff.ObjectDiff(config, expectedConfig))
  80. }
  81. }
  82. func TestRequestNodeCertificateNoKeyData(t *testing.T) {
  83. certData, err := requestNodeCertificate(&fakeClient{}, []byte{}, "fake-node-name")
  84. if err == nil {
  85. t.Errorf("Got no error, wanted error an error because there was an empty private key passed in.")
  86. }
  87. if certData != nil {
  88. t.Errorf("Got cert data, wanted nothing as there should have been an error.")
  89. }
  90. }
  91. func TestRequestNodeCertificateErrorCreatingCSR(t *testing.T) {
  92. client := &fakeClient{
  93. failureType: createError,
  94. }
  95. privateKeyData, err := keyutil.MakeEllipticPrivateKeyPEM()
  96. if err != nil {
  97. t.Fatalf("Unable to generate a new private key: %v", err)
  98. }
  99. certData, err := requestNodeCertificate(client, privateKeyData, "fake-node-name")
  100. if err == nil {
  101. t.Errorf("Got no error, wanted error an error because client.Create failed.")
  102. }
  103. if certData != nil {
  104. t.Errorf("Got cert data, wanted nothing as there should have been an error.")
  105. }
  106. }
  107. func TestRequestNodeCertificate(t *testing.T) {
  108. privateKeyData, err := keyutil.MakeEllipticPrivateKeyPEM()
  109. if err != nil {
  110. t.Fatalf("Unable to generate a new private key: %v", err)
  111. }
  112. certData, err := requestNodeCertificate(&fakeClient{}, privateKeyData, "fake-node-name")
  113. if err != nil {
  114. t.Errorf("Got %v, wanted no error.", err)
  115. }
  116. if certData == nil {
  117. t.Errorf("Got nothing, expected a CSR.")
  118. }
  119. }
  120. type failureType int
  121. const (
  122. noError failureType = iota
  123. createError
  124. certificateSigningRequestDenied
  125. )
  126. type fakeClient struct {
  127. certificatesclient.CertificateSigningRequestInterface
  128. watch *watch.FakeWatcher
  129. failureType failureType
  130. }
  131. func (c *fakeClient) Create(*certificates.CertificateSigningRequest) (*certificates.CertificateSigningRequest, error) {
  132. if c.failureType == createError {
  133. return nil, fmt.Errorf("fakeClient failed creating request")
  134. }
  135. csr := certificates.CertificateSigningRequest{
  136. ObjectMeta: metav1.ObjectMeta{
  137. UID: "fake-uid",
  138. Name: "fake-certificate-signing-request-name",
  139. },
  140. }
  141. return &csr, nil
  142. }
  143. func (c *fakeClient) List(opts metav1.ListOptions) (*certificates.CertificateSigningRequestList, error) {
  144. return &certificates.CertificateSigningRequestList{}, nil
  145. }
  146. func (c *fakeClient) Watch(opts metav1.ListOptions) (watch.Interface, error) {
  147. c.watch = watch.NewFakeWithChanSize(1, false)
  148. c.watch.Add(c.generateCSR())
  149. c.watch.Stop()
  150. return c.watch, nil
  151. }
  152. func (c *fakeClient) generateCSR() *certificates.CertificateSigningRequest {
  153. var condition certificates.CertificateSigningRequestCondition
  154. if c.failureType == certificateSigningRequestDenied {
  155. condition = certificates.CertificateSigningRequestCondition{
  156. Type: certificates.CertificateDenied,
  157. }
  158. } else {
  159. condition = certificates.CertificateSigningRequestCondition{
  160. Type: certificates.CertificateApproved,
  161. }
  162. }
  163. csr := certificates.CertificateSigningRequest{
  164. ObjectMeta: metav1.ObjectMeta{
  165. UID: "fake-uid",
  166. },
  167. Status: certificates.CertificateSigningRequestStatus{
  168. Conditions: []certificates.CertificateSigningRequestCondition{
  169. condition,
  170. },
  171. Certificate: []byte{},
  172. },
  173. }
  174. return &csr
  175. }