4
0

webhook.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. package api
  2. import (
  3. "encoding/base64"
  4. "fmt"
  5. "net/http"
  6. "net/url"
  7. "github.com/go-chi/chi/v5"
  8. "github.com/smallstep/certificates/api/read"
  9. "github.com/smallstep/certificates/api/render"
  10. "github.com/smallstep/certificates/authority/admin"
  11. "github.com/smallstep/linkedca"
  12. "go.step.sm/crypto/randutil"
  13. )
  14. // WebhookAdminResponder is the interface responsible for writing webhook admin
  15. // responses.
  16. type WebhookAdminResponder interface {
  17. CreateProvisionerWebhook(w http.ResponseWriter, r *http.Request)
  18. UpdateProvisionerWebhook(w http.ResponseWriter, r *http.Request)
  19. DeleteProvisionerWebhook(w http.ResponseWriter, r *http.Request)
  20. }
  21. // webhoookAdminResponder implements WebhookAdminResponder
  22. type webhookAdminResponder struct{}
  23. // NewWebhookAdminResponder returns a new WebhookAdminResponder
  24. func NewWebhookAdminResponder() WebhookAdminResponder {
  25. return &webhookAdminResponder{}
  26. }
  27. func validateWebhook(webhook *linkedca.Webhook) error {
  28. if webhook == nil {
  29. return nil
  30. }
  31. // name
  32. if webhook.Name == "" {
  33. return admin.NewError(admin.ErrorBadRequestType, "webhook name is required")
  34. }
  35. // url
  36. parsedURL, err := url.Parse(webhook.Url)
  37. if err != nil {
  38. return admin.NewError(admin.ErrorBadRequestType, "webhook url is invalid")
  39. }
  40. if parsedURL.Host == "" {
  41. return admin.NewError(admin.ErrorBadRequestType, "webhook url is invalid")
  42. }
  43. if parsedURL.Scheme != "https" {
  44. return admin.NewError(admin.ErrorBadRequestType, "webhook url must use https")
  45. }
  46. if parsedURL.User != nil {
  47. return admin.NewError(admin.ErrorBadRequestType, "webhook url may not contain username or password")
  48. }
  49. // kind
  50. if _, ok := linkedca.Webhook_Kind_name[int32(webhook.Kind)]; !ok || webhook.Kind == linkedca.Webhook_NO_KIND {
  51. return admin.NewError(admin.ErrorBadRequestType, "webhook kind %q is invalid", webhook.Kind)
  52. }
  53. return nil
  54. }
  55. func (war *webhookAdminResponder) CreateProvisionerWebhook(w http.ResponseWriter, r *http.Request) {
  56. ctx := r.Context()
  57. auth := mustAuthority(ctx)
  58. prov := linkedca.MustProvisionerFromContext(ctx)
  59. var newWebhook = new(linkedca.Webhook)
  60. if err := read.ProtoJSON(r.Body, newWebhook); err != nil {
  61. render.Error(w, r, err)
  62. return
  63. }
  64. if err := validateWebhook(newWebhook); err != nil {
  65. render.Error(w, r, err)
  66. return
  67. }
  68. if newWebhook.Secret != "" {
  69. err := admin.NewError(admin.ErrorBadRequestType, "webhook secret must not be set")
  70. render.Error(w, r, err)
  71. return
  72. }
  73. if newWebhook.Id != "" {
  74. err := admin.NewError(admin.ErrorBadRequestType, "webhook ID must not be set")
  75. render.Error(w, r, err)
  76. return
  77. }
  78. newWebhook.Id = randutil.UUIDv4()
  79. // verify the name is unique
  80. for _, wh := range prov.Webhooks {
  81. if wh.Name == newWebhook.Name {
  82. err := admin.NewError(admin.ErrorConflictType, "provisioner %q already has a webhook with the name %q", prov.Name, newWebhook.Name)
  83. render.Error(w, r, err)
  84. return
  85. }
  86. }
  87. newWebhook.Secret = base64.StdEncoding.EncodeToString(randutil.Bytes(64))
  88. prov.Webhooks = append(prov.Webhooks, newWebhook)
  89. if err := auth.UpdateProvisioner(ctx, prov); err != nil {
  90. if isBadRequest(err) {
  91. render.Error(w, r, admin.WrapError(admin.ErrorBadRequestType, err, "error creating provisioner webhook"))
  92. return
  93. }
  94. render.Error(w, r, admin.WrapErrorISE(err, "error creating provisioner webhook"))
  95. return
  96. }
  97. render.ProtoJSONStatus(w, newWebhook, http.StatusCreated)
  98. }
  99. func (war *webhookAdminResponder) DeleteProvisionerWebhook(w http.ResponseWriter, r *http.Request) {
  100. ctx := r.Context()
  101. auth := mustAuthority(ctx)
  102. prov := linkedca.MustProvisionerFromContext(ctx)
  103. webhookName := chi.URLParam(r, "webhookName")
  104. found := false
  105. for i, wh := range prov.Webhooks {
  106. if wh.Name == webhookName {
  107. prov.Webhooks = append(prov.Webhooks[0:i], prov.Webhooks[i+1:]...)
  108. found = true
  109. break
  110. }
  111. }
  112. if !found {
  113. render.JSONStatus(w, r, DeleteResponse{Status: "ok"}, http.StatusOK)
  114. return
  115. }
  116. if err := auth.UpdateProvisioner(ctx, prov); err != nil {
  117. if isBadRequest(err) {
  118. render.Error(w, r, admin.WrapError(admin.ErrorBadRequestType, err, "error deleting provisioner webhook"))
  119. return
  120. }
  121. render.Error(w, r, admin.WrapErrorISE(err, "error deleting provisioner webhook"))
  122. return
  123. }
  124. render.JSONStatus(w, r, DeleteResponse{Status: "ok"}, http.StatusOK)
  125. }
  126. func (war *webhookAdminResponder) UpdateProvisionerWebhook(w http.ResponseWriter, r *http.Request) {
  127. ctx := r.Context()
  128. auth := mustAuthority(ctx)
  129. prov := linkedca.MustProvisionerFromContext(ctx)
  130. var newWebhook = new(linkedca.Webhook)
  131. if err := read.ProtoJSON(r.Body, newWebhook); err != nil {
  132. render.Error(w, r, err)
  133. return
  134. }
  135. if err := validateWebhook(newWebhook); err != nil {
  136. render.Error(w, r, err)
  137. return
  138. }
  139. found := false
  140. for i, wh := range prov.Webhooks {
  141. if wh.Name != newWebhook.Name {
  142. continue
  143. }
  144. if newWebhook.Secret != "" && newWebhook.Secret != wh.Secret {
  145. err := admin.NewError(admin.ErrorBadRequestType, "webhook secret cannot be updated")
  146. render.Error(w, r, err)
  147. return
  148. }
  149. newWebhook.Secret = wh.Secret
  150. if newWebhook.Id != "" && newWebhook.Id != wh.Id {
  151. err := admin.NewError(admin.ErrorBadRequestType, "webhook ID cannot be updated")
  152. render.Error(w, r, err)
  153. return
  154. }
  155. newWebhook.Id = wh.Id
  156. prov.Webhooks[i] = newWebhook
  157. found = true
  158. break
  159. }
  160. if !found {
  161. msg := fmt.Sprintf("provisioner %q has no webhook with the name %q", prov.Name, newWebhook.Name)
  162. err := admin.NewError(admin.ErrorNotFoundType, msg)
  163. render.Error(w, r, err)
  164. return
  165. }
  166. if err := auth.UpdateProvisioner(ctx, prov); err != nil {
  167. if isBadRequest(err) {
  168. render.Error(w, r, admin.WrapError(admin.ErrorBadRequestType, err, "error updating provisioner webhook"))
  169. return
  170. }
  171. render.Error(w, r, admin.WrapErrorISE(err, "error updating provisioner webhook"))
  172. return
  173. }
  174. // Return a copy without the signing secret. Include the client-supplied
  175. // auth secrets since those may have been updated in this request and we
  176. // should show in the response that they changed
  177. whResponse := &linkedca.Webhook{
  178. Id: newWebhook.Id,
  179. Name: newWebhook.Name,
  180. Url: newWebhook.Url,
  181. Kind: newWebhook.Kind,
  182. CertType: newWebhook.CertType,
  183. Auth: newWebhook.Auth,
  184. DisableTlsClientAuth: newWebhook.DisableTlsClientAuth,
  185. }
  186. render.ProtoJSONStatus(w, whResponse, http.StatusCreated)
  187. }