pki_helpers_test.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  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 pkiutil
  14. import (
  15. "crypto"
  16. "crypto/ecdsa"
  17. "crypto/elliptic"
  18. "crypto/rand"
  19. "crypto/rsa"
  20. "crypto/x509"
  21. "io/ioutil"
  22. "net"
  23. "os"
  24. "testing"
  25. certutil "k8s.io/client-go/util/cert"
  26. kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
  27. )
  28. func TestNewCertificateAuthority(t *testing.T) {
  29. cert, key, err := NewCertificateAuthority(&certutil.Config{CommonName: "kubernetes"})
  30. if cert == nil {
  31. t.Error("failed NewCertificateAuthority, cert == nil")
  32. } else if !cert.IsCA {
  33. t.Error("cert is not a valida CA")
  34. }
  35. if key == nil {
  36. t.Error("failed NewCertificateAuthority, key == nil")
  37. }
  38. if err != nil {
  39. t.Errorf("failed NewCertificateAuthority with an error: %+v", err)
  40. }
  41. }
  42. func TestNewCertAndKey(t *testing.T) {
  43. var tests = []struct {
  44. name string
  45. keyGenFunc func() (crypto.Signer, error)
  46. expected bool
  47. }{
  48. {
  49. name: "RSA key too small",
  50. keyGenFunc: func() (crypto.Signer, error) {
  51. return rsa.GenerateKey(rand.Reader, 128)
  52. },
  53. expected: false,
  54. },
  55. {
  56. name: "RSA should succeed",
  57. keyGenFunc: func() (crypto.Signer, error) {
  58. return rsa.GenerateKey(rand.Reader, 2048)
  59. },
  60. expected: true,
  61. },
  62. {
  63. name: "ECDSA should succeed",
  64. keyGenFunc: func() (crypto.Signer, error) {
  65. return ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
  66. },
  67. expected: true,
  68. },
  69. }
  70. for _, rt := range tests {
  71. t.Run(rt.name, func(t *testing.T) {
  72. caKey, err := rt.keyGenFunc()
  73. if err != nil {
  74. t.Fatalf("Couldn't create Private Key")
  75. }
  76. caCert := &x509.Certificate{}
  77. config := &certutil.Config{
  78. CommonName: "test",
  79. Organization: []string{"test"},
  80. Usages: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth},
  81. }
  82. _, _, actual := NewCertAndKey(caCert, caKey, config)
  83. if (actual == nil) != rt.expected {
  84. t.Errorf(
  85. "failed NewCertAndKey:\n\texpected: %t\n\t actual: %t",
  86. rt.expected,
  87. (actual == nil),
  88. )
  89. }
  90. })
  91. }
  92. }
  93. func TestHasServerAuth(t *testing.T) {
  94. caCert, caKey, _ := NewCertificateAuthority(&certutil.Config{CommonName: "kubernetes"})
  95. var tests = []struct {
  96. name string
  97. config certutil.Config
  98. expected bool
  99. }{
  100. {
  101. name: "has ServerAuth",
  102. config: certutil.Config{
  103. CommonName: "test",
  104. Usages: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
  105. },
  106. expected: true,
  107. },
  108. {
  109. name: "doesn't have ServerAuth",
  110. config: certutil.Config{
  111. CommonName: "test",
  112. Usages: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth},
  113. },
  114. expected: false,
  115. },
  116. }
  117. for _, rt := range tests {
  118. t.Run(rt.name, func(t *testing.T) {
  119. cert, _, err := NewCertAndKey(caCert, caKey, &rt.config)
  120. if err != nil {
  121. t.Fatalf("Couldn't create cert: %v", err)
  122. }
  123. actual := HasServerAuth(cert)
  124. if actual != rt.expected {
  125. t.Errorf(
  126. "failed HasServerAuth:\n\texpected: %t\n\t actual: %t",
  127. rt.expected,
  128. actual,
  129. )
  130. }
  131. })
  132. }
  133. }
  134. func TestWriteCertAndKey(t *testing.T) {
  135. tmpdir, err := ioutil.TempDir("", "")
  136. if err != nil {
  137. t.Fatalf("Couldn't create tmpdir")
  138. }
  139. defer os.RemoveAll(tmpdir)
  140. caKey, err := rsa.GenerateKey(rand.Reader, 2048)
  141. if err != nil {
  142. t.Fatalf("Couldn't create rsa Private Key")
  143. }
  144. caCert := &x509.Certificate{}
  145. actual := WriteCertAndKey(tmpdir, "foo", caCert, caKey)
  146. if actual != nil {
  147. t.Errorf(
  148. "failed WriteCertAndKey with an error: %v",
  149. actual,
  150. )
  151. }
  152. }
  153. func TestWriteCert(t *testing.T) {
  154. tmpdir, err := ioutil.TempDir("", "")
  155. if err != nil {
  156. t.Fatalf("Couldn't create tmpdir")
  157. }
  158. defer os.RemoveAll(tmpdir)
  159. caCert := &x509.Certificate{}
  160. actual := WriteCert(tmpdir, "foo", caCert)
  161. if actual != nil {
  162. t.Errorf(
  163. "failed WriteCertAndKey with an error: %v",
  164. actual,
  165. )
  166. }
  167. }
  168. func TestWriteKey(t *testing.T) {
  169. tmpdir, err := ioutil.TempDir("", "")
  170. if err != nil {
  171. t.Fatalf("Couldn't create tmpdir")
  172. }
  173. defer os.RemoveAll(tmpdir)
  174. caKey, err := rsa.GenerateKey(rand.Reader, 2048)
  175. if err != nil {
  176. t.Fatalf("Couldn't create rsa Private Key")
  177. }
  178. actual := WriteKey(tmpdir, "foo", caKey)
  179. if actual != nil {
  180. t.Errorf(
  181. "failed WriteCertAndKey with an error: %v",
  182. actual,
  183. )
  184. }
  185. }
  186. func TestWritePublicKey(t *testing.T) {
  187. tmpdir, err := ioutil.TempDir("", "")
  188. if err != nil {
  189. t.Fatalf("Couldn't create tmpdir")
  190. }
  191. defer os.RemoveAll(tmpdir)
  192. caKey, err := rsa.GenerateKey(rand.Reader, 2048)
  193. if err != nil {
  194. t.Fatalf("Couldn't create rsa Private Key")
  195. }
  196. actual := WritePublicKey(tmpdir, "foo", &caKey.PublicKey)
  197. if actual != nil {
  198. t.Errorf(
  199. "failed WriteCertAndKey with an error: %v",
  200. actual,
  201. )
  202. }
  203. }
  204. func TestCertOrKeyExist(t *testing.T) {
  205. tmpdir, err := ioutil.TempDir("", "")
  206. if err != nil {
  207. t.Fatalf("Couldn't create tmpdir")
  208. }
  209. defer os.RemoveAll(tmpdir)
  210. caKey, err := rsa.GenerateKey(rand.Reader, 2048)
  211. if err != nil {
  212. t.Fatalf("Couldn't create rsa Private Key")
  213. }
  214. caCert := &x509.Certificate{}
  215. actual := WriteCertAndKey(tmpdir, "foo", caCert, caKey)
  216. if actual != nil {
  217. t.Errorf(
  218. "failed WriteCertAndKey with an error: %v",
  219. actual,
  220. )
  221. }
  222. var tests = []struct {
  223. desc string
  224. path string
  225. name string
  226. expected bool
  227. }{
  228. {
  229. desc: "empty path and name",
  230. path: "",
  231. name: "",
  232. expected: false,
  233. },
  234. {
  235. desc: "valid path and name",
  236. path: tmpdir,
  237. name: "foo",
  238. expected: true,
  239. },
  240. }
  241. for _, rt := range tests {
  242. t.Run(rt.name, func(t *testing.T) {
  243. actual := CertOrKeyExist(rt.path, rt.name)
  244. if actual != rt.expected {
  245. t.Errorf(
  246. "failed CertOrKeyExist:\n\texpected: %t\n\t actual: %t",
  247. rt.expected,
  248. actual,
  249. )
  250. }
  251. })
  252. }
  253. }
  254. func TestTryLoadCertAndKeyFromDisk(t *testing.T) {
  255. tmpdir, err := ioutil.TempDir("", "")
  256. if err != nil {
  257. t.Fatalf("Couldn't create tmpdir")
  258. }
  259. defer os.RemoveAll(tmpdir)
  260. caCert, caKey, err := NewCertificateAuthority(&certutil.Config{CommonName: "kubernetes"})
  261. if err != nil {
  262. t.Errorf(
  263. "failed to create cert and key with an error: %v",
  264. err,
  265. )
  266. }
  267. err = WriteCertAndKey(tmpdir, "foo", caCert, caKey)
  268. if err != nil {
  269. t.Errorf(
  270. "failed to write cert and key with an error: %v",
  271. err,
  272. )
  273. }
  274. var tests = []struct {
  275. desc string
  276. path string
  277. name string
  278. expected bool
  279. }{
  280. {
  281. desc: "empty path and name",
  282. path: "",
  283. name: "",
  284. expected: false,
  285. },
  286. {
  287. desc: "valid path and name",
  288. path: tmpdir,
  289. name: "foo",
  290. expected: true,
  291. },
  292. }
  293. for _, rt := range tests {
  294. t.Run(rt.desc, func(t *testing.T) {
  295. _, _, actual := TryLoadCertAndKeyFromDisk(rt.path, rt.name)
  296. if (actual == nil) != rt.expected {
  297. t.Errorf(
  298. "failed TryLoadCertAndKeyFromDisk:\n\texpected: %t\n\t actual: %t",
  299. rt.expected,
  300. (actual == nil),
  301. )
  302. }
  303. })
  304. }
  305. }
  306. func TestTryLoadCertFromDisk(t *testing.T) {
  307. tmpdir, err := ioutil.TempDir("", "")
  308. if err != nil {
  309. t.Fatalf("Couldn't create tmpdir")
  310. }
  311. defer os.RemoveAll(tmpdir)
  312. caCert, _, err := NewCertificateAuthority(&certutil.Config{CommonName: "kubernetes"})
  313. if err != nil {
  314. t.Errorf(
  315. "failed to create cert and key with an error: %v",
  316. err,
  317. )
  318. }
  319. err = WriteCert(tmpdir, "foo", caCert)
  320. if err != nil {
  321. t.Errorf(
  322. "failed to write cert and key with an error: %v",
  323. err,
  324. )
  325. }
  326. var tests = []struct {
  327. desc string
  328. path string
  329. name string
  330. expected bool
  331. }{
  332. {
  333. desc: "empty path and name",
  334. path: "",
  335. name: "",
  336. expected: false,
  337. },
  338. {
  339. desc: "valid path and name",
  340. path: tmpdir,
  341. name: "foo",
  342. expected: true,
  343. },
  344. }
  345. for _, rt := range tests {
  346. t.Run(rt.desc, func(t *testing.T) {
  347. _, actual := TryLoadCertFromDisk(rt.path, rt.name)
  348. if (actual == nil) != rt.expected {
  349. t.Errorf(
  350. "failed TryLoadCertAndKeyFromDisk:\n\texpected: %t\n\t actual: %t",
  351. rt.expected,
  352. (actual == nil),
  353. )
  354. }
  355. })
  356. }
  357. }
  358. func TestTryLoadKeyFromDisk(t *testing.T) {
  359. var tests = []struct {
  360. desc string
  361. pathSuffix string
  362. name string
  363. keyGenFunc func() (crypto.Signer, error)
  364. expected bool
  365. }{
  366. {
  367. desc: "empty path and name",
  368. pathSuffix: "somegarbage",
  369. name: "",
  370. keyGenFunc: func() (crypto.Signer, error) {
  371. return rsa.GenerateKey(rand.Reader, 2048)
  372. },
  373. expected: false,
  374. },
  375. {
  376. desc: "RSA valid path and name",
  377. pathSuffix: "",
  378. name: "foo",
  379. keyGenFunc: func() (crypto.Signer, error) {
  380. return rsa.GenerateKey(rand.Reader, 2048)
  381. },
  382. expected: true,
  383. },
  384. {
  385. desc: "ECDSA valid path and name",
  386. pathSuffix: "",
  387. name: "foo",
  388. keyGenFunc: func() (crypto.Signer, error) {
  389. return ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
  390. },
  391. expected: true,
  392. },
  393. }
  394. for _, rt := range tests {
  395. t.Run(rt.desc, func(t *testing.T) {
  396. tmpdir, err := ioutil.TempDir("", "")
  397. if err != nil {
  398. t.Fatalf("Couldn't create tmpdir")
  399. }
  400. defer os.RemoveAll(tmpdir)
  401. caKey, err := rt.keyGenFunc()
  402. if err != nil {
  403. t.Errorf(
  404. "failed to create key with an error: %v",
  405. err,
  406. )
  407. }
  408. err = WriteKey(tmpdir, "foo", caKey)
  409. if err != nil {
  410. t.Errorf(
  411. "failed to write key with an error: %v",
  412. err,
  413. )
  414. }
  415. _, actual := TryLoadKeyFromDisk(tmpdir+rt.pathSuffix, rt.name)
  416. if (actual == nil) != rt.expected {
  417. t.Errorf(
  418. "failed TryLoadCertAndKeyFromDisk:\n\texpected: %t\n\t actual: %t",
  419. rt.expected,
  420. (actual == nil),
  421. )
  422. }
  423. })
  424. }
  425. }
  426. func TestPathsForCertAndKey(t *testing.T) {
  427. crtPath, keyPath := PathsForCertAndKey("/foo", "bar")
  428. if crtPath != "/foo/bar.crt" {
  429. t.Errorf("unexpected certificate path: %s", crtPath)
  430. }
  431. if keyPath != "/foo/bar.key" {
  432. t.Errorf("unexpected key path: %s", keyPath)
  433. }
  434. }
  435. func TestPathForCert(t *testing.T) {
  436. crtPath := pathForCert("/foo", "bar")
  437. if crtPath != "/foo/bar.crt" {
  438. t.Errorf("unexpected certificate path: %s", crtPath)
  439. }
  440. }
  441. func TestPathForKey(t *testing.T) {
  442. keyPath := pathForKey("/foo", "bar")
  443. if keyPath != "/foo/bar.key" {
  444. t.Errorf("unexpected certificate path: %s", keyPath)
  445. }
  446. }
  447. func TestPathForPublicKey(t *testing.T) {
  448. pubPath := pathForPublicKey("/foo", "bar")
  449. if pubPath != "/foo/bar.pub" {
  450. t.Errorf("unexpected certificate path: %s", pubPath)
  451. }
  452. }
  453. func TestPathForCSR(t *testing.T) {
  454. csrPath := pathForCSR("/foo", "bar")
  455. if csrPath != "/foo/bar.csr" {
  456. t.Errorf("unexpected certificate path: %s", csrPath)
  457. }
  458. }
  459. func TestGetAPIServerAltNames(t *testing.T) {
  460. var tests = []struct {
  461. desc string
  462. name string
  463. cfg *kubeadmapi.InitConfiguration
  464. expectedDNSNames []string
  465. expectedIPAddresses []string
  466. }{
  467. {
  468. desc: "empty name",
  469. name: "",
  470. cfg: &kubeadmapi.InitConfiguration{
  471. LocalAPIEndpoint: kubeadmapi.APIEndpoint{AdvertiseAddress: "1.2.3.4"},
  472. ClusterConfiguration: kubeadmapi.ClusterConfiguration{
  473. ControlPlaneEndpoint: "api.k8s.io:6443",
  474. Networking: kubeadmapi.Networking{ServiceSubnet: "10.96.0.0/12", DNSDomain: "cluster.local"},
  475. APIServer: kubeadmapi.APIServer{
  476. CertSANs: []string{"10.1.245.94", "10.1.245.95", "1.2.3.L", "invalid,commas,in,DNS"},
  477. },
  478. },
  479. NodeRegistration: kubeadmapi.NodeRegistrationOptions{Name: "valid-hostname"},
  480. },
  481. expectedDNSNames: []string{"valid-hostname", "kubernetes", "kubernetes.default", "kubernetes.default.svc", "kubernetes.default.svc.cluster.local", "api.k8s.io"},
  482. expectedIPAddresses: []string{"10.96.0.1", "1.2.3.4", "10.1.245.94", "10.1.245.95"},
  483. },
  484. {
  485. desc: "ControlPlaneEndpoint IP",
  486. name: "ControlPlaneEndpoint IP",
  487. cfg: &kubeadmapi.InitConfiguration{
  488. LocalAPIEndpoint: kubeadmapi.APIEndpoint{AdvertiseAddress: "1.2.3.4"},
  489. ClusterConfiguration: kubeadmapi.ClusterConfiguration{
  490. ControlPlaneEndpoint: "4.5.6.7:6443",
  491. Networking: kubeadmapi.Networking{ServiceSubnet: "10.96.0.0/12", DNSDomain: "cluster.local"},
  492. APIServer: kubeadmapi.APIServer{
  493. CertSANs: []string{"10.1.245.94", "10.1.245.95", "1.2.3.L", "invalid,commas,in,DNS"},
  494. },
  495. },
  496. NodeRegistration: kubeadmapi.NodeRegistrationOptions{Name: "valid-hostname"},
  497. },
  498. expectedDNSNames: []string{"valid-hostname", "kubernetes", "kubernetes.default", "kubernetes.default.svc", "kubernetes.default.svc.cluster.local"},
  499. expectedIPAddresses: []string{"10.96.0.1", "1.2.3.4", "10.1.245.94", "10.1.245.95", "4.5.6.7"},
  500. },
  501. }
  502. for _, rt := range tests {
  503. t.Run(rt.desc, func(t *testing.T) {
  504. altNames, err := GetAPIServerAltNames(rt.cfg)
  505. if err != nil {
  506. t.Fatalf("failed calling GetAPIServerAltNames: %s: %v", rt.name, err)
  507. }
  508. for _, DNSName := range rt.expectedDNSNames {
  509. found := false
  510. for _, val := range altNames.DNSNames {
  511. if val == DNSName {
  512. found = true
  513. break
  514. }
  515. }
  516. if !found {
  517. t.Errorf("%s: altNames does not contain DNSName %s but %v", rt.name, DNSName, altNames.DNSNames)
  518. }
  519. }
  520. for _, IPAddress := range rt.expectedIPAddresses {
  521. found := false
  522. for _, val := range altNames.IPs {
  523. if val.Equal(net.ParseIP(IPAddress)) {
  524. found = true
  525. break
  526. }
  527. }
  528. if !found {
  529. t.Errorf("%s: altNames does not contain IPAddress %s but %v", rt.name, IPAddress, altNames.IPs)
  530. }
  531. }
  532. })
  533. }
  534. }
  535. func TestGetEtcdAltNames(t *testing.T) {
  536. proxy := "user-etcd-proxy"
  537. proxyIP := "10.10.10.100"
  538. cfg := &kubeadmapi.InitConfiguration{
  539. LocalAPIEndpoint: kubeadmapi.APIEndpoint{
  540. AdvertiseAddress: "1.2.3.4",
  541. },
  542. NodeRegistration: kubeadmapi.NodeRegistrationOptions{
  543. Name: "myNode",
  544. },
  545. ClusterConfiguration: kubeadmapi.ClusterConfiguration{
  546. Etcd: kubeadmapi.Etcd{
  547. Local: &kubeadmapi.LocalEtcd{
  548. ServerCertSANs: []string{
  549. proxy,
  550. proxyIP,
  551. "1.2.3.L",
  552. "invalid,commas,in,DNS",
  553. },
  554. },
  555. },
  556. },
  557. }
  558. altNames, err := GetEtcdAltNames(cfg)
  559. if err != nil {
  560. t.Fatalf("failed calling GetEtcdAltNames: %v", err)
  561. }
  562. expectedDNSNames := []string{"myNode", "localhost", proxy}
  563. for _, DNSName := range expectedDNSNames {
  564. t.Run(DNSName, func(t *testing.T) {
  565. found := false
  566. for _, val := range altNames.DNSNames {
  567. if val == DNSName {
  568. found = true
  569. break
  570. }
  571. }
  572. if !found {
  573. t.Errorf("altNames does not contain DNSName %s", DNSName)
  574. }
  575. })
  576. }
  577. expectedIPAddresses := []string{"1.2.3.4", "127.0.0.1", net.IPv6loopback.String(), proxyIP}
  578. for _, IPAddress := range expectedIPAddresses {
  579. t.Run(IPAddress, func(t *testing.T) {
  580. found := false
  581. for _, val := range altNames.IPs {
  582. if val.Equal(net.ParseIP(IPAddress)) {
  583. found = true
  584. break
  585. }
  586. }
  587. if !found {
  588. t.Errorf("altNames does not contain IPAddress %s", IPAddress)
  589. }
  590. })
  591. }
  592. }
  593. func TestGetEtcdPeerAltNames(t *testing.T) {
  594. hostname := "valid-hostname"
  595. proxy := "user-etcd-proxy"
  596. proxyIP := "10.10.10.100"
  597. advertiseIP := "1.2.3.4"
  598. cfg := &kubeadmapi.InitConfiguration{
  599. LocalAPIEndpoint: kubeadmapi.APIEndpoint{AdvertiseAddress: advertiseIP},
  600. ClusterConfiguration: kubeadmapi.ClusterConfiguration{
  601. Etcd: kubeadmapi.Etcd{
  602. Local: &kubeadmapi.LocalEtcd{
  603. PeerCertSANs: []string{
  604. proxy,
  605. proxyIP,
  606. "1.2.3.L",
  607. "invalid,commas,in,DNS",
  608. },
  609. },
  610. },
  611. },
  612. NodeRegistration: kubeadmapi.NodeRegistrationOptions{Name: hostname},
  613. }
  614. altNames, err := GetEtcdPeerAltNames(cfg)
  615. if err != nil {
  616. t.Fatalf("failed calling GetEtcdPeerAltNames: %v", err)
  617. }
  618. expectedDNSNames := []string{hostname, proxy}
  619. for _, DNSName := range expectedDNSNames {
  620. t.Run(DNSName, func(t *testing.T) {
  621. found := false
  622. for _, val := range altNames.DNSNames {
  623. if val == DNSName {
  624. found = true
  625. break
  626. }
  627. }
  628. if !found {
  629. t.Errorf("altNames does not contain DNSName %s", DNSName)
  630. }
  631. expectedIPAddresses := []string{advertiseIP, proxyIP}
  632. for _, IPAddress := range expectedIPAddresses {
  633. found := false
  634. for _, val := range altNames.IPs {
  635. if val.Equal(net.ParseIP(IPAddress)) {
  636. found = true
  637. break
  638. }
  639. }
  640. if !found {
  641. t.Errorf("altNames does not contain IPAddress %s", IPAddress)
  642. }
  643. }
  644. })
  645. }
  646. }
  647. func TestAppendSANsToAltNames(t *testing.T) {
  648. var tests = []struct {
  649. sans []string
  650. expected int
  651. }{
  652. {[]string{}, 0},
  653. {[]string{"abc"}, 1},
  654. {[]string{"*.abc"}, 1},
  655. {[]string{"**.abc"}, 0},
  656. {[]string{"a.*.bc"}, 0},
  657. {[]string{"a.*.bc", "abc.def"}, 1},
  658. {[]string{"a*.bc", "abc.def"}, 1},
  659. }
  660. for _, rt := range tests {
  661. altNames := certutil.AltNames{}
  662. appendSANsToAltNames(&altNames, rt.sans, "foo")
  663. actual := len(altNames.DNSNames)
  664. if actual != rt.expected {
  665. t.Errorf(
  666. "failed AppendSANsToAltNames Numbers:\n\texpected: %d\n\t actual: %d",
  667. rt.expected,
  668. actual,
  669. )
  670. }
  671. }
  672. }