Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
chore: refactor for webhook (#2977)
ecmadaod9b2757ac719
diff --git a/server/webhook.go b/server/webhook.goindex 5db7406f19..d06b63689b 100644--- a/server/webhook.go+++ b/server/webhook.go@@ -58,10 +58,14 @@ func (s *Server) registerWebhookRoutes(g *echo.Group) {}repositoryID := fmt.Sprintf("%v", pushEvent.Project.ID)- filter := func(token string) (bool, error) {- return c.Request().Header.Get("X-Gitlab-Token") == token, nil+ filter := func(repo *api.Repository) (bool, error) {+ if c.Request().Header.Get("X-Gitlab-Token") != repo.WebhookSecretToken {+ return false, nil+ }++ return s.isWebhookEventBranch(pushEvent.Ref, repo.BranchFilter)}- repositoryList, err := s.filterRepository(ctx, c.Param("id"), pushEvent.Ref, repositoryID, filter)+ repositoryList, err := s.filterRepository(ctx, c.Param("id"), repositoryID, filter)if err != nil {return err}@@ -107,14 +111,18 @@ func (s *Server) registerWebhookRoutes(g *echo.Group) {}repositoryID := pushEvent.Repository.FullName- filter := func(token string) (bool, error) {- ok, err := validateGitHubWebhookSignature256(c.Request().Header.Get("X-Hub-Signature-256"), token, body)+ filter := func(repo *api.Repository) (bool, error) {+ ok, err := validateGitHubWebhookSignature256(c.Request().Header.Get("X-Hub-Signature-256"), repo.WebhookSecretToken, body)if err != nil {return false, echo.NewHTTPError(http.StatusInternalServerError, "Failed to validate GitHub webhook signature").SetInternal(err)}- return ok, nil+ if !ok {+ return false, nil+ }++ return s.isWebhookEventBranch(pushEvent.Ref, repo.BranchFilter)}- repositoryList, err := s.filterRepository(ctx, c.Param("id"), pushEvent.Ref, repositoryID, filter)+ repositoryList, err := s.filterRepository(ctx, c.Param("id"), repositoryID, filter)if err != nil {return err}@@ -133,9 +141,9 @@ func (s *Server) registerWebhookRoutes(g *echo.Group) {})}-type repositoryFilter func(string) (bool, error)+type repositoryFilter func(*api.Repository) (bool, error)-func (s *Server) filterRepository(ctx context.Context, webhookEndpointID string, pushEventRef, pushEventRepositoryID string, filter repositoryFilter) ([]*api.Repository, error) {+func (s *Server) filterRepository(ctx context.Context, webhookEndpointID string, pushEventRepositoryID string, filter repositoryFilter) ([]*api.Repository, error) {repos, err := s.store.FindRepository(ctx, &api.RepositoryFind{WebhookEndpointID: &webhookEndpointID})if err != nil {return nil, echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to respond webhook event for endpoint: %v", webhookEndpointID)).SetInternal(err)@@ -144,17 +152,8 @@ func (s *Server) filterRepository(ctx context.Context, webhookEndpointID string,return nil, echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Repository for webhook endpoint %s not found", webhookEndpointID))}- branch, err := parseBranchNameFromRefs(pushEventRef)- if err != nil {- return nil, echo.NewHTTPError(http.StatusBadRequest, "Invalid ref: %s", pushEventRef).SetInternal(err)- }-var filteredRepos []*api.Repositoryfor _, repo := range repos {- if repo.BranchFilter != branch {- log.Debug("Skipping repo due to branch filter mismatch", zap.Int("repoID", repo.ID), zap.String("branch", branch), zap.String("filter", repo.BranchFilter))- continue- }if repo.VCS == nil {log.Debug("Skipping repo due to missing VCS", zap.Int("repoID", repo.ID))continue@@ -164,7 +163,7 @@ func (s *Server) filterRepository(ctx context.Context, webhookEndpointID string,continue}- ok, err := filter(repo.WebhookSecretToken)+ ok, err := filter(repo)if err != nil {return nil, err}@@ -178,6 +177,19 @@ func (s *Server) filterRepository(ctx context.Context, webhookEndpointID string,return filteredRepos, nil}+func (*Server) isWebhookEventBranch(pushEventRef, branchFilter string) (bool, error) {+ branch, err := parseBranchNameFromRefs(pushEventRef)+ if err != nil {+ return false, echo.NewHTTPError(http.StatusBadRequest, "Invalid ref: %s", pushEventRef).SetInternal(err)+ }+ if branch != branchFilter {+ log.Debug("Skipping repo due to branch filter mismatch", zap.String("branch", branch), zap.String("filter", branchFilter))+ return false, nil+ }++ return true, nil+}+// validateGitHubWebhookSignature256 returns true if the signature matches the// HMAC hex digested SHA256 hash of the body using the given key.func validateGitHubWebhookSignature256(signature, key string, body []byte) (bool, error) {@@ -862,8 +874,7 @@ func convertSQLAdviceToGitLabCIResult(adviceMap map[string][]advisor.Advice) *vcstatus = advice.Status}- content := fmt.Sprintf(`Error: %s-You can check the docs at %s#%d`,+ content := fmt.Sprintf("Error: %s.\nYou can check the docs at %s#%d",advice.Content,sqlReviewDocs,advice.Code,diff --git a/server/webhook_test.go b/server/webhook_test.goindex 1a88ad4fc5..0e51d5de6a 100644--- a/server/webhook_test.go+++ b/server/webhook_test.go@@ -135,13 +135,13 @@ func TestVCSSQLReview_ConvertSQLAdviceToGitLabCIResult(t *testing.T) {<testsuite name="file1.sql"><testcase name="column.no-null" classname="file1.sql" file="file1.sql#L1"><failure>-Error: Column "id" in "public"."book" cannot have NULL value+Error: Column "id" in "public"."book" cannot have NULL value.You can check the docs at https://www.bytebase.com/docs/reference/error-code/advisor#402</failure></testcase><testcase name="naming.index.idx" classname="file1.sql" file="file1.sql#L2"><failure>-Error: Index in table "tech_book" mismatches the naming convention, expect "^$|^idx_tech_book_id_name$" but found "tech_book_id_name"+Error: Index in table "tech_book" mismatches the naming convention, expect "^$|^idx_tech_book_id_name$" but found "tech_book_id_name".You can check the docs at https://www.bytebase.com/docs/reference/error-code/advisor#303</failure></testcase>@@ -149,13 +149,13 @@ You can check the docs at https://www.bytebase.com/docs/reference/error-code/adv<testsuite name="file2.sql"><testcase name="naming.table" classname="file2.sql" file="file2.sql#L1"><failure>-Error: "techBook" mismatches table naming convention, naming format should be "^[a-z]+(_[a-z]+)*$"+Error: "techBook" mismatches table naming convention, naming format should be "^[a-z]+(_[a-z]+)*$".You can check the docs at https://www.bytebase.com/docs/reference/error-code/advisor#301</failure></testcase><testcase name="naming.index.uk" classname="file2.sql" file="file2.sql#L4"><failure>-Error: Unique key in table "tech_book" mismatches the naming convention, expect "^$|^uk_tech_book_id_name$" but found "tech_book_id_name"+Error: Unique key in table "tech_book" mismatches the naming convention, expect "^$|^uk_tech_book_id_name$" but found "tech_book_id_name".You can check the docs at https://www.bytebase.com/docs/reference/error-code/advisor#304</failure></testcase>@@ -169,7 +169,8 @@ You can check the docs at https://www.bytebase.com/docs/reference/error-code/advfunc TestVCSSQLReview_ConvertSQLAdiceToGitHubActionResult(t *testing.T) {expect := []string{- "::warning file=file1.sql,line=1,col=1,endColumn=2,title=column.no-null (402)::Column \"id\" in \"public\".\"book\" cannot have NULL value%0ADoc: https://www.bytebase.com/docs/reference/error-code/advisor#402", "::error file=file1.sql,line=2,col=1,endColumn=2,title=naming.index.idx (303)::Index in table \"tech_book\" mismatches the naming convention, expect \"^$|^idx_tech_book_id_name$\" but found \"tech_book_id_name\"%0ADoc: https://www.bytebase.com/docs/reference/error-code/advisor#303",+ "::warning file=file1.sql,line=1,col=1,endColumn=2,title=column.no-null (402)::Column \"id\" in \"public\".\"book\" cannot have NULL value%0ADoc: https://www.bytebase.com/docs/reference/error-code/advisor#402",+ "::error file=file1.sql,line=2,col=1,endColumn=2,title=naming.index.idx (303)::Index in table \"tech_book\" mismatches the naming convention, expect \"^$|^idx_tech_book_id_name$\" but found \"tech_book_id_name\"%0ADoc: https://www.bytebase.com/docs/reference/error-code/advisor#303","::warning file=file2.sql,line=1,col=1,endColumn=2,title=naming.table (301)::\"techBook\" mismatches table naming convention, naming format should be \"^[a-z]+(_[a-z]+)*$\"%0ADoc: https://www.bytebase.com/docs/reference/error-code/advisor#301","::error file=file2.sql,line=4,col=1,endColumn=2,title=naming.index.uk (304)::Unique key in table \"tech_book\" mismatches the naming convention, expect \"^$|^uk_tech_book_id_name$\" but found \"tech_book_id_name\"%0ADoc: https://www.bytebase.com/docs/reference/error-code/advisor#304",}