tls_options_test.go 13 KB

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