Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[savedObjects] fix error handling when Kibana index is missing #14141

Merged
merged 34 commits into from
Oct 3, 2017
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
4b2f6cb
[savedObjects/delete+bulk_get] add failing tests
spalger Sep 21, 2017
859a5ce
[savedObjects/delete+bulk_get] improve 404 handling
spalger Sep 21, 2017
cf1b5a7
[savedObjects/client] fix mocha tests
spalger Sep 21, 2017
5032648
Merge branch 'master' of github.com:elastic/kibana into fix/savedObje…
spalger Sep 22, 2017
b99bf09
[savedObjects/tests] remove extra test wrapper
spalger Sep 22, 2017
00aaac6
[apiIntegration/kbnServer] basically disable es healthcheck
spalger Sep 22, 2017
a93cae0
[savedObjects/create] add integration test
spalger Sep 22, 2017
ec21fe4
[savedObjects/find] add failing integration tests
spalger Sep 22, 2017
3c6ee2f
[savedObjects/find] fix failing test
spalger Sep 22, 2017
de1a3fc
[savedObjects/client] explain reason for generic 404s
spalger Sep 22, 2017
8d2e623
[savedObjects/get] add integration tests
spalger Sep 22, 2017
bcd3880
[savedObjects/find] test request with unkown type
spalger Sep 22, 2017
dae161d
[savedObjects/find] add some more weird param tests
spalger Sep 22, 2017
058e66d
[savedObjects/find] test that weird params pass when no index
spalger Sep 22, 2017
69272c7
[savedObjects/update] use generic 404
spalger Sep 22, 2017
4353cd2
fix typos
spalger Sep 22, 2017
7381d0c
[savedObjects/update] add integration tests
spalger Sep 22, 2017
56842cf
remove debugging uncomment
spalger Sep 22, 2017
64e7bfb
[savedObjects/tests] move backup kibana index delete out of tests
spalger Sep 22, 2017
73df8dd
[savedObjects/tests/esArchives] remove logstash data
spalger Sep 22, 2017
ba595f8
[savedObjects] update test
spalger Sep 22, 2017
d80cf10
[uiSettings] remove detailed previously leaked from API
spalger Sep 22, 2017
a2c38e6
Merge branch 'master' of github.com:elastic/kibana into fix/savedObje…
spalger Sep 26, 2017
499af72
[functional/dashboard] wrap check that is only failing on Jenkins
spalger Sep 26, 2017
47dde37
Merge branch 'master' of github.com:elastic/kibana into fix/savedObje…
spalger Sep 28, 2017
36386dc
Merge branch 'master' of github.com:elastic/kibana into fix/savedObje…
spalger Sep 29, 2017
baa9e07
[savedObjects/error] replace decorateNotFound with createGenericNotFound
spalger Sep 29, 2017
b976a21
fix typo
spalger Sep 29, 2017
aaf4bb4
[savedObjectsClient/errors] fix decorateEsError() test
spalger Sep 29, 2017
4b683cb
Merge branch 'master' of github.com:elastic/kibana into fix/savedObje…
spalger Oct 2, 2017
eaa739f
[savedObjectsClient] fix typos
spalger Oct 2, 2017
977728f
[savedObjects/tests/functional] delete document that would normally e…
spalger Oct 2, 2017
f98c009
[savedObjectsClient/tests] use sinon assertions
spalger Oct 2, 2017
9b2f127
[savedObjects/apiTests] create without index responds with 503 after …
spalger Oct 3, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,9 @@ describe('SavedObjectsClient', () => {

describe('#delete', () => {
it('throws notFound when ES is unable to find the document', async () => {
callAdminCluster.returns(Promise.resolve({ found: false }));
callAdminCluster.returns(Promise.resolve({
result: 'not_found'
}));

try {
await savedObjectsClient.delete('index-pattern', 'logstash-*');
Expand All @@ -303,7 +305,9 @@ describe('SavedObjectsClient', () => {
});

it('passes the parameters to callAdminCluster', async () => {
callAdminCluster.returns({});
callAdminCluster.returns({
result: 'deleted'
});
await savedObjectsClient.delete('index-pattern', 'logstash-*');

expect(callAdminCluster.calledOnce).to.be(true);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: since you're here already :)

sinon.assert.calledOnce(onBeforeWrite);
sinon.assert.calledOnce(callAdminCluster);
sinon.assert.calledWithExactly(callAdminCluster, 'delete', {
	type: 'doc',
	id: 'index-pattern:logstash-*',
	refresh: 'wait_for',
	index: '.kibana-test',
	ignore: [404],
});

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a lot of that in this file, but if you like!

Copy link
Contributor Author

@spalger spalger Oct 2, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pretty sure I'm falling in love with sinon's matcher API. I'm going to merge if the tests pass, but if you wouldn't mind double checking this commit @azasypkin, I would appreciate it

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thanks!

Expand All @@ -314,7 +318,8 @@ describe('SavedObjectsClient', () => {
type: 'doc',
id: 'index-pattern:logstash-*',
refresh: 'wait_for',
index: '.kibana-test'
index: '.kibana-test',
ignore: [404],
});
});
});
Expand Down Expand Up @@ -562,6 +567,7 @@ describe('SavedObjectsClient', () => {
body: {
doc: { updated_at: mockTimestamp, 'index-pattern': { title: 'Testing' } }
},
ignore: [404],
refresh: 'wait_for',
index: '.kibana-test'
});
Expand Down
46 changes: 44 additions & 2 deletions src/server/saved_objects/client/saved_objects_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,25 @@ export class SavedObjectsClient {
id: this._generateEsId(type, id),
type: this._type,
refresh: 'wait_for',
ignore: [404],
});

if (response.found === false) {
const deleted = response.result === 'deleted';
if (deleted) {
return {};
}

// 404 might be because document is missing or index is missing,
// don't leak that implementation detail to the user
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be great if we could add an explanation here. (Basically answering the question: Why don't we want to leak this to the user?)

We've got this same comment in multiple places. Not sure if we should add an explanation in one place and "link" there, but it feels like we should make it clear here, at least.

const docNotFound = response.result === 'not_found';
const indexNotFound = response.error && response.error.type === 'index_not_found_exception';
if (docNotFound || indexNotFound) {
throw errors.decorateNotFoundError(Boom.notFound());
}

throw new Error(
`Unexpected Elasticsearch DELETE response: ${JSON.stringify({ type, id, response, })}`
);
}

/**
Expand Down Expand Up @@ -188,6 +202,7 @@ export class SavedObjectsClient {
size: perPage,
from: perPage * (page - 1),
_source: includedFields(type, fields),
ignore: [404],
body: {
version: true,
...getSearchDsl(this._mappings, {
Expand All @@ -202,6 +217,17 @@ export class SavedObjectsClient {

const response = await this._withKibanaIndex('search', esOptions);

if (response.status === 404) {
// 404 is only possible here if the index is missing, which
// is an implementation detail we don't want to leak
return {
page,
per_page: perPage,
total: 0,
saved_objects: []
};
}

return {
page,
per_page: perPage,
Expand Down Expand Up @@ -249,13 +275,14 @@ export class SavedObjectsClient {
saved_objects: response.docs.map((doc, i) => {
const { id, type } = objects[i];

if (doc.found === false) {
if (!doc.found) {
return {
id,
type,
error: { statusCode: 404, message: 'Not found' }
};
}

const time = doc._source.updated_at;
return {
id,
Expand All @@ -279,7 +306,16 @@ export class SavedObjectsClient {
const response = await this._withKibanaIndex('get', {
id: this._generateEsId(type, id),
type: this._type,
ignore: [404]
});

const docNotFound = response.found === false;
const indexNotFound = response.status === 404;
if (docNotFound || indexNotFound) {
// don't leak implementation details about why there was a 404
throw errors.decorateNotFoundError(Boom.notFound());
}

const { updated_at: updatedAt } = response._source;

return {
Expand Down Expand Up @@ -307,6 +343,7 @@ export class SavedObjectsClient {
type: this._type,
version: options.version,
refresh: 'wait_for',
ignore: [404],
body: {
doc: {
updated_at: time,
Expand All @@ -315,6 +352,11 @@ export class SavedObjectsClient {
},
});

if (response.status === 404) {
// don't leak implementation details about why there was a 404
throw errors.decorateNotFoundError(Boom.notFound());
}

return {
id,
type,
Expand Down
3 changes: 1 addition & 2 deletions src/ui/ui_settings/routes/__tests__/lib/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ export function assertDocMissingResponse({ result }) {
assertSinonMatch(result, {
statusCode: 404,
error: 'Not Found',
message: sinon.match('document_missing_exception')
.and(sinon.match('document missing'))
message: 'Not Found'
});
}
1 change: 1 addition & 0 deletions tasks/config/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ module.exports = function (grunt) {
...stdDevArgs,
'--optimize.enabled=false',
'--elasticsearch.url=' + esTestConfig.getUrl(),
'--elasticsearch.healthCheck.delay=360000',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: just curious, why exactly 6 minutes? :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haha, I'm horrible at math

'--server.port=' + kibanaTestServerUrlParts.port,
'--server.xsrf.disableProtection=true',
...kbnServerFlags,
Expand Down
1 change: 1 addition & 0 deletions test/api_integration/apis/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export default function ({ loadTestFile }) {
describe('apis', () => {
loadTestFile(require.resolve('./index_patterns'));
loadTestFile(require.resolve('./saved_objects'));
loadTestFile(require.resolve('./scripts'));
loadTestFile(require.resolve('./search'));
loadTestFile(require.resolve('./suggestions'));
Expand Down
122 changes: 122 additions & 0 deletions test/api_integration/apis/saved_objects/bulk_get.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import expect from 'expect.js';

export default function ({ getService }) {
const supertest = getService('supertest');
const es = getService('es');
const esArchiver = getService('esArchiver');

const BULK_REQUESTS = [
{
type: 'visualization',
id: 'dd7caf20-9efd-11e7-acb3-3dab96693fab',
},
{
type: 'dashboard',
id: 'does not exist',
},
{
type: 'config',
id: '7.0.0-alpha1',
},
];

describe('bulk_get', () => {
describe('with kibana index', () => {
before(() => esArchiver.load('saved_objects/basic'));
after(() => esArchiver.unload('saved_objects/basic'));

it('should return 200 with individual responses', async () => (
await supertest
.post(`/api/saved_objects/bulk_get`)
.send(BULK_REQUESTS)
.expect(200)
.then(resp => {
expect(resp.body).to.eql({
saved_objects: [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be 🎉 to have snapshot tests for these, but that's of course not possible yet

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are basically snapshot tests, no? What would be different with a snapshot test?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

++ Yeah, it's basically the same (I just prefer working with snapshots over large objects, e.g. when changing code etc)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shit, I totally meant to delete my comment, thought it failed, and accidentally deleted yours 😿

{
id: 'dd7caf20-9efd-11e7-acb3-3dab96693fab',
type: 'visualization',
updated_at: '2017-09-21T18:51:23.794Z',
version: resp.body.saved_objects[0].version,
attributes: {
title: 'Count of requests',
description: '',
version: 1,
// cheat for some of the more complex attributes
visState: resp.body.saved_objects[0].attributes.visState,
uiStateJSON: resp.body.saved_objects[0].attributes.uiStateJSON,
kibanaSavedObjectMeta: resp.body.saved_objects[0].attributes.kibanaSavedObjectMeta
}
},
{
id: 'does not exist',
type: 'dashboard',
error: {
statusCode: 404,
message: 'Not found'
}
},
{
id: '7.0.0-alpha1',
type: 'config',
updated_at: '2017-09-21T18:49:16.302Z',
version: resp.body.saved_objects[2].version,
attributes: {
buildNum: 8467,
defaultIndex: '91200a00-9efd-11e7-acb3-3dab96693fab'
}
}
]
});
})
));
});

describe('without kibana index', () => {
before(async () => (
// just in case the kibana server has recreated it
await es.indices.delete({
index: '.kibana',
ignore: [404],
})
));

it('should return 200 with individual responses', async () => (
await supertest
.post('/api/saved_objects/bulk_get')
.send(BULK_REQUESTS)
.expect(200)
.then(resp => {
expect(resp.body).to.eql({
saved_objects: [
{
id: 'dd7caf20-9efd-11e7-acb3-3dab96693fab',
type: 'visualization',
error: {
statusCode: 404,
message: 'Not found'
}
},
{
id: 'does not exist',
type: 'dashboard',
error: {
statusCode: 404,
message: 'Not found'
}
},
{
id: '7.0.0-alpha1',
type: 'config',
error: {
statusCode: 404,
message: 'Not found'
}
}
]
});
})
));
});
});
}
86 changes: 86 additions & 0 deletions test/api_integration/apis/saved_objects/create.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import expect from 'expect.js';

export default function ({ getService }) {
const supertest = getService('supertest');
const es = getService('es');
const esArchiver = getService('esArchiver');

async function sendAndValidateCreate() {
await supertest
.post(`/api/saved_objects/visualization`)
.send({
attributes: {
title: 'My favorite vis'
}
})
.expect(200)
.then(resp => {
// loose uuid validation
expect(resp.body).to.have.property('id').match(/^[0-9a-f-]{36}$/);

// loose ISO8601 UTC time with milliseconds validation
expect(resp.body).to.have.property('updated_at').match(/^[\d-]{10}T[\d:\.]{12}Z$/);

expect(resp.body).to.eql({
id: resp.body.id,
type: 'visualization',
updated_at: resp.body.updated_at,
version: 1,
attributes: {
title: 'My favorite vis'
}
});
});
}

describe('create', () => {
describe('with kibana index', () => {
before(() => esArchiver.load('saved_objects/basic'));
after(() => esArchiver.unload('saved_objects/basic'));
it('should return 200', sendAndValidateCreate);
});

describe('without kibana index', () => {
before(async () => (
// just in case the kibana server has recreated it
await es.indices.delete({
index: '.kibana',
ignore: [404],
})
));

after(async () => (
// make sure to delete the invalid kibana index
await es.indices.delete({ index: '.kibana' })
));

it('should return 200 and create invalid kibana index', async () => {
// just in case the kibana server has recreated it
await es.indices.delete({
index: '.kibana',
ignore: [404],
});

await sendAndValidateCreate();

const index = await es.indices.get({
index: '.kibana'
});

const { mappings } = index['.kibana'];
expect(mappings).to.have.keys('doc');
expect(mappings.doc.properties).to.have.keys([
'type',
'updated_at',
'visualization'
]);
expect(mappings.doc.properties).to.not.have.keys([
'config',
'dashboard',
'index-pattern',
'search'
]);
});
});
});
}
Loading