MCPcopy Index your code
hub / github.com/devspace-sh/devspace / startLocalSSH

Function startLocalSSH

pkg/devspace/services/proxycommands/commands.go:81–151  ·  view source on GitHub ↗
(ctx devspacecontext.Context, selector targetselector.TargetSelector, reverseCommands []*latest.ProxyCommand, remotePort int, addr string, parent *tomb.Tomb)

Source from the content-addressed store, hash-verified

79}
80
81func startLocalSSH(ctx devspacecontext.Context, selector targetselector.TargetSelector, reverseCommands []*latest.ProxyCommand, remotePort int, addr string, parent *tomb.Tomb) error {
82 if ctx.IsDone() {
83 return nil
84 }
85
86 // find target container
87 container, err := selector.SelectSingleContainer(ctx.Context(), ctx.KubeClient(), ctx.Log())
88 if err != nil {
89 return errors.Wrap(err, "error selecting container")
90 } else if container == nil {
91 return nil
92 }
93
94 // create a new public / private key
95 publicKey, privateKey, err := ssh.MakeSSHKeyPair()
96 if err != nil {
97 return errors.Wrap(err, "generate key pair")
98 }
99
100 // gather all commands that should get replaced in the container
101 commandsToReplace := []string{}
102 gitCredentials := false
103 for _, r := range reverseCommands {
104 if r.GitCredentials {
105 gitCredentials = true
106 }
107 if r.Command == "" {
108 continue
109 }
110
111 commandsToReplace = append(commandsToReplace, r.Command)
112 }
113
114 // execute configure command in container
115 command := []string{inject.DevSpaceHelperContainerPath, "proxy-commands", "configure", "--port", strconv.Itoa(remotePort), "--public-key", base64.StdEncoding.EncodeToString([]byte(publicKey)), "--private-key", base64.StdEncoding.EncodeToString([]byte(privateKey))}
116 if len(commandsToReplace) > 0 {
117 command = append(command, "--commands", strings.Join(commandsToReplace, ","))
118 }
119 if gitCredentials {
120 command = append(command, "--git-credentials")
121 }
122
123 stdout, stderr, err := ctx.KubeClient().ExecBuffered(ctx.Context(), container.Pod, container.Container.Name, command, nil)
124 if err != nil {
125 return fmt.Errorf("error setting up proxy commands in container: %s %s %v", string(stdout), string(stderr), err)
126 }
127 containerWorkingDir := strings.TrimSpace(string(stdout))
128 if containerWorkingDir == "" {
129 return fmt.Errorf("couldn't retrieve container working dir")
130 }
131
132 // parse key
133 var keys []sshpkg.PublicKey
134 keyBytes := []byte(publicKey)
135 for len(keyBytes) > 0 {
136 key, _, _, rest, err := sshpkg.ParseAuthorizedKey(keyBytes)
137 if err != nil {
138 return errors.Wrap(err, "parse authorized key")

Callers 1

startProxyCommandsFunction · 0.85

Calls 12

MakeSSHKeyPairFunction · 0.92
NewReverseCommandsServerFunction · 0.85
GoMethod · 0.80
IsDoneMethod · 0.65
SelectSingleContainerMethod · 0.65
ContextMethod · 0.65
KubeClientMethod · 0.65
LogMethod · 0.65
ExecBufferedMethod · 0.65
WorkingDirMethod · 0.65
ErrorfMethod · 0.45
ListenAndServeMethod · 0.45

Tested by

no test coverage detected