Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GitLab connector: add GitLab additional group with role #2941

Merged
merged 14 commits into from
Oct 24, 2024
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 68 additions & 14 deletions connector/gitlab/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@ const (

// Config holds configuration options for gitlab logins.
type Config struct {
BaseURL string `json:"baseURL"`
ClientID string `json:"clientID"`
ClientSecret string `json:"clientSecret"`
RedirectURI string `json:"redirectURI"`
Groups []string `json:"groups"`
UseLoginAsID bool `json:"useLoginAsID"`
BaseURL string `json:"baseURL"`
ClientID string `json:"clientID"`
ClientSecret string `json:"clientSecret"`
RedirectURI string `json:"redirectURI"`
Groups []string `json:"groups"`
UseLoginAsID bool `json:"useLoginAsID"`
GetGroupsPermission bool `json:"getGroupsPermission"`
}

type gitlabUser struct {
Expand All @@ -51,13 +52,14 @@ func (c *Config) Open(id string, logger log.Logger) (connector.Connector, error)
c.BaseURL = "https://gitlab.com"
}
return &gitlabConnector{
baseURL: c.BaseURL,
redirectURI: c.RedirectURI,
clientID: c.ClientID,
clientSecret: c.ClientSecret,
logger: logger,
groups: c.Groups,
useLoginAsID: c.UseLoginAsID,
baseURL: c.BaseURL,
redirectURI: c.RedirectURI,
clientID: c.ClientID,
clientSecret: c.ClientSecret,
logger: logger,
groups: c.Groups,
useLoginAsID: c.UseLoginAsID,
getGroupsPermission: c.GetGroupsPermission,
}, nil
}

Expand All @@ -82,6 +84,8 @@ type gitlabConnector struct {
httpClient *http.Client
// if set to true will use the user's handle rather than their numeric id as the ID
useLoginAsID bool

zvlb marked this conversation as resolved.
Show resolved Hide resolved
getGroupsPermission bool
}

func (c *gitlabConnector) oauth2Config(scopes connector.Scopes) *oauth2.Config {
Expand Down Expand Up @@ -256,7 +260,10 @@ func (c *gitlabConnector) user(ctx context.Context, client *http.Client) (gitlab
}

type userInfo struct {
Groups []string
Groups []string `json:"groups"`
OwnerPermission []string `json:"https://gitlab.org/claims/groups/owner"`
MaintainerPermission []string `json:"https://gitlab.org/claims/groups/maintainer"`
DeveloperPermission []string `json:"https://gitlab.org/claims/groups/developer"`
}

// userGroups queries the GitLab API for group membership.
Expand Down Expand Up @@ -287,9 +294,56 @@ func (c *gitlabConnector) userGroups(ctx context.Context, client *http.Client) (
return nil, fmt.Errorf("failed to decode response: %v", err)
}

if c.getGroupsPermission {
zvlb marked this conversation as resolved.
Show resolved Hide resolved
groups := c.setGroupsPermission(u)
return groups, nil
}

return u.Groups, nil
}

func (c *gitlabConnector) setGroupsPermission(u userInfo) []string {
groups := u.Groups

L1:
for _, g := range groups {
zvlb marked this conversation as resolved.
Show resolved Hide resolved
if len(u.OwnerPermission) > 0 {
for _, op := range u.OwnerPermission {
if len(g) >= len(op) {
if g[0:len(op)] == op {
groups = append(groups, fmt.Sprintf("%s:owner", g))
continue L1
}
}
}
}

if len(u.MaintainerPermission) > 0 {
for _, mp := range u.MaintainerPermission {
if len(g) >= len(mp) {
if g[0:len(mp)] == mp {
groups = append(groups, fmt.Sprintf("%s:maintainer", g))
continue L1
}
}
}
}

if len(u.DeveloperPermission) > 0 {
for _, dp := range u.DeveloperPermission {
if len(g) >= len(dp) {
if g[0:len(dp)] == dp {
groups = append(groups, fmt.Sprintf("%s:developer", g))
continue L1
}
}
}
}
}

return groups
}

func (c *gitlabConnector) getGroups(ctx context.Context, client *http.Client, groupScope bool, userLogin string) ([]string, error) {
gitlabGroups, err := c.userGroups(ctx, client)
if err != nil {
Expand Down