-
Notifications
You must be signed in to change notification settings - Fork 111
/
sshusercheckservice.go
76 lines (64 loc) · 3.42 KB
/
sshusercheckservice.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
package sshusercheckservice
import (
"fmt"
"strings"
"github.com/chef/automate/components/automate-cli/pkg/verifyserver/models"
"github.com/chef/automate/components/automate-cli/pkg/verifyserver/constants"
"github.com/chef/automate/lib/io/fileutils"
"github.com/chef/automate/lib/logger"
"github.com/chef/automate/lib/sshutils"
)
type SshUserCheckService interface {
CheckSshUserDetails(*models.SshUserChecksRequest) (*models.ChecksResponse, error)
}
type SshUserServiceImpl struct {
logger logger.Logger
SshUtil sshutils.SSHUtil
pemFileName string
FileUtils fileutils.FileUtils
}
func NewSshUserCheckService(logger logger.Logger, fileutils fileutils.FileUtils, sshUtils sshutils.SSHUtil) *SshUserServiceImpl {
return &SshUserServiceImpl{
logger: logger,
SshUtil: sshUtils,
pemFileName: constants.PEM_FILE_NAME,
FileUtils: fileutils,
}
}
func (ssu *SshUserServiceImpl) CheckSshUserDetails(req *models.SshUserChecksRequest) (*models.ChecksResponse, error) {
ssu.logger.Debugf("The Request value entered by the user ip: %v, port: %v, userName: %v ", req.Ip, req.Port, req.UserName)
filePath, err := ssu.FileUtils.CreateTempFile(req.PrivateKey, constants.PEM_FILE_NAME)
if err != nil {
ssu.logger.Error("Error while creating the key file on the desired file path: ", err)
return nil, err
}
sshConfig := sshutils.NewSshConfig(req.Ip, req.Port, filePath, req.UserName)
sshCheckResponse, isCheckPassed := ssu.CheckSshConnection(sshConfig, req.Ip, req.SudoPassword)
serviceResponse := &models.ChecksResponse{}
serviceResponse.Passed = isCheckPassed
serviceResponse.Checks = sshCheckResponse
ssu.FileUtils.DeleteFile(filePath)
return serviceResponse, nil
}
func (ssu *SshUserServiceImpl) CheckSshConnection(sshConfig sshutils.SSHConfig, ip string, sudoPassword string) ([]models.Checks, bool) {
responseArray := []models.Checks{}
command := fmt.Sprintf(constants.SUDO_PASSWORD_CMD, sudoPassword)
checkResponse, err := ssu.SshUtil.Execute(sshConfig, command)
ssu.logger.Debug("The response after creating connection and executing command on the terminal: ", checkResponse)
if err != nil {
if strings.Contains(checkResponse, "Connection creation failed") {
sshCheckResponse := models.NewFailureCheck(constants.SSH_USER_FAILURE_TITLE, constants.SSH_USER_ERROR_MESSAGE+ip, constants.SSH_USER_RESOLUTION_MESSAGE+ip)
sudoPasswordCheckResponse := models.NewFailureCheck(constants.SUDO_PASSWORD_FAILURE_TITLE, constants.SUDO_PASSWORD_CONNECTION_ERROR_MESSAGE+ip, constants.SUDO_PASSWORD_CONNECTION_RESOLUTION_MESSAGE+ip)
responseArray = append(responseArray, *sshCheckResponse, *sudoPasswordCheckResponse)
return responseArray, false
}
sshCheckResponse := models.NewSuccessCheck(constants.SSH_USER_SUCCESS_TITLE, constants.SSH_USER_SUCCESS_MESSAGE+ip)
sudoPasswordCheckResponse := models.NewFailureCheck(constants.SUDO_PASSWORD_FAILURE_TITLE, constants.SUDO_PASSWORD_ERROR_MESSAGE+ip, constants.SUDO_PASSWORD_FAILURE_RESOLUTION_MESSAGE+ip)
responseArray = append(responseArray, *sshCheckResponse, *sudoPasswordCheckResponse)
return responseArray, false
}
sshCheckResponse := models.NewSuccessCheck(constants.SSH_USER_SUCCESS_TITLE, constants.SSH_USER_SUCCESS_MESSAGE+ip)
sudoPasswordCheckResponse := models.NewSuccessCheck(constants.SUDO_PASSWORD_TITLE, constants.SUDO_PASSWORD_SUCCESS_MESSAGE+ip)
responseArray = append(responseArray, *sshCheckResponse, *sudoPasswordCheckResponse)
return responseArray, true
}