-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
Changes from 24 commits
4b2f6cb
859a5ce
cf1b5a7
5032648
b99bf09
00aaac6
a93cae0
ec21fe4
3c6ee2f
de1a3fc
8d2e623
bcd3880
dae161d
058e66d
69272c7
4353cd2
7381d0c
56842cf
64e7bfb
73df8dd
ba595f8
d80cf10
a2c38e6
499af72
47dde37
36386dc
baa9e07
b976a21
aaf4bb4
4b683cb
eaa739f
977728f
f98c009
9b2f127
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, })}` | ||
); | ||
} | ||
|
||
/** | ||
|
@@ -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, { | ||
|
@@ -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, | ||
|
@@ -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, | ||
|
@@ -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 { | ||
|
@@ -307,6 +343,7 @@ export class SavedObjectsClient { | |
type: this._type, | ||
version: options.version, | ||
refresh: 'wait_for', | ||
ignore: [404], | ||
body: { | ||
doc: { | ||
updated_at: time, | ||
|
@@ -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, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,6 +55,7 @@ module.exports = function (grunt) { | |
...stdDevArgs, | ||
'--optimize.enabled=false', | ||
'--elasticsearch.url=' + esTestConfig.getUrl(), | ||
'--elasticsearch.healthCheck.delay=360000', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. question: just curious, why exactly 6 minutes? :) There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
|
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: [ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) There was a problem hiding this comment. Choose a reason for hiding this commentThe 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' | ||
} | ||
} | ||
] | ||
}); | ||
}) | ||
)); | ||
}); | ||
}); | ||
} |
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' | ||
]); | ||
}); | ||
}); | ||
}); | ||
} |
There was a problem hiding this comment.
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 :)
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, thanks!