forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
v3_ssh_command.go
164 lines (135 loc) · 5.52 KB
/
v3_ssh_command.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
package v3
import (
"net/http"
"code.cloudfoundry.org/cli/actor/sharedaction"
"code.cloudfoundry.org/cli/actor/v3action"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccversion"
"code.cloudfoundry.org/cli/command"
"code.cloudfoundry.org/cli/command/flag"
"code.cloudfoundry.org/cli/command/translatableerror"
"code.cloudfoundry.org/cli/command/v3/shared"
"code.cloudfoundry.org/cli/util/clissh"
)
//go:generate counterfeiter . SSHActor
type SSHActor interface {
ExecuteSecureShell(sshClient sharedaction.SecureShellClient, sshOptions sharedaction.SSHOptions) error
}
//go:generate counterfeiter . V3SSHActor
type V3SSHActor interface {
CloudControllerAPIVersion() string
GetSecureShellConfigurationByApplicationNameSpaceProcessTypeAndIndex(appName string, spaceGUID string, processType string, processIndex uint) (v3action.SSHAuthentication, v3action.Warnings, error)
}
type V3SSHCommand struct {
RequiredArgs flag.AppName `positional-args:"yes"`
ProcessIndex uint `long:"app-instance-index" short:"i" description:"App process instance index (Default: 0)"`
Commands []string `long:"command" short:"c" description:"Command to run"`
DisablePseudoTTY bool `long:"disable-pseudo-tty" short:"T" description:"Disable pseudo-tty allocation"`
ForcePseudoTTY bool `long:"force-pseudo-tty" description:"Force pseudo-tty allocation"`
LocalPortForwardSpecs []flag.SSHPortForwarding `short:"L" description:"Local port forward specification"`
ProcessType string `long:"process" description:"App process name (Default: web)"`
RequestPseudoTTY bool `long:"request-pseudo-tty" short:"t" description:"Request pseudo-tty allocation"`
SkipHostValidation bool `long:"skip-host-validation" short:"k" description:"Skip host key validation. Not recommended!"`
SkipRemoteExecution bool `long:"skip-remote-execution" short:"N" description:"Do not execute a remote command"`
usage interface{} `usage:"cf v3-ssh APP_NAME [--process PROCESS] [-i INDEX] [-c COMMAND]\n [-L [BIND_ADDRESS:]LOCAL_PORT:REMOTE_HOST:REMOTE_PORT]... [--skip-remote-execution]\n [--disable-pseudo-tty | --force-pseudo-tty | --request-pseudo-tty] [--skip-host-validation]"`
relatedCommands interface{} `related_commands:"allow-space-ssh, enable-ssh, space-ssh-allowed, ssh-code, ssh-enabled"`
UI command.UI
Config command.Config
SharedActor command.SharedActor
Actor V3SSHActor
SSHActor SSHActor
SSHClient *clissh.SecureShell
}
func (cmd *V3SSHCommand) Setup(config command.Config, ui command.UI) error {
cmd.UI = ui
cmd.Config = config
sharedActor := sharedaction.NewActor(config)
cmd.SharedActor = sharedActor
cmd.SSHActor = sharedActor
ccClient, uaaClient, err := shared.NewClients(config, ui, true)
if err != nil {
if v3Err, ok := err.(ccerror.V3UnexpectedResponseError); ok && v3Err.ResponseCode == http.StatusNotFound {
return translatableerror.MinimumAPIVersionNotMetError{MinimumVersion: ccversion.MinVersionV3}
}
return err
}
cmd.Actor = v3action.NewActor(ccClient, config, sharedActor, uaaClient)
cmd.SSHClient = clissh.NewDefaultSecureShell()
return nil
}
func (cmd V3SSHCommand) Execute(args []string) error {
cmd.UI.DisplayWarning(command.ExperimentalWarning)
err := command.MinimumAPIVersionCheck(cmd.Actor.CloudControllerAPIVersion(), ccversion.MinVersionV3)
if err != nil {
return err
}
err = cmd.SharedActor.CheckTarget(true, true)
if err != nil {
return err
}
ttyOption, err := cmd.EvaluateTTYOption()
if err != nil {
return err
}
if cmd.ProcessType == "" {
cmd.ProcessType = "web"
}
var forwardSpecs []sharedaction.LocalPortForward
for _, spec := range cmd.LocalPortForwardSpecs {
forwardSpecs = append(forwardSpecs, sharedaction.LocalPortForward(spec))
}
sshAuth, warnings, err := cmd.Actor.GetSecureShellConfigurationByApplicationNameSpaceProcessTypeAndIndex(
cmd.RequiredArgs.AppName,
cmd.Config.TargetedSpace().GUID,
cmd.ProcessType,
cmd.ProcessIndex,
)
cmd.UI.DisplayWarnings(warnings)
if err != nil {
return err
}
err = cmd.SSHActor.ExecuteSecureShell(
cmd.SSHClient,
sharedaction.SSHOptions{
Commands: cmd.Commands,
Endpoint: sshAuth.Endpoint,
HostKeyFingerprint: sshAuth.HostKeyFingerprint,
LocalPortForwardSpecs: forwardSpecs,
Passcode: sshAuth.Passcode,
SkipHostValidation: cmd.SkipHostValidation,
SkipRemoteExecution: cmd.SkipRemoteExecution,
TTYOption: ttyOption,
Username: sshAuth.Username,
})
if err != nil {
return err
}
return nil
}
func (cmd V3SSHCommand) parseForwardSpecs() ([]sharedaction.LocalPortForward, error) {
return nil, nil
}
// EvaluateTTYOption determines which TTY options are mutually exclusive and
// returns an error accordingly.
func (cmd V3SSHCommand) EvaluateTTYOption() (sharedaction.TTYOption, error) {
var count int
option := sharedaction.RequestTTYAuto
if cmd.DisablePseudoTTY {
option = sharedaction.RequestTTYNo
count++
}
if cmd.ForcePseudoTTY {
option = sharedaction.RequestTTYForce
count++
}
if cmd.RequestPseudoTTY {
option = sharedaction.RequestTTYYes
count++
}
if count > 1 {
return option, translatableerror.ArgumentCombinationError{Args: []string{
"--disable-pseudo-tty", "-T", "--force-pseudo-tty", "--request-pseudo-tty", "-t",
}}
}
return option, nil
}