tls_options_test.go 14 KB

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