-
Notifications
You must be signed in to change notification settings - Fork 786
/
login.go
272 lines (231 loc) · 7.12 KB
/
login.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package cmd
import (
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"path/filepath"
"strings"
"github.com/chromedp/chromedp/runner"
"github.com/hpcloud/tail"
"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
jxlog "github.com/jenkins-x/jx/pkg/log"
"github.com/jenkins-x/jx/pkg/util"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"gopkg.in/AlecAivazis/survey.v1/terminal"
)
const (
defaultNamespace = "jx"
UserOnboardingEndpoint = "/api/v1/users"
SsoCookieName = "sso-core"
)
// Login holds the login information
type Login struct {
Data UserLoginInfo `form:"data,omitempty" json:"data,omitempty" yaml:"data,omitempty" xml:"data,omitempty"`
}
// UserLoginInfo user login information
type UserLoginInfo struct {
// The Kubernetes API server public CA data
Ca string `form:"ca,omitempty" json:"ca,omitempty" yaml:"ca,omitempty" xml:"ca,omitempty"`
// The login username of the user
Login string `form:"login,omitempty" json:"login,omitempty" yaml:"login,omitempty" xml:"login,omitempty"`
// The Kubernetes API server address
Server string `form:"server,omitempty" json:"server,omitempty" yaml:"server,omitempty" xml:"server,omitempty"`
// The login token of the user
Token string `form:"token,omitempty" json:"token,omitempty" yaml:"token,omitempty" xml:"token,omitempty"`
}
// LoginOptions options for login command
type LoginOptions struct {
CommonOptions
URL string
Team string
}
var (
login_long = templates.LongDesc(`
Onboards an user into the CloudBees application and configures the Kubernetes client configuration.
A CloudBess app can be created as an addon with 'jx create addon cloudbees'`)
login_example = templates.Examples(`
# Onboard into CloudBees application
jx login -u https://cloudbees-app-url
# Onboard into CloudBees application and switched to team 'cheese'
jx login -u https://cloudbees-app-url -t cheese
`)
)
func NewCmdLogin(f Factory, in terminal.FileReader, out terminal.FileWriter, errOut io.Writer) *cobra.Command {
options := &LoginOptions{
CommonOptions: CommonOptions{
Factory: f,
In: in,
Out: out,
Err: errOut,
},
}
cmd := &cobra.Command{
Use: "login",
Short: "Onboard an user into the CloudBees application",
Long: login_long,
Example: login_example,
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
CheckErr(err)
},
}
cmd.Flags().StringVarP(&options.URL, "url", "u", "", "The URL of the CloudBees application")
cmd.Flags().StringVarP(&options.Team, "team", "t", "", "The team to use upon login")
return cmd
}
func (o *LoginOptions) Run() error {
_, err := url.ParseRequestURI(o.URL)
if err != nil {
return errors.Wrap(err, "validation failed for URL, ensure URL is well formed including scheme, i.e. https://foo.com")
}
// ensure base set of binaries are installed which are required by jx
err = o.installRequirements("")
if err != nil {
return errors.Wrap(err, "installing required binaries")
}
userLoginInfo, err := o.Login()
if err != nil {
return errors.Wrap(err, "logging into the CloudBees application")
}
err = o.Kube().UpdateConfig(defaultNamespace, userLoginInfo.Server, userLoginInfo.Ca, userLoginInfo.Login, userLoginInfo.Token)
if err != nil {
return errors.Wrap(err, "updating the ~/kube/config file")
}
jxlog.Infof("You are %s. You credentials are stored in %s file.\n",
util.ColorInfo("successfully logged in"), util.ColorInfo("~/.kube/config"))
teamOptions := TeamOptions{
CommonOptions: o.CommonOptions,
}
teamOptions.Args = []string{o.Team}
err = teamOptions.Run()
if err != nil {
return errors.Wrap(err, "switching team")
}
return nil
}
func (o *LoginOptions) Login() (*UserLoginInfo, error) {
url := o.URL
if url == "" {
return nil, errors.New("please provide the URL of the CloudBees application in '--url' option")
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
userDataDir, err := ioutil.TempDir("", "jx-login-chrome-userdata-dir")
if err != nil {
return nil, errors.Wrap(err, "creating the chrome user data dir")
}
defer os.RemoveAll(userDataDir)
netLogFile := filepath.Join(userDataDir, "net-logs.json")
options := func(m map[string]interface{}) error {
m["start-url"] = o.URL
m["user-data-dir"] = userDataDir
m["log-net-log"] = netLogFile
m["net-log-capture-mode"] = "IncludeCookiesAndCredentials"
m["v"] = 1
return nil
}
r, err := runner.New(options)
if err != nil {
return nil, errors.Wrap(err, "creating chrome runner")
}
err = r.Start(ctx)
if err != nil {
return nil, errors.Wrap(err, "starting chrome")
}
t, err := tail.TailFile(netLogFile, tail.Config{
Follow: true,
Poll: true, // ionotify does not work on all platforms and the cost for this is not significantly high
Logger: log.New(ioutil.Discard, "", log.LstdFlags)})
if err != nil {
return nil, errors.Wrap(err, "reading the netlog file")
}
cookie := ""
pattern := fmt.Sprintf("%s=", SsoCookieName)
for line := range t.Lines {
if strings.Contains(line.Text, pattern) {
cookie = ExtractSsoCookie(line.Text)
break
}
}
if cookie == "" {
return nil, errors.New("failed to log into the CloudBees application")
}
userLoginInfo, err := o.OnboardUser(cookie)
if err != nil {
return nil, errors.Wrap(err, "onboarding user")
}
err = r.Shutdown(ctx)
if err != nil {
return nil, errors.Wrap(err, "shutting down Chrome")
}
err = r.Wait()
if err != nil {
return nil, errors.Wrap(err, "waiting for Chrome to exit")
}
return userLoginInfo, nil
}
func (o *LoginOptions) OnboardUser(cookie string) (*UserLoginInfo, error) {
client := http.Client{}
req, err := http.NewRequest(http.MethodPost, o.onboardingURL(), nil)
if err != nil {
return nil, errors.Wrap(err, "building onboarding request")
}
req.Header.Add("Accept", "application/json")
if cookie == "" {
return nil, errors.New("empty SSO cookie")
}
ssoCookie := http.Cookie{Name: SsoCookieName, Value: cookie}
req.AddCookie(&ssoCookie)
resp, err := client.Do(req)
if err != nil {
return nil, errors.Wrap(err, "executing onboarding request")
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, errors.Wrap(err, "reading user onboarding information from response body")
}
if resp.StatusCode != http.StatusCreated {
return nil, fmt.Errorf("user onboarding status code: %d, error: %s", resp.StatusCode, string(body))
}
login := &Login{}
if err := json.Unmarshal(body, login); err != nil {
return nil, errors.Wrap(err, "parsing the login information from response")
}
return &login.Data, nil
}
func (o *LoginOptions) onboardingURL() string {
url := o.URL
if strings.HasSuffix(url, "/") {
url = strings.TrimSuffix(url, "/")
}
return url + UserOnboardingEndpoint
}
func ExtractSsoCookie(text string) string {
cookiePattern := fmt.Sprintf("%s=", SsoCookieName)
start := strings.Index(text, cookiePattern)
if start < 0 {
return ""
}
end := -1
cookieStart := start + len(cookiePattern)
for i, ch := range text[cookieStart:] {
if ch == ';' {
end = cookieStart + i
break
}
}
if end < 0 {
return ""
}
return text[cookieStart:end]
}