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

[Reporting] Restore legacy compatibility shim for PDF job creation #108271

Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const getMockJob = (base: object) => base as TaskPayloadPNG & TaskPayloadPDF;
test(`fails if no URL is passed`, async () => {
const fn = () => getFullUrls(mockConfig, getMockJob({}));
expect(fn).toThrowErrorMatchingInlineSnapshot(
`"No valid URL fields found in Job Params! Expected \`job.relativeUrl: string\` or \`job.relativeUrls: string[]\`"`
`"No valid URL fields found in Job Params! Expected \`job.relativeUrl\` or \`job.objects[{ relativeUrl }]\`"`
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

);
});

Expand All @@ -54,14 +54,7 @@ test(`fails if URLs are absolute for PNGs`, async () => {
test(`fails if URLs are file-protocols for PDF`, async () => {
const forceNow = '2000-01-01T00:00:00.000Z';
const relativeUrl = 'file://etc/passwd/#/something';
const fn = () =>
getFullUrls(
mockConfig,
getMockJob({
relativeUrls: [relativeUrl],
forceNow,
})
);
const fn = () => getFullUrls(mockConfig, getMockJob({ objects: [{ relativeUrl }], forceNow }));
expect(fn).toThrowErrorMatchingInlineSnapshot(
`"Found invalid URL(s), all URLs must be relative: file://etc/passwd/#/something"`
);
Expand All @@ -75,7 +68,7 @@ test(`fails if URLs are absolute for PDF`, async () => {
getFullUrls(
mockConfig,
getMockJob({
relativeUrls: [relativeUrl],
objects: [{ relativeUrl }],
forceNow,
})
);
Expand All @@ -86,13 +79,16 @@ test(`fails if URLs are absolute for PDF`, async () => {

test(`fails if any URLs are absolute or file's for PDF`, async () => {
const forceNow = '2000-01-01T00:00:00.000Z';
const relativeUrls = [
'/app/kibana#/something_aaa',
'http://169.254.169.254/latest/meta-data/iam/security-credentials/profileName/#/something',
'file://etc/passwd/#/something',
const objects = [
{ relativeUrl: '/app/kibana#/something_aaa' },
{
relativeUrl:
'http://169.254.169.254/latest/meta-data/iam/security-credentials/profileName/#/something',
},
{ relativeUrl: 'file://etc/passwd/#/something' },
];

const fn = () => getFullUrls(mockConfig, getMockJob({ relativeUrls, forceNow }));
const fn = () => getFullUrls(mockConfig, getMockJob({ objects, forceNow }));
expect(fn).toThrowErrorMatchingInlineSnapshot(
`"Found invalid URL(s), all URLs must be relative: http://169.254.169.254/latest/meta-data/iam/security-credentials/profileName/#/something file://etc/passwd/#/something"`
);
Expand All @@ -107,7 +103,7 @@ test(`fails if URL does not route to a visualization`, async () => {

test(`adds forceNow to hash's query, if it exists`, async () => {
const forceNow = '2000-01-01T00:00:00.000Z';
const urls = await getFullUrls(
const urls = getFullUrls(
mockConfig,
getMockJob({ relativeUrl: '/app/kibana#/something', forceNow })
);
Expand All @@ -120,7 +116,7 @@ test(`adds forceNow to hash's query, if it exists`, async () => {
test(`appends forceNow to hash's query, if it exists`, async () => {
const forceNow = '2000-01-01T00:00:00.000Z';

const urls = await getFullUrls(
const urls = getFullUrls(
mockConfig,
getMockJob({ relativeUrl: '/app/kibana#/something?_g=something', forceNow })
);
Expand All @@ -131,21 +127,21 @@ test(`appends forceNow to hash's query, if it exists`, async () => {
});

test(`doesn't append forceNow query to url, if it doesn't exists`, async () => {
const urls = await getFullUrls(mockConfig, getMockJob({ relativeUrl: '/app/kibana#/something' }));
const urls = getFullUrls(mockConfig, getMockJob({ relativeUrl: '/app/kibana#/something' }));

expect(urls[0]).toEqual('http://localhost:5601/sbp/app/kibana#/something');
});

test(`adds forceNow to each of multiple urls`, async () => {
const forceNow = '2000-01-01T00:00:00.000Z';
const urls = await getFullUrls(
const urls = getFullUrls(
mockConfig,
getMockJob({
relativeUrls: [
'/app/kibana#/something_aaa',
'/app/kibana#/something_bbb',
'/app/kibana#/something_ccc',
'/app/kibana#/something_ddd',
objects: [
{ relativeUrl: '/app/kibana#/something_aaa' },
{ relativeUrl: '/app/kibana#/something_bbb' },
{ relativeUrl: '/app/kibana#/something_ccc' },
{ relativeUrl: '/app/kibana#/something_ddd' },
],
forceNow,
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function isPngJob(job: TaskPayloadPNG | TaskPayloadPDF): job is TaskPayloadPNG {
return (job as TaskPayloadPNG).relativeUrl !== undefined;
}
function isPdfJob(job: TaskPayloadPNG | TaskPayloadPDF): job is TaskPayloadPDF {
return (job as TaskPayloadPDF).relativeUrls !== undefined;
return (job as TaskPayloadPDF).objects !== undefined;
}

export function getFullUrls(config: ReportingConfig, job: TaskPayloadPDF | TaskPayloadPNG) {
Expand All @@ -39,17 +39,17 @@ export function getFullUrls(config: ReportingConfig, job: TaskPayloadPDF | TaskP
if (isPngJob(job)) {
relativeUrls = [job.relativeUrl];
} else if (isPdfJob(job)) {
relativeUrls = job.relativeUrls;
relativeUrls = job.objects.map((obj) => obj.relativeUrl);
} else {
throw new Error(
`No valid URL fields found in Job Params! Expected \`job.relativeUrl: string\` or \`job.relativeUrls: string[]\``
`No valid URL fields found in Job Params! Expected \`job.relativeUrl\` or \`job.objects[{ relativeUrl }]\``
);
}

validateUrls(relativeUrls);

const urls = relativeUrls.map((relativeUrl) => {
const parsedRelative: UrlWithStringQuery = urlParse(relativeUrl);
const parsedRelative: UrlWithStringQuery = urlParse(relativeUrl); // FIXME: '(urlStr: string): UrlWithStringQuery' is deprecated
const jobUrl = getAbsoluteUrl({
path: parsedRelative.pathname === null ? undefined : parsedRelative.pathname,
hash: parsedRelative.hash === null ? undefined : parsedRelative.hash,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ const isBogusUrl = (url: string) => {
};

export const validateUrls = (urls: string[]): void => {
if (!Array.isArray(urls)) {
throw new Error('Invalid relativeUrls. String[] is expected.');
}
urls.forEach((url) => {
if (typeof url !== 'string') {
throw new Error('Invalid Relative URL in relativeUrls. String is expected.');
}
});

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const badUrls = filter(urls, (url) => isBogusUrl(url));

if (badUrls.length) {
Expand Down
Loading