Skip to content

Commit

Permalink
[6.8][Backport] Load configuration from EMS-metadata in region-maps (#…
Browse files Browse the repository at this point in the history
…70888) (#71162)

Manual backport to account for accumulated refactors. 6.8 branch predates angular->react conversion, does not use the external ems-client package, and Visualize was still not using expression-pipeline.
  • Loading branch information
thomasneirynck authored Jul 9, 2020
1 parent 235aca8 commit 0bf552b
Show file tree
Hide file tree
Showing 7 changed files with 183 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ import LogstashIndexPatternStubProvider from 'fixtures/stubbed_logstash_index_pa
import * as visModule from 'ui/vis';
import { ImageComparator } from 'test_utils/image_comparator';
import worldJson from './world.json';
import EMS_CATALOGUE from '../../../../../ui/public/vis/__tests__/map/ems_mocks/sample_manifest_6.6.json';
import EMS_FILES from '../../../../../ui/public/vis/__tests__/map/ems_mocks/sample_files_6.6.json';
import EMS_TILES from '../../../../../ui/public/vis/__tests__/map/ems_mocks/sample_tiles_6.6.json';

import initialPng from './initial.png';
import toiso3Png from './toiso3.png';
Expand All @@ -49,6 +46,8 @@ describe('RegionMapsVisualizationTests', function () {
let Vis;
let indexPattern;
let vis;
let serviceSettings;
let loadFileLayerConfig;

let imageComparator;

Expand Down Expand Up @@ -80,7 +79,6 @@ describe('RegionMapsVisualizationTests', function () {

beforeEach(ngMock.module('kibana'));

let getManifestStub;
beforeEach(ngMock.inject((Private, $injector) => {

Vis = Private(visModule.VisProvider);
Expand All @@ -96,24 +94,24 @@ describe('RegionMapsVisualizationTests', function () {
});
};

const serviceSettings = $injector.get('serviceSettings');
getManifestStub = serviceSettings.__debugStubManifestCalls(async (url) => {
//simulate network calls
if (url.startsWith('https://foobar')) {
return EMS_CATALOGUE;
} else if (url.startsWith('https://tiles.foobar')) {
return EMS_TILES;
} else if (url.startsWith('https://files.foobar')) {
return EMS_FILES;
}
});
serviceSettings = $injector.get('serviceSettings');
loadFileLayerConfig = serviceSettings.loadFileLayerConfig;
serviceSettings.loadFileLayerConfig = async (fl) => {
// Region-maps visualization calls EMS to dynamically load attribution iso grabbing it from visState
// Mock this call to avoid network-roundtrip
return {
attribution: fl.attribution + '_sanitized',
name: fl.name,
};
};


}));


afterEach(function () {
ChoroplethLayer.prototype._makeJsonAjaxCall = _makeJsonAjaxCallOld;
getManifestStub.removeStub();
loadFileLayerConfig.loadFileLayerConfig = loadFileLayerConfig;
});


Expand Down
11 changes: 9 additions & 2 deletions src/legacy/core_plugins/region_map/public/choropleth_layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export default class ChoroplethLayer extends KibanaMapLayer {
}


constructor(name, attribution, format, showAllShapes, meta, layerConfig) {
constructor({ name, attribution, format, showAllShapes, meta, layerConfig }) {

super();

Expand Down Expand Up @@ -263,7 +263,14 @@ CORS configuration of the server permits requests from the Kibana application on
}

cloneChoroplethLayerForNewData(name, attribution, format, showAllData, meta, layerConfig) {
const clonedLayer = new ChoroplethLayer(name, attribution, format, showAllData, meta, layerConfig);
const clonedLayer = new ChoroplethLayer({
name,
attribution,
format,
showAllShapes: showAllData,
meta,
layerConfig }
);
clonedLayer.setJoinField(this._joinField);
clonedLayer.setColorRamp(this._colorRamp);
clonedLayer.setLineWeight(this._lineWeight);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@
import 'plugins/kbn_vislib_vis_types/controls/vislib_basic_options';
import _ from 'lodash';
import { BaseMapsVisualizationProvider } from '../../tile_map/public/base_maps_visualization';
import { ORIGIN } from '../../tile_map/common/origin';
import ChoroplethLayer from './choropleth_layer';
import { truncatedColorMaps } from 'ui/vislib/components/color/truncated_colormaps';
import AggResponsePointSeriesTooltipFormatterProvider from './tooltip_formatter';
import 'ui/vis/map/service_settings';
import { toastNotifications } from 'ui/notify';

export function RegionMapsVisualizationProvider(Private, config, i18n) {
export function RegionMapsVisualizationProvider(Private, config, i18n, regionmapsConfig, serviceSettings) {

const tooltipFormatter = Private(AggResponsePointSeriesTooltipFormatterProvider);
const BaseMapsVisualization = Private(BaseMapsVisualizationProvider);
Expand Down Expand Up @@ -61,17 +62,18 @@ export function RegionMapsVisualizationProvider(Private, config, i18n) {
});
}

if (!this._vis.params.selectedJoinField && this._vis.params.selectedLayer) {
this._vis.params.selectedJoinField = this._vis.params.selectedLayer.fields[0];
const selectedLayer = await this._loadConfig(this.vis.params.selectedLayer);
if (!this.vis.params.selectedJoinField && selectedLayer) {
this.vis.params.selectedJoinField = selectedLayer.fields[0];
}

if (!this._vis.params.selectedLayer) {
if (!selectedLayer) {
return;
}

this._updateChoroplethLayerForNewMetrics(
this._vis.params.selectedLayer.name,
this._vis.params.selectedLayer.attribution,
selectedLayer.name,
selectedLayer.attribution,
this._vis.params.showAllShapes,
results
);
Expand All @@ -82,27 +84,58 @@ export function RegionMapsVisualizationProvider(Private, config, i18n) {
this._kibanaMap.useUiStateFromVisualization(this._vis);
}

async _loadConfig(fileLayerConfig) {
// Load the selected layer from the metadata-service.
// Do not use the selectedLayer from the visState.
// These settings are stored in the URL and can be used to inject dirty display content.
if (!fileLayerConfig) {
return;
}

if (
fileLayerConfig.isEMS || //Hosted by EMS. Metadata needs to be resolved through EMS
(fileLayerConfig.layerId && fileLayerConfig.layerId.startsWith(`${ORIGIN.EMS}.`)) //fallback for older saved objects
) {
return await serviceSettings.loadFileLayerConfig(fileLayerConfig);
}

//Configured in the kibana.yml. Needs to be resolved through the settings.
const configuredLayer = regionmapsConfig.layers.find(
(layer) => layer.name === fileLayerConfig.name
);

if (configuredLayer) {
return {
...configuredLayer,
attribution: _.escape(configuredLayer.attribution ? configuredLayer.attribution : ''),
};
}

return null;
}

async _updateParams() {

await super._updateParams();

const visParams = this.vis.params;
if (!visParams.selectedJoinField && visParams.selectedLayer) {
visParams.selectedJoinField = visParams.selectedLayer.fields[0];
const selectedLayer = await this._loadConfig(this.vis.params.selectedLayer);

if (!this.vis.params.selectedJoinField && selectedLayer) {
this.vis.params.selectedJoinField = selectedLayer.fields[0];
}

if (!visParams.selectedJoinField || !visParams.selectedLayer) {
if (!this.vis.params.selectedJoinField || !selectedLayer) {
return;
}

this._updateChoroplethLayerForNewProperties(
visParams.selectedLayer.name,
visParams.selectedLayer.attribution,
selectedLayer.name,
selectedLayer.attribution,
this._vis.params.showAllShapes
);
this._choroplethLayer.setJoinField(visParams.selectedJoinField.name);
this._choroplethLayer.setColorRamp(truncatedColorMaps[visParams.colorSchema].value);
this._choroplethLayer.setLineWeight(visParams.outlineWeight);
this._choroplethLayer.setJoinField(this.vis.params.selectedJoinField.name);
this._choroplethLayer.setColorRamp(truncatedColorMaps[this.vis.params.colorSchema].value);
this._choroplethLayer.setLineWeight(this.vis.params.outlineWeight);
this._setTooltipFormatter();

}
Expand Down Expand Up @@ -136,14 +169,14 @@ export function RegionMapsVisualizationProvider(Private, config, i18n) {
this.vis.params.selectedLayer
);
} else {
this._choroplethLayer = new ChoroplethLayer(
this._choroplethLayer = new ChoroplethLayer({
name,
attribution,
this.vis.params.selectedLayer.format,
showAllData,
this.vis.params.selectedLayer.meta,
this.vis.params.selectedLayer
);
format: this.vis.params.selectedLayer.format,
showAllShapes: showAllData,
meta: this.vis.params.selectedLayer.meta,
layerConfig: this.vis.params.selectedLayer
});
}

this._choroplethLayer.on('select', (event) => {
Expand Down
2 changes: 1 addition & 1 deletion src/ui/public/vis/__tests__/map/ems_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ describe('ems_client', () => {
it('.getFileLayers', async () => {
const emsClient = getEMSClient();
const layers = await emsClient.getFileLayers();
expect(layers.length).to.be(18);
expect(layers.length).to.be(19);
});

it('.getFileLayers[0]', async () => {
Expand Down
42 changes: 42 additions & 0 deletions src/ui/public/vis/__tests__/map/ems_mocks/sample_files_6.6.json
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,48 @@
"zh-tw": "國家"
}
},
{
"layer_id": "world_countries_with_compromised_attribution",
"created_at": "2017-04-26T17:12:15.978370",
"attribution": [
{
"label": {
"en": "<div onclick='alert(1')>Made with NaturalEarth</div>"
},
"url": {
"en": "http://www.naturalearthdata.com/about/terms-of-use"
}
},
{
"label": {
"en": "Elastic Maps Service"
},
"url": {
"en": "javascript:alert('foobar')"
}
}
],
"formats": [
{
"type": "geojson",
"url": "https://vector-staging.maps.elastic.co/files/world_countries_v1.geo.json?elastic_tile_service_tos=agree",
"legacy_default": true
}
],
"fields": [
{
"type": "id",
"id": "iso2",
"label": {
"en": "ISO 3166-1 alpha-2 code"
}
}
],
"legacy_ids": [],
"layer_name": {
"en": "World Countries (compromised)"
}
},
{
"layer_id": "australia_states",
"created_at": "2018-06-27T23:47:32.202380",
Expand Down
17 changes: 15 additions & 2 deletions src/ui/public/vis/__tests__/map/service_settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ describe('service_settings (FKA tilemaptest)', function () {
expect(mapUrl).to.contain('{x}');
expect(mapUrl).to.contain('{y}');
expect(mapUrl).to.contain('{z}');
expect(tmsService.attribution).to.eql(
'<p>&#169; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors | <a href="https://www.elastic.co/elastic-maps-service">Elastic Maps Service</a></p>&#10;'
);

const urlObject = url.parse(mapUrl, true);
expect(urlObject.hostname).to.be('tiles-stage.elastic.co');
Expand Down Expand Up @@ -240,7 +243,7 @@ describe('service_settings (FKA tilemaptest)', function () {

serviceSettings.addQueryParams({ foo: 'bar' });
const fileLayers = await serviceSettings.getFileLayers();
expect(fileLayers.length).to.be(18);
expect(fileLayers.length).to.be(19);
const assertions = fileLayers.map(async function (fileLayer) {

expect(fileLayer.origin).to.be(ORIGIN.EMS);
Expand All @@ -258,7 +261,7 @@ describe('service_settings (FKA tilemaptest)', function () {
it('should load manifest (individual props)', async () => {

const expected = {
attribution: '<a href="http://www.naturalearthdata.com/about/terms-of-use">Made with NaturalEarth</a> | <a href="https://www.elastic.co/elastic-maps-service">Elastic Maps Service</a>',
attribution: '<a rel="noreferrer noopener" href="http://www.naturalearthdata.com/about/terms-of-use">Made with NaturalEarth</a> | <a rel="noreferrer noopener" href="https://www.elastic.co/elastic-maps-service">Elastic Maps Service</a>',
format: 'geojson',
fields: [
{ 'type': 'id', 'name': 'iso2', 'description': 'ISO 3166-1 alpha-2 code' },
Expand Down Expand Up @@ -295,5 +298,15 @@ describe('service_settings (FKA tilemaptest)', function () {

});

it('should sanitize EMS attribution', async () => {
const fileLayers = await serviceSettings.getFileLayers();
const fileLayer = fileLayers.find((layer) => {
return layer.id === 'world_countries_with_compromised_attribution';
});
expect(fileLayer.attribution).to.eql(
'<a rel="noreferrer noopener" href="http://www.naturalearthdata.com/about/terms-of-use">&lt;div onclick=\'alert(1\')&gt;Made with NaturalEarth&lt;/div&gt;</a> | <a rel="noreferrer noopener">Elastic Maps Service</a>'
);
});

});
});
Loading

0 comments on commit 0bf552b

Please sign in to comment.