diff --git a/src/ui/public/persisted_log/create_log_key.js b/src/ui/public/persisted_log/create_log_key.js new file mode 100644 index 00000000000000..bbc91d65f11127 --- /dev/null +++ b/src/ui/public/persisted_log/create_log_key.js @@ -0,0 +1,31 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { Sha256 } from '../crypto'; + +export function createLogKey(type, optionalIdentifier) { + const baseKey = `kibana.history.${type}`; + + if (!optionalIdentifier) { + return baseKey; + } + + const protectedIdentifier = new Sha256().update(optionalIdentifier, 'utf8').digest('base64'); + return `${baseKey}-${protectedIdentifier}`; +} \ No newline at end of file diff --git a/src/ui/public/persisted_log/create_log_key.test.js b/src/ui/public/persisted_log/create_log_key.test.js new file mode 100644 index 00000000000000..3f7f69a5271e0a --- /dev/null +++ b/src/ui/public/persisted_log/create_log_key.test.js @@ -0,0 +1,35 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { createLogKey } from './create_log_key'; + +describe('createLogKey', () => { + it('should create a key starting with "kibana.history"', () => { + expect(createLogKey('foo', 'bar')).toMatch(/^kibana\.history/); + }); + + it('should include a hashed suffix of the identifier when present', () => { + const expectedSuffix = `/N4rLtula/QIYB+3If6bXDONEO5CnqBPrlURto+/j7k=`; + expect(createLogKey('foo', 'bar')).toMatch(`kibana.history.foo-${expectedSuffix}`); + }); + + it('should not include a hashed suffix if the identifier is not present', () => { + expect(createLogKey('foo')).toEqual('kibana.history.foo'); + }); +}); \ No newline at end of file diff --git a/src/ui/public/persisted_log/persisted_log.test.js b/src/ui/public/persisted_log/persisted_log.test.js index ec0a659d6d063a..5d5409a035a044 100644 --- a/src/ui/public/persisted_log/persisted_log.test.js +++ b/src/ui/public/persisted_log/persisted_log.test.js @@ -22,6 +22,12 @@ import sinon from 'sinon'; import expect from 'expect.js'; import { PersistedLog } from './'; +jest.mock('../chrome', () => { + return { + getBasePath: () => `/some/base/path` + }; +}); + const historyName = 'testHistory'; const historyLimit = 10; const payload = [ diff --git a/src/ui/public/persisted_log/recently_accessed.js b/src/ui/public/persisted_log/recently_accessed.js index af8280f6ab5b70..9392e276dbeb99 100644 --- a/src/ui/public/persisted_log/recently_accessed.js +++ b/src/ui/public/persisted_log/recently_accessed.js @@ -16,8 +16,9 @@ * specific language governing permissions and limitations * under the License. */ - +import chrome from '../chrome'; import { PersistedLog } from './'; +import { createLogKey } from './create_log_key'; class RecentlyAccessed { constructor() { @@ -28,7 +29,8 @@ class RecentlyAccessed { return oldItem.id === newItem.id; } }; - this.history = new PersistedLog('kibana.history.recentlyAccessed', historyOptions); + const logKey = createLogKey('recentlyAccessed', chrome.getBasePath()); + this.history = new PersistedLog(logKey, historyOptions); } add(link, label, id) {