Skip to content

Commit

Permalink
[Discover] guarantee properties order within json.stringify
Browse files Browse the repository at this point in the history
  • Loading branch information
dimaanj committed Nov 17, 2022
1 parent 5139706 commit 317db09
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,21 @@ type NonNullableEntry<T> = { [K in keyof T]: NonNullable<T[keyof T]> };

const getDataViewParamsChecksum = (dataViewSpec: DataViewSpec) => {
const { title, timeFieldName, sourceFilters, runtimeFieldMap } = dataViewSpec;
return sha256
.create()
.update(JSON.stringify({ title, timeFieldName, sourceFilters, runtimeFieldMap }))
.hex();
const orderedParams = Object.values({ title, timeFieldName, sourceFilters, runtimeFieldMap });
return sha256.create().update(JSON.stringify(orderedParams)).hex();
};

/**
* Get rule params checksum skipping serialized data view object
*/
const getRuleParamsChecksum = (params: SearchThresholdAlertParams) => {
const orderedParams = Object.values(params);
return sha256
.create()
.update(
JSON.stringify(params, (key: string, value: string) => (key === 'index' ? undefined : value))
JSON.stringify(orderedParams, (key: string, value: string) =>
key === 'index' ? undefined : value
)
)
.hex();
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,20 +110,21 @@ function isTimeBasedDataView(index?: DataView) {

function getDataViewChecksum(index: DataViewSpec) {
const { title, timeFieldName, sourceFilters, runtimeFieldMap } = index;
return sha256
.create()
.update(JSON.stringify({ title, timeFieldName, sourceFilters, runtimeFieldMap }))
.hex();
const orderedParams = Object.values({ title, timeFieldName, sourceFilters, runtimeFieldMap });
return sha256.create().update(JSON.stringify(orderedParams)).hex();
}

/**
* Get rule params checksum skipping serialized data view object
*/
function getRuleParamsChecksum(params: OnlySearchSourceRuleParams) {
const orderedParams = Object.values(params);
return sha256
.create()
.update(
JSON.stringify(params, (key: string, value: string) => (key === 'index' ? undefined : value))
JSON.stringify(orderedParams, (key: string, value: string) =>
key === 'index' ? undefined : value
)
)
.hex();
}

0 comments on commit 317db09

Please sign in to comment.