tls_options_test.go 14 KB

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