github.com/cloudfoundry/cli@v7.1.0+incompatible/actor/v3action/ssh.go (about)

     1  package v3action
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"code.cloudfoundry.org/cli/actor/actionerror"
     7  )
     8  
     9  type SSHAuthentication struct {
    10  	Endpoint           string
    11  	HostKeyFingerprint string
    12  	Passcode           string
    13  	Username           string
    14  }
    15  
    16  // GetSecureShellConfigurationByApplicationNameSpaceProcessTypeAndIndex returns
    17  // back the SSH authentication information for the SSH session.
    18  func (actor Actor) GetSecureShellConfigurationByApplicationNameSpaceProcessTypeAndIndex(
    19  	appName string, spaceGUID string, processType string, processIndex uint,
    20  ) (SSHAuthentication, Warnings, error) {
    21  	endpoint := actor.CloudControllerClient.AppSSHEndpoint()
    22  	if endpoint == "" {
    23  		return SSHAuthentication{}, nil, actionerror.SSHEndpointNotSetError{}
    24  	}
    25  
    26  	fingerprint := actor.CloudControllerClient.AppSSHHostKeyFingerprint()
    27  	if fingerprint == "" {
    28  		return SSHAuthentication{}, nil, actionerror.SSHHostKeyFingerprintNotSetError{}
    29  	}
    30  
    31  	passcode, err := actor.UAAClient.GetSSHPasscode(actor.Config.AccessToken(), actor.Config.SSHOAuthClient())
    32  	if err != nil {
    33  		return SSHAuthentication{}, Warnings{}, err
    34  	}
    35  
    36  	// TODO: don't use Summary object for this
    37  	appSummary, warnings, err := actor.GetApplicationSummaryByNameAndSpace(appName, spaceGUID, false)
    38  	if err != nil {
    39  		return SSHAuthentication{}, warnings, err
    40  	}
    41  
    42  	var processSummary ProcessSummary
    43  	for _, appProcessSummary := range appSummary.ProcessSummaries {
    44  		if appProcessSummary.Type == processType {
    45  			processSummary = appProcessSummary
    46  			break
    47  		}
    48  	}
    49  	if processSummary.GUID == "" {
    50  		return SSHAuthentication{}, warnings, actionerror.ProcessNotFoundError{ProcessType: processType}
    51  	}
    52  
    53  	if !appSummary.Application.Started() {
    54  		return SSHAuthentication{}, warnings, actionerror.ApplicationNotStartedError{Name: appName}
    55  	}
    56  
    57  	var processInstance ProcessInstance
    58  	for _, instance := range processSummary.InstanceDetails {
    59  		if uint(instance.Index) == processIndex {
    60  			processInstance = instance
    61  			break
    62  		}
    63  	}
    64  
    65  	if processInstance == (ProcessInstance{}) {
    66  		return SSHAuthentication{}, warnings, actionerror.ProcessInstanceNotFoundError{ProcessType: processType, InstanceIndex: processIndex}
    67  	}
    68  
    69  	if !processInstance.Running() {
    70  		return SSHAuthentication{}, warnings, actionerror.ProcessInstanceNotRunningError{ProcessType: processType,
    71  			InstanceIndex: processIndex}
    72  	}
    73  
    74  	return SSHAuthentication{
    75  		Endpoint:           endpoint,
    76  		HostKeyFingerprint: fingerprint,
    77  		Passcode:           passcode,
    78  		Username:           fmt.Sprintf("cf:%s/%d", processSummary.GUID, processIndex),
    79  	}, warnings, err
    80  }