forked from cloudfoundry/cli
/
ssh.go
108 lines (88 loc) · 3.36 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
98
99
100
101
102
103
104
105
106
107
108
package v7action
import (
"fmt"
"github.com/LukasHeimann/cloudfoundrycli/v8/actor/actionerror"
"github.com/LukasHeimann/cloudfoundrycli/v8/resources"
)
type SSHAuthentication struct {
Endpoint string
HostKeyFingerprint string
Passcode string
Username string
}
func (actor Actor) GetSSHPasscode() (string, error) {
return actor.UAAClient.GetSSHPasscode(actor.Config.AccessToken(), actor.Config.SSHOAuthClient())
}
// 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
rootInfo, warnings, err := actor.CloudControllerClient.GetInfo()
allWarnings = append(allWarnings, warnings...)
if err != nil {
return SSHAuthentication{}, allWarnings, err
}
endpoint := rootInfo.AppSSHEndpoint()
if endpoint == "" {
return SSHAuthentication{}, nil, actionerror.SSHEndpointNotSetError{}
}
fingerprint := rootInfo.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 resources.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
}