webhook.go 6.4 KB

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