From e216104a2b74a84de4129a68fc70ed49aa968ff9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Corentin=20N=C3=A9au?= Date: Thu, 8 Aug 2024 10:27:42 +0200 Subject: [PATCH] Remove vendored go-playground/webhooks code (#2710) Vendoring is no longer needed now that this PR [1] has been merged. This commit now relies on that merged logic. [1]: https://github.com/go-playground/webhooks/pull/191 --- go.mod | 2 +- go.sum | 5 +- pkg/webhook/azuredevops/webhook.go | 111 ----------------------------- pkg/webhook/webhook.go | 7 +- pkg/webhook/webhook_test.go | 2 +- 5 files changed, 7 insertions(+), 120 deletions(-) delete mode 100644 pkg/webhook/azuredevops/webhook.go diff --git a/go.mod b/go.mod index 52c8728b05..e92ffe04f6 100644 --- a/go.mod +++ b/go.mod @@ -20,7 +20,7 @@ require ( github.com/go-git/go-billy/v5 v5.5.1-0.20240701174852-839dcd3ad096 github.com/go-git/go-git/v5 v5.12.1-0.20240629213349-b00c68ab7140 github.com/go-logr/logr v1.4.2 - github.com/go-playground/webhooks/v6 v6.3.0 + github.com/go-playground/webhooks/v6 v6.4.0 github.com/gobwas/glob v0.2.3 github.com/gogits/go-gogs-client v0.0.0-20210131175652-1d7215cd8d85 github.com/golang/mock v1.6.0 diff --git a/go.sum b/go.sum index 8a8ebf6fa3..7167ac1e07 100644 --- a/go.sum +++ b/go.sum @@ -391,8 +391,8 @@ github.com/go-openapi/jsonreference v0.21.0 h1:Rs+Y7hSXT83Jacb7kFyjn4ijOuVGSvOdF github.com/go-openapi/jsonreference v0.21.0/go.mod h1:LmZmgsrTkVg9LG4EaHeY8cBDslNPMo06cago5JNLkm4= github.com/go-openapi/swag v0.23.0 h1:vsEVJDUo2hPJ2tu0/Xc+4noaxyEffXNIs3cOULZ+GrE= github.com/go-openapi/swag v0.23.0/go.mod h1:esZ8ITTYEsH1V2trKHjAN8Ai7xHb8RV+YSZ577vPjgQ= -github.com/go-playground/webhooks/v6 v6.3.0 h1:zBLUxK1Scxwi97TmZt5j/B/rLlard2zY7P77FHg58FE= -github.com/go-playground/webhooks/v6 v6.3.0/go.mod h1:GCocmfMtpJdkEOM1uG9p2nXzg1kY5X/LtvQgtPHUaaA= +github.com/go-playground/webhooks/v6 v6.4.0 h1:KLa6y7bD19N48rxJDHM0DpE3T4grV7GxMy1b/aHMWPY= +github.com/go-playground/webhooks/v6 v6.4.0/go.mod h1:5lBxopx+cAJiBI4+kyRbuHrEi+hYRDdRHuRR4Ya5Ums= github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE= github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= @@ -408,7 +408,6 @@ github.com/gobuffalo/packr/v2 v2.8.3 h1:xE1yzvnO56cUC0sTpKR3DIbxZgB54AftTFMhB2XE github.com/gobuffalo/packr/v2 v2.8.3/go.mod h1:0SahksCVcx4IMnigTjiFuyldmTrdTctXsOdiU5KwbKc= github.com/gobwas/glob v0.2.3 h1:A4xDbljILXROh+kObIiy5kIaPYD8e96x1tgBhUI5J+Y= github.com/gobwas/glob v0.2.3/go.mod h1:d3Ez4x06l9bZtSvzIay5+Yzi0fmZzPgnTbPcKjJAkT8= -github.com/gogits/go-gogs-client v0.0.0-20200905025246-8bb8a50cb355/go.mod h1:cY2AIrMgHm6oOHmR7jY+9TtjzSjQ3iG7tURJG3Y6XH0= github.com/gogits/go-gogs-client v0.0.0-20210131175652-1d7215cd8d85 h1:04sojTxgYxu1L4Hn7Tgf7UVtIosVa6CuHtvNY+7T1K4= github.com/gogits/go-gogs-client v0.0.0-20210131175652-1d7215cd8d85/go.mod h1:cY2AIrMgHm6oOHmR7jY+9TtjzSjQ3iG7tURJG3Y6XH0= github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ= diff --git a/pkg/webhook/azuredevops/webhook.go b/pkg/webhook/azuredevops/webhook.go deleted file mode 100644 index f0a3c3095b..0000000000 --- a/pkg/webhook/azuredevops/webhook.go +++ /dev/null @@ -1,111 +0,0 @@ -// File copied from https://github.com/go-playground/webhooks/blob/master/azuredevops/azuredevops.go -// TODO Basic Auth is added here since it's not available upstream. -// Remove this file once https://github.com/go-playground/webhooks/pull/191 is merged. - -package azuredevops - -import ( - "encoding/json" - "errors" - "fmt" - "io" - "net/http" - - "github.com/go-playground/webhooks/v6/azuredevops" -) - -// parse errors -var ( - ErrInvalidHTTPMethod = errors.New("invalid HTTP Method") - ErrParsingPayload = errors.New("error parsing payload") - ErrBasicAuthVerificationFailed = errors.New("basic auth verification failed") -) - -// Option is a configuration option for the webhook -type Option func(*Webhook) error - -// Options is a namespace var for configuration options -var Options = WebhookOptions{} - -// WebhookOptions is a namespace for configuration option methods -type WebhookOptions struct{} - -// BasicAuth verifies payload using basic auth -func (WebhookOptions) BasicAuth(username, password string) Option { - return func(hook *Webhook) error { - hook.username = username - hook.password = password - return nil - } -} - -// Webhook instance contains all methods needed to process events -type Webhook struct { - username string - password string -} - -// New creates and returns a WebHook instance -func New(options ...Option) (*Webhook, error) { - hook := new(Webhook) - for _, opt := range options { - if err := opt(hook); err != nil { - return nil, errors.New("Error applying Option") - } - } - return hook, nil -} - -// Parse verifies and parses the events specified and returns the payload object or an error -func (hook Webhook) Parse(r *http.Request, events ...azuredevops.Event) (interface{}, error) { - defer func() { - _, _ = io.Copy(io.Discard, r.Body) - _ = r.Body.Close() - }() - - if !hook.verifyBasicAuth(r) { - return nil, ErrBasicAuthVerificationFailed - } - - if r.Method != http.MethodPost { - return nil, ErrInvalidHTTPMethod - } - - payload, err := io.ReadAll(r.Body) - if err != nil || len(payload) == 0 { - return nil, ErrParsingPayload - } - - var pl azuredevops.BasicEvent - err = json.Unmarshal([]byte(payload), &pl) - if err != nil { - return nil, ErrParsingPayload - } - - switch pl.EventType { - case azuredevops.GitPushEventType: - var fpl azuredevops.GitPushEvent - err = json.Unmarshal([]byte(payload), &fpl) - return fpl, err - case azuredevops.GitPullRequestCreatedEventType, azuredevops.GitPullRequestMergedEventType, azuredevops.GitPullRequestUpdatedEventType: - var fpl azuredevops.GitPullRequestEvent - err = json.Unmarshal([]byte(payload), &fpl) - return fpl, err - case azuredevops.BuildCompleteEventType: - var fpl azuredevops.BuildCompleteEvent - err = json.Unmarshal([]byte(payload), &fpl) - return fpl, err - default: - return nil, fmt.Errorf("unknown event %s", pl.EventType) - } -} - -func (hook Webhook) verifyBasicAuth(r *http.Request) bool { - // skip validation if username or password was not provided - if hook.username == "" && hook.password == "" { - return true - } - username, password, ok := r.BasicAuth() - - return ok && username == hook.username && password == hook.password -} diff --git a/pkg/webhook/webhook.go b/pkg/webhook/webhook.go index 71d4e57e31..a7e94c58d5 100644 --- a/pkg/webhook/webhook.go +++ b/pkg/webhook/webhook.go @@ -10,7 +10,7 @@ import ( "time" "github.com/go-logr/logr" - goPlaygroundAzuredevops "github.com/go-playground/webhooks/v6/azuredevops" + "github.com/go-playground/webhooks/v6/azuredevops" gogsclient "github.com/gogits/go-gogs-client" "github.com/gorilla/mux" "gopkg.in/go-playground/webhooks.v5/bitbucket" @@ -20,7 +20,6 @@ import ( "gopkg.in/go-playground/webhooks.v5/gogs" v1alpha1 "github.com/rancher/fleet/pkg/apis/fleet.cattle.io/v1alpha1" - "github.com/rancher/fleet/pkg/webhook/azuredevops" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -171,7 +170,7 @@ func (w *Webhook) ServeHTTP(rw http.ResponseWriter, r *http.Request) { case r.Header.Get("X-Event-Key") != "": payload, err = w.bitbucketServer.Parse(r, bitbucketserver.RepositoryReferenceChangedEvent) case r.Header.Get("X-Vss-Activityid") != "" || r.Header.Get("X-Vss-Subscriptionid") != "": - payload, err = w.azureDevops.Parse(r, goPlaygroundAzuredevops.GitPushEventType) + payload, err = w.azureDevops.Parse(r, azuredevops.GitPushEventType) default: w.log.V(1).Info("Ignoring unknown webhook event") return @@ -366,7 +365,7 @@ func parsePayload(payload interface{}) (revision, branch, tag string, repoURLs [ repoURLs = append(repoURLs, t.Repo.HTMLURL) branch, tag = getBranchTagFromRef(t.Ref) revision = t.After - case goPlaygroundAzuredevops.GitPushEvent: + case azuredevops.GitPushEvent: repoURLs = append(repoURLs, t.Resource.Repository.RemoteURL) for _, refUpdate := range t.Resource.RefUpdates { branch, tag = getBranchTagFromRef(refUpdate.Name) diff --git a/pkg/webhook/webhook_test.go b/pkg/webhook/webhook_test.go index 1b9bf64b94..53dd5d4ab1 100644 --- a/pkg/webhook/webhook_test.go +++ b/pkg/webhook/webhook_test.go @@ -9,8 +9,8 @@ import ( "k8s.io/apimachinery/pkg/types" "k8s.io/kubectl/pkg/scheme" + "github.com/go-playground/webhooks/v6/azuredevops" v1alpha1 "github.com/rancher/fleet/pkg/apis/fleet.cattle.io/v1alpha1" - "github.com/rancher/fleet/pkg/webhook/azuredevops" "gopkg.in/go-playground/webhooks.v5/github" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" cfake "sigs.k8s.io/controller-runtime/pkg/client/fake"