Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjust metrics for HTTP Request node #3359

Merged
merged 1 commit into from
May 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/workflow/src/Interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1444,6 +1444,7 @@ export interface INodeGraphItem {
mode?: string;
credential_type?: string; // HTTP Request node v2
credential_set?: boolean; // HTTP Request node v2
method?: string; // HTTP Request node v2
}

export interface INodeNameIndex {
Expand Down
36 changes: 11 additions & 25 deletions packages/workflow/src/TelemetryHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ function areOverlapping(
);
}

const URL_PARTS_REGEX = /(?<protocolPlusDomain>.*?\..*?)(?<pathnamePlusQs>\/.*)/;
const URL_PARTS_REGEX = /(?<protocolPlusDomain>.*?\..*?)(?<pathname>\/.*)/;

export function getDomainBase(raw: string, urlParts = URL_PARTS_REGEX): string {
try {
Expand All @@ -77,7 +77,9 @@ export function getDomainBase(raw: string, urlParts = URL_PARTS_REGEX): string {
}

function isSensitive(segment: string) {
return /%40/.test(segment) || /^\d+$/.test(segment) || /^[0-9A-F]{8}/i.test(segment);
if (/^v\d+$/.test(segment)) return false;

return /%40/.test(segment) || /\d/.test(segment) || /^[0-9A-F]{8}/i.test(segment);
}

export const ANONYMIZATION_CHARACTER = '*';
Expand All @@ -89,26 +91,6 @@ function sanitizeRoute(raw: string, check = isSensitive, char = ANONYMIZATION_CH
.join('/');
}

function sanitizeQuery(raw: string, check = isSensitive, char = ANONYMIZATION_CHARACTER) {
return raw
.split('&')
.map((segment) => {
const [key, value] = segment.split('=');
return [key, check(value) ? char.repeat(value.length) : value].join('=');
})
.join('&');
}

function sanitizeUrl(raw: string) {
if (/\?/.test(raw)) {
const [route, query] = raw.split('?');

return [sanitizeRoute(route), sanitizeQuery(query)].join('?');
}

return sanitizeRoute(raw);
}

/**
* Return pathname plus query string from URL, anonymizing IDs in route and query params.
*/
Expand All @@ -118,13 +100,16 @@ export function getDomainPath(raw: string, urlParts = URL_PARTS_REGEX): string {

if (!url.hostname) throw new Error('Malformed URL');

return sanitizeUrl(url.pathname + url.search);
return sanitizeRoute(url.pathname);
} catch (_) {
const match = urlParts.exec(raw);

if (!match?.groups?.pathnamePlusQs) return '';
if (!match?.groups?.pathname) return '';

// discard query string
const route = match.groups.pathname.split('?').shift() as string;

return sanitizeUrl(match.groups.pathnamePlusQs);
return sanitizeRoute(route);
}
}

Expand Down Expand Up @@ -191,6 +176,7 @@ export function generateNodesGraph(

nodeItem.domain_base = getDomainBase(url);
nodeItem.domain_path = getDomainPath(url);
nodeItem.method = node.parameters.requestMethod as string;
} else {
const nodeType = nodeTypes.getByNameAndVersion(node.type);

Expand Down
71 changes: 43 additions & 28 deletions packages/workflow/test/TelemetryHelpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,57 +21,57 @@ describe('getDomainBase should return protocol plus domain', () => {
});
});

