Skip to content

Commit

Permalink
feat: enable tenant check on AMT operations
Browse files Browse the repository at this point in the history
  • Loading branch information
rsdmike committed Sep 21, 2023
1 parent ce0d8dc commit 72fb399
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 4 deletions.
66 changes: 66 additions & 0 deletions src/middleware/cira.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*********************************************************************
* Copyright (c) Intel Corporation 2022
* SPDX-License-Identifier: Apache-2.0
**********************************************************************/

import { createSpyObj } from '../test/helper/jest'
import { ErrorResponse } from '../utils/amtHelper'
import ciraMiddleware from './cira'
import { devices } from '../server/mpsserver'

describe('CIRA Middleware', () => {
let req, next, resSpy

beforeEach(() => {
req = {
params: { guid: 'some-guid' },
tenantId: null,
deviceAction: null
}
next = jest.fn()
resSpy = createSpyObj('Response', ['status', 'json', 'end', 'send'])
resSpy.status.mockReturnThis()
resSpy.json.mockReturnThis()
resSpy.send.mockReturnThis()
})

it('Should handle unauthorized tenant', async () => {
// mock device
const device: any = { ciraSocket: { readyState: 'open' }, tenantId: 'another-tenant' }
req.tenantId = 'some-tenant'
devices['some-guid'] = device

await ciraMiddleware(req, resSpy, next)

expect(resSpy.status).toHaveBeenCalledWith(401)
expect(resSpy.json).toHaveBeenCalledWith(ErrorResponse(401, 'Unauthorized'))
expect(resSpy.end).toHaveBeenCalled()
expect(next).not.toHaveBeenCalled()
})

it('Should handle missing device', async () => {
// no device for the provided guid
delete devices['some-guid']

await ciraMiddleware(req, resSpy, next)

expect(resSpy.status).toHaveBeenCalledWith(404)
expect(resSpy.json).toHaveBeenCalledWith(ErrorResponse(404, 'guid : some-guid', 'device'))
expect(resSpy.end).toHaveBeenCalled()
expect(next).not.toHaveBeenCalled()
})

// Similarly, add other test cases based on the conditions mentioned above...

// For example:
it('Should pass the middleware if multitenancy isnt used and ciraSocket is open', async () => {
// mock device
const device: any = { ciraSocket: { readyState: 'open' }, tenantId: 'some-tenant' }
devices['some-guid'] = device

await ciraMiddleware(req, resSpy, next)

expect(req.deviceAction).toBeDefined()
expect(next).toHaveBeenCalled()
})
})
10 changes: 7 additions & 3 deletions src/middleware/cira.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@ const ciraMiddleware = async (req: Request, res: Response, next): Promise<void>
const device = devices[guid]

if ((device as any)?.ciraSocket.readyState === 'open') {
// const cred = await req.mpsService.secrets.getAMTCredentials(guid);
// req.amtStack = req.amtFactory.getAmtStack(guid, amtPort, cred[0], cred[1], 0)
// (req as any).httpHandler = new HttpHandler(cred[0], cred[1])
// if a tenantId is provided, ensure the request is for the same tenant/device
if (req.tenantId != null) {
if (req.tenantId !== device.tenantId) {
res.status(401).json(ErrorResponse(401, 'Unauthorized')).end()
return
}
}

const ciraHandler = new CIRAHandler(device.httpHandler, device.username, device.password, device.limiter)
req.deviceAction = new DeviceAction(ciraHandler, device.ciraSocket)
Expand Down
2 changes: 1 addition & 1 deletion src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const UUIDRegex = /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA
export const HTTPErrorTable = {
200: 'httpErrorTableOK',
400: 'Incorrect URI or Bad Request',
401: 'Authentication Error',
401: 'Unauthorized',
404: {
alarm: 'Alarm instance not found',
device: 'Device not found/connected. Please connect again using CIRA.',
Expand Down

0 comments on commit 72fb399

Please sign in to comment.