4
0

regular_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package sceptest
  2. import (
  3. "crypto"
  4. "encoding/json"
  5. "fmt"
  6. "net"
  7. "net/http"
  8. "path/filepath"
  9. "sync"
  10. "testing"
  11. "github.com/stretchr/testify/assert"
  12. "github.com/stretchr/testify/require"
  13. "go.step.sm/crypto/keyutil"
  14. "go.step.sm/crypto/minica"
  15. "go.step.sm/crypto/pemutil"
  16. "github.com/smallstep/certificates/authority/config"
  17. "github.com/smallstep/certificates/authority/provisioner"
  18. "github.com/smallstep/certificates/ca"
  19. )
  20. func TestIssuesCertificateUsingRegularSCEPConfiguration(t *testing.T) {
  21. signer, err := keyutil.GenerateSigner("RSA", "", 2048)
  22. require.NoError(t, err)
  23. dir := t.TempDir()
  24. m, err := minica.New(minica.WithName("Step E2E | SCEP Regular"), minica.WithGetSignerFunc(func() (crypto.Signer, error) {
  25. return signer, nil
  26. }))
  27. require.NoError(t, err)
  28. rootFilepath := filepath.Join(dir, "root.crt")
  29. _, err = pemutil.Serialize(m.Root, pemutil.WithFilename(rootFilepath))
  30. require.NoError(t, err)
  31. intermediateCertFilepath := filepath.Join(dir, "intermediate.crt")
  32. _, err = pemutil.Serialize(m.Intermediate, pemutil.WithFilename(intermediateCertFilepath))
  33. require.NoError(t, err)
  34. intermediateKeyFilepath := filepath.Join(dir, "intermediate.key")
  35. _, err = pemutil.Serialize(m.Signer, pemutil.WithFilename(intermediateKeyFilepath))
  36. require.NoError(t, err)
  37. // get a random address to listen on and connect to; currently no nicer way to get one before starting the server
  38. // TODO(hs): find/implement a nicer way to expose the CA URL, similar to how e.g. httptest.Server exposes it?
  39. host, port := reservePort(t)
  40. prov := &provisioner.SCEP{
  41. ID: "scep",
  42. Name: "scep",
  43. Type: "SCEP",
  44. ForceCN: false,
  45. ChallengePassword: "",
  46. EncryptionAlgorithmIdentifier: 2,
  47. MinimumPublicKeyLength: 2048,
  48. Claims: &config.GlobalProvisionerClaims,
  49. }
  50. err = prov.Init(provisioner.Config{})
  51. require.NoError(t, err)
  52. cfg := &config.Config{
  53. Root: []string{rootFilepath},
  54. IntermediateCert: intermediateCertFilepath,
  55. IntermediateKey: intermediateKeyFilepath,
  56. Address: net.JoinHostPort(host, port), // reuse the address that was just "reserved"
  57. DNSNames: []string{"127.0.0.1", "[::1]", "localhost"},
  58. AuthorityConfig: &config.AuthConfig{
  59. AuthorityID: "stepca-test-scep",
  60. DeploymentType: "standalone-test",
  61. Provisioners: provisioner.List{prov},
  62. },
  63. Logger: json.RawMessage(`{"format": "text"}`),
  64. }
  65. c, err := ca.New(cfg)
  66. require.NoError(t, err)
  67. var wg sync.WaitGroup
  68. wg.Add(1)
  69. go func() {
  70. defer wg.Done()
  71. err = c.Run()
  72. require.ErrorIs(t, err, http.ErrServerClosed)
  73. }()
  74. // instantiate a client for the CA running at the random address
  75. caClient := newCAClient(t, fmt.Sprintf("https://localhost:%s", port), rootFilepath)
  76. requireHealthyCA(t, caClient)
  77. scepClient := createSCEPClient(t, fmt.Sprintf("https://localhost:%s/scep/scep", port), m.Root)
  78. cert, err := scepClient.requestCertificate(t, "test.localhost", []string{"test.localhost"})
  79. assert.NoError(t, err)
  80. require.NotNil(t, cert)
  81. assert.Equal(t, "test.localhost", cert.Subject.CommonName)
  82. assert.Equal(t, "Step E2E | SCEP Regular Intermediate CA", cert.Issuer.CommonName)
  83. // done testing; stop and wait for the server to quit
  84. err = c.Stop()
  85. require.NoError(t, err)
  86. wg.Wait()
  87. }