Skip to content

Commit

Permalink
Increased the RunCommand timeout during the registration process for …
Browse files Browse the repository at this point in the history
…the on-prem instances
  • Loading branch information
drmihalj authored and Aperocky committed Oct 21, 2024
1 parent b63d4f5 commit ab4144f
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 25 deletions.
10 changes: 8 additions & 2 deletions agent/managedInstances/fingerprint/fingerprint.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,16 @@ func SetSimilarityThreshold(log log.T, value int) (err error) {
}

// generateFingerprint generates new fingerprint and saves it in the vault
func generateFingerprint(log log.T) (string, error) {
func generateFingerprint(log log.T) (fingerprint string, err error) {
defer func() {
if r := recover(); r != nil {
log.Errorf("Recovered from panic: %v", r)
err = fmt.Errorf("Unexpected error while calculateing fingerprint: %v", r)
}
}()

var hardwareHash map[string]string
var savedHwInfo hwInfo
var err error
var hwHashErr error

// retry getting the new hash and compare with the saved hash for 3 times
Expand Down
41 changes: 22 additions & 19 deletions agent/managedInstances/fingerprint/hardwareInfo_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,16 +106,15 @@ var currentHwHash = func() (map[string]string, error) {
log.Debug("WMI Service is ready to be queried....")

wmiInterface, _ := getWMIInterface(log)

hardwareHash[hardwareID], _ = csproductUuid(log, wmiInterface)
hardwareHash["processor-hash"], _ = processorInfoHash(wmiInterface)
hardwareHash["memory-hash"], _ = memoryInfoHash(wmiInterface)
hardwareHash["bios-hash"], _ = biosInfoHash(wmiInterface)
hardwareHash["system-hash"], _ = systemInfoHash(wmiInterface)
hardwareHash["processor-hash"], _ = processorInfoHash(log, wmiInterface)
hardwareHash["memory-hash"], _ = memoryInfoHash(log, wmiInterface)
hardwareHash["bios-hash"], _ = biosInfoHash(log, wmiInterface)
hardwareHash["system-hash"], _ = systemInfoHash(log, wmiInterface)
hardwareHash["hostname-info"], _ = hostnameInfo()
hardwareHash[ipAddressID], _ = primaryIpInfo()
hardwareHash["macaddr-info"], _ = macAddrInfo()
hardwareHash["disk-info"], _ = diskInfoHash(wmiInterface)
hardwareHash["disk-info"], _ = diskInfoHash(log, wmiInterface)

return hardwareHash, nil
}
Expand Down Expand Up @@ -157,8 +156,10 @@ func getWMIInterface(logger log.T) (wmiInterface WMIInterface, err error) {

// if it is Windows 2025 or later use CIM Instance, otherwise use WMIC
if winMajorVersion >= win2025MajorVersion && winMinorVersion >= win2025MinorVersion && winBuildNumber >= win2025BuildNumber {
logger.Debugf("Windows version is %d.%d.%d, setting CIM Instance as WMI interface", winMajorVersion, winMinorVersion, winBuildNumber)
return cimInstance, nil
} else {
logger.Debugf("Windows version is %d.%d.%d, setting wmic as WMI interface", winMajorVersion, winMinorVersion, winBuildNumber)
return wmic, nil
}
}
Expand All @@ -173,7 +174,7 @@ func csproductUuid(logger log.T, wmiInterface WMIInterface) (encodedValue string
case wmic:
encodedValue, uuid, err = commandOutputHash(wmicCommand, "csproduct", "get", "UUID")
case cimInstance:
encodedValue, uuid, err = commandOutputHash(appconfig.PowerShellPluginCommandName, getCimInstanceCmdArgs("Win32_ComputerSystemProduct", "UUID"))
encodedValue, uuid, err = commandOutputHash(appconfig.PowerShellPluginCommandName, getCimInstanceCmdArgs(logger, "Win32_ComputerSystemProduct", "UUID"))
default:
logger.Warnf("Unknown WMI interface: %v", wmiInterface)
}
Expand All @@ -182,77 +183,77 @@ func csproductUuid(logger log.T, wmiInterface WMIInterface) (encodedValue string
return
}

func processorInfoHash(wmiInterface WMIInterface) (value string, err error) {
func processorInfoHash(logger log.T, wmiInterface WMIInterface) (value string, err error) {
switch wmiInterface {
case wmic:
value, _, err = commandOutputHash(wmicCommand, "cpu", "list", "brief")
case cimInstance:
propertiesToRetrieve := []string{"Caption", "DeviceID", "Manufacturer", "MaxClockSpeed", "Name", "SocketDesignation"}
value, _, err = commandOutputHash(appconfig.PowerShellPluginCommandName, getCimInstanceCmdArgs("WIN32_PROCESSOR", propertiesToRetrieve...))
value, _, err = commandOutputHash(appconfig.PowerShellPluginCommandName, getCimInstanceCmdArgs(logger, "WIN32_PROCESSOR", propertiesToRetrieve...))
default:
logger.Warnf("Unknown WMI interface: %v", wmiInterface)
}

return
}

func memoryInfoHash(wmiInterface WMIInterface) (value string, err error) {
func memoryInfoHash(logger log.T, wmiInterface WMIInterface) (value string, err error) {
switch wmiInterface {
case wmic:
value, _, err = commandOutputHash(wmicCommand, "memorychip", "list", "brief")
case cimInstance:
propertiesToRetrieve := []string{"Capacity", "DeviceLocator", "MemoryType", "Name", "Tag", "TotalWidth"}
value, _, err = commandOutputHash(appconfig.PowerShellPluginCommandName, getCimInstanceCmdArgs("Win32_PhysicalMemory", propertiesToRetrieve...))
value, _, err = commandOutputHash(appconfig.PowerShellPluginCommandName, getCimInstanceCmdArgs(logger, "Win32_PhysicalMemory", propertiesToRetrieve...))
default:
logger.Warnf("Unknown WMI interface: %v", wmiInterface)
}

return
}

func biosInfoHash(wmiInterface WMIInterface) (value string, err error) {
func biosInfoHash(logger log.T, wmiInterface WMIInterface) (value string, err error) {
switch wmiInterface {
case wmic:
value, _, err = commandOutputHash(wmicCommand, "bios", "list", "brief")
case cimInstance:
propertiesToRetrieve := []string{"Manufacturer", "Name", "SerialNumber", "SMBIOSBIOSVersion", "Version"}
value, _, err = commandOutputHash(appconfig.PowerShellPluginCommandName, getCimInstanceCmdArgs("Win32_BIOS", propertiesToRetrieve...))
value, _, err = commandOutputHash(appconfig.PowerShellPluginCommandName, getCimInstanceCmdArgs(logger, "Win32_BIOS", propertiesToRetrieve...))
default:
logger.Warnf("Unknown WMI interface: %v", wmiInterface)
}

return
}

func systemInfoHash(wmiInterface WMIInterface) (value string, err error) {
func systemInfoHash(logger log.T, wmiInterface WMIInterface) (value string, err error) {
switch wmiInterface {
case wmic:
value, _, err = commandOutputHash(wmicCommand, "computersystem", "list", "brief")
case cimInstance:
propertiesToRetrieve := []string{"Domain", "Manufacturer", "Model", "Name", "PrimaryOwnerName", "TotalPhysicalMemory"}
value, _, err = commandOutputHash(appconfig.PowerShellPluginCommandName, getCimInstanceCmdArgs("Win32_ComputerSystem", propertiesToRetrieve...))
value, _, err = commandOutputHash(appconfig.PowerShellPluginCommandName, getCimInstanceCmdArgs(logger, "Win32_ComputerSystem", propertiesToRetrieve...))
default:
logger.Warnf("Unknown WMI interface: %v", wmiInterface)
}

return
}

func diskInfoHash(wmiInterface WMIInterface) (value string, err error) {
func diskInfoHash(logger log.T, wmiInterface WMIInterface) (value string, err error) {
switch wmiInterface {
case wmic:
value, _, err = commandOutputHash(wmicCommand, "diskdrive", "list", "brief")
case cimInstance:
propertiesToRetrieve := []string{"Caption", "DeviceID", "Model", "Partitions", "Size"}
value, _, err = commandOutputHash(appconfig.PowerShellPluginCommandName, getCimInstanceCmdArgs("Win32_DiskDrive", propertiesToRetrieve...))
value, _, err = commandOutputHash(appconfig.PowerShellPluginCommandName, getCimInstanceCmdArgs(logger, "Win32_DiskDrive", propertiesToRetrieve...))
default:
logger.Warnf("Unknown WMI interface: %v", wmiInterface)
}

return
}

func getCimInstanceCmdArgs(className string, properties ...string) string {
func getCimInstanceCmdArgs(logger log.T, className string, properties ...string) string {
var sb strings.Builder
sb.WriteString(fmt.Sprintf("Get-CimInstance -ClassName %s", className))
if len(properties) > 0 {
Expand All @@ -262,5 +263,7 @@ func getCimInstanceCmdArgs(className string, properties ...string) string {
}
}

return fmt.Sprintf("%s | ConvertTo-Json", strings.TrimRight(sb.String(), ", "))
cmd := fmt.Sprintf("%s | ConvertTo-Json", strings.TrimRight(sb.String(), ", "))
logger.Debugf("getCimInstanceCmdArgs cmd=%s", cmd)
return cmd
}
3 changes: 2 additions & 1 deletion agent/setupcli/managers/registermanager/registerManager.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package registermanager

import (
"fmt"
"time"

"github.com/aws/amazon-ssm-agent/agent/setupcli/managers/common"
"github.com/aws/amazon-ssm-agent/agent/setupcli/utility"
Expand Down Expand Up @@ -89,7 +90,7 @@ func getAgentBinaryPath() string {
}

func (m *registerManager) generateMIRegisterCommand(registerAgentInpModel *RegisterAgentInputModel) (string, error) {
return m.managerHelper.RunCommand(m.agentBinPath, "-register", "-y",
return m.managerHelper.RunCommandWithCustomTimeout(60*time.Second, m.agentBinPath, "-register", "-y", //run command with custom timeout of 60s
"-region", registerAgentInpModel.Region,
"-code", registerAgentInpModel.ActivationCode,
"-id", registerAgentInpModel.ActivationId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package registermanager
import (
"fmt"
"testing"
"time"

mhMock "github.com/aws/amazon-ssm-agent/agent/setupcli/managers/common/mocks"
"github.com/stretchr/testify/assert"
Expand All @@ -42,7 +43,7 @@ func TestRegisterAgent_Success_Onprem(t *testing.T) {
helperMock := &mhMock.IManagerHelper{}

rm := registerManager{helperMock, "SomeBinPath"}
helperMock.On("RunCommand", "SomeBinPath", "-register", mock.Anything, mock.Anything, "Region", "-code", "test1", "-id", "test2").Return("", nil).Once()
helperMock.On("RunCommandWithCustomTimeout", 60*time.Second, "SomeBinPath", "-register", mock.Anything, mock.Anything, "Region", "-code", "test1", "-id", "test2").Return("", nil).Once()
input := &RegisterAgentInputModel{
Region: "Region",
ActivationCode: "test1",
Expand All @@ -56,7 +57,7 @@ func TestRegisterAgent_Failure_Onprem(t *testing.T) {
helperMock := &mhMock.IManagerHelper{}

rm := registerManager{helperMock, ""}
helperMock.On("RunCommand", "", "-register", mock.Anything, mock.Anything, "Region", "-code", "test1", "-id", "test2").Return("", nil).Once()
helperMock.On("RunCommandWithCustomTimeout", 60*time.Second, "", "-register", mock.Anything, mock.Anything, "Region", "-code", "test1", "-id", "test2").Return("", nil).Once()
input := &RegisterAgentInputModel{
Region: "Region",
ActivationCode: "test1",
Expand Down Expand Up @@ -108,7 +109,7 @@ func TestRegisterAgent_ValidActionIdActionCode_Success(t *testing.T) {
helperMock := &mhMock.IManagerHelper{}

rm := registerManager{helperMock, "SomeBinPath"}
helperMock.On("RunCommand", mock.Anything, mock.Anything, mock.Anything,
helperMock.On("RunCommandWithCustomTimeout", 60*time.Second, mock.Anything, mock.Anything, mock.Anything,
"-region", "SomeRegion",
"-code", "SomeActivationCode",
"-id", "SomeActivationId").Return("", nil).Once()
Expand Down

0 comments on commit ab4144f

Please sign in to comment.