options.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package webhook
  2. import (
  3. "crypto/x509"
  4. "go.step.sm/crypto/sshutil"
  5. "go.step.sm/crypto/x509util"
  6. "golang.org/x/crypto/ssh"
  7. )
  8. type RequestBodyOption func(*RequestBody) error
  9. type Provisioner interface {
  10. GetName() string
  11. }
  12. func NewRequestBody(options ...RequestBodyOption) (*RequestBody, error) {
  13. rb := &RequestBody{}
  14. for _, fn := range options {
  15. if err := fn(rb); err != nil {
  16. return nil, err
  17. }
  18. }
  19. return rb, nil
  20. }
  21. // WithProvisionerName sets the provisioner name in the webhook request body
  22. // using the name from the given provisioner. If p is nil, the provisioner name
  23. // is left unchanged.
  24. func WithProvisionerName(p Provisioner) RequestBodyOption {
  25. return func(rb *RequestBody) error {
  26. if p != nil {
  27. rb.ProvisionerName = p.GetName()
  28. }
  29. return nil
  30. }
  31. }
  32. func WithX509CertificateRequest(cr *x509.CertificateRequest) RequestBodyOption {
  33. return func(rb *RequestBody) error {
  34. rb.X509CertificateRequest = &X509CertificateRequest{
  35. CertificateRequest: x509util.NewCertificateRequestFromX509(cr),
  36. PublicKeyAlgorithm: cr.PublicKeyAlgorithm.String(),
  37. Raw: cr.Raw,
  38. }
  39. if cr.PublicKey != nil {
  40. key, err := x509.MarshalPKIXPublicKey(cr.PublicKey)
  41. if err != nil {
  42. return err
  43. }
  44. rb.X509CertificateRequest.PublicKey = key
  45. }
  46. return nil
  47. }
  48. }
  49. func WithX509Certificate(cert *x509util.Certificate, leaf *x509.Certificate) RequestBodyOption {
  50. return func(rb *RequestBody) error {
  51. rb.X509Certificate = &X509Certificate{
  52. Certificate: cert,
  53. PublicKeyAlgorithm: leaf.PublicKeyAlgorithm.String(),
  54. NotBefore: leaf.NotBefore,
  55. NotAfter: leaf.NotAfter,
  56. }
  57. if leaf.PublicKey != nil {
  58. key, err := x509.MarshalPKIXPublicKey(leaf.PublicKey)
  59. if err != nil {
  60. return err
  61. }
  62. rb.X509Certificate.PublicKey = key
  63. }
  64. return nil
  65. }
  66. }
  67. func WithAttestationData(data *AttestationData) RequestBodyOption {
  68. return func(rb *RequestBody) error {
  69. rb.AttestationData = data
  70. return nil
  71. }
  72. }
  73. // WithAttestation sets the attestation evidence an ACME remote-attest-01
  74. // challenge verified.
  75. func WithAttestation(a *Attestation) RequestBodyOption {
  76. return func(rb *RequestBody) error {
  77. rb.Attestation = a
  78. return nil
  79. }
  80. }
  81. func WithAuthorizationPrincipal(p string) RequestBodyOption {
  82. return func(rb *RequestBody) error {
  83. rb.AuthorizationPrincipal = p
  84. return nil
  85. }
  86. }
  87. func WithSSHCertificateRequest(cr sshutil.CertificateRequest) RequestBodyOption {
  88. return func(rb *RequestBody) error {
  89. rb.SSHCertificateRequest = &SSHCertificateRequest{
  90. Type: cr.Type,
  91. KeyID: cr.KeyID,
  92. Principals: cr.Principals,
  93. }
  94. if cr.Key != nil {
  95. rb.SSHCertificateRequest.PublicKey = cr.Key.Marshal()
  96. }
  97. return nil
  98. }
  99. }
  100. func WithSSHCertificate(cert *sshutil.Certificate, certTpl *ssh.Certificate) RequestBodyOption {
  101. return func(rb *RequestBody) error {
  102. rb.SSHCertificate = &SSHCertificate{
  103. Certificate: cert,
  104. ValidBefore: certTpl.ValidBefore,
  105. ValidAfter: certTpl.ValidAfter,
  106. }
  107. if certTpl.Key != nil {
  108. rb.SSHCertificate.PublicKey = certTpl.Key.Marshal()
  109. }
  110. return nil
  111. }
  112. }
  113. func WithX5CCertificate(leaf *x509.Certificate) RequestBodyOption {
  114. return func(rb *RequestBody) error {
  115. rb.X5CCertificate = &X5CCertificate{
  116. Raw: leaf.Raw,
  117. PublicKeyAlgorithm: leaf.PublicKeyAlgorithm.String(),
  118. NotBefore: leaf.NotBefore,
  119. NotAfter: leaf.NotAfter,
  120. }
  121. if leaf.PublicKey != nil {
  122. key, err := x509.MarshalPKIXPublicKey(leaf.PublicKey)
  123. if err != nil {
  124. return err
  125. }
  126. rb.X5CCertificate.PublicKey = key
  127. }
  128. return nil
  129. }
  130. }