requestid_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package e2e
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "net"
  7. "path/filepath"
  8. "sync"
  9. "testing"
  10. "github.com/smallstep/certificates/authority/config"
  11. "github.com/smallstep/certificates/ca"
  12. "github.com/smallstep/certificates/ca/client"
  13. "github.com/smallstep/certificates/errs"
  14. "github.com/stretchr/testify/assert"
  15. "github.com/stretchr/testify/require"
  16. "go.step.sm/crypto/minica"
  17. "go.step.sm/crypto/pemutil"
  18. )
  19. // reserveAddress "reserves" a TCP address by opening a listener on a random
  20. // port and immediately closing it. The address can then be assumed to be
  21. // available for running a server on.
  22. func reserveAddress(t *testing.T) string {
  23. t.Helper()
  24. l, err := net.Listen("tcp", "127.0.0.1:0")
  25. if err != nil {
  26. if l, err = net.Listen("tcp6", "[::1]:0"); err != nil {
  27. require.NoError(t, err, "failed to listen on a port")
  28. }
  29. }
  30. address := l.Addr().String()
  31. err = l.Close()
  32. require.NoError(t, err)
  33. return address
  34. }
  35. func Test_reflectRequestID(t *testing.T) {
  36. dir := t.TempDir()
  37. m, err := minica.New(minica.WithName("Step E2E"))
  38. require.NoError(t, err)
  39. rootFilepath := filepath.Join(dir, "root.crt")
  40. _, err = pemutil.Serialize(m.Root, pemutil.WithFilename(rootFilepath))
  41. require.NoError(t, err)
  42. intermediateCertFilepath := filepath.Join(dir, "intermediate.crt")
  43. _, err = pemutil.Serialize(m.Intermediate, pemutil.WithFilename(intermediateCertFilepath))
  44. require.NoError(t, err)
  45. intermediateKeyFilepath := filepath.Join(dir, "intermediate.key")
  46. _, err = pemutil.Serialize(m.Signer, pemutil.WithFilename(intermediateKeyFilepath))
  47. require.NoError(t, err)
  48. // get a random address to listen on and connect to; currently no nicer way to get one before starting the server
  49. // TODO(hs): find/implement a nicer way to expose the CA URL, similar to how e.g. httptest.Server exposes it?
  50. address := reserveAddress(t)
  51. cfg := &config.Config{
  52. Root: []string{rootFilepath},
  53. IntermediateCert: intermediateCertFilepath,
  54. IntermediateKey: intermediateKeyFilepath,
  55. Address: address, // reuse the address that was just "reserved"
  56. DNSNames: []string{"127.0.0.1", "[::1]", "localhost"},
  57. AuthorityConfig: &config.AuthConfig{
  58. AuthorityID: "stepca-test",
  59. DeploymentType: "standalone-test",
  60. },
  61. Logger: json.RawMessage(`{"format": "text"}`),
  62. }
  63. c, err := ca.New(cfg)
  64. require.NoError(t, err)
  65. // instantiate a client for the CA running at the random address
  66. caClient, err := ca.NewClient(
  67. fmt.Sprintf("https://%s", address),
  68. ca.WithRootFile(rootFilepath),
  69. )
  70. require.NoError(t, err)
  71. var wg sync.WaitGroup
  72. wg.Add(1)
  73. go func() {
  74. defer wg.Done()
  75. err = c.Run()
  76. require.Error(t, err) // expect error when server is stopped
  77. }()
  78. // require OK health response as the baseline
  79. ctx := context.Background()
  80. healthResponse, err := caClient.HealthWithContext(ctx)
  81. assert.NoError(t, err)
  82. assert.Equal(t, "ok", healthResponse.Status)
  83. // expect an error when retrieving an invalid root
  84. rootResponse, err := caClient.RootWithContext(ctx, "invalid")
  85. if assert.Error(t, err) {
  86. apiErr := &errs.Error{}
  87. if assert.ErrorAs(t, err, &apiErr) {
  88. assert.Equal(t, 404, apiErr.StatusCode())
  89. assert.Equal(t, "The requested resource could not be found. Please see the certificate authority logs for more info.", apiErr.Err.Error())
  90. assert.NotEmpty(t, apiErr.RequestID)
  91. // TODO: include the below error in the JSON? It's currently only output to the CA logs. Also see https://github.com/smallstep/certificates/pull/759
  92. //assert.Equal(t, "/root/invalid was not found: certificate with fingerprint invalid was not found", apiErr.Msg)
  93. }
  94. }
  95. assert.Nil(t, rootResponse)
  96. // expect an error when retrieving an invalid root and provided request ID
  97. rootResponse, err = caClient.RootWithContext(client.WithRequestID(ctx, "reqID"), "invalid")
  98. if assert.Error(t, err) {
  99. apiErr := &errs.Error{}
  100. if assert.ErrorAs(t, err, &apiErr) {
  101. assert.Equal(t, 404, apiErr.StatusCode())
  102. assert.Equal(t, "The requested resource could not be found. Please see the certificate authority logs for more info.", apiErr.Err.Error())
  103. assert.Equal(t, "reqID", apiErr.RequestID)
  104. }
  105. }
  106. assert.Nil(t, rootResponse)
  107. // done testing; stop and wait for the server to quit
  108. err = c.Stop()
  109. require.NoError(t, err)
  110. wg.Wait()
  111. }