Skip to content

Commit

Permalink
refactor(api): set amt features uses new CIRA connection
Browse files Browse the repository at this point in the history
closes AB#4554
  • Loading branch information
rsdmike committed Jan 4, 2022
1 parent 3476a30 commit 7087eb3
Show file tree
Hide file tree
Showing 13 changed files with 283 additions and 244 deletions.
16 changes: 8 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
"@types/body-parser": "^1.19.2",
"@types/express": "^4.17.13",
"@types/jest": "^27.4.0",
"@types/node": "^14.17.4",
"@types/node": "^16.11.15",
"@types/node-forge": "^0.10.10",
"@types/pg": "^8.6.3",
"@types/ws": "^8.2.2",
Expand Down
1 change: 1 addition & 0 deletions src/amt/CIRAHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ export class CIRAHandler {
}

writeData (channel: CIRAChannel, rawXML: string): boolean {
logger.silly(rawXML)
this.rawChunkedData = ''
const params: connectionParams = {
guid: channel.socket.tag.nodeid,
Expand Down
24 changes: 24 additions & 0 deletions src/amt/ConnectedDevice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,42 @@ export class ConnectedDevice {
return result.Envelope.Body
}

async putIpsOptInService (data: IPS.Models.OptInServiceResponse): Promise<IPS.Models.OptInServiceResponse> {
const xmlRequestBody = this.ips.OptInService(IPS.Methods.PUT, (this.messageId++).toString(), null, data)
const result = await this.ciraHandler.Get<IPS.Models.OptInServiceResponse>(this.ciraSocket, xmlRequestBody)
return result.Envelope.Body
}

async getRedirectionService (): Promise<AMT.Models.RedirectionResponse> {
const xmlRequestBody = this.amt.RedirectionService(AMT.Methods.GET, (this.messageId++).toString())
const result = await this.ciraHandler.Get<AMT.Models.RedirectionResponse>(this.ciraSocket, xmlRequestBody)
return result.Envelope.Body
}

async setRedirectionService (requestState: number): Promise<any> {
const xmlRequestBody = this.amt.RedirectionService(AMT.Methods.REQUEST_STATE_CHANGE, (this.messageId++).toString(), requestState)
const result = await this.ciraHandler.Send(this.ciraSocket, xmlRequestBody)
return result.Envelope.Body
}

async putRedirectionService (data: AMT.Models.RedirectionResponse): Promise<any> {
const xmlRequestBody = this.amt.RedirectionService(AMT.Methods.PUT, (this.messageId++).toString(), null, data)
const result = await this.ciraHandler.Send(this.ciraSocket, xmlRequestBody)
return result.Envelope.Body
}

async getKvmRedirectionSap (): Promise<CIM.Models.KVMRedirectionSAPResponse> {
const xmlRequestBody = this.cim.KVMRedirectionSAP(CIM.Methods.GET, (this.messageId++).toString())
const result = await this.ciraHandler.Get<CIM.Models.KVMRedirectionSAPResponse>(this.ciraSocket, xmlRequestBody)
return result.Envelope.Body
}

async setKvmRedirectionSap (requestedState: number): Promise<any> {
const xmlRequestBody = this.cim.KVMRedirectionSAP(CIM.Methods.REQUEST_STATE_CHANGE, (this.messageId++).toString(), requestedState)
const result = await this.ciraHandler.Send(this.ciraSocket, xmlRequestBody)
return result.Envelope.Body
}

async forceBootMode (bootSource: string = 'Intel(r) AMT: Boot Configuration 0', role: number = 1): Promise<number> {
const xmlRequestBody = this.cim.BootService(CIM.Methods.SET_BOOT_CONFIG_ROLE, (this.messageId++).toString(), bootSource, role)
const result = await this.ciraHandler.Send(this.ciraSocket, xmlRequestBody)
Expand Down
2 changes: 1 addition & 1 deletion src/amt/HttpHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class HttpHandler {
parser: any
constructor () {
this.stripPrefix = xml2js.processors.stripPrefix
this.parser = new xml2js.Parser({ ignoreAttrs: true, mergeAttrs: false, explicitArray: false, tagNameProcessors: [this.stripPrefix] })
this.parser = new xml2js.Parser({ ignoreAttrs: true, mergeAttrs: false, explicitArray: false, tagNameProcessors: [this.stripPrefix], valueProcessors: [xml2js.processors.parseNumbers, xml2js.processors.parseBooleans] })
}

wrapIt (connectionParams: connectionParams, data: string): string {
Expand Down
2 changes: 1 addition & 1 deletion src/middleware/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { validationResult } from 'express-validator'
const validateMiddleware = async (req: Request, res: Response, next): Promise<void> => {
const errors = validationResult(req)
if (!errors.isEmpty()) {
res.status(400).json({ errors: errors.array() }).end()
res.status(400).json({ errors: errors.array() })
} else {
next()
}
Expand Down
4 changes: 4 additions & 0 deletions src/routes/amt/getAMTFeatures.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/*********************************************************************
* Copyright (c) Intel Corporation 2021
* SPDX-License-Identifier: Apache-2.0
**********************************************************************/
import { ConnectedDevice } from '../../amt/ConnectedDevice'
import { devices } from '../../server/mpsserver'
import { createSpyObj } from '../../test/helper/jest'
Expand Down
6 changes: 3 additions & 3 deletions src/routes/amt/getAMTFeatures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ export async function getAMTFeatures (req: Request, res: Response): Promise<void
}

export function processAmtRedirectionResponse (amtRedirection: AMT.Models.RedirectionService): {redir: boolean, sol: boolean, ider: boolean} {
const redir = (amtRedirection.ListenerEnabled as any) === 'true' // not sure the best way to handle this at the moment...ok. Ready for a debug?yes, ok.. need to fix all the problems in tests
const sol = amtRedirection.EnabledState === AMT_REDIRECTION_SERVICE_ENABLE_STATE.Enabled
const ider = amtRedirection.EnabledState === AMT_REDIRECTION_SERVICE_ENABLE_STATE.Other
const redir = amtRedirection.ListenerEnabled
const sol = (amtRedirection.EnabledState & AMT_REDIRECTION_SERVICE_ENABLE_STATE.Enabled) !== 0
const ider = (amtRedirection.EnabledState & AMT_REDIRECTION_SERVICE_ENABLE_STATE.Other) !== 0
return { redir, sol, ider }
}

Expand Down
157 changes: 157 additions & 0 deletions src/routes/amt/setAMTFeatures.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
/*********************************************************************
* Copyright (c) Intel Corporation 2021
* SPDX-License-Identifier: Apache-2.0
**********************************************************************/
import { devices } from '../../server/mpsserver'
import { MqttProvider } from '../../utils/MqttProvider'
import { createSpyObj } from '../../test/helper/jest'
import { setAMTFeatures } from './setAMTFeatures'
import { ConnectedDevice } from '../../amt/ConnectedDevice'
import { AMT_REDIRECTION_SERVICE_ENABLE_STATE } from '@open-amt-cloud-toolkit/wsman-messages/dist/models/common'

describe('set amt features', () => {
let resSpy
let req
let redirectionSpy: jest.SpyInstance
let optInServiceSpy: jest.SpyInstance
let kvmRedirectionSpy: jest.SpyInstance
let setRedirectionServiceSpy: jest.SpyInstance
let setKvmRedirectionSapSpy: jest.SpyInstance
let putRedirectionServiceSpy: jest.SpyInstance
let putIpsOptInServiceSpy: jest.SpyInstance
let mqttSpy: jest.SpyInstance

beforeEach(() => {
resSpy = createSpyObj('Response', ['status', 'json', 'end', 'send'])
req = {
params: {
guid: '4c4c4544-004b-4210-8033-b6c04f504633'
},
body: {
userConsent: 'all',
enableSOL: true,
enableIDER: false,
enableKVM: true
}
}
resSpy.status.mockReturnThis()
resSpy.json.mockReturnThis()
resSpy.send.mockReturnThis()

devices['4c4c4544-004b-4210-8033-b6c04f504633'] = new ConnectedDevice(null, 'admin', 'P@ssw0rd')
redirectionSpy = jest.spyOn(devices['4c4c4544-004b-4210-8033-b6c04f504633'], 'getRedirectionService')
optInServiceSpy = jest.spyOn(devices['4c4c4544-004b-4210-8033-b6c04f504633'], 'getIpsOptInService')
kvmRedirectionSpy = jest.spyOn(devices['4c4c4544-004b-4210-8033-b6c04f504633'], 'getKvmRedirectionSap')
setRedirectionServiceSpy = jest.spyOn(devices['4c4c4544-004b-4210-8033-b6c04f504633'], 'setRedirectionService')
setKvmRedirectionSapSpy = jest.spyOn(devices['4c4c4544-004b-4210-8033-b6c04f504633'], 'setKvmRedirectionSap')
putRedirectionServiceSpy = jest.spyOn(devices['4c4c4544-004b-4210-8033-b6c04f504633'], 'putRedirectionService')
putIpsOptInServiceSpy = jest.spyOn(devices['4c4c4544-004b-4210-8033-b6c04f504633'], 'putIpsOptInService')

mqttSpy = jest.spyOn(MqttProvider, 'publishEvent')

redirectionSpy.mockResolvedValue({ AMT_RedirectionService: { EnabledState: AMT_REDIRECTION_SERVICE_ENABLE_STATE.Enabled, ListenerEnabled: 'true' } })
optInServiceSpy.mockResolvedValue({ IPS_OptInService: { OptInRequired: '4294967295' } })
kvmRedirectionSpy.mockResolvedValue({
CIM_KVMRedirectionSAP: {
RequestedState: 2,
EnabledState: 2
}
})
setRedirectionServiceSpy.mockResolvedValue({})
setKvmRedirectionSapSpy.mockResolvedValue({})
putIpsOptInServiceSpy.mockResolvedValue({})
putRedirectionServiceSpy.mockResolvedValue({})
})
it('should set amt features - no change', async () => {
await setAMTFeatures(req, resSpy)
expect(resSpy.status).toHaveBeenCalledWith(200)
expect(resSpy.json).toHaveBeenCalled()
expect(mqttSpy).toHaveBeenCalled()
expect(putIpsOptInServiceSpy).not.toHaveBeenCalled()
expect(setRedirectionServiceSpy).not.toHaveBeenCalled()
expect(setKvmRedirectionSapSpy).not.toHaveBeenCalled()
expect(putRedirectionServiceSpy).not.toHaveBeenCalled()
})
it('should set amt features - change user consent from all to kvm', async () => {
req.body.userConsent = 'kvm'
await setAMTFeatures(req, resSpy)
expect(resSpy.status).toHaveBeenCalledWith(200)
expect(resSpy.json).toHaveBeenCalled()
expect(mqttSpy).toHaveBeenCalled()
expect(putRedirectionServiceSpy).not.toHaveBeenCalled()
expect(setRedirectionServiceSpy).not.toHaveBeenCalled()
expect(setKvmRedirectionSapSpy).not.toHaveBeenCalled()
expect(putIpsOptInServiceSpy).toHaveBeenCalled()
})
it('should set amt features - disable KVM', async () => {
req.body.enableKVM = false

await setAMTFeatures(req, resSpy)
expect(resSpy.status).toHaveBeenCalledWith(200)
expect(resSpy.json).toHaveBeenCalled()
expect(mqttSpy).toHaveBeenCalled()
expect(putRedirectionServiceSpy).toHaveBeenCalled()
expect(setRedirectionServiceSpy).toHaveBeenCalledWith(32770)
expect(setKvmRedirectionSapSpy).toHaveBeenCalledWith(AMT_REDIRECTION_SERVICE_ENABLE_STATE.Disabled)
expect(putIpsOptInServiceSpy).not.toHaveBeenCalled()
})
it('should set amt features - disable SOL', async () => {
req.body.enableSOL = false

await setAMTFeatures(req, resSpy)
expect(resSpy.status).toHaveBeenCalledWith(200)
expect(resSpy.json).toHaveBeenCalled()
expect(mqttSpy).toHaveBeenCalled()
expect(putRedirectionServiceSpy).toHaveBeenCalled()
expect(setRedirectionServiceSpy).toHaveBeenCalledWith(32768)
expect(setKvmRedirectionSapSpy).toHaveBeenCalledWith(AMT_REDIRECTION_SERVICE_ENABLE_STATE.Enabled)
expect(putIpsOptInServiceSpy).not.toHaveBeenCalled()
})
it('should set amt features - enable IDER', async () => {
req.body.enableIDER = true

await setAMTFeatures(req, resSpy)
expect(resSpy.status).toHaveBeenCalledWith(200)
expect(resSpy.json).toHaveBeenCalled()
expect(mqttSpy).toHaveBeenCalled()
expect(putRedirectionServiceSpy).toHaveBeenCalled()
expect(setRedirectionServiceSpy).toHaveBeenCalledWith(32771)
expect(setKvmRedirectionSapSpy).toHaveBeenCalledWith(AMT_REDIRECTION_SERVICE_ENABLE_STATE.Enabled)
expect(putIpsOptInServiceSpy).not.toHaveBeenCalled()
})
it('should set amt features - disable IDER', async () => {
redirectionSpy.mockResolvedValue({ AMT_RedirectionService: { EnabledState: AMT_REDIRECTION_SERVICE_ENABLE_STATE.Other, ListenerEnabled: 'false' } })

await setAMTFeatures(req, resSpy)
expect(resSpy.status).toHaveBeenCalledWith(200)
expect(resSpy.json).toHaveBeenCalled()
expect(mqttSpy).toHaveBeenCalled()
expect(putRedirectionServiceSpy).toHaveBeenCalled()
expect(setRedirectionServiceSpy).toHaveBeenCalledWith(32770)
expect(setKvmRedirectionSapSpy).toHaveBeenCalledWith(AMT_REDIRECTION_SERVICE_ENABLE_STATE.Enabled)
expect(putIpsOptInServiceSpy).not.toHaveBeenCalled()
})
it('should set amt features - disable all', async () => {
redirectionSpy.mockResolvedValue({ AMT_RedirectionService: { EnabledState: AMT_REDIRECTION_SERVICE_ENABLE_STATE.Disabled, ListenerEnabled: 'false' } })

req.body.enableIDER = false
req.body.enableSOL = false
req.body.enableKVM = false

await setAMTFeatures(req, resSpy)
expect(resSpy.status).toHaveBeenCalledWith(200)
expect(resSpy.json).toHaveBeenCalled()
expect(mqttSpy).toHaveBeenCalled()
expect(putRedirectionServiceSpy).toHaveBeenCalled()
expect(setRedirectionServiceSpy).toHaveBeenCalledWith(32768)
expect(setKvmRedirectionSapSpy).toHaveBeenCalledWith(AMT_REDIRECTION_SERVICE_ENABLE_STATE.Disabled)
expect(putIpsOptInServiceSpy).not.toHaveBeenCalled()
})
it('should set amt features - and fail', async () => {
redirectionSpy.mockRejectedValue({})
await setAMTFeatures(req, resSpy)
expect(resSpy.status).toHaveBeenCalledWith(500)
expect(resSpy.json).toHaveBeenCalled()
expect(mqttSpy).toHaveBeenCalled()
})
})
Loading

0 comments on commit 7087eb3

Please sign in to comment.