describe('getDomainPath should return pathname plus query string', () => {
describe('anonymizing numeric IDs', () => {
describe('getDomainPath should return pathname, excluding query string', () => {
describe('anonymizing strings containing at least one number', () => {
test('in valid URLs', () => {
for (const url of validUrls(numericId)) {
const { full, pathnamePlusQs } = url;
expect(getDomainPath(full)).toBe(pathnamePlusQs);
for (const url of validUrls(alphanumericId)) {
const { full, pathname } = url;
expect(getDomainPath(full)).toBe(pathname);
}
});

test('in malformed URLs', () => {
for (const url of malformedUrls(numericId)) {
const { full, pathnamePlusQs } = url;
expect(getDomainPath(full)).toBe(pathnamePlusQs);
for (const url of malformedUrls(alphanumericId)) {
const { full, pathname } = url;
expect(getDomainPath(full)).toBe(pathname);
}
});
});

describe('anonymizing UUIDs', () => {
test('in valid URLs', () => {
for (const url of uuidUrls(validUrls)) {
const { full, pathnamePlusQs } = url;
expect(getDomainPath(full)).toBe(pathnamePlusQs);
const { full, pathname } = url;
expect(getDomainPath(full)).toBe(pathname);
}
});

test('in malformed URLs', () => {
for (const url of uuidUrls(malformedUrls)) {
const { full, pathnamePlusQs } = url;
expect(getDomainPath(full)).toBe(pathnamePlusQs);
const { full, pathname } = url;
expect(getDomainPath(full)).toBe(pathname);
}
});
});

describe('anonymizing emails', () => {
test('in valid URLs', () => {
for (const url of validUrls(email)) {
const { full, pathnamePlusQs } = url;
expect(getDomainPath(full)).toBe(pathnamePlusQs);
const { full, pathname } = url;
expect(getDomainPath(full)).toBe(pathname);
}
});

test('in malformed URLs', () => {
for (const url of malformedUrls(email)) {
const { full, pathnamePlusQs } = url;
expect(getDomainPath(full)).toBe(pathnamePlusQs);
const { full, pathname } = url;
expect(getDomainPath(full)).toBe(pathname);
}
});
});
});

function validUrls(idMaker: typeof numericId | typeof email, char = CHAR) {
function validUrls(idMaker: typeof alphanumericId | typeof email, char = CHAR) {
const firstId = idMaker();
const secondId = idMaker();
const firstIdObscured = char.repeat(firstId.length);
Expand All @@ -81,37 +81,42 @@ function validUrls(idMaker: typeof numericId | typeof email, char = CHAR) {
{
full: `https://test.com/api/v1/users/${firstId}`,
protocolPlusDomain: 'https://test.com',
pathnamePlusQs: `/api/v1/users/${firstIdObscured}`,
pathname: `/api/v1/users/${firstIdObscured}`,
},
{
full: `https://test.com/api/v1/users/${firstId}/`,
protocolPlusDomain: 'https://test.com',
pathnamePlusQs: `/api/v1/users/${firstIdObscured}/`,
pathname: `/api/v1/users/${firstIdObscured}/`,
},
{
full: `https://test.com/api/v1/users/${firstId}/posts/${secondId}`,
protocolPlusDomain: 'https://test.com',
pathnamePlusQs: `/api/v1/users/${firstIdObscured}/posts/${secondIdObscured}`,
pathname: `/api/v1/users/${firstIdObscured}/posts/${secondIdObscured}`,
},
{
full: `https://test.com/api/v1/users/${firstId}/posts/${secondId}/`,
protocolPlusDomain: 'https://test.com',
pathnamePlusQs: `/api/v1/users/${firstIdObscured}/posts/${secondIdObscured}/`,
pathname: `/api/v1/users/${firstIdObscured}/posts/${secondIdObscured}/`,
},
{
full: `https://test.com/api/v1/users/${firstId}/posts/${secondId}/`,
protocolPlusDomain: 'https://test.com',
pathnamePlusQs: `/api/v1/users/${firstIdObscured}/posts/${secondIdObscured}/`,
pathname: `/api/v1/users/${firstIdObscured}/posts/${secondIdObscured}/`,
},
{
full: `https://test.com/api/v1/users?id=${firstId}`,
protocolPlusDomain: 'https://test.com',
pathnamePlusQs: `/api/v1/users?id=${firstIdObscured}`,
pathname: '/api/v1/users',
},
{
full: `https://test.com/api/v1/users?id=${firstId}&post=${secondId}`,
protocolPlusDomain: 'https://test.com',
pathnamePlusQs: `/api/v1/users?id=${firstIdObscured}&post=${secondIdObscured}`,
pathname: '/api/v1/users',
},
{
full: `https://test.com/api/v1/users/${firstId}/posts/${secondId}`,
protocolPlusDomain: 'https://test.com',
pathname: `/api/v1/users/${firstIdObscured}/posts/${secondIdObscured}`,
},
];
}
Expand All @@ -126,22 +131,22 @@ function malformedUrls(idMaker: typeof numericId | typeof email, char = CHAR) {
{
full: `test.com/api/v1/users/${firstId}/posts/${secondId}/`,
protocolPlusDomain: 'test.com',
pathnamePlusQs: `/api/v1/users/${firstIdObscured}/posts/${secondIdObscured}/`,
pathname: `/api/v1/users/${firstIdObscured}/posts/${secondIdObscured}/`,
},
{
full: `htp://test.com/api/v1/users/${firstId}/posts/${secondId}/`,
protocolPlusDomain: 'htp://test.com',
pathnamePlusQs: `/api/v1/users/${firstIdObscured}/posts/${secondIdObscured}/`,
pathname: `/api/v1/users/${firstIdObscured}/posts/${secondIdObscured}/`,
},
{
full: `test.com/api/v1/users?id=${firstId}`,
protocolPlusDomain: 'test.com',
pathnamePlusQs: `/api/v1/users?id=${firstIdObscured}`,
pathname: '/api/v1/users',
},
{
full: `test.com/api/v1/users?id=${firstId}&post=${secondId}`,
protocolPlusDomain: 'test.com',
pathnamePlusQs: `/api/v1/users?id=${firstIdObscured}&post=${secondIdObscured}`,
pathname: '/api/v1/users',
},
];
}
Expand Down Expand Up @@ -174,3 +179,13 @@ function positiveDigit(): number {
function numericId(length = positiveDigit()) {
return Array.from({ length }, digit).join('');
}

function alphanumericId() {
return chooseRandomly([
`john${numericId()}`,
`title${numericId(1)}`,
numericId(),
]);
}

const chooseRandomly = <T>(array: T[]) => array[Math.floor(Math.random() * array.length)];