Skip to content

Commit

Permalink
feat: scrub mac address from objects
Browse files Browse the repository at this point in the history
  • Loading branch information
mrnagydavid committed Jul 4, 2024
1 parent c94b67d commit 60ccea8
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 1 deletion.
29 changes: 29 additions & 0 deletions src/scrubber.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -430,3 +430,32 @@ test('getScrubberSql', () => {
expect(scrubber.getScrubberSql('pw')).toMatchInlineSnapshot(`"'notsecret'"`)
expect(scrubber.getScrubberSql('name')).toMatchInlineSnapshot(`"'Jane Doe'"`)
})

test('macAndIdScrubber should scrub a list of objects', () => {
const data = {
HardwareDevices: [
{ id: '123|mac', mac: 'mac', foo: 'bar' },
{ id: '123|cheese', mac: 'cheese', foo: 'bar' },
{ id: 'tom', mac: 'tom', foo: 'bar' },
{ id: 'mac|123|mac', mac: 'mac', foo: 'bar' },
],
}

const result = scrub(data, {
fields: {
HardwareDevices: {
scrubber: 'macAndIdScrubber',
params: { otherFieldsToScrub: ['id'] },
},
},
})

expect(result).toEqual({
HardwareDevices: [
{ id: '123|1', mac: '1', foo: 'bar' },
{ id: '123|2', mac: '2', foo: 'bar' },
{ id: '3', mac: '3', foo: 'bar' },
{ id: '4|123|4', mac: '4', foo: 'bar' },
],
})
})
68 changes: 68 additions & 0 deletions src/scrubbers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
charsFromRightScrubberSQL,
isoDateStringScrubber,
isoDateStringScrubberSQL,
macAndIdScrubber,
preserveOriginalScrubber,
preserveOriginalScrubberSQL,
randomEmailInContentScrubber,
Expand Down Expand Up @@ -434,3 +435,70 @@ test('bcryptStringScrubberSQL', () => {
bcryptStringScrubberSQL({ replacements: '$2a$10$:$2a$10$456,$2a$12$:$2a$12$123' }),
).toMatchSnapshot()
})

describe('macAndIdScrubber', () => {
test('should scrub the entire `mac` field with a counter', () => {
const data = [{ mac: '00:00:00:00:00:00' }, { mac: '00:00:00:00:00:01' }, { mac: 'foo' }]

const result = macAndIdScrubber(data)

expect(result).toEqual([{ mac: '1' }, { mac: '2' }, { mac: '3' }])
})

test('should be possible to change the name of property with the MAC address', () => {
const data = [
{ foo: '00:00:00:00:00:00' },
{ foo: '00:00:00:00:00:01' },
{ mac: '00:00:00:00:00:01' },
]

const result = macAndIdScrubber(data, { fieldNameOfMacAddress: 'foo' })

expect(result).toEqual([{ foo: '1' }, { foo: '2' }, { mac: '00:00:00:00:00:01' }])
})

test('should scrub the mac address from other fields with the same value', () => {
const data = [
{ mac: '00:00:00:00:00:00', id: 'foo|00:00:00:00:00:00|bar' },
{ mac: '00:00:00:00:00:01' },
]

const result = macAndIdScrubber(data, { otherFieldsToScrub: ['id'] })

expect(result).toEqual([{ mac: '1', id: 'foo|1|bar' }, { mac: '2' }])
})

test('should not scrub the mac address from other fields when not instructed', () => {
const data = [{ mac: '00:00:00:00:00:00', id: 'foo|00:00:00:00:00:00|bar' }]

const result = macAndIdScrubber(data)

expect(result).toEqual([{ mac: '1', id: 'foo|00:00:00:00:00:00|bar' }])
})

test('should not scrub when the field is missing', () => {
const data = [{ foo: '00:00:00:00:00:00' }, { foo: '00:00:00:00:00:01' }, { foo: 'foo' }]

expect(macAndIdScrubber(data)).toEqual([
{ foo: '00:00:00:00:00:00' },
{ foo: '00:00:00:00:00:01' },
{ foo: 'foo' },
])

expect(macAndIdScrubber(data, { fieldNameOfMacAddress: 'mac' })).toEqual([
{ foo: '00:00:00:00:00:00' },
{ foo: '00:00:00:00:00:01' },
{ foo: 'foo' },
])

expect(
macAndIdScrubber(data, { fieldNameOfMacAddress: 'mac', otherFieldsToScrub: ['id'] }),
).toEqual([{ foo: '00:00:00:00:00:00' }, { foo: '00:00:00:00:00:01' }, { foo: 'foo' }])
})

test('should not scrub when the data is not a list', () => {
const data = { mac: '00:00:00:00:00:00' }

expect(macAndIdScrubber(data as any)).toEqual({ mac: '00:00:00:00:00:00' })
})
})
48 changes: 47 additions & 1 deletion src/scrubbers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as crypto from 'node:crypto'
import { _assert } from '@naturalcycles/js-lib'
import { _assert, AnyObject } from '@naturalcycles/js-lib'
import { nanoIdCustomAlphabet } from '@naturalcycles/nodejs-lib'
import { ScrubberFn, ScrubbersMap, ScrubberSQLFn, ScrubbersSQLMap } from './scrubber.model'

Expand Down Expand Up @@ -462,6 +462,51 @@ export const bcryptStringScrubberSQL: BcryptStringScrubberSQLFn = params => {
END`
}

export type MacAndIdScrubberFn = ScrubberFn<AnyObject[] | undefined, MacAndIdScrubberParams>

export interface MacAndIdScrubberParams {
/**
* Defaults to `mac`
*/
fieldNameOfMacAddress?: string

/**
* Defaults to `[]`
*/
otherFieldsToScrub?: string[]
}

export const macAndIdScrubber: MacAndIdScrubberFn = (hardwareDevices, opts) => {
if (!hardwareDevices) return
if (!Array.isArray(hardwareDevices)) return hardwareDevices

const fieldNameOfMacAddress = opts?.fieldNameOfMacAddress || 'mac'
const otherFieldsToScrub = opts?.otherFieldsToScrub || []

let counter = 0

return hardwareDevices.map(hardwareDevice => {
const mac = hardwareDevice[fieldNameOfMacAddress]
if (typeof mac !== 'string') return hardwareDevice

counter += 1
const replacement = String(counter)

const newHardwareDevice: AnyObject = {
...hardwareDevice,
[fieldNameOfMacAddress]: replacement,
}

otherFieldsToScrub.forEach(key => {
const value = newHardwareDevice[key]
if (typeof value !== 'string') return
newHardwareDevice[key] = value.replaceAll(mac, replacement)
})

return newHardwareDevice
})
}

function nthChar(str: string, character: string, n: number): number | undefined {
let count = 0
let i = 0
Expand Down Expand Up @@ -489,6 +534,7 @@ export const defaultScrubbers: ScrubbersMap = {
saltedHashScrubber,
saltedHashEmailScrubber,
bcryptStringScrubber,
macAndIdScrubber,
}

export const defaultScrubbersSQL: ScrubbersSQLMap = {
Expand Down

0 comments on commit 60ccea8

Please sign in to comment.