-
Notifications
You must be signed in to change notification settings - Fork 244
/
login.go
150 lines (127 loc) · 5.01 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
package auth
import (
"bytes"
"fmt"
"io"
"os"
"github.com/openshift/oc/pkg/cli/login"
odolog "github.com/redhat-developer/odo/pkg/log"
kapierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/client-go/tools/clientcmd"
)
type KubernetesClient struct{}
var _ Client = (*KubernetesClient)(nil)
func NewKubernetesClient() *KubernetesClient {
return &KubernetesClient{}
}
// Login takes care of authentication part and returns error, if any
func (o KubernetesClient) Login(server, username, password, token, caAuth string, skipTLS bool) error {
// Here we are grabbing the stdout output and then
// throwing it through "copyAndFilter" in order to get
// a correctly filtered result from `odo login`
filteredReader, filteredWriter := io.Pipe()
go func() {
defer filteredWriter.Close()
_, _ = copyAndFilter(odolog.GetStdout(), filteredReader)
}()
a := login.LoginOptions{
Server: server,
CommandName: "odo",
CAFile: caAuth,
InsecureTLS: skipTLS,
Username: username,
Password: password,
Project: "",
Token: token,
PathOptions: &clientcmd.PathOptions{GlobalFile: clientcmd.RecommendedHomeFile, EnvVar: clientcmd.RecommendedConfigPathEnvVar, ExplicitFileFlag: "config", LoadingRules: &clientcmd.ClientConfigLoadingRules{ExplicitPath: ""}},
RequestTimeout: 0,
IOStreams: genericclioptions.IOStreams{Out: filteredWriter, In: os.Stdin, ErrOut: odolog.GetStderr()},
}
// initialize client-go client and read starting kubeconfig file
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
configOverrides := &clientcmd.ConfigOverrides{}
kubeConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, configOverrides)
rawKubeConfig, _ := kubeConfig.RawConfig()
a.StartingKubeConfig = &rawKubeConfig
// if server URL is not given as argument, we will look for current context from kubeconfig file
if len(a.Server) == 0 {
if defaultContext, defaultContextExists := a.StartingKubeConfig.Contexts[a.StartingKubeConfig.CurrentContext]; defaultContextExists {
if cluster, exists := a.StartingKubeConfig.Clusters[defaultContext.Cluster]; exists {
a.Server = cluster.Server
}
}
}
// if defaultNamespace is not defined, we will look for current namespace from kubeconfig file if defined
if len(a.DefaultNamespace) == 0 {
if defaultContext, defaultContextExists := a.StartingKubeConfig.Contexts[a.StartingKubeConfig.CurrentContext]; defaultContextExists {
if len(defaultContext.Namespace) > 0 {
a.DefaultNamespace = defaultContext.Namespace
}
}
}
// 1. Say we're connecting
odolog.Info("Connecting to the OpenShift cluster\n")
// 2. Handle the error messages here. This is copied over from:
// https://github.com/openshift/origin/blob/master/pkg/oc/cli/login/login.go#L60
// as unauthorized errors are handled MANUALLY by oc.
if err := a.GatherInfo(); err != nil {
if kapierrors.IsUnauthorized(err) {
fmt.Println("Login failed (401 Unauthorized)")
fmt.Println("Verify you have provided correct credentials.")
if err, isStatusErr := err.(*kapierrors.StatusError); isStatusErr {
if details := err.Status().Details; details != nil {
for _, cause := range details.Causes {
fmt.Println(cause.Message)
}
}
}
}
return err
}
// 3. Correctly save the configuration
newFileCreated, err := a.SaveConfig()
if err != nil {
return err
}
// If a new file has been created, we output what to do next (obviously odo help). This is taken from:
// https://github.com/openshift/origin/blob/4c293b86b111d9aaeba7bb1e72ee57410652ae9d/pkg/oc/cli/login/login.go#L184
if newFileCreated {
odolog.Infof("\nWelcome! See '%s help' to get started.", a.CommandName)
}
return nil
}
// copyAndFilter captures the output, filters it and then spits it back out to stdout.
// Kindly taken from https://stackoverflow.com/questions/54570268/filtering-the-output-of-a-terminal-output-using-golang
func copyAndFilter(w io.Writer, r io.Reader) ([]byte, error) {
var out []byte
buf := make([]byte, 1024)
for {
n, err := r.Read(buf[:])
if n > 0 {
d := buf[:n]
out = append(out, d...)
if _, e := w.Write(filteredInformation(d)); e != nil {
return out, e
}
}
if err != nil {
// Read returns io.EOF at the end of file, which is not an error for us
if err == io.EOF {
err = nil
}
return out, err
}
}
}
// filteredInformation takes a list of strings ([]byte), replaces them and spits it back out
// This is used since we utilize `oc login` with odo and require certain strings to be filtered / changed
// to their odo equivalent
func filteredInformation(s []byte) []byte {
// List of strings to correctly filter
s = bytes.Replace(s, []byte("oc new-project"), []byte("odo project create"), -1)
s = bytes.Replace(s, []byte("<projectname>"), []byte("<project-name>"), -1)
s = bytes.Replace(s, []byte("project <project-name>"), []byte("project set <project-name>"), -1)
s = bytes.Replace(s, []byte("odo projects"), []byte("odo project list"), -1)
return s
}