Skip to content

Commit

Permalink
Adds a new config flag to encode with BOM for our CSVs (elastic#63006) (
Browse files Browse the repository at this point in the history
elastic#63265)

* Adds a new config flag to encode with BOM for our CSVs

* Push out bom-chars to it's own constant

* Getting those snapshots back into shape 💪

Co-authored-by: Elastic Machine <[email protected]>

Co-authored-by: Joel Griffith <[email protected]>
Co-authored-by: Elastic Machine <[email protected]>
  • Loading branch information
3 people authored Apr 10, 2020
1 parent 1d4706f commit 3665dfb
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 2 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions x-pack/legacy/plugins/reporting/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export const API_GENERATE_IMMEDIATE = `${API_BASE_URL_V1}/generate/immediate/csv

export const CONTENT_TYPE_CSV = 'text/csv';
export const CSV_REPORTING_ACTION = 'downloadCsvReport';
export const CSV_BOM_CHARS = '\ufeff';

export const WHITELISTED_JOB_CONTENT_TYPES = [
'application/json',
Expand Down
1 change: 1 addition & 0 deletions x-pack/legacy/plugins/reporting/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ export async function config(Joi: any) {
.default(),
}).default(),
csv: Joi.object({
useByteOrderMarkEncoding: Joi.boolean().default(false),
checkForFormulas: Joi.boolean().default(true),
enablePanelActionDownload: Joi.boolean().default(true),
maxSizeBytes: Joi.number()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { createMockReportingCore } from '../../../test_helpers';
import { LevelLogger } from '../../../server/lib/level_logger';
import { setFieldFormats } from '../../../server/services';
import { executeJobFactory } from './execute_job';
import { CSV_BOM_CHARS } from '../../../common/constants';

const delay = ms => new Promise(resolve => setTimeout(() => resolve(), ms));

Expand Down Expand Up @@ -374,6 +375,50 @@ describe('CSV Execute Job', function() {
});
});

describe('Byte order mark encoding', () => {
it('encodes CSVs with BOM', async () => {
configGetStub.withArgs('csv', 'useByteOrderMarkEncoding').returns(true);
callAsCurrentUserStub.onFirstCall().returns({
hits: {
hits: [{ _source: { one: 'one', two: 'bar' } }],
},
_scroll_id: 'scrollId',
});

const executeJob = await executeJobFactory(mockReportingPlugin, mockLogger);
const jobParams = {
headers: encryptedHeaders,
fields: ['one', 'two'],
conflictedTypesFields: [],
searchRequest: { index: null, body: null },
};
const { content } = await executeJob('job123', jobParams, cancellationToken);

expect(content).toEqual(`${CSV_BOM_CHARS}one,two\none,bar\n`);
});

it('encodes CSVs without BOM', async () => {
configGetStub.withArgs('csv', 'useByteOrderMarkEncoding').returns(false);
callAsCurrentUserStub.onFirstCall().returns({
hits: {
hits: [{ _source: { one: 'one', two: 'bar' } }],
},
_scroll_id: 'scrollId',
});

const executeJob = await executeJobFactory(mockReportingPlugin, mockLogger);
const jobParams = {
headers: encryptedHeaders,
fields: ['one', 'two'],
conflictedTypesFields: [],
searchRequest: { index: null, body: null },
};
const { content } = await executeJob('job123', jobParams, cancellationToken);

expect(content).toEqual('one,two\none,bar\n');
});
});

describe('Elasticsearch call errors', function() {
it('should reject Promise if search call errors out', async function() {
callAsCurrentUserStub.rejects(new Error());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import { i18n } from '@kbn/i18n';
import Hapi from 'hapi';
import { IUiSettingsClient, KibanaRequest } from '../../../../../../../src/core/server';
import { CSV_JOB_TYPE } from '../../../common/constants';
import { CSV_JOB_TYPE, CSV_BOM_CHARS } from '../../../common/constants';
import { ReportingCore } from '../../../server/core';
import { cryptoFactory } from '../../../server/lib';
import { getFieldFormats } from '../../../server/services';
Expand Down Expand Up @@ -121,6 +121,8 @@ export const executeJobFactory: ExecuteJobFactory<ESQueueWorkerExecuteFn<
]);

const generateCsv = createGenerateCsv(jobLogger);
const bom = config.get('csv', 'useByteOrderMarkEncoding') ? CSV_BOM_CHARS : '';

const { content, maxSizeReached, size, csvContainsFormulas } = await generateCsv({
searchRequest,
fields,
Expand All @@ -139,7 +141,7 @@ export const executeJobFactory: ExecuteJobFactory<ESQueueWorkerExecuteFn<

return {
content_type: 'text/csv',
content,
content: bom + content,
max_size_reached: maxSizeReached,
size,
csv_contains_formulas: csvContainsFormulas,
Expand Down
1 change: 1 addition & 0 deletions x-pack/legacy/plugins/reporting/server/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ export interface ReportingConfigType {
enablePanelActionDownload: boolean;
checkForFormulas: boolean;
maxSizeBytes: number;
useByteOrderMarkEncoding: boolean;
};
encryptionKey: string;
kibanaServer: any;
Expand Down

0 comments on commit 3665dfb

Please sign in to comment.