options_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package webhook
  2. import (
  3. "crypto/x509"
  4. "crypto/x509/pkix"
  5. "testing"
  6. "time"
  7. "github.com/smallstep/assert"
  8. "go.step.sm/crypto/sshutil"
  9. "go.step.sm/crypto/x509util"
  10. "golang.org/x/crypto/ssh"
  11. )
  12. func TestNewRequestBody(t *testing.T) {
  13. t1 := time.Now()
  14. t2 := t1.Add(time.Hour)
  15. type test struct {
  16. options []RequestBodyOption
  17. want *RequestBody
  18. wantErr bool
  19. }
  20. tests := map[string]test{
  21. "Permanent Identifier": {
  22. options: []RequestBodyOption{WithAttestationData(&AttestationData{PermanentIdentifier: "mydevice123"})},
  23. want: &RequestBody{
  24. AttestationData: &AttestationData{
  25. PermanentIdentifier: "mydevice123",
  26. },
  27. },
  28. wantErr: false,
  29. },
  30. "X509 Certificate Request": {
  31. options: []RequestBodyOption{
  32. WithX509CertificateRequest(&x509.CertificateRequest{
  33. PublicKeyAlgorithm: x509.ECDSA,
  34. Subject: pkix.Name{CommonName: "foo"},
  35. Raw: []byte("csr der"),
  36. }),
  37. },
  38. want: &RequestBody{
  39. X509CertificateRequest: &X509CertificateRequest{
  40. CertificateRequest: &x509util.CertificateRequest{
  41. PublicKeyAlgorithm: x509.ECDSA,
  42. Subject: x509util.Subject{CommonName: "foo"},
  43. },
  44. PublicKeyAlgorithm: "ECDSA",
  45. Raw: []byte("csr der"),
  46. },
  47. },
  48. wantErr: false,
  49. },
  50. "X509 Certificate": {
  51. options: []RequestBodyOption{
  52. WithX509Certificate(&x509util.Certificate{}, &x509.Certificate{
  53. NotBefore: t1,
  54. NotAfter: t2,
  55. PublicKeyAlgorithm: x509.ECDSA,
  56. }),
  57. },
  58. want: &RequestBody{
  59. X509Certificate: &X509Certificate{
  60. Certificate: &x509util.Certificate{},
  61. PublicKeyAlgorithm: "ECDSA",
  62. NotBefore: t1,
  63. NotAfter: t2,
  64. },
  65. },
  66. },
  67. "SSH Certificate Request": {
  68. options: []RequestBodyOption{
  69. WithSSHCertificateRequest(sshutil.CertificateRequest{
  70. Type: "User",
  71. KeyID: "key1",
  72. Principals: []string{"areed", "other"},
  73. })},
  74. want: &RequestBody{
  75. SSHCertificateRequest: &SSHCertificateRequest{
  76. Type: "User",
  77. KeyID: "key1",
  78. Principals: []string{"areed", "other"},
  79. },
  80. },
  81. wantErr: false,
  82. },
  83. "SSH Certificate": {
  84. options: []RequestBodyOption{
  85. WithSSHCertificate(
  86. &sshutil.Certificate{},
  87. &ssh.Certificate{
  88. ValidAfter: uint64(t1.Unix()),
  89. ValidBefore: uint64(t2.Unix()),
  90. },
  91. ),
  92. },
  93. want: &RequestBody{
  94. SSHCertificate: &SSHCertificate{
  95. Certificate: &sshutil.Certificate{},
  96. ValidAfter: uint64(t1.Unix()),
  97. ValidBefore: uint64(t2.Unix()),
  98. },
  99. },
  100. wantErr: false,
  101. },
  102. }
  103. for name, test := range tests {
  104. t.Run(name, func(t *testing.T) {
  105. got, err := NewRequestBody(test.options...)
  106. if (err != nil) != test.wantErr {
  107. t.Fatalf("Got err %v, wanted %t", err, test.wantErr)
  108. }
  109. assert.Equals(t, test.want, got)
  110. })
  111. }
  112. }