pki_helpers_test.go 17 KB

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