certlist_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. // Copyright 2019 Yunion
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /*
  15. Copyright 2018 The Kubernetes Authors.
  16. Licensed under the Apache License, Version 2.0 (the "License");
  17. you may not use this file except in compliance with the License.
  18. You may obtain a copy of the License at
  19. http://www.apache.org/licenses/LICENSE-2.0
  20. Unless required by applicable law or agreed to in writing, software
  21. distributed under the License is distributed on an "AS IS" BASIS,
  22. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  23. See the License for the specific language governing permissions and
  24. limitations under the License.
  25. */
  26. package certs
  27. import (
  28. "crypto"
  29. "crypto/tls"
  30. "crypto/x509"
  31. "io/ioutil"
  32. "os"
  33. "path"
  34. "testing"
  35. certutil "yunion.io/x/onecloud/pkg/util/tls/cert"
  36. )
  37. func TestCAPointersValid(t *testing.T) {
  38. tests := []struct {
  39. certs Certificates
  40. name string
  41. }{
  42. {
  43. name: "Default Certificate List",
  44. certs: GetDefaultCertList(),
  45. },
  46. }
  47. for _, test := range tests {
  48. t.Run(test.name, func(t *testing.T) {
  49. certMap := test.certs.AsMap()
  50. for _, cert := range test.certs {
  51. if cert.CAName != "" && certMap[cert.CAName] == nil {
  52. t.Errorf("Certificate %q references non existent CA %q", cert.Name, cert.CAName)
  53. }
  54. }
  55. })
  56. }
  57. }
  58. func TestMakeCertTree(t *testing.T) {
  59. rootCert := &QemuCert{
  60. Name: "root",
  61. }
  62. leaf0 := &QemuCert{
  63. Name: "leaf0",
  64. CAName: "root",
  65. }
  66. leaf1 := &QemuCert{
  67. Name: "leaf1",
  68. CAName: "root",
  69. }
  70. selfSigned := &QemuCert{
  71. Name: "self-signed",
  72. }
  73. certMap := CertificateMap{
  74. "root": rootCert,
  75. "leaf0": leaf0,
  76. "leaf1": leaf1,
  77. "self-signed": selfSigned,
  78. }
  79. orphanCertMap := CertificateMap{
  80. "leaf0": leaf0,
  81. }
  82. if _, err := orphanCertMap.CertTree(); err == nil {
  83. t.Error("expected orphan cert map to error, but got nil")
  84. }
  85. certTree, err := certMap.CertTree()
  86. t.Logf("cert tree: %v", certTree)
  87. if err != nil {
  88. t.Errorf("expected no error, but got %v", err)
  89. }
  90. if len(certTree) != 2 {
  91. t.Errorf("Expected tree to have 2 roots, got %d", len(certTree))
  92. }
  93. if len(certTree[rootCert]) != 2 {
  94. t.Errorf("Expected root to have 2 leaves, got %d", len(certTree[rootCert]))
  95. }
  96. if _, ok := certTree[selfSigned]; !ok {
  97. t.Error("Expected selfSigned to be present in tree, but missing")
  98. }
  99. }
  100. func TestCreateCertificateChain(t *testing.T) {
  101. dir, err := ioutil.TempDir("", t.Name())
  102. if err != nil {
  103. t.Fatal(err)
  104. }
  105. defer os.RemoveAll(dir)
  106. caCfg := Certificates{
  107. {
  108. config: certutil.Config{},
  109. Name: "test-ca",
  110. BaseName: "test-ca",
  111. },
  112. {
  113. config: certutil.Config{
  114. AltNames: certutil.AltNames{
  115. DNSNames: []string{"test-domain.space"},
  116. },
  117. Usages: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth},
  118. },
  119. configMutators: []configMutatorsFunc{
  120. setCommonNameToNodeName("test-node"),
  121. },
  122. CAName: "test-ca",
  123. Name: "test-daughter",
  124. BaseName: "test-daughter",
  125. },
  126. }
  127. certTree, err := caCfg.AsMap().CertTree()
  128. if err != nil {
  129. t.Fatalf("unexpected error getting tree: %v", err)
  130. }
  131. if certTree.CreateTree(dir); err != nil {
  132. t.Fatal(err)
  133. }
  134. caCert, _ := parseCertAndKey(path.Join(dir, "test-ca"), t)
  135. daughterCert, _ := parseCertAndKey(path.Join(dir, "test-daughter"), t)
  136. pool := x509.NewCertPool()
  137. pool.AddCert(caCert)
  138. _, err = daughterCert.Verify(x509.VerifyOptions{
  139. DNSName: "test-domain.space",
  140. Roots: pool,
  141. KeyUsages: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth},
  142. })
  143. if err != nil {
  144. t.Errorf("couldn't verify daughter cert: %v", err)
  145. }
  146. }
  147. func parseCertAndKey(basePath string, t *testing.T) (*x509.Certificate, crypto.PrivateKey) {
  148. certPair, err := tls.LoadX509KeyPair(basePath+"-cert.pem", basePath+"-key.pem")
  149. if err != nil {
  150. t.Fatalf("couldn't parse certificate and key: %v", err)
  151. }
  152. parsedCert, err := x509.ParseCertificate(certPair.Certificate[0])
  153. if err != nil {
  154. t.Fatalf("couldn't parse certificate: %v", err)
  155. }
  156. return parsedCert, certPair.PrivateKey
  157. }