gitdeploy/internal/webhooks/gitea/gitea.go

118 lines
3.1 KiB
Go
Raw Permalink Normal View History

2020-10-09 08:15:39 +00:00
package gitea
import (
2020-10-09 08:57:01 +00:00
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
2020-10-09 08:15:39 +00:00
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"
2020-12-01 00:48:09 +00:00
"git.rootprojects.org/root/gitdeploy/internal/log"
2020-10-09 09:22:06 +00:00
"git.rootprojects.org/root/gitdeploy/internal/options"
"git.rootprojects.org/root/gitdeploy/internal/webhooks"
2020-10-09 08:15:39 +00:00
"github.com/go-chi/chi"
)
func init() {
var secret string
name := "gitea"
options.ServerFlags.StringVar(
&secret, fmt.Sprintf("%s-secret", name), "",
fmt.Sprintf(
"secret for %s webhooks (same as %s_SECRET=)",
name, strings.ToUpper(name)),
)
webhooks.AddProvider("gitea", InitWebhook("gitea", &secret, "GITEA_SECRET"))
}
// InitWebhook prepares the webhook router.
// It should be called after arguments are parsed and ENVs are set.InitWebhook
func InitWebhook(providername string, secretList *string, envname string) func() {
2020-10-09 08:15:39 +00:00
return func() {
secrets := webhooks.ParseSecrets(providername, *secretList, envname)
if 0 == len(secrets) {
fmt.Fprintf(os.Stderr, "skipped route for missing %q\n", envname)
2020-10-09 08:15:39 +00:00
return
}
2020-10-09 08:15:39 +00:00
webhooks.AddRouteHandler(providername, func(router chi.Router) {
router.Post("/", func(w http.ResponseWriter, r *http.Request) {
r.Body = http.MaxBytesReader(w, r.Body, options.DefaultMaxBodySize)
payload, err := ioutil.ReadAll(r.Body)
if err != nil {
// if there's a read error, it should have been handled already by the MaxBytesReader
return
}
var valid bool
2020-10-09 08:57:01 +00:00
sig := r.Header.Get("X-Gitea-Signature")
sigB, err := hex.DecodeString(sig)
for _, secret := range secrets {
if ValidMAC(payload, sigB, secret) {
valid = true
break
}
}
if !valid {
log.Printf("invalid %q signature: %q\n", providername, sig)
http.Error(w, fmt.Sprintf("invalid %q signature", providername), http.StatusBadRequest)
2020-10-09 08:15:39 +00:00
return
}
info := Webhook{}
if err := json.Unmarshal(payload, &info); nil != err {
log.Printf("invalid gitea payload: error: %s\n%s\n", err, string(payload))
http.Error(w, "invalid gitea payload", http.StatusBadRequest)
return
}
2021-02-21 11:25:42 +00:00
//var tag string
//var branch string
2020-10-09 08:15:39 +00:00
ref := info.Ref // refs/heads/master
parts := strings.Split(ref, "/")
refType := parts[1] // refs/[heads]/master
prefixLen := len("refs/") + len(refType) + len("/")
refName := ref[prefixLen:]
switch refType {
case "tags":
refType = "tag"
2021-02-21 11:25:42 +00:00
//tag = refName
2020-10-09 08:15:39 +00:00
case "heads":
refType = "branch"
2021-02-21 11:25:42 +00:00
//branch = refName
2020-10-09 08:15:39 +00:00
default:
refType = "unknown"
}
webhooks.Hook(webhooks.Ref{
2021-02-21 11:25:42 +00:00
// missing Timestamp
2020-10-09 08:15:39 +00:00
HTTPSURL: info.Repository.CloneURL,
SSHURL: info.Repository.SSHURL,
Rev: info.After,
Ref: ref,
RefType: refType,
RefName: refName,
Repo: info.Repository.Name,
Owner: info.Repository.Owner.Login,
2021-02-21 11:25:42 +00:00
//Branch: branch,
//Tag: tag,
2020-10-09 08:15:39 +00:00
})
})
})
}
}
2020-10-09 08:57:01 +00:00
// ValidMAC reports whether messageMAC is a valid HMAC tag for message.
func ValidMAC(message, messageMAC, key []byte) bool {
mac := hmac.New(sha256.New, key)
mac.Write(message)
expectedMAC := mac.Sum(nil)
return hmac.Equal(messageMAC, expectedMAC)
}