webhook.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. "go.step.sm/crypto/randutil"
  12. "go.step.sm/linkedca"
  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. id, err := randutil.UUIDv4()
  79. if err != nil {
  80. render.Error(w, r, admin.WrapErrorISE(err, "error generating webhook id"))
  81. return
  82. }
  83. newWebhook.Id = id
  84. // verify the name is unique
  85. for _, wh := range prov.Webhooks {
  86. if wh.Name == newWebhook.Name {
  87. err := admin.NewError(admin.ErrorConflictType, "provisioner %q already has a webhook with the name %q", prov.Name, newWebhook.Name)
  88. render.Error(w, r, err)
  89. return
  90. }
  91. }
  92. secret, err := randutil.Bytes(64)
  93. if err != nil {
  94. render.Error(w, r, admin.WrapErrorISE(err, "error generating webhook secret"))
  95. return
  96. }
  97. newWebhook.Secret = base64.StdEncoding.EncodeToString(secret)
  98. prov.Webhooks = append(prov.Webhooks, newWebhook)
  99. if err := auth.UpdateProvisioner(ctx, prov); err != nil {
  100. if isBadRequest(err) {
  101. render.Error(w, r, admin.WrapError(admin.ErrorBadRequestType, err, "error creating provisioner webhook"))
  102. return
  103. }
  104. render.Error(w, r, admin.WrapErrorISE(err, "error creating provisioner webhook"))
  105. return
  106. }
  107. render.ProtoJSONStatus(w, newWebhook, http.StatusCreated)
  108. }
  109. func (war *webhookAdminResponder) DeleteProvisionerWebhook(w http.ResponseWriter, r *http.Request) {
  110. ctx := r.Context()
  111. auth := mustAuthority(ctx)
  112. prov := linkedca.MustProvisionerFromContext(ctx)
  113. webhookName := chi.URLParam(r, "webhookName")
  114. found := false
  115. for i, wh := range prov.Webhooks {
  116. if wh.Name == webhookName {
  117. prov.Webhooks = append(prov.Webhooks[0:i], prov.Webhooks[i+1:]...)
  118. found = true
  119. break
  120. }
  121. }
  122. if !found {
  123. render.JSONStatus(w, r, DeleteResponse{Status: "ok"}, http.StatusOK)
  124. return
  125. }
  126. if err := auth.UpdateProvisioner(ctx, prov); err != nil {
  127. if isBadRequest(err) {
  128. render.Error(w, r, admin.WrapError(admin.ErrorBadRequestType, err, "error deleting provisioner webhook"))
  129. return
  130. }
  131. render.Error(w, r, admin.WrapErrorISE(err, "error deleting provisioner webhook"))
  132. return
  133. }
  134. render.JSONStatus(w, r, DeleteResponse{Status: "ok"}, http.StatusOK)
  135. }
  136. func (war *webhookAdminResponder) UpdateProvisionerWebhook(w http.ResponseWriter, r *http.Request) {
  137. ctx := r.Context()
  138. auth := mustAuthority(ctx)
  139. prov := linkedca.MustProvisionerFromContext(ctx)
  140. var newWebhook = new(linkedca.Webhook)
  141. if err := read.ProtoJSON(r.Body, newWebhook); err != nil {
  142. render.Error(w, r, err)
  143. return
  144. }
  145. if err := validateWebhook(newWebhook); err != nil {
  146. render.Error(w, r, err)
  147. return
  148. }
  149. found := false
  150. for i, wh := range prov.Webhooks {
  151. if wh.Name != newWebhook.Name {
  152. continue
  153. }
  154. if newWebhook.Secret != "" && newWebhook.Secret != wh.Secret {
  155. err := admin.NewError(admin.ErrorBadRequestType, "webhook secret cannot be updated")
  156. render.Error(w, r, err)
  157. return
  158. }
  159. newWebhook.Secret = wh.Secret
  160. if newWebhook.Id != "" && newWebhook.Id != wh.Id {
  161. err := admin.NewError(admin.ErrorBadRequestType, "webhook ID cannot be updated")
  162. render.Error(w, r, err)
  163. return
  164. }
  165. newWebhook.Id = wh.Id
  166. prov.Webhooks[i] = newWebhook
  167. found = true
  168. break
  169. }
  170. if !found {
  171. msg := fmt.Sprintf("provisioner %q has no webhook with the name %q", prov.Name, newWebhook.Name)
  172. err := admin.NewError(admin.ErrorNotFoundType, msg)
  173. render.Error(w, r, err)
  174. return
  175. }
  176. if err := auth.UpdateProvisioner(ctx, prov); err != nil {
  177. if isBadRequest(err) {
  178. render.Error(w, r, admin.WrapError(admin.ErrorBadRequestType, err, "error updating provisioner webhook"))
  179. return
  180. }
  181. render.Error(w, r, admin.WrapErrorISE(err, "error updating provisioner webhook"))
  182. return
  183. }
  184. // Return a copy without the signing secret. Include the client-supplied
  185. // auth secrets since those may have been updated in this request and we
  186. // should show in the response that they changed
  187. whResponse := &linkedca.Webhook{
  188. Id: newWebhook.Id,
  189. Name: newWebhook.Name,
  190. Url: newWebhook.Url,
  191. Kind: newWebhook.Kind,
  192. CertType: newWebhook.CertType,
  193. Auth: newWebhook.Auth,
  194. DisableTlsClientAuth: newWebhook.DisableTlsClientAuth,
  195. }
  196. render.ProtoJSONStatus(w, whResponse, http.StatusCreated)
  197. }