2
0

webhook.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package gitinstallation
  2. import (
  3. "crypto/hmac"
  4. "crypto/sha256"
  5. "encoding/hex"
  6. "io/ioutil"
  7. "net/http"
  8. "strings"
  9. "github.com/google/go-github/v41/github"
  10. "github.com/porter-dev/porter/api/server/authz"
  11. "github.com/porter-dev/porter/api/server/handlers"
  12. "github.com/porter-dev/porter/api/server/shared"
  13. "github.com/porter-dev/porter/api/server/shared/apierrors"
  14. "github.com/porter-dev/porter/api/server/shared/config"
  15. "gorm.io/gorm"
  16. ints "github.com/porter-dev/porter/internal/models/integrations"
  17. )
  18. type GithubAppWebhookHandler struct {
  19. handlers.PorterHandlerReadWriter
  20. authz.KubernetesAgentGetter
  21. }
  22. func NewGithubAppWebhookHandler(
  23. config *config.Config,
  24. decoderValidator shared.RequestDecoderValidator,
  25. writer shared.ResultWriter,
  26. ) *GithubAppWebhookHandler {
  27. return &GithubAppWebhookHandler{
  28. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  29. }
  30. }
  31. func (c *GithubAppWebhookHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  32. payload, err := ioutil.ReadAll(r.Body)
  33. if err != nil {
  34. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  35. return
  36. }
  37. // verify webhook secret
  38. signature := r.Header.Get("X-Hub-Signature-256")
  39. if !verifySignature([]byte(c.Config().GithubAppConf.WebhookSecret), signature, payload) {
  40. c.HandleAPIError(w, r, apierrors.NewErrForbidden(err))
  41. return
  42. }
  43. event, err := github.ParseWebHook(github.WebHookType(r), payload)
  44. if err != nil {
  45. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  46. return
  47. }
  48. switch e := event.(type) {
  49. case *github.InstallationEvent:
  50. if *e.Action == "created" {
  51. _, err := c.Repo().GithubAppInstallation().ReadGithubAppInstallationByAccountID(*e.Installation.Account.ID)
  52. if err != nil && err == gorm.ErrRecordNotFound {
  53. // insert account/installation pair into database
  54. _, err := c.Repo().GithubAppInstallation().CreateGithubAppInstallation(&ints.GithubAppInstallation{
  55. AccountID: *e.Installation.Account.ID,
  56. InstallationID: *e.Installation.ID,
  57. })
  58. if err != nil {
  59. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  60. }
  61. return
  62. } else if err != nil {
  63. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  64. return
  65. }
  66. }
  67. if *e.Action == "deleted" {
  68. err := c.Repo().GithubAppInstallation().DeleteGithubAppInstallationByAccountID(*e.Installation.Account.ID)
  69. if err != nil {
  70. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  71. return
  72. }
  73. }
  74. }
  75. }
  76. // verifySignature verifies a signature based on hmac protocal
  77. // https://docs.github.com/en/developers/webhooks-and-events/webhooks/securing-your-webhooks
  78. func verifySignature(secret []byte, signature string, body []byte) bool {
  79. if len(signature) != 71 || !strings.HasPrefix(signature, "sha256=") {
  80. return false
  81. }
  82. actual := make([]byte, 32)
  83. _, err := hex.Decode(actual, []byte(signature[7:]))
  84. if err != nil {
  85. return false
  86. }
  87. computed := hmac.New(sha256.New, secret)
  88. _, err = computed.Write(body)
  89. if err != nil {
  90. return false
  91. }
  92. return hmac.Equal(computed.Sum(nil), actual)
  93. }