Skip to content

Commit

Permalink
add if checks
Browse files Browse the repository at this point in the history
  • Loading branch information
Bamieh committed Apr 6, 2020
1 parent bed4eb4 commit 74c17a3
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import { i18n } from '@kbn/i18n';
import { cryptoFactory } from '../../../server/lib/crypto';
import { CryptoFactory, Logger } from '../../../types';
import { Logger } from '../../../types';

interface HasEncryptedHeaders {
headers?: string;
Expand All @@ -25,9 +25,12 @@ export const decryptJobHeaders = async <
job: JobDocPayloadType;
logger: Logger;
}): Promise<Record<string, string>> => {
const crypto: CryptoFactory = cryptoFactory(encryptionKey);
try {
const decryptedHeaders: Record<string, string> = await crypto.decrypt(job.headers);
if (typeof job.headers !== 'string') {
throw new Error('Job headers are missing');
}
const crypto = cryptoFactory(encryptionKey);
const decryptedHeaders = (await crypto.decrypt(job.headers)) as Record<string, string>;
return decryptedHeaders;
} catch (err) {
logger.error(err);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,11 @@ export const executeJobFactory: ExecuteJobFactory<ESQueueWorkerExecuteFn<
} = job;

const decryptHeaders = async () => {
let decryptedHeaders;
try {
decryptedHeaders = await crypto.decrypt(headers);
if (typeof headers !== 'string') {
throw new Error('Job headers are missing');
}
return await crypto.decrypt(headers);
} catch (err) {
logger.error(err);
throw new Error(
Expand All @@ -58,7 +60,6 @@ export const executeJobFactory: ExecuteJobFactory<ESQueueWorkerExecuteFn<
)
); // prettier-ignore
}
return decryptedHeaders;
};

const fakeRequest = KibanaRequest.from({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,13 @@ export const executeJobFactory: ExecuteJobFactory<ImmediateExecuteFn<
let decryptedHeaders: Record<string, unknown>;
const serializedEncryptedHeaders = job.headers;
try {
decryptedHeaders = await crypto.decrypt(serializedEncryptedHeaders);
if (typeof serializedEncryptedHeaders !== 'string') {
throw new Error('Job headers are missing');
}
decryptedHeaders = (await crypto.decrypt(serializedEncryptedHeaders)) as Record<
string,
unknown
>;
} catch (err) {
jobLogger.error(err);
throw new Error(
Expand Down
6 changes: 5 additions & 1 deletion x-pack/legacy/plugins/reporting/server/lib/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

import nodeCrypto from '@elastic/node-crypto';

export function cryptoFactory(encryptionKey: string | undefined) {
export function cryptoFactory(encryptionKey?: string) {
if (typeof encryptionKey !== 'string') {
throw new Error('Encryption Key required.');
}

return nodeCrypto({ encryptionKey });
}
4 changes: 0 additions & 4 deletions x-pack/legacy/plugins/reporting/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,6 @@ export interface ConditionalHeadersConditions {
basePath: string;
}

export interface CryptoFactory {
decrypt: (headers?: string) => any;
}

export interface IndexPatternSavedObject {
attributes: {
fieldFormatMap: string;
Expand Down

0 comments on commit 74c17a3

Please sign in to comment.