1 package auth
2
3 import (
4 "bytes"
5 "fmt"
6 "io"
7 "os"
8
9 "github.com/openshift/oc/pkg/cli/login"
10 kapierrors "k8s.io/apimachinery/pkg/api/errors"
11 "k8s.io/cli-runtime/pkg/genericclioptions"
12 "k8s.io/client-go/tools/clientcmd"
13
14 odolog "github.com/redhat-developer/odo/pkg/log"
15 )
16
17 type KubernetesClient struct{}
18
19 var _ Client = (*KubernetesClient)(nil)
20
21 func NewKubernetesClient() *KubernetesClient {
22 return &KubernetesClient{}
23 }
24
25
26 func (o KubernetesClient) Login(server, username, password, token, caAuth string, skipTLS bool) error {
27
28
29
30 filteredReader, filteredWriter := io.Pipe()
31 go func() {
32 defer filteredWriter.Close()
33 _, _ = copyAndFilter(odolog.GetStdout(), filteredReader)
34 }()
35
36 a := login.LoginOptions{
37 Server: server,
38 CommandName: "oc",
39 CAFile: caAuth,
40 InsecureTLS: skipTLS,
41 Username: username,
42 Password: password,
43 Project: "",
44 Token: token,
45 PathOptions: &clientcmd.PathOptions{GlobalFile: clientcmd.RecommendedHomeFile, EnvVar: clientcmd.RecommendedConfigPathEnvVar, ExplicitFileFlag: "config", LoadingRules: &clientcmd.ClientConfigLoadingRules{ExplicitPath: ""}},
46 RequestTimeout: 0,
47 IOStreams: genericclioptions.IOStreams{Out: filteredWriter, In: os.Stdin, ErrOut: odolog.GetStderr()},
48 }
49
50
51
52 loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
53 configOverrides := &clientcmd.ConfigOverrides{}
54 kubeConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, configOverrides)
55
56 rawKubeConfig, _ := kubeConfig.RawConfig()
57
58 a.StartingKubeConfig = &rawKubeConfig
59
60
61 if len(a.Server) == 0 {
62 if defaultContext, defaultContextExists := a.StartingKubeConfig.Contexts[a.StartingKubeConfig.CurrentContext]; defaultContextExists {
63 if cluster, exists := a.StartingKubeConfig.Clusters[defaultContext.Cluster]; exists {
64 a.Server = cluster.Server
65 }
66 }
67 }
68
69
70 if len(a.DefaultNamespace) == 0 {
71 if defaultContext, defaultContextExists := a.StartingKubeConfig.Contexts[a.StartingKubeConfig.CurrentContext]; defaultContextExists {
72 if len(defaultContext.Namespace) > 0 {
73 a.DefaultNamespace = defaultContext.Namespace
74 }
75 }
76 }
77
78
79 odolog.Info("Connecting to the OpenShift cluster\n")
80
81
82
83
84 if err := a.GatherInfo(); err != nil {
85 if kapierrors.IsUnauthorized(err) {
86 fmt.Println("Login failed (401 Unauthorized)")
87 fmt.Println("Verify you have provided correct credentials.")
88
89 if err, isStatusErr := err.(*kapierrors.StatusError); isStatusErr {
90 if details := err.Status().Details; details != nil {
91 for _, cause := range details.Causes {
92 fmt.Println(cause.Message)
93 }
94 }
95 }
96 }
97 return err
98 }
99
100
101 newFileCreated, err := a.SaveConfig()
102 if err != nil {
103 return err
104 }
105
106
107
108 if newFileCreated {
109 odolog.Infof("\nWelcome! See '%s help' to get started.", a.CommandName)
110 }
111
112 return nil
113 }
114
115
116
117 func copyAndFilter(w io.Writer, r io.Reader) ([]byte, error) {
118 var out []byte
119 buf := make([]byte, 1024)
120 for {
121 n, err := r.Read(buf[:])
122 if n > 0 {
123 d := buf[:n]
124 out = append(out, d...)
125 if _, e := w.Write(filteredInformation(d)); e != nil {
126 return out, e
127 }
128 }
129 if err != nil {
130
131 if err == io.EOF {
132 err = nil
133 }
134 return out, err
135 }
136 }
137 }
138
139
140
141
142 func filteredInformation(s []byte) []byte {
143
144
145 s = bytes.Replace(s, []byte("oc new-project"), []byte("odo create project"), -1)
146 s = bytes.Replace(s, []byte("oc project "), []byte("odo set project "), -1)
147 s = bytes.Replace(s, []byte("oc projects"), []byte("odo list project"), -1)
148
149 return s
150 }
151
View as plain text