tls_options_test.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  1. package ca
  2. import (
  3. "crypto/tls"
  4. "crypto/x509"
  5. "fmt"
  6. "io/ioutil"
  7. "net/http"
  8. "reflect"
  9. "sort"
  10. "testing"
  11. "github.com/smallstep/certificates/api"
  12. )
  13. func Test_newTLSOptionCtx(t *testing.T) {
  14. client, err := NewClient("https://ca.smallstep.com", WithTransport(http.DefaultTransport))
  15. if err != nil {
  16. t.Fatalf("NewClient() error = %v", err)
  17. }
  18. type args struct {
  19. c *Client
  20. config *tls.Config
  21. sign *api.SignResponse
  22. }
  23. tests := []struct {
  24. name string
  25. args args
  26. want *TLSOptionCtx
  27. }{
  28. {"ok", args{client, &tls.Config{}, &api.SignResponse{}}, &TLSOptionCtx{Client: client, Config: &tls.Config{}, Sign: &api.SignResponse{}, mutableConfig: newMutableTLSConfig()}},
  29. }
  30. for _, tt := range tests {
  31. t.Run(tt.name, func(t *testing.T) {
  32. if got := newTLSOptionCtx(tt.args.c, tt.args.config, tt.args.sign); !reflect.DeepEqual(got, tt.want) {
  33. t.Errorf("newTLSOptionCtx() = %v, want %v", got, tt.want)
  34. }
  35. })
  36. }
  37. }
  38. func TestTLSOptionCtx_apply(t *testing.T) {
  39. fail := func() TLSOption {
  40. return func(ctx *TLSOptionCtx) error {
  41. return fmt.Errorf("an error")
  42. }
  43. }
  44. type fields struct {
  45. Config *tls.Config
  46. }
  47. type args struct {
  48. options []TLSOption
  49. }
  50. tests := []struct {
  51. name string
  52. fields fields
  53. args args
  54. wantErr bool
  55. }{
  56. {"ok", fields{&tls.Config{}}, args{[]TLSOption{RequireAndVerifyClientCert()}}, false},
  57. {"ok", fields{&tls.Config{}}, args{[]TLSOption{VerifyClientCertIfGiven()}}, false},
  58. {"fail", fields{&tls.Config{}}, args{[]TLSOption{VerifyClientCertIfGiven(), fail()}}, true},
  59. }
  60. for _, tt := range tests {
  61. t.Run(tt.name, func(t *testing.T) {
  62. ctx := &TLSOptionCtx{
  63. Config: tt.fields.Config,
  64. mutableConfig: newMutableTLSConfig(),
  65. }
  66. if err := ctx.apply(tt.args.options); (err != nil) != tt.wantErr {
  67. t.Errorf("TLSOptionCtx.apply() error = %v, wantErr %v", err, tt.wantErr)
  68. }
  69. })
  70. }
  71. }
  72. func TestRequireAndVerifyClientCert(t *testing.T) {
  73. tests := []struct {
  74. name string
  75. want *tls.Config
  76. }{
  77. {"ok", &tls.Config{ClientAuth: tls.RequireAndVerifyClientCert}},
  78. }
  79. for _, tt := range tests {
  80. t.Run(tt.name, func(t *testing.T) {
  81. ctx := &TLSOptionCtx{
  82. Config: &tls.Config{},
  83. mutableConfig: newMutableTLSConfig(),
  84. }
  85. if err := RequireAndVerifyClientCert()(ctx); err != nil {
  86. t.Errorf("RequireAndVerifyClientCert() error = %v", err)
  87. return
  88. }
  89. if !reflect.DeepEqual(ctx.Config, tt.want) {
  90. t.Errorf("RequireAndVerifyClientCert() = %v, want %v", ctx.Config, tt.want)
  91. }
  92. })
  93. }
  94. }
  95. func TestVerifyClientCertIfGiven(t *testing.T) {
  96. tests := []struct {
  97. name string
  98. want *tls.Config
  99. }{
  100. {"ok", &tls.Config{ClientAuth: tls.VerifyClientCertIfGiven}},
  101. }
  102. for _, tt := range tests {
  103. t.Run(tt.name, func(t *testing.T) {
  104. ctx := &TLSOptionCtx{
  105. Config: &tls.Config{},
  106. mutableConfig: newMutableTLSConfig(),
  107. }
  108. if err := VerifyClientCertIfGiven()(ctx); err != nil {
  109. t.Errorf("VerifyClientCertIfGiven() error = %v", err)
  110. return
  111. }
  112. if !reflect.DeepEqual(ctx.Config, tt.want) {
  113. t.Errorf("VerifyClientCertIfGiven() = %v, want %v", ctx.Config, tt.want)
  114. }
  115. })
  116. }
  117. }
  118. func TestAddRootCA(t *testing.T) {
  119. cert := parseCertificate(rootPEM)
  120. pool := x509.NewCertPool()
  121. pool.AddCert(cert)
  122. type args struct {
  123. cert *x509.Certificate
  124. }
  125. tests := []struct {
  126. name string
  127. args args
  128. want *tls.Config
  129. }{
  130. {"ok", args{cert}, &tls.Config{RootCAs: pool}},
  131. }
  132. for _, tt := range tests {
  133. t.Run(tt.name, func(t *testing.T) {
  134. ctx := &TLSOptionCtx{
  135. Config: &tls.Config{},
  136. mutableConfig: newMutableTLSConfig(),
  137. }
  138. if err := AddRootCA(tt.args.cert)(ctx); err != nil {
  139. t.Errorf("AddRootCA() error = %v", err)
  140. return
  141. }
  142. if !reflect.DeepEqual(ctx.Config, tt.want) && !equalPools(ctx.Config.RootCAs, tt.want.RootCAs) {
  143. t.Errorf("AddRootCA() = %v, want %v", ctx.Config, tt.want)
  144. }
  145. })
  146. }
  147. }
  148. func TestAddClientCA(t *testing.T) {
  149. cert := parseCertificate(rootPEM)
  150. pool := x509.NewCertPool()
  151. pool.AddCert(cert)
  152. type args struct {
  153. cert *x509.Certificate
  154. }
  155. tests := []struct {
  156. name string
  157. args args
  158. want *tls.Config
  159. }{
  160. {"ok", args{cert}, &tls.Config{ClientCAs: pool}},
  161. }
  162. for _, tt := range tests {
  163. t.Run(tt.name, func(t *testing.T) {
  164. ctx := &TLSOptionCtx{
  165. Config: &tls.Config{},
  166. mutableConfig: newMutableTLSConfig(),
  167. }
  168. if err := AddClientCA(tt.args.cert)(ctx); err != nil {
  169. t.Errorf("AddClientCA() error = %v", err)
  170. return
  171. }
  172. if !reflect.DeepEqual(ctx.Config, tt.want) && !equalPools(ctx.Config.ClientCAs, tt.want.ClientCAs) {
  173. t.Errorf("AddClientCA() = %v, want %v", ctx.Config, tt.want)
  174. }
  175. })
  176. }
  177. }
  178. func TestAddRootsToRootCAs(t *testing.T) {
  179. ca := startCATestServer()
  180. defer ca.Close()
  181. client, err := NewClient(ca.URL, WithRootFile("testdata/secrets/root_ca.crt"))
  182. if err != nil {
  183. t.Fatal(err)
  184. }
  185. clientFail, err := NewClient(ca.URL, WithTransport(http.DefaultTransport))
  186. if err != nil {
  187. t.Fatal(err)
  188. }
  189. root, err := ioutil.ReadFile("testdata/secrets/root_ca.crt")
  190. if err != nil {
  191. t.Fatal(err)
  192. }
  193. cert := parseCertificate(string(root))
  194. pool := x509.NewCertPool()
  195. pool.AddCert(cert)
  196. type args struct {
  197. client *Client
  198. config *tls.Config
  199. }
  200. tests := []struct {
  201. name string
  202. args args
  203. want *tls.Config
  204. wantErr bool
  205. }{
  206. {"ok", args{client, &tls.Config{}}, &tls.Config{RootCAs: pool}, false},
  207. {"fail", args{clientFail, &tls.Config{}}, &tls.Config{}, true},
  208. }
  209. for _, tt := range tests {
  210. t.Run(tt.name, func(t *testing.T) {
  211. ctx := &TLSOptionCtx{
  212. Client: tt.args.client,
  213. Config: tt.args.config,
  214. mutableConfig: newMutableTLSConfig(),
  215. }
  216. if err := ctx.apply([]TLSOption{AddRootsToRootCAs()}); (err != nil) != tt.wantErr {
  217. t.Errorf("AddRootsToRootCAs() error = %v, wantErr %v", err, tt.wantErr)
  218. return
  219. }
  220. if !equalPools(ctx.Config.RootCAs, tt.want.RootCAs) {
  221. t.Errorf("AddRootsToRootCAs() = %v, want %v", ctx.Config, tt.want)
  222. }
  223. })
  224. }
  225. }
  226. func TestAddRootsToClientCAs(t *testing.T) {
  227. ca := startCATestServer()
  228. defer ca.Close()
  229. client, err := NewClient(ca.URL, WithRootFile("testdata/secrets/root_ca.crt"))
  230. if err != nil {
  231. t.Fatal(err)
  232. }
  233. clientFail, err := NewClient(ca.URL, WithTransport(http.DefaultTransport))
  234. if err != nil {
  235. t.Fatal(err)
  236. }
  237. root, err := ioutil.ReadFile("testdata/secrets/root_ca.crt")
  238. if err != nil {
  239. t.Fatal(err)
  240. }
  241. cert := parseCertificate(string(root))
  242. pool := x509.NewCertPool()
  243. pool.AddCert(cert)
  244. type args struct {
  245. client *Client
  246. config *tls.Config
  247. }
  248. tests := []struct {
  249. name string
  250. args args
  251. want *tls.Config
  252. wantErr bool
  253. }{
  254. {"ok", args{client, &tls.Config{}}, &tls.Config{ClientCAs: pool}, false},
  255. {"fail", args{clientFail, &tls.Config{}}, &tls.Config{}, true},
  256. }
  257. for _, tt := range tests {
  258. t.Run(tt.name, func(t *testing.T) {
  259. ctx := &TLSOptionCtx{
  260. Client: tt.args.client,
  261. Config: tt.args.config,
  262. mutableConfig: newMutableTLSConfig(),
  263. }
  264. if err := ctx.apply([]TLSOption{AddRootsToClientCAs()}); (err != nil) != tt.wantErr {
  265. t.Errorf("AddRootsToClientCAs() error = %v, wantErr %v", err, tt.wantErr)
  266. return
  267. }
  268. if !equalPools(ctx.Config.ClientCAs, tt.want.ClientCAs) {
  269. t.Errorf("AddRootsToClientCAs() = %v, want %v", ctx.Config, tt.want)
  270. }
  271. })
  272. }
  273. }
  274. func TestAddFederationToRootCAs(t *testing.T) {
  275. ca := startCATestServer()
  276. defer ca.Close()
  277. client, err := NewClient(ca.URL, WithRootFile("testdata/secrets/root_ca.crt"))
  278. if err != nil {
  279. t.Fatal(err)
  280. }
  281. clientFail, err := NewClient(ca.URL, WithTransport(http.DefaultTransport))
  282. if err != nil {
  283. t.Fatal(err)
  284. }
  285. root, err := ioutil.ReadFile("testdata/secrets/root_ca.crt")
  286. if err != nil {
  287. t.Fatal(err)
  288. }
  289. federated, err := ioutil.ReadFile("testdata/secrets/federated_ca.crt")
  290. if err != nil {
  291. t.Fatal(err)
  292. }
  293. crt1 := parseCertificate(string(root))
  294. crt2 := parseCertificate(string(federated))
  295. pool := x509.NewCertPool()
  296. pool.AddCert(crt1)
  297. pool.AddCert(crt2)
  298. type args struct {
  299. client *Client
  300. config *tls.Config
  301. }
  302. tests := []struct {
  303. name string
  304. args args
  305. want *tls.Config
  306. wantErr bool
  307. }{
  308. {"ok", args{client, &tls.Config{}}, &tls.Config{RootCAs: pool}, false},
  309. {"fail", args{clientFail, &tls.Config{}}, &tls.Config{}, true},
  310. }
  311. for _, tt := range tests {
  312. t.Run(tt.name, func(t *testing.T) {
  313. ctx := &TLSOptionCtx{
  314. Client: tt.args.client,
  315. Config: tt.args.config,
  316. mutableConfig: newMutableTLSConfig(),
  317. }
  318. if err := ctx.apply([]TLSOption{AddFederationToRootCAs()}); (err != nil) != tt.wantErr {
  319. t.Errorf("AddFederationToRootCAs() error = %v, wantErr %v", err, tt.wantErr)
  320. return
  321. }
  322. if !reflect.DeepEqual(ctx.Config, tt.want) {
  323. // Federated roots are randomly sorted
  324. if !equalPools(ctx.Config.RootCAs, tt.want.RootCAs) || ctx.Config.ClientCAs != nil {
  325. t.Errorf("AddFederationToRootCAs() = %v, want %v", ctx.Config, tt.want)
  326. }
  327. }
  328. })
  329. }
  330. }
  331. func TestAddFederationToClientCAs(t *testing.T) {
  332. ca := startCATestServer()
  333. defer ca.Close()
  334. client, err := NewClient(ca.URL, WithRootFile("testdata/secrets/root_ca.crt"))
  335. if err != nil {
  336. t.Fatal(err)
  337. }
  338. clientFail, err := NewClient(ca.URL, WithTransport(http.DefaultTransport))
  339. if err != nil {
  340. t.Fatal(err)
  341. }
  342. root, err := ioutil.ReadFile("testdata/secrets/root_ca.crt")
  343. if err != nil {
  344. t.Fatal(err)
  345. }
  346. federated, err := ioutil.ReadFile("testdata/secrets/federated_ca.crt")
  347. if err != nil {
  348. t.Fatal(err)
  349. }
  350. crt1 := parseCertificate(string(root))
  351. crt2 := parseCertificate(string(federated))
  352. pool := x509.NewCertPool()
  353. pool.AddCert(crt1)
  354. pool.AddCert(crt2)
  355. type args struct {
  356. client *Client
  357. config *tls.Config
  358. }
  359. tests := []struct {
  360. name string
  361. args args
  362. want *tls.Config
  363. wantErr bool
  364. }{
  365. {"ok", args{client, &tls.Config{}}, &tls.Config{ClientCAs: pool}, false},
  366. {"fail", args{clientFail, &tls.Config{}}, &tls.Config{}, true},
  367. }
  368. for _, tt := range tests {
  369. t.Run(tt.name, func(t *testing.T) {
  370. ctx := &TLSOptionCtx{
  371. Client: tt.args.client,
  372. Config: tt.args.config,
  373. mutableConfig: newMutableTLSConfig(),
  374. }
  375. if err := ctx.apply([]TLSOption{AddFederationToClientCAs()}); (err != nil) != tt.wantErr {
  376. t.Errorf("AddFederationToClientCAs() error = %v, wantErr %v", err, tt.wantErr)
  377. return
  378. }
  379. if !reflect.DeepEqual(ctx.Config, tt.want) {
  380. // Federated roots are randomly sorted
  381. if !equalPools(ctx.Config.ClientCAs, tt.want.ClientCAs) || ctx.Config.RootCAs != nil {
  382. t.Errorf("AddFederationToClientCAs() = %v, want %v", ctx.Config, tt.want)
  383. }
  384. }
  385. })
  386. }
  387. }
  388. func TestAddRootsToCAs(t *testing.T) {
  389. ca := startCATestServer()
  390. defer ca.Close()
  391. client, err := NewClient(ca.URL, WithRootFile("testdata/secrets/root_ca.crt"))
  392. if err != nil {
  393. t.Fatal(err)
  394. }
  395. clientFail, err := NewClient(ca.URL, WithTransport(http.DefaultTransport))
  396. if err != nil {
  397. t.Fatal(err)
  398. }
  399. root, err := ioutil.ReadFile("testdata/secrets/root_ca.crt")
  400. if err != nil {
  401. t.Fatal(err)
  402. }
  403. cert := parseCertificate(string(root))
  404. pool := x509.NewCertPool()
  405. pool.AddCert(cert)
  406. type args struct {
  407. client *Client
  408. config *tls.Config
  409. }
  410. tests := []struct {
  411. name string
  412. args args
  413. want *tls.Config
  414. wantErr bool
  415. }{
  416. {"ok", args{client, &tls.Config{}}, &tls.Config{ClientCAs: pool, RootCAs: pool}, false},
  417. {"fail", args{clientFail, &tls.Config{}}, &tls.Config{}, true},
  418. }
  419. for _, tt := range tests {
  420. t.Run(tt.name, func(t *testing.T) {
  421. ctx := &TLSOptionCtx{
  422. Client: tt.args.client,
  423. Config: tt.args.config,
  424. mutableConfig: newMutableTLSConfig(),
  425. }
  426. if err := ctx.apply([]TLSOption{AddRootsToCAs()}); (err != nil) != tt.wantErr {
  427. t.Errorf("AddRootsToCAs() error = %v, wantErr %v", err, tt.wantErr)
  428. return
  429. }
  430. if !equalPools(ctx.Config.RootCAs, tt.want.RootCAs) || !equalPools(ctx.Config.ClientCAs, tt.want.ClientCAs) {
  431. t.Errorf("AddRootsToCAs() = %v, want %v", ctx.Config, tt.want)
  432. }
  433. })
  434. }
  435. }
  436. func TestAddFederationToCAs(t *testing.T) {
  437. ca := startCATestServer()
  438. defer ca.Close()
  439. client, err := NewClient(ca.URL, WithRootFile("testdata/secrets/root_ca.crt"))
  440. if err != nil {
  441. t.Fatal(err)
  442. }
  443. clientFail, err := NewClient(ca.URL, WithTransport(http.DefaultTransport))
  444. if err != nil {
  445. t.Fatal(err)
  446. }
  447. root, err := ioutil.ReadFile("testdata/secrets/root_ca.crt")
  448. if err != nil {
  449. t.Fatal(err)
  450. }
  451. federated, err := ioutil.ReadFile("testdata/secrets/federated_ca.crt")
  452. if err != nil {
  453. t.Fatal(err)
  454. }
  455. crt1 := parseCertificate(string(root))
  456. crt2 := parseCertificate(string(federated))
  457. pool := x509.NewCertPool()
  458. pool.AddCert(crt1)
  459. pool.AddCert(crt2)
  460. type args struct {
  461. client *Client
  462. config *tls.Config
  463. }
  464. tests := []struct {
  465. name string
  466. args args
  467. want *tls.Config
  468. wantErr bool
  469. }{
  470. {"ok", args{client, &tls.Config{}}, &tls.Config{ClientCAs: pool, RootCAs: pool}, false},
  471. {"fail", args{clientFail, &tls.Config{}}, &tls.Config{}, true},
  472. }
  473. for _, tt := range tests {
  474. t.Run(tt.name, func(t *testing.T) {
  475. ctx := &TLSOptionCtx{
  476. Client: tt.args.client,
  477. Config: tt.args.config,
  478. mutableConfig: newMutableTLSConfig(),
  479. }
  480. if err := ctx.apply([]TLSOption{AddFederationToCAs()}); (err != nil) != tt.wantErr {
  481. t.Errorf("AddFederationToCAs() error = %v, wantErr %v", err, tt.wantErr)
  482. return
  483. }
  484. if !reflect.DeepEqual(ctx.Config, tt.want) {
  485. // Federated roots are randomly sorted
  486. if !equalPools(ctx.Config.ClientCAs, tt.want.ClientCAs) || !equalPools(ctx.Config.RootCAs, tt.want.RootCAs) {
  487. t.Errorf("AddFederationToCAs() = %v, want %v", ctx.Config, tt.want)
  488. }
  489. }
  490. })
  491. }
  492. }
  493. func equalPools(a, b *x509.CertPool) bool {
  494. if reflect.DeepEqual(a, b) {
  495. return true
  496. }
  497. subjects := a.Subjects()
  498. sA := make([]string, len(subjects))
  499. for i := range subjects {
  500. sA[i] = string(subjects[i])
  501. }
  502. subjects = b.Subjects()
  503. sB := make([]string, len(subjects))
  504. for i := range subjects {
  505. sB[i] = string(subjects[i])
  506. }
  507. sort.Strings(sA)
  508. sort.Strings(sB)
  509. return reflect.DeepEqual(sA, sB)
  510. }