From 0c4036898297281f1c8ff6b0e2b070e638307618 Mon Sep 17 00:00:00 2001 From: Andrew Lee <1517745+andrewrlee@users.noreply.github.com> Date: Tue, 28 May 2024 16:18:35 +0100 Subject: [PATCH] HEAT-230 sanitise hostname (#22) --- src/data/appInsights/index.test.ts | 13 +++++++++++++ src/data/appInsights/index.ts | 4 +++- 2 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 src/data/appInsights/index.test.ts diff --git a/src/data/appInsights/index.test.ts b/src/data/appInsights/index.test.ts new file mode 100644 index 0000000..b2e232a --- /dev/null +++ b/src/data/appInsights/index.test.ts @@ -0,0 +1,13 @@ +import { sanitize } from '.' + +describe('sanitise', () => { + test('check removes :443', () => { + expect(sanitize('approved-premises-api.hmpps.service.justice.gov.uk:443')).toStrictEqual('approved-premises-api.hmpps.service.justice.gov.uk') + }) + + test('check removes random suffix', () => { + expect(sanitize('education-employment-api.hmpps.service.justice.gov.uk | aaaaaaa-eeee-dddd-ccccc-bbbbbbbb')).toStrictEqual( + 'education-employment-api.hmpps.service.justice.gov.uk', + ) + }) +}) diff --git a/src/data/appInsights/index.ts b/src/data/appInsights/index.ts index 0aec949..b5a9f95 100644 --- a/src/data/appInsights/index.ts +++ b/src/data/appInsights/index.ts @@ -3,11 +3,13 @@ import { type Dependency } from '../Components' import AppInsights from './Client' import Queries from './queries' +export const sanitize = (s: string) => (s ? s.replace(':443', '').replace(/(.*?\.gov\.uk).*/, '$1') : '') + const getDependencies = async (appInsightsCreds: AppInsightsCreds): Promise => { const appInsights = new AppInsights(appInsightsCreds) const results = await appInsights.query(Queries.DEPENDENCIES()) - return results.rows.map(row => ({ componentName: row[0], dependencyHostname: row[1], type: row[2] })) + return results.rows.map(row => ({ componentName: row[0], dependencyHostname: sanitize(row[1]), type: row[2] })) } export default getDependencies