requestid_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. func TestXxx(t *testing.T) {
  20. dir := t.TempDir()
  21. m, err := minica.New(minica.WithName("Step E2E"))
  22. require.NoError(t, err)
  23. rootFilepath := filepath.Join(dir, "root.crt")
  24. _, err = pemutil.Serialize(m.Root, pemutil.WithFilename(rootFilepath))
  25. require.NoError(t, err)
  26. intermediateCertFilepath := filepath.Join(dir, "intermediate.crt")
  27. _, err = pemutil.Serialize(m.Intermediate, pemutil.WithFilename(intermediateCertFilepath))
  28. require.NoError(t, err)
  29. intermediateKeyFilepath := filepath.Join(dir, "intermediate.key")
  30. _, err = pemutil.Serialize(m.Signer, pemutil.WithFilename(intermediateKeyFilepath))
  31. require.NoError(t, err)
  32. // get a random address to listen on and connect to; currently no nicer way to get one before starting the server
  33. l, err := net.Listen("tcp", "127.0.0.1:0")
  34. require.NoError(t, err)
  35. randomAddress := l.Addr().String()
  36. err = l.Close()
  37. require.NoError(t, err)
  38. cfg := &config.Config{
  39. Root: []string{rootFilepath},
  40. IntermediateCert: intermediateCertFilepath,
  41. IntermediateKey: intermediateKeyFilepath,
  42. Address: randomAddress, // reuse the address that was just "reserved"
  43. DNSNames: []string{"127.0.0.1", "stepca.localhost"},
  44. AuthorityConfig: &config.AuthConfig{
  45. AuthorityID: "stepca-test",
  46. DeploymentType: "standalone-test",
  47. },
  48. Logger: json.RawMessage(`{"format": "text"}`),
  49. }
  50. c, err := ca.New(cfg)
  51. require.NoError(t, err)
  52. // instantiate a client for the CA running at the random address
  53. caClient, err := ca.NewClient(
  54. fmt.Sprintf("https://%s", randomAddress),
  55. ca.WithRootFile(rootFilepath),
  56. )
  57. require.NoError(t, err)
  58. var wg sync.WaitGroup
  59. wg.Add(1)
  60. go func() {
  61. defer wg.Done()
  62. err = c.Run()
  63. require.Error(t, err) // expect error when server is stopped
  64. }()
  65. // require OK health response as the baseline
  66. ctx := context.Background()
  67. healthResponse, err := caClient.HealthWithContext(ctx)
  68. assert.NoError(t, err)
  69. assert.Equal(t, "ok", healthResponse.Status)
  70. // expect an error when retrieving an invalid root
  71. rootResponse, err := caClient.RootWithContext(ctx, "invalid")
  72. if assert.Error(t, err) {
  73. apiErr := &errs.Error{}
  74. if assert.ErrorAs(t, err, &apiErr) {
  75. assert.Equal(t, 404, apiErr.StatusCode())
  76. assert.Equal(t, "The requested resource could not be found. Please see the certificate authority logs for more info.", apiErr.Err.Error())
  77. assert.NotEmpty(t, apiErr.RequestID)
  78. // TODO: include the below error in the JSON? It's currently only output to the CA logs
  79. //assert.Equal(t, "/root/invalid was not found: certificate with fingerprint invalid was not found", apiErr.Msg)
  80. }
  81. }
  82. assert.Nil(t, rootResponse)
  83. // expect an error when retrieving an invalid root and provided request ID
  84. rootResponse, err = caClient.RootWithContext(client.WithRequestID(ctx, "reqID"), "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.Equal(t, "reqID", apiErr.RequestID)
  91. // TODO: include the below error in the JSON? It's currently only output to the CA logs
  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. // done testing; stop and wait for the server to quit
  97. err = c.Stop()
  98. require.NoError(t, err)
  99. wg.Wait()
  100. }