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: fixed an issue caused by GitLab's api (#773)
Zilong Zhou4abf2602b0a9
diff --git a/frontend/src/components/ProjectMemberPanel.vue b/frontend/src/components/ProjectMemberPanel.vueindex 4787197c9c..d11a0c8431 100644--- a/frontend/src/components/ProjectMemberPanel.vue+++ b/frontend/src/components/ProjectMemberPanel.vue@@ -260,9 +260,7 @@ export default defineComponent({});const allowSyncVCS = computed(() => {- // NOTICE: will dis annotate this when this function is ready- return false;- // return props.project.workflowType === "VCS" && allowAddMember.value;+ return props.project.workflowType === "VCS" && allowAddMember.value;});const hasValidMember = computed(() => {diff --git a/plugin/vcs/gitlab/gitlab.go b/plugin/vcs/gitlab/gitlab.goindex 1ded9aba2c..a984b54af3 100644--- a/plugin/vcs/gitlab/gitlab.go+++ b/plugin/vcs/gitlab/gitlab.go@@ -155,11 +155,11 @@ func (e ProjectRole) String() string {// gitLabRepositoryMember is the API message for repository membertype gitLabRepositoryMember struct {- Email string `json:"email"`- Name string `json:"name"`- State vcs.State `json:"state"`- MembershipState vcs.State `json:"membership_state"`- AccessLevel int32 `json:"access_level"`+ ID int `json:"id"`+ Email string `json:"email"`+ Name string `json:"name"`+ State vcs.State `json:"state"`+ AccessLevel int32 `json:"access_level"`}func init() {@@ -182,11 +182,11 @@ func (provider *Provider) APIURL(instanceURL string) string {return fmt.Sprintf("%s/%s", instanceURL, apiPath)}-// TryLogin will try to login GitLab.-func (provider *Provider) TryLogin(ctx context.Context, oauthCtx common.OauthContext, instanceURL string) (*vcs.UserInfo, error) {+// fetchUserInfo will fetch user info from the given resourceURI, resourceURI should be either 'user' or 'users/:userID'+func fetchUserInfo(ctx context.Context, oauthCtx common.OauthContext, instanceURL, resourceURI string) (*vcs.UserInfo, error) {code, body, err := httpGet(instanceURL,- "user",+ resourceURI,&oauthCtx.AccessToken,oauthContext{ClientID: oauthCtx.ClientID,@@ -200,7 +200,14 @@ func (provider *Provider) TryLogin(ctx context.Context, oauthCtx common.OauthCon}if code == 404 {- return nil, common.Errorf(common.NotFound, fmt.Errorf("failed to fetch user info from GitLab instance %s", instanceURL))+ errInfo := []string{fmt.Sprintf("failed to fetch user info from GitLab instance %s", instanceURL)}++ resourceURISplit := strings.Split(resourceURI, "/")+ if len(resourceURI) > 1 {+ errInfo = append(errInfo, fmt.Sprintf("UserID: %v", resourceURISplit[1]))+ }++ return nil, common.Errorf(common.NotFound, fmt.Errorf(strings.Join(errInfo, ", ")))} else if code >= 300 {return nil, fmt.Errorf("failed to read user info from GitLab instance %s, status code: %d",instanceURL,@@ -208,12 +215,22 @@ func (provider *Provider) TryLogin(ctx context.Context, oauthCtx common.OauthCon)}- UserInfo := &vcs.UserInfo{}- if err := json.Unmarshal([]byte(body), UserInfo); err != nil {+ userInfo := &vcs.UserInfo{}+ if err := json.Unmarshal([]byte(body), userInfo); err != nil {return nil, err}- return UserInfo, err+ return userInfo, err+}++// TryLogin will try to fetch the user info from the current OAuth content of GitLab.+func (provider *Provider) TryLogin(ctx context.Context, oauthCtx common.OauthContext, instanceURL string) (*vcs.UserInfo, error) {+ return fetchUserInfo(ctx, oauthCtx, instanceURL, "user")+}++// FetchUserInfo will fetch user info from GitLab. If userID is set to nil, the user info of the current oauth would be returned.+func (provider *Provider) FetchUserInfo(ctx context.Context, oauthCtx common.OauthContext, instanceURL string, userID int) (*vcs.UserInfo, error) {+ return fetchUserInfo(ctx, oauthCtx, instanceURL, fmt.Sprintf("users/%d", userID))}func getRoleAndMappedRole(accessLevel int32) (gitLabRole ProjectRole, bytebaseRole common.ProjectRole) {@@ -242,7 +259,8 @@ func getRoleAndMappedRole(accessLevel int32) (gitLabRole ProjectRole, bytebaseRofunc (provider *Provider) FetchRepositoryActiveMemberList(ctx context.Context, oauthCtx common.OauthContext, instanceURL string, repositoryID string) ([]*vcs.RepositoryMember, error) {code, body, err := httpGet(instanceURL,- fmt.Sprintf("projects/%s/members", repositoryID),+ // official API doc: https://docs.gitlab.com/14.6/ee/api/members.html+ fmt.Sprintf("projects/%s/members/all", repositoryID),&oauthCtx.AccessToken,oauthContext{ClientID: oauthCtx.ClientID,@@ -272,11 +290,18 @@ func (provider *Provider) FetchRepositoryActiveMemberList(ctx context.Context, o// we only return active member (both state and membership_state is active)activeRepositoryMember := make([]*vcs.RepositoryMember, 0)for _, gitLabMember := range gitLabrepositoryMember {- if gitLabMember.State == vcs.StateActive && gitLabMember.MembershipState == vcs.StateActive {+ if gitLabMember.State == vcs.StateActive {+ // the email field does not return as expected via projects/<<projectId>>/members/all, possibly caused by: https://gitlab.com/gitlab-org/gitlab/-/issues/25077+ // so we need to fetch this field one by one+ // TODO: if the number of the member is too large, fetching sequentially may cause performance issue+ userInfo, err := provider.FetchUserInfo(ctx, oauthCtx, instanceURL, gitLabMember.ID)+ if err != nil {+ return nil, err+ }gitLabRole, bytebaseRole := getRoleAndMappedRole(gitLabMember.AccessLevel)repositoryMember := &vcs.RepositoryMember{Name: gitLabMember.Name,- Email: gitLabMember.Email,+ Email: userInfo.Email,Role: bytebaseRole,VCSRole: gitLabRole.String(),State: vcs.StateActive,diff --git a/plugin/vcs/vcs.go b/plugin/vcs/vcs.goindex 5d82f7aac3..b14eb11681 100644--- a/plugin/vcs/vcs.go+++ b/plugin/vcs/vcs.go@@ -96,12 +96,19 @@ type Provider interface {// Returns the API URL for a given VCS instance URLAPIURL(instanceURL string) string- // Try to use this provider as an auth provider and fetch the user info stored at this provider+ // Try to use this provider as an auth provider and fetch the user info from the OAuth context//// oauthCtx: OAuth context to write the file content// instanceURL: VCS instance URLTryLogin(ctx context.Context, oauthCtx common.OauthContext, instanceURL string) (*UserInfo, error)+ // Fetch the user info of the given userID+ //+ // oauthCtx: OAuth context to write the file content+ // instanceURL: VCS instance URL+ // userID: the ID of the desired user+ FetchUserInfo(ctx context.Context, oauthCtx common.OauthContext, instanceURL string, userID int) (*UserInfo, error)+// Fetch all active members of a given repository//// oauthCtx: OAuth context to write the file contentdiff --git a/server/auth.go b/server/auth.goindex 8a8e719fff..f8d55cc113 100644--- a/server/auth.go+++ b/server/auth.go@@ -81,7 +81,7 @@ func (s *Server) registerAuthRoutes(g *echo.Group) {if err := jsonapi.UnmarshalPayload(c.Request().Body, gitlabLogin); err != nil {return echo.NewHTTPError(http.StatusBadRequest, "Malformatted gitlab login request").SetInternal(err)}- GitlabUserInfo, err := vcsPlugin.Get("GITLAB_SELF_HOST", vcsPlugin.ProviderConfig{Logger: s.l}).TryLogin(ctx,+ gitlabUserInfo, err := vcsPlugin.Get("GITLAB_SELF_HOST", vcsPlugin.ProviderConfig{Logger: s.l}).TryLogin(ctx,common.OauthContext{ClientID: gitlabLogin.ApplicationID,ClientSecret: gitlabLogin.Secret,@@ -96,12 +96,12 @@ func (s *Server) registerAuthRoutes(g *echo.Group) {}// we only allow active user to login via gitlab- if GitlabUserInfo.State != vcsPlugin.StateActive {+ if gitlabUserInfo.State != vcsPlugin.StateActive {return echo.NewHTTPError(http.StatusUnauthorized, "Fail to login via Gitlab, user is Archived")}principalFind := &api.PrincipalFind{- Email: &GitlabUserInfo.Email,+ Email: &gitlabUserInfo.Email,}user, err = s.PrincipalService.FindPrincipal(ctx, principalFind)if err != nil {@@ -114,9 +114,9 @@ func (s *Server) registerAuthRoutes(g *echo.Group) {// The random password is supposed to be not guessable. If user wants to login// via password, she needs to set the new password from the profile page.signup := &api.Signup{- Email: GitlabUserInfo.Email,+ Email: gitlabUserInfo.Email,Password: common.RandomString(20),- Name: GitlabUserInfo.Name,+ Name: gitlabUserInfo.Name,}var httpError *echo.HTTPErroruser, httpError = trySignup(ctx, s, signup, api.SystemBotID)diff --git a/server/project_member.go b/server/project_member.goindex 70efddf9bd..827659c3da 100644--- a/server/project_member.go+++ b/server/project_member.go@@ -70,7 +70,7 @@ func (s *Server) registerProjectMemberRoutes(g *echo.Group) {// The following block will check whether the relevant principal exists in our system.// If the principal does not exist, we will try to create one out of the vcs member info.- createList := make([]*api.ProjectMemberCreate, 0)+ var createList []*api.ProjectMemberCreate// we declare latSyncTs to ensure that every projectMember would have the same sync time.lastSyncTs := time.Now().UTC().Unix()for _, projectMember := range vcsProjectMemberList {