forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ssh.go
97 lines (79 loc) · 2.99 KB
/
ssh.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
package v7action
import (
"fmt"
"code.cloudfoundry.org/cli/actor/actionerror"
)
type SSHAuthentication struct {
Endpoint string
HostKeyFingerprint string
Passcode string
Username string
}
// GetSecureShellConfigurationByApplicationNameSpaceProcessTypeAndIndex returns
// back the SSH authentication information for the SSH session.
func (actor Actor) GetSecureShellConfigurationByApplicationNameSpaceProcessTypeAndIndex(
appName string, spaceGUID string, processType string, processIndex uint,
) (SSHAuthentication, Warnings, error) {
var allWarnings Warnings
endpoint := actor.CloudControllerClient.AppSSHEndpoint()
if endpoint == "" {
return SSHAuthentication{}, nil, actionerror.SSHEndpointNotSetError{}
}
fingerprint := actor.CloudControllerClient.AppSSHHostKeyFingerprint()
if fingerprint == "" {
return SSHAuthentication{}, nil, actionerror.SSHHostKeyFingerprintNotSetError{}
}
passcode, err := actor.UAAClient.GetSSHPasscode(actor.Config.AccessToken(), actor.Config.SSHOAuthClient())
if err != nil {
return SSHAuthentication{}, Warnings{}, err
}
application, appWarnings, err := actor.GetApplicationByNameAndSpace(appName, spaceGUID)
allWarnings = append(allWarnings, appWarnings...)
if err != nil {
return SSHAuthentication{}, allWarnings, err
}
if !application.Started() {
return SSHAuthentication{}, allWarnings, actionerror.ApplicationNotStartedError{Name: appName}
}
username, processWarnings, err := actor.getUsername(application, processType, processIndex)
allWarnings = append(allWarnings, processWarnings...)
if err != nil {
return SSHAuthentication{}, allWarnings, err
}
return SSHAuthentication{
Endpoint: endpoint,
HostKeyFingerprint: fingerprint,
Passcode: passcode,
Username: username,
}, allWarnings, err
}
func (actor Actor) getUsername(application Application, processType string, processIndex uint) (string, Warnings, error) {
processSummaries, processWarnings, err := actor.getProcessSummariesForApp(application.GUID, false)
if err != nil {
return "", processWarnings, err
}
var processSummary ProcessSummary
for _, appProcessSummary := range processSummaries {
if appProcessSummary.Type == processType {
processSummary = appProcessSummary
break
}
}
if processSummary.GUID == "" {
return "", processWarnings, actionerror.ProcessNotFoundError{ProcessType: processType}
}
var processInstance ProcessInstance
for _, instance := range processSummary.InstanceDetails {
if uint(instance.Index) == processIndex {
processInstance = instance
break
}
}
if processInstance == (ProcessInstance{}) {
return "", processWarnings, actionerror.ProcessInstanceNotFoundError{ProcessType: processType, InstanceIndex: processIndex}
}
if !processInstance.Running() {
return "", processWarnings, actionerror.ProcessInstanceNotRunningError{ProcessType: processType, InstanceIndex: processIndex}
}
return fmt.Sprintf("cf:%s/%d", processSummary.GUID, processIndex), processWarnings, nil
}