forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ssh.go
80 lines (67 loc) · 2.49 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
package v3action
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) {
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
}
// TODO: don't use Summary object for this
appSummary, warnings, err := actor.GetApplicationSummaryByNameAndSpace(appName, spaceGUID, false)
if err != nil {
return SSHAuthentication{}, warnings, err
}
var processSummary ProcessSummary
for _, appProcessSummary := range appSummary.ProcessSummaries {
if appProcessSummary.Type == processType {
processSummary = appProcessSummary
break
}
}
if processSummary.GUID == "" {
return SSHAuthentication{}, warnings, actionerror.ProcessNotFoundError{ProcessType: processType}
}
if !appSummary.Application.Started() {
return SSHAuthentication{}, warnings, actionerror.ApplicationNotStartedError{Name: appName}
}
var processInstance ProcessInstance
for _, instance := range processSummary.InstanceDetails {
if uint(instance.Index) == processIndex {
processInstance = instance
break
}
}
if processInstance == (ProcessInstance{}) {
return SSHAuthentication{}, warnings, actionerror.ProcessInstanceNotFoundError{ProcessType: processType, InstanceIndex: processIndex}
}
if !processInstance.Running() {
return SSHAuthentication{}, warnings, actionerror.ProcessInstanceNotRunningError{ProcessType: processType,
InstanceIndex: processIndex}
}
return SSHAuthentication{
Endpoint: endpoint,
HostKeyFingerprint: fingerprint,
Passcode: passcode,
Username: fmt.Sprintf("cf:%s/%d", processSummary.GUID, processIndex),
}, warnings, err
}