github.com/loafoe/cli@v7.1.0+incompatible/actor/v7action/ssh.go (about) 1 package v7action 2 3 import ( 4 "fmt" 5 6 "code.cloudfoundry.org/cli/actor/actionerror" 7 "code.cloudfoundry.org/cli/resources" 8 ) 9 10 type SSHAuthentication struct { 11 Endpoint string 12 HostKeyFingerprint string 13 Passcode string 14 Username string 15 } 16 17 func (actor Actor) GetSSHPasscode() (string, error) { 18 return actor.UAAClient.GetSSHPasscode(actor.Config.AccessToken(), actor.Config.SSHOAuthClient()) 19 } 20 21 // GetSecureShellConfigurationByApplicationNameSpaceProcessTypeAndIndex returns 22 // back the SSH authentication information for the SSH session. 23 func (actor Actor) GetSecureShellConfigurationByApplicationNameSpaceProcessTypeAndIndex( 24 appName string, spaceGUID string, processType string, processIndex uint, 25 ) (SSHAuthentication, Warnings, error) { 26 var allWarnings Warnings 27 28 endpoint := actor.CloudControllerClient.AppSSHEndpoint() 29 if endpoint == "" { 30 return SSHAuthentication{}, nil, actionerror.SSHEndpointNotSetError{} 31 } 32 33 fingerprint := actor.CloudControllerClient.AppSSHHostKeyFingerprint() 34 if fingerprint == "" { 35 return SSHAuthentication{}, nil, actionerror.SSHHostKeyFingerprintNotSetError{} 36 } 37 38 passcode, err := actor.UAAClient.GetSSHPasscode(actor.Config.AccessToken(), actor.Config.SSHOAuthClient()) 39 if err != nil { 40 return SSHAuthentication{}, Warnings{}, err 41 } 42 43 application, appWarnings, err := actor.GetApplicationByNameAndSpace(appName, spaceGUID) 44 allWarnings = append(allWarnings, appWarnings...) 45 if err != nil { 46 return SSHAuthentication{}, allWarnings, err 47 } 48 49 if !application.Started() { 50 return SSHAuthentication{}, allWarnings, actionerror.ApplicationNotStartedError{Name: appName} 51 } 52 53 username, processWarnings, err := actor.getUsername(application, processType, processIndex) 54 allWarnings = append(allWarnings, processWarnings...) 55 if err != nil { 56 return SSHAuthentication{}, allWarnings, err 57 } 58 59 return SSHAuthentication{ 60 Endpoint: endpoint, 61 HostKeyFingerprint: fingerprint, 62 Passcode: passcode, 63 Username: username, 64 }, allWarnings, err 65 } 66 67 func (actor Actor) getUsername(application resources.Application, processType string, processIndex uint) (string, Warnings, error) { 68 processSummaries, processWarnings, err := actor.getProcessSummariesForApp(application.GUID, false) 69 if err != nil { 70 return "", processWarnings, err 71 } 72 73 var processSummary ProcessSummary 74 for _, appProcessSummary := range processSummaries { 75 if appProcessSummary.Type == processType { 76 processSummary = appProcessSummary 77 break 78 } 79 } 80 81 if processSummary.GUID == "" { 82 return "", processWarnings, actionerror.ProcessNotFoundError{ProcessType: processType} 83 } 84 85 var processInstance ProcessInstance 86 for _, instance := range processSummary.InstanceDetails { 87 if uint(instance.Index) == processIndex { 88 processInstance = instance 89 break 90 } 91 } 92 93 if processInstance == (ProcessInstance{}) { 94 return "", processWarnings, actionerror.ProcessInstanceNotFoundError{ProcessType: processType, InstanceIndex: processIndex} 95 } 96 97 if !processInstance.Running() { 98 return "", processWarnings, actionerror.ProcessInstanceNotRunningError{ProcessType: processType, InstanceIndex: processIndex} 99 } 100 101 return fmt.Sprintf("cf:%s/%d", processSummary.GUID, processIndex), processWarnings, nil 102 }