/
shell.go
177 lines (156 loc) · 5.77 KB
/
shell.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
// Copyright 2021 Workrise Technologies Inc.
//
// 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 proxy
import (
"bytes"
"fmt"
"io"
"io/fs"
"io/ioutil"
"os"
"os/exec"
"os/signal"
"path"
"syscall"
"github.com/creack/pty"
"github.com/google/uuid"
"golang.org/x/term"
"k8s.io/apimachinery/pkg/runtime"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
clientcmdapilatest "k8s.io/client-go/tools/clientcmd/api/latest"
"github.com/rigup/ephemeral-iam/internal/appconfig"
util "github.com/rigup/ephemeral-iam/internal/eiamutil"
errorsutil "github.com/rigup/ephemeral-iam/internal/errors"
)
func startShell(svcAcct, accessToken, expiry string, defaultCluster map[string]string, oldState **term.State) {
tmpKubeConfig, err := createTempKubeConfig()
if err != nil {
util.Logger.WithError(err).Fatal("failed to create temp kubeconfig")
}
defer os.Remove(tmpKubeConfig.Name()) // Remove tmpKubeConfig after priv session ends.
// Copy environment variables from user, set PS1 prompt, and set the KUBECONFIG env var.
cmdEnv := append(os.Environ(), buildPrompt(svcAcct), fmt.Sprintf("KUBECONFIG=%s", tmpKubeConfig.Name()))
if len(defaultCluster) > 0 {
// Create the kubeconfig entry for the privileged service account.
c := exec.Command( //nolint:gosec // This would just get you code exec on your own computer
"gcloud", "container", "clusters", "get-credentials", defaultCluster["name"],
"--zone", defaultCluster["location"],
)
c.Env = cmdEnv
errOut := bytes.Buffer{}
c.Stderr = &errOut
if err = c.Run(); err != nil {
util.Logger.Errorf(errOut.String())
} else {
util.Logger.Infof("kubectl is now authenticated as %s", svcAcct)
}
}
if err = writeCredsToKubeConfig(tmpKubeConfig, accessToken, expiry); err != nil {
util.Logger.WithError(err).Fatal("failed to write credentials to temp kubeconfig")
}
// Create the shell command and copy the environment variables from the previous command.
shellCmd := exec.Command("bash")
shellCmd.Env = cmdEnv
util.Logger.Warn("Enter `exit` or press CTRL+D to quit privileged session")
// Start the pty sub-shell.
ptmx, err := pty.Start(shellCmd)
if err != nil {
util.Logger.WithError(err).Fatal("failed to start privileged sub-shell")
}
defer func() {
if err = ptmx.Close(); err != nil {
util.Logger.WithError(err).Fatal("failed to close privileged sub-shell")
}
}()
// Resize the pty shell when the user's terminal is resized.
ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGWINCH)
go func() {
for range ch {
if err = pty.InheritSize(os.Stdin, ptmx); err != nil {
util.Logger.WithError(err).Fatal("failed to resize pty")
}
}
}()
ch <- syscall.SIGWINCH
// Save the state of the current shell so it can be restored later.
if *oldState, err = term.MakeRaw(int(os.Stdin.Fd())); err != nil {
util.Logger.WithError(err).Fatal("failed to save state of current shell")
}
defer func() {
if err := term.Restore(int(os.Stdin.Fd()), *oldState); err != nil {
util.Logger.WithError(err).Fatal("failed to restore original shell")
}
}()
// Send user input to the sub-shell.
go func() {
if _, err := io.Copy(ptmx, os.Stdin); err != nil {
util.Logger.WithError(err).Error("failed to send user input to the sub-shell")
}
}()
// Write the output from the sub-shell to stdout.
if _, err := io.Copy(os.Stdout, ptmx); err != nil {
// On some linux systems, this error is thrown when CTRL-D is received.
if serr, ok := err.(*fs.PathError); ok {
if serr.Path == "/dev/ptmx" {
wg.Done()
return
}
} else {
util.Logger.WithError(err).Error("failed to write the output from the sub-shell to stdout")
}
}
wg.Done()
}
func buildPrompt(svcAcct string) string {
yellow := "\\[\\e[33m\\]"
green := "\\[\\e[36m\\]"
endColor := "\\[\\e[m\\]"
return fmt.Sprintf("PS1=\n[%s%s%s]\n[%seiam%s] > ", yellow, svcAcct, endColor, green, endColor)
}
func createTempKubeConfig() (*os.File, error) {
kubeConfigDir := path.Join(appconfig.GetConfigDir(), "tmp_kube_config")
tmpFileName := uuid.New().String()
tmpKubeConfig, err := os.CreateTemp(kubeConfigDir, tmpFileName)
if err != nil {
return nil, err
}
return tmpKubeConfig, nil
}
func writeCredsToKubeConfig(tmpKubeConfig *os.File, accessToken, expiry string) error {
// Read the tmpKubeConfig into a client-go config object.
config := clientcmdapi.NewConfig()
configBytes, err := ioutil.ReadFile(tmpKubeConfig.Name())
if err != nil {
return errorsutil.New("Failed to read generated tmp kubeconfig", err)
}
if err = runtime.DecodeInto(clientcmdapilatest.Codec, configBytes, config); err != nil {
return errorsutil.New("Failed to deserialize generated tmp kubeconfig", err)
}
// There should only be one, this is an efficient way of getting it.
for _, authInfo := range config.AuthInfos {
// Write the service account's token to the temp kubeconfig.
authInfo.AuthProvider.Config["access-token"] = accessToken
authInfo.AuthProvider.Config["expiry"] = expiry
}
// Serialize the updated config and write it back to the file.
newConfigBytes, err := runtime.Encode(clientcmdapilatest.Codec, config)
if err != nil {
return errorsutil.New("Failed to serialize updated tmp kubeconfig", err)
}
if _, err := tmpKubeConfig.Write(newConfigBytes); err != nil {
return errorsutil.New("Failed to write updated tmp kubeconfig", err)
}
return nil
}