Skip to content

Commit

Permalink
fix: add error handling when TLS activation is enforced (#678)
Browse files Browse the repository at this point in the history
  • Loading branch information
matt-primrose authored Oct 31, 2024
1 parent 00cc7ba commit f538952
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 12 deletions.
3 changes: 3 additions & 0 deletions internal/amt/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ func (r ChangeEnabledResponse) IsAMTEnabled() bool {
func (r ChangeEnabledResponse) IsNewInterfaceVersion() bool {
return ((r >> 7) & 1) == 1
}
func (r ChangeEnabledResponse) IsTlsEnforcedOnLocalPorts() bool {
return ((r >> 6) & 1) == 1
}

type Interface interface {
Initialize() error
Expand Down
10 changes: 9 additions & 1 deletion internal/local/activate.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,15 @@ func (service *ProvisioningService) Activate() error {
return utils.UnableToActivate
}

service.CheckAndEnableAMT(service.flags.SkipIPRenew)
tlsEnforced, err := service.CheckAndEnableAMT(service.flags.SkipIPRenew)
if err != nil {
return err
}

if tlsEnforced {
log.Error("TLS is enforced on local ports, unable to activate")
return utils.UnsupportedAMTVersion
}

// for local activation, wsman client needs local system account credentials
lsa, err := service.amtCommand.GetLocalSystemAccount()
Expand Down
2 changes: 2 additions & 0 deletions internal/local/lps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,8 @@ type MockAMT struct{}
const ChangeEnabledResponseNewEnabled = 0x82
const ChangeEnabledResponseNewDisabled = 0x80
const ChangeEnabledResponseNotNew = 0x00
const ChangeEnabledResponseNewTLSEnforcedEnabled = 0xC2
const ChangeEnabledResponseNewTLSEnforcedDisabled = 0xC0

var mockChangeEnabledResponse = amt2.ChangeEnabledResponse(ChangeEnabledResponseNewEnabled)
var errMockChangeEnabled error = nil
Expand Down
25 changes: 15 additions & 10 deletions internal/local/opstate.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,34 @@ func (service *ProvisioningService) EnableAMT() error {
return nil
}

func (service *ProvisioningService) CheckAndEnableAMT(skipIPRenewal bool) error {
rsp, err := service.amtCommand.GetChangeEnabled()
func (service *ProvisioningService) CheckAndEnableAMT(skipIPRenewal bool) (bool, error) {
resp, err := service.amtCommand.GetChangeEnabled()
tlsIsEnforced := false
if err != nil {
log.Error(err)
return utils.AMTConnectionFailed
return tlsIsEnforced, utils.AMTConnectionFailed
}
if !rsp.IsNewInterfaceVersion() {
if !resp.IsNewInterfaceVersion() {
log.Debug("this AMT version does not support SetAmtOperationalState")
return nil
return tlsIsEnforced, nil
}
if rsp.IsAMTEnabled() {
if resp.IsTlsEnforcedOnLocalPorts() {
tlsIsEnforced = true
log.Debug("TLS is enforced on local ports")
}
if resp.IsAMTEnabled() {
log.Debug("AMT is already enabled")
return nil
return tlsIsEnforced, nil
}
err = service.EnableAMT()
if err != nil {
return err
return tlsIsEnforced, err
}
if !skipIPRenewal {
err := service.RenewIP()
return err
return tlsIsEnforced, err
}
return nil
return tlsIsEnforced, nil
}

func (service *ProvisioningService) RenewIP() error {
Expand Down
10 changes: 9 additions & 1 deletion internal/local/opstate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func TestCheckAndEnableAMT(t *testing.T) {
name string
skipIPRenewal bool
expectedRC error
expectedTLS bool
rsp amt.ChangeEnabledResponse
errChangeEnabled error
errEnableAMT error
Expand All @@ -42,6 +43,12 @@ func TestCheckAndEnableAMT(t *testing.T) {
expectedRC: nil,
rsp: ChangeEnabledResponseNewEnabled,
},
{
name: "expect 1 if TLS is enforced",
expectedRC: nil,
expectedTLS: true,
rsp: ChangeEnabledResponseNewTLSEnforcedEnabled,
},
{
name: "expect AmtNotReady for enable if error occurs",
expectedRC: utils.AmtNotReady,
Expand Down Expand Up @@ -76,7 +83,8 @@ func TestCheckAndEnableAMT(t *testing.T) {
mockRenewDHCPLeaseerr = tc.renewDHCPLeaseRC
f := &flags.Flags{}
lps := setupService(f)
err := lps.CheckAndEnableAMT(tc.skipIPRenewal)
tlsForced, err := lps.CheckAndEnableAMT(tc.skipIPRenewal)
assert.Equal(t, tc.expectedTLS, tlsForced)
assert.Equal(t, tc.expectedRC, err)
mockChangeEnabledResponse = origRsp
errMockChangeEnabled = origChangeEnabledErr
Expand Down
1 change: 1 addition & 0 deletions pkg/utils/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ var ActivationFailedGetControlMode = CustomError{Code: 134, Message: "Activation
var ActivationFailedControlMode = CustomError{Code: 135, Message: "ActivationFailed", Details: "recieved invalid control mode"}
var DuplicateKey = CustomError{Code: 136, Message: "DuplicateKey", Details: "Key pair already exists"}
var WiredConfigurationFailed = CustomError{Code: 137, Message: "WiredConfigurationFailed"}
var UnsupportedAMTVersion = CustomError{Code: 138, Message: "UnsupportedAMTVersion"}

// (150-199) Maintenance Errors
var SyncClockFailed = CustomError{Code: 150, Message: "SyncClockFailed"}
Expand Down

0 comments on commit f538952

Please sign in to comment.