-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Support for group restriction for Gitlab provider #312
base: master
Are you sure you want to change the base?
Changes from 3 commits
19eaf7d
d429722
4f637a0
6b5a334
27643bc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,7 @@ type Options struct { | |
EmailDomains []string `flag:"email-domain" cfg:"email_domains"` | ||
GitHubOrg string `flag:"github-org" cfg:"github_org"` | ||
GitHubTeam string `flag:"github-team" cfg:"github_team"` | ||
GitLabGroup string `flag:"gitlab-group" cfg:"gitlab_group"` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be nice if multiple groups could be specified, just like multiple |
||
GoogleGroups []string `flag:"google-group" cfg:"google_group"` | ||
GoogleAdminEmail string `flag:"google-admin-email" cfg:"google_admin_email"` | ||
GoogleServiceAccountJSON string `flag:"google-service-account-json" cfg:"google_service_account_json"` | ||
|
@@ -229,6 +230,8 @@ func parseProviderInfo(o *Options, msgs []string) []string { | |
p.Configure(o.AzureTenant) | ||
case *providers.GitHubProvider: | ||
p.SetOrgTeam(o.GitHubOrg, o.GitHubTeam) | ||
case *providers.GitLabProvider: | ||
p.SetGroup(o.GitLabGroup) | ||
case *providers.GoogleProvider: | ||
if o.GoogleServiceAccountJSON != "" { | ||
file, err := os.Open(o.GoogleServiceAccountJSON) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,10 +6,14 @@ import ( | |
"net/url" | ||
|
||
"github.com/bitly/oauth2_proxy/api" | ||
"fmt" | ||
"io/ioutil" | ||
"encoding/json" | ||
) | ||
|
||
type GitLabProvider struct { | ||
*ProviderData | ||
Group string | ||
} | ||
|
||
func NewGitLabProvider(p *ProviderData) *GitLabProvider { | ||
|
@@ -41,8 +45,57 @@ func NewGitLabProvider(p *ProviderData) *GitLabProvider { | |
return &GitLabProvider{ProviderData: p} | ||
} | ||
|
||
func (p *GitLabProvider) SetGroup(group string) { | ||
p.Group = group | ||
} | ||
|
||
func (p *GitLabProvider) hasGroup(accessToken string) (bool, error) { | ||
|
||
var groups []struct { | ||
Group string `json:"name"` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a serious security issue since the Consider this scenario: [
{
"id": 1,
"web_url": "https://gitlab.com/groups/admins",
"name": "admins",
"path": "admins",
"description": "",
"visibility": "private",
"lfs_enabled": true,
"avatar_url": null,
"request_access_enabled": false,
"full_name": "admins",
"full_path": "admins",
"parent_id": null
},
{
"id": 2,
"web_url": "https://gitlab.com/groups/myfunnyproject",
"name": "myfunnyproject",
"path": "myfunnyproject",
"description": "",
"visibility": "private",
"lfs_enabled": true,
"avatar_url": null,
"request_access_enabled": false,
"full_name": "myfunnyproject",
"full_path": "myfunnyproject",
"parent_id": null
},
{
"id": 3,
"web_url": "https://gitlab.com/groups/myfunnyproject/admins",
"name": "admins",
"path": "admins",
"description": "",
"visibility": "private",
"lfs_enabled": true,
"avatar_url": null,
"request_access_enabled": false,
"full_name": "myfunnyproject / myfunnyproject",
"full_path": "myfunnyproject/myfunnyproject",
"parent_id": 2
}
] There are three groups: I suggest using |
||
} | ||
|
||
endpoint := p.ValidateURL.Scheme + "://" + p.ValidateURL.Host + "/api/v3/groups?access_token="+accessToken | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nitpick: spaces around |
||
req, _ := http.NewRequest("GET", endpoint, nil) | ||
resp, err := http.DefaultClient.Do(req) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
body, err := ioutil.ReadAll(resp.Body) | ||
resp.Body.Close() | ||
if err != nil { | ||
return false, err | ||
} | ||
if resp.StatusCode != 200 { | ||
return false, fmt.Errorf("got %d from %q %s", resp.StatusCode, endpoint, body) | ||
} | ||
|
||
if err := json.Unmarshal(body, &groups); err != nil { | ||
return false, err | ||
} | ||
|
||
for _, group := range groups { | ||
if( p.Group == group.Group) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nitpick: no round brackets required here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. probably best to just ask to run "gofmt" |
||
// Found the group | ||
return true, nil | ||
} | ||
} | ||
|
||
log.Printf("Group %s not found in %s", p.Group, groups) | ||
return false, nil | ||
} | ||
|
||
|
||
func (p *GitLabProvider) GetEmailAddress(s *SessionState) (string, error) { | ||
|
||
// if we require a group, check that first | ||
if p.Group != "" { | ||
if ok, err := p.hasGroup(s.AccessToken); err != nil || !ok { | ||
return "", err | ||
} | ||
} | ||
|
||
req, err := http.NewRequest("GET", | ||
p.ValidateURL.String()+"?access_token="+s.AccessToken, nil) | ||
if err != nil { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nitpick: "
oneadditional parameters" or "one additional parameters"