Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
fix: vcs connector (#11350)
Danny Xud217cff8017a
diff --git a/backend/api/v1/acl_config.go b/backend/api/v1/acl_config.goindex 7ee047211e..ddc4bdfd3e 100644--- a/backend/api/v1/acl_config.go+++ b/backend/api/v1/acl_config.go@@ -100,6 +100,12 @@ var methodPermissionMap = map[string]iam.Permission{v1pb.RoleService_UpdateRole_FullMethodName: iam.PermissionRolesUpdate,v1pb.RoleService_DeleteRole_FullMethodName: iam.PermissionRolesDelete,+ v1pb.VCSConnectorService_ListVCSConnectors_FullMethodName: iam.PermissionVCSConnectorsList,+ v1pb.VCSConnectorService_GetVCSConnector_FullMethodName: iam.PermissionVCSConnectorsGet,+ v1pb.VCSConnectorService_CreateVCSConnector_FullMethodName: iam.PermissionVCSConnectorsCreate,+ v1pb.VCSConnectorService_UpdateVCSConnector_FullMethodName: iam.PermissionVCSConnectorsUpdate,+ v1pb.VCSConnectorService_DeleteVCSConnector_FullMethodName: iam.PermissionVCSConnectorsDelete,+v1pb.ChangelistService_ListChangelists_FullMethodName: iam.PermissionChangelistsList,v1pb.ChangelistService_GetChangelist_FullMethodName: iam.PermissionChangelistsGet,v1pb.ChangelistService_CreateChangelist_FullMethodName: iam.PermissionChangelistsCreate,diff --git a/backend/api/v1/acl_iam.go b/backend/api/v1/acl_iam.goindex 6c7cf84e03..7679263157 100644--- a/backend/api/v1/acl_iam.go+++ b/backend/api/v1/acl_iam.go@@ -274,6 +274,14 @@ func (in *ACLInterceptor) doIAMPermissionCheck(ctx context.Context, fullMethod sv1pb.DatabaseService_GetChangeHistory_FullMethodName:projectIDsGetter = in.getProjectIDsForDatabaseService+ case+ v1pb.VCSConnectorService_CreateVCSConnector_FullMethodName,+ v1pb.VCSConnectorService_GetVCSConnector_FullMethodName,+ v1pb.VCSConnectorService_ListVCSConnectors_FullMethodName,+ v1pb.VCSConnectorService_UpdateVCSConnector_FullMethodName,+ v1pb.VCSConnectorService_DeleteVCSConnector_FullMethodName:++ projectIDsGetter = in.getProjectIDsForVCSConnectorServicecasev1pb.ChangelistService_CreateChangelist_FullMethodName,v1pb.ChangelistService_GetChangelist_FullMethodName:@@ -370,6 +378,40 @@ func getDatabaseMessage(ctx context.Context, s *store.Store, databaseResourceNamreturn database, nil}+func (*ACLInterceptor) getProjectIDsForVCSConnectorService(_ context.Context, req any) ([]string, error) {+ var projects, vcsConnectors []string+ switch r := req.(type) {+ case *v1pb.CreateVCSConnectorRequest:+ projects = append(projects, r.GetParent())+ case *v1pb.ListVCSConnectorsRequest:+ projects = append(projects, r.GetParent())+ case *v1pb.GetVCSConnectorRequest:+ vcsConnectors = append(vcsConnectors, r.GetName())+ case *v1pb.UpdateVCSConnectorRequest:+ vcsConnectors = append(vcsConnectors, r.GetVcsConnector().GetName())+ case *v1pb.DeleteVCSConnectorRequest:+ vcsConnectors = append(vcsConnectors, r.GetName())+ }++ var projectIDs []string+ for _, vcsConnector := range vcsConnectors {+ projectID, _, err := common.GetProjectVCSConnectorID(vcsConnector)+ if err != nil {+ return nil, errors.Wrapf(err, "failed to parse vcsConnector %q", vcsConnector)+ }+ projectIDs = append(projectIDs, projectID)+ }+ for _, project := range projects {+ projectID, err := common.GetProjectID(project)+ if err != nil {+ return nil, errors.Wrapf(err, "failed to parse project %q", project)+ }+ projectIDs = append(projectIDs, projectID)+ }++ return uniq(projectIDs), nil+}+func (*ACLInterceptor) getProjectIDsForChangelistService(_ context.Context, req any) ([]string, error) {var projects, changelists []stringswitch r := req.(type) {diff --git a/backend/api/v1/vcs_connector_service.go b/backend/api/v1/vcs_connector_service.goindex 62b63c4973..575d7e0530 100644--- a/backend/api/v1/vcs_connector_service.go+++ b/backend/api/v1/vcs_connector_service.go@@ -13,8 +13,6 @@ import ("github.com/bytebase/bytebase/backend/common""github.com/bytebase/bytebase/backend/common/log"- "github.com/bytebase/bytebase/backend/component/config"- "github.com/bytebase/bytebase/backend/component/iam"vcsplugin "github.com/bytebase/bytebase/backend/plugin/vcs""github.com/bytebase/bytebase/backend/plugin/vcs/gitlab""github.com/bytebase/bytebase/backend/store"@@ -24,17 +22,13 @@ import (// VCSConnectorService implements the vcs connector service.type VCSConnectorService struct {v1pb.UnimplementedVCSConnectorServiceServer- store *store.Store- profile *config.Profile- iamManager *iam.Manager+ store *store.Store}// NewVCSConnectorService creates a new VCSConnectorService.-func NewVCSConnectorService(store *store.Store, profile *config.Profile, iamManager *iam.Manager) *VCSConnectorService {+func NewVCSConnectorService(store *store.Store) *VCSConnectorService {return &VCSConnectorService{- store: store,- profile: profile,- iamManager: iamManager,+ store: store,}}@@ -244,16 +238,12 @@ func (s *VCSConnectorService) UpdateVCSConnector(ctx context.Context, request *vreturn nil, status.Errorf(codes.NotFound, "vcs connector %q not found", vcsConnectorID)}- vcsResourceID, err := common.GetVCSProviderID(request.GetVcsConnector().GetVcsProvider())- if err != nil {- return nil, status.Errorf(codes.InvalidArgument, err.Error())- }- vcs, err := s.store.GetVCSProvider(ctx, &store.FindVCSProviderMessage{ResourceID: &vcsResourceID})+ vcs, err := s.store.GetVCSProvider(ctx, &store.FindVCSProviderMessage{ResourceID: &vcsConnector.VCSResourceID})if err != nil {return nil, status.Errorf(codes.Internal, "failed to find vcs: %s", err.Error())}if vcs == nil {- return nil, status.Errorf(codes.NotFound, "vcs %s not found", vcsResourceID)+ return nil, status.Errorf(codes.NotFound, "vcs %s not found", vcsConnector.VCSResourceID)}user, ok := ctx.Value(common.UserContextKey).(*store.UserMessage)@@ -367,11 +357,19 @@ func (s *VCSConnectorService) convertStoreVCSConnector(ctx context.Context, vcsC}v1VCSConnector := &v1pb.VCSConnector{- Name: fmt.Sprintf("projects/%s/vcs connectors/%s", vcsConnector.ProjectID, vcsConnector.ResourceID),- CreateTime: timestamppb.New(vcsConnector.CreatedTime),- UpdateTime: timestamppb.New(vcsConnector.UpdatedTime),- Creator: fmt.Sprintf("users/%s", creator.Email),- Updater: fmt.Sprintf("users/%s", updater.Email),+ Name: fmt.Sprintf("%s%s/%s%s", common.ProjectNamePrefix, vcsConnector.ProjectID, common.VCSConnectorPrefix, vcsConnector.ResourceID),+ CreateTime: timestamppb.New(vcsConnector.CreatedTime),+ UpdateTime: timestamppb.New(vcsConnector.UpdatedTime),+ Creator: fmt.Sprintf("users/%s", creator.Email),+ Updater: fmt.Sprintf("users/%s", updater.Email),+ Title: vcsConnector.Title,+ VcsProvider: fmt.Sprintf("%s%s", common.VCSProviderPrefix, vcsConnector.VCSResourceID),+ ExternalId: vcsConnector.ExternalID,+ BaseDirectory: vcsConnector.BaseDirectory,+ Branch: vcsConnector.Branch,+ WebhookEndpointId: vcsConnector.WebhookEndpointID,+ FullPath: vcsConnector.FullPath,+ WebUrl: vcsConnector.WebURL,}return v1VCSConnector, nil}diff --git a/backend/server/grpc_routes.go b/backend/server/grpc_routes.goindex 7435e2560b..31862621e8 100644--- a/backend/server/grpc_routes.go+++ b/backend/server/grpc_routes.go@@ -88,6 +88,7 @@ func configureGrpcRouters(v1pb.RegisterCelServiceServer(grpcServer, apiv1.NewCelService())v1pb.RegisterLoggingServiceServer(grpcServer, apiv1.NewLoggingService(stores))v1pb.RegisterChangelistServiceServer(grpcServer, apiv1.NewChangelistService(stores, profile, iamManager))+ v1pb.RegisterVCSConnectorServiceServer(grpcServer, apiv1.NewVCSConnectorService(stores))// REST gateway proxy.grpcEndpoint := fmt.Sprintf(":%d", profile.GrpcPort)@@ -159,5 +160,8 @@ func configureGrpcRouters(if err := v1pb.RegisterChangelistServiceHandler(ctx, mux, grpcConn); err != nil {return nil, nil, err}+ if err := v1pb.RegisterVCSConnectorServiceHandler(ctx, mux, grpcConn); err != nil {+ return nil, nil, err+ }return rolloutService, issueService, nil}diff --git a/backend/store/vcs_connector.go b/backend/store/vcs_connector.goindex 8a85b930fd..ee54607035 100644--- a/backend/store/vcs_connector.go+++ b/backend/store/vcs_connector.go@@ -99,6 +99,8 @@ func (s *Store) ListVCSConnectors(ctx context.Context, find *FindVCSConnectorMesproject.resource_id AS project_resource_id,repository.resource_id,repository.name AS name,+ repository.creator_id,+ repository.updater_id,full_path,web_url,branch,@@ -129,6 +131,8 @@ func (s *Store) ListVCSConnectors(ctx context.Context, find *FindVCSConnectorMes&vcsConnector.ProjectID,&vcsConnector.ResourceID,&vcsConnector.Title,+ &vcsConnector.CreatorID,+ &vcsConnector.UpdaterID,&vcsConnector.FullPath,&vcsConnector.WebURL,&vcsConnector.Branch,diff --git a/backend/tests/schema_update_test.go b/backend/tests/schema_update_test.goindex c2678d5050..c8ee536b97 100644--- a/backend/tests/schema_update_test.go+++ b/backend/tests/schema_update_test.go@@ -13,9 +13,12 @@ import ("github.com/google/uuid""github.com/stretchr/testify/require""google.golang.org/protobuf/testing/protocmp"+ "google.golang.org/protobuf/types/known/fieldmaskpb"api "github.com/bytebase/bytebase/backend/legacyapi""github.com/bytebase/bytebase/backend/plugin/vcs"+ "github.com/bytebase/bytebase/backend/plugin/vcs/bitbucket"+ "github.com/bytebase/bytebase/backend/plugin/vcs/github""github.com/bytebase/bytebase/backend/plugin/vcs/gitlab""github.com/bytebase/bytebase/backend/resources/mysql""github.com/bytebase/bytebase/backend/resources/postgres"@@ -170,6 +173,108 @@ func TestSimpleVCS(t *testing.T) {}},},+ {+ name: "GitHub",+ vcsProviderCreator: fake.NewGitHub,+ vcsType: v1pb.VCSProvider_GITHUB,+ externalID: "octocat/Hello-World",+ repositoryFullPath: "octocat/Hello-World",+ newWebhookPushEvent: func(added, modified [][]string, beforeSHA, afterSHA string) any {+ var commits []github.WebhookCommit+ for i := range added {+ commits = append(commits, github.WebhookCommit{+ ID: "fake_github_commit_id",+ Distinct: true,+ Message: "Fake GitHub commit message",+ Timestamp: time.Now(),+ URL: "https://api.github.com/octocat/Hello-World/commits/fake_github_commit_id",+ Author: github.WebhookCommitAuthor{+ Name: "fake_github_author",+ Email: "fake_github_author@localhost",+ },+ Added: added[i],+ Modified: modified[i],+ })+ }+ return github.WebhookPushEvent{+ Ref: "refs/heads/feature/foo",+ Before: beforeSHA,+ After: afterSHA,+ Repository: github.WebhookRepository{+ ID: 211,+ FullName: "octocat/Hello-World",+ HTMLURL: "https://github.com/octocat/Hello-World",+ },+ Sender: github.WebhookSender{+ Login: "fake_github_author",+ },+ Commits: commits,+ }+ },+ },+ {+ name: "Bitbucket",+ vcsProviderCreator: fake.NewBitbucket,+ vcsType: v1pb.VCSProvider_BITBUCKET,+ externalID: "octocat/Hello-World",+ repositoryFullPath: "octocat/Hello-World",+ newWebhookPushEvent: func(added, _ [][]string, beforeSHA, afterSHA string) any {+ var commits []bitbucket.WebhookCommit+ for range added {+ commits = append(commits, bitbucket.WebhookCommit{+ Hash: afterSHA,+ Date: time.Now(),+ Author: bitbucket.Author{+ Raw: "fake_bitbucket_author <fake_bitbucket_author@localhost>",+ User: bitbucket.User{+ Nickname: "fake_bitbucket_author",+ },+ },+ Message: "Fake Bitbucket commit message",+ Links: bitbucket.Links{+ HTML: bitbucket.Link{+ Href: "https://bitbucket.org/octocat/Hello-World/commits/fake_github_commit_id",+ },+ },+ Parents: []bitbucket.Target{+ {Hash: beforeSHA},+ },+ })+ }+ return bitbucket.WebhookPushEvent{+ Push: bitbucket.WebhookPush{+ Changes: []bitbucket.WebhookPushChange{+ {+ Old: bitbucket.Branch{+ Name: "feature/foo",+ Target: bitbucket.Target{+ Hash: beforeSHA,+ },+ },+ New: bitbucket.Branch{+ Name: "feature/foo",+ Target: bitbucket.Target{+ Hash: afterSHA,+ },+ },+ Commits: commits,+ },+ },+ },+ Repository: bitbucket.Repository{+ FullName: "octocat/Hello-World",+ Links: bitbucket.Links{+ HTML: bitbucket.Link{+ Href: "https://bitbuket.org/octocat/Hello-World",+ },+ },+ },+ Actor: bitbucket.User{+ Nickname: "fake_bitbucket_author",+ },+ }+ },+ },}for _, test := range tests {// Fix the problem that closure in a for loop will always use the last element.@@ -206,20 +311,29 @@ func TestSimpleVCS(t *testing.T) {err = ctl.vcsProvider.CreateBranch(test.externalID, "feature/foo")a.NoError(err)- _, err = ctl.projectServiceClient.UpdateProjectGitOpsInfo(ctx, &v1pb.UpdateProjectGitOpsInfoRequest{- ProjectGitopsInfo: &v1pb.ProjectGitOpsInfo{- Name: fmt.Sprintf("%s/gitOpsInfo", ctl.project.Name),- Vcs: evcs.Name,- Title: "Test Repository",+ oldVcsConnector, err := ctl.vcsConnectorServiceClient.CreateVCSConnector(ctx, &v1pb.CreateVCSConnectorRequest{+ Parent: ctl.project.Name,+ VcsConnector: &v1pb.VCSConnector{+ VcsProvider: evcs.Name,+ Title: "Test VCS Connector",FullPath: test.repositoryFullPath,WebUrl: fmt.Sprintf("%s/%s", ctl.vcsURL, test.repositoryFullPath),- Branch: "feature/foo",+ Branch: "feature/bar",BaseDirectory: baseDirectory,ExternalId: test.externalID,},- AllowMissing: true,+ VcsConnectorId: "default",})a.NoError(err)+ vcsConnector, err := ctl.vcsConnectorServiceClient.UpdateVCSConnector(ctx, &v1pb.UpdateVCSConnectorRequest{+ VcsConnector: &v1pb.VCSConnector{+ Name: oldVcsConnector.Name,+ Branch: "feature/foo",+ },+ UpdateMask: &fieldmaskpb.FieldMask{Paths: []string{"branch"}},+ })+ a.NoError(err)+ a.Equal("feature/foo", vcsConnector.Branch)// Provision an instance.instanceName := "testInstance1"@@ -415,6 +529,9 @@ func TestSimpleVCS(t *testing.T) {a.Equal("0003-ddl", histories[2].Version)a.Equal("0002-ddl", histories[3].Version)a.Equal("0001-ddl", histories[4].Version)++ _, err = ctl.vcsConnectorServiceClient.DeleteVCSConnector(ctx, &v1pb.DeleteVCSConnectorRequest{Name: vcsConnector.Name})+ a.NoError(err)})}}diff --git a/backend/tests/tests.go b/backend/tests/tests.goindex a17a580ba0..673cadb47c 100644--- a/backend/tests/tests.go+++ b/backend/tests/tests.go@@ -164,6 +164,7 @@ type controller struct {rolloutServiceClient v1pb.RolloutServiceClientorgPolicyServiceClient v1pb.OrgPolicyServiceClientprojectServiceClient v1pb.ProjectServiceClient+ vcsConnectorServiceClient v1pb.VCSConnectorServiceClientauthServiceClient v1pb.AuthServiceClientsettingServiceClient v1pb.SettingServiceClientenvironmentServiceClient v1pb.EnvironmentServiceClient@@ -423,6 +424,7 @@ func (ctl *controller) start(ctx context.Context, port int) (context.Context, erctl.rolloutServiceClient = v1pb.NewRolloutServiceClient(ctl.grpcConn)ctl.orgPolicyServiceClient = v1pb.NewOrgPolicyServiceClient(ctl.grpcConn)ctl.projectServiceClient = v1pb.NewProjectServiceClient(ctl.grpcConn)+ ctl.vcsConnectorServiceClient = v1pb.NewVCSConnectorServiceClient(ctl.grpcConn)ctl.authServiceClient = v1pb.NewAuthServiceClient(ctl.grpcConn)ctl.settingServiceClient = v1pb.NewSettingServiceClient(ctl.grpcConn)ctl.environmentServiceClient = v1pb.NewEnvironmentServiceClient(ctl.grpcConn)