diff --git a/agent/managedInstances/fingerprint/fingerprint.go b/agent/managedInstances/fingerprint/fingerprint.go index 7c7c59952..016acbaa6 100644 --- a/agent/managedInstances/fingerprint/fingerprint.go +++ b/agent/managedInstances/fingerprint/fingerprint.go @@ -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 diff --git a/agent/managedInstances/fingerprint/hardwareInfo_windows.go b/agent/managedInstances/fingerprint/hardwareInfo_windows.go index b7bd740d2..d8e47582c 100644 --- a/agent/managedInstances/fingerprint/hardwareInfo_windows.go +++ b/agent/managedInstances/fingerprint/hardwareInfo_windows.go @@ -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 } @@ -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 } } @@ -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) } @@ -182,13 +183,13 @@ 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) } @@ -196,13 +197,13 @@ func processorInfoHash(wmiInterface WMIInterface) (value string, err error) { 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) } @@ -210,13 +211,13 @@ func memoryInfoHash(wmiInterface WMIInterface) (value string, err error) { 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) } @@ -224,13 +225,13 @@ func biosInfoHash(wmiInterface WMIInterface) (value string, err error) { 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) } @@ -238,13 +239,13 @@ func systemInfoHash(wmiInterface WMIInterface) (value string, err error) { 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) } @@ -252,7 +253,7 @@ func diskInfoHash(wmiInterface WMIInterface) (value string, err error) { 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 { @@ -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 } diff --git a/agent/setupcli/managers/registermanager/registerManager.go b/agent/setupcli/managers/registermanager/registerManager.go index 78522cf8a..c66848b0e 100644 --- a/agent/setupcli/managers/registermanager/registerManager.go +++ b/agent/setupcli/managers/registermanager/registerManager.go @@ -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" @@ -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) diff --git a/agent/setupcli/managers/registermanager/registerManager_test.go b/agent/setupcli/managers/registermanager/registerManager_test.go index 8a85511de..ae8d2c21f 100644 --- a/agent/setupcli/managers/registermanager/registerManager_test.go +++ b/agent/setupcli/managers/registermanager/registerManager_test.go @@ -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" @@ -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", @@ -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", @@ -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()