-
Notifications
You must be signed in to change notification settings - Fork 141
/
github.go
165 lines (140 loc) · 3.88 KB
/
github.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Copyright 2022 The PipeCD Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package github
import (
"context"
"fmt"
"net/http"
"net/url"
"github.com/google/go-github/v29/github"
"golang.org/x/oauth2"
oauth2github "golang.org/x/oauth2/github"
"github.com/pipe-cd/pipecd/pkg/model"
)
const (
listPerPage = 100
)
// OAuthClient is a oauth client for github.
type OAuthClient struct {
*github.Client
project *model.Project
adminTeam string
editorTeam string
viewerTeam string
}
// NewOAuthClient creates a new oauth client for GitHub.
func NewOAuthClient(ctx context.Context,
sso *model.ProjectSSOConfig_GitHub,
rbac *model.ProjectRBACConfig,
project *model.Project,
code string,
) (*OAuthClient, error) {
c := &OAuthClient{
project: project,
adminTeam: rbac.Admin,
editorTeam: rbac.Editor,
viewerTeam: rbac.Viewer,
}
cfg := oauth2.Config{
ClientID: sso.ClientId,
ClientSecret: sso.ClientSecret,
Endpoint: oauth2github.Endpoint,
}
if sso.ProxyUrl != "" {
proxyURL, err := url.Parse(sso.ProxyUrl)
if err != nil {
return nil, err
}
t := http.DefaultTransport.(*http.Transport).Clone()
t.Proxy = http.ProxyURL(proxyURL)
ctx = context.WithValue(ctx, oauth2.HTTPClient, &http.Client{Transport: t})
}
if sso.BaseUrl != "" {
baseURL, err := url.Parse(sso.BaseUrl)
if err != nil {
return nil, err
}
cfg.Endpoint.TokenURL = fmt.Sprintf("%s://%s%s", baseURL.Scheme, baseURL.Host, "/login/oauth/access_token")
token, err := cfg.Exchange(ctx, code)
if err != nil {
return nil, err
}
cli, err := github.NewEnterpriseClient(sso.BaseUrl, sso.UploadUrl, cfg.Client(ctx, token))
if err != nil {
return nil, err
}
c.Client = cli
return c, nil
}
token, err := cfg.Exchange(ctx, code)
if err != nil {
return nil, err
}
c.Client = github.NewClient(cfg.Client(ctx, token))
return c, nil
}
// GetUser returns a user model.
func (c *OAuthClient) GetUser(ctx context.Context) (*model.User, error) {
user, _, err := c.Users.Get(ctx, "")
if err != nil {
return nil, err
}
teams, _, err := c.Teams.ListUserTeams(ctx, &github.ListOptions{PerPage: listPerPage})
if err != nil {
return nil, err
}
role, err := c.decideRole(user.GetLogin(), teams)
if err != nil {
return nil, err
}
return &model.User{
Username: user.GetLogin(),
AvatarUrl: user.GetAvatarURL(),
Role: role,
}, nil
}
func (c *OAuthClient) decideRole(user string, teams []*github.Team) (role *model.Role, err error) {
role = &model.Role{
ProjectId: c.project.Id,
ProjectRbacRoles: make([]string, 0, len(teams)),
}
groups := c.project.UserGroups
roles := make(map[string]string, len(groups))
for _, g := range groups {
roles[g.SsoGroup] = g.Role
}
for _, team := range teams {
slug := team.GetSlug()
org := team.Organization.GetLogin()
if org == "" || slug == "" {
continue
}
t := fmt.Sprintf("%s/%s", org, slug)
if v, ok := roles[t]; ok {
role.ProjectRbacRoles = append(role.ProjectRbacRoles, v)
}
}
if len(role.ProjectRbacRoles) != 0 {
return
}
// In case the current user does not belong to any registered
// teams, if AllowStrayAsViewer option is set, assign Viewer role
// as user's role.
if c.project.AllowStrayAsViewer {
role.ProjectRbacRoles = []string{model.BuiltinRBACRoleViewer.String()}
return
}
err = fmt.Errorf("user (%s) not found in any of the %d project teams", user, len(teams))
return
}