Automatically create default index pattern

Change-Id: I50ed1d6abdc1dba93758f22d443a155c0cb37207
This commit is contained in:
Jakub Wachowski 2016-08-19 08:45:54 +02:00
parent aab355dae6
commit aa1b40a94d
11 changed files with 276 additions and 4 deletions

View File

@ -13,6 +13,7 @@ where keystone_port should be the keystone admin port (default: 35357) not the k
fts-keystone.port: ${keystone_port}
fts-keystone.url: http://${keystone_host}
fts-keystone.enabled: True
fts-keystone.defaultTimeField: '@timestamp'
```
To install the fts-keystone plugin, first the latest release should be downloaded:

View File

@ -49,6 +49,7 @@ export default (kibana) => {
.uri({scheme: ['http', 'https']})
.required(),
port : Joi.number().required(),
defaultTimeField: Joi.string().default('@timestamp'),
cookie : cookie
}).default();
}

View File

@ -1,6 +1,6 @@
{
"name": "fts-keystone",
"version": "0.0.2",
"version": "0.0.3",
"description": "Keystone authentication & multitenancy support for Kibana 4.4.x",
"author": "Fujitsu Enabling Software Technology GmbH",
"license": "Apache-2.0",

View File

@ -0,0 +1,128 @@
/*
* Copyright 2016 FUJITSU LIMITED
*
* Licensed 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.
*/
const chai = require('chai');
const sinon = require('sinon');
describe('plugins/fts-keystone', () => {
const indexName = '.kibana-testdefaultindex';
let server;
let userObj;
let configGet;
beforeEach(function () {
configGet = sinon.stub();
configGet.withArgs('pkg.version').returns('4.4.0');
configGet.withArgs('fts-keystone.defaultTimeField').returns('@timestamp');
server = {
log : sinon.stub(),
config: function () {
return {
get: configGet
};
},
plugins: {
elasticsearch: {
client: {
indices: {}
}
}
}
};
userObj = {
project: {
id: 'abcdef'
}
};
});
describe('defaultIndexPattern_exists', ()=> {
it('should return false if default pattern does not exist', (done) => {
let exists = require('../mt/kibana/defaultIndexPattern/_exists').default;
let count = sinon.stub();
count.returns(Promise.resolve({ count: 0 }));
server.plugins.elasticsearch.client.count = count;
exists(server, indexName)
.then((resp) => {
chai.assert.equal(resp, false);
chai.assert.isOk(count.calledOnce);
chai.assert.equal(count.args[0][0].index, '.kibana-testdefaultindex');
chai.assert.equal(count.args[0][0].type, 'index-pattern');
})
.then(done);
});
it('should return true if default pattern already exists', (done) => {
let patternExists = require('../mt/kibana/defaultIndexPattern/_exists').default;
let count = sinon.stub();
count.returns(Promise.resolve({ count: 1 }));
server.plugins.elasticsearch.client.count = count;
patternExists(server, indexName)
.then((resp) => {
chai.assert.equal(resp, true);
chai.assert.isOk(count.calledOnce);
chai.assert.equal(count.args[0][0].index, '.kibana-testdefaultindex');
chai.assert.equal(count.args[0][0].type, 'index-pattern');
})
.then(done);
});
});
describe('defaultIndexPattern_create', () => {
it('should create pattern with proper value', (done) => {
let createPattern = require('../mt/kibana/defaultIndexPattern/_create').default;
let create = sinon.stub();
create.returns(Promise.resolve(true));
server.plugins.elasticsearch.client.create = create;
let update = sinon.stub();
update.returns(Promise.resolve(true));
server.plugins.elasticsearch.client.update = update;
let refresh = sinon.stub();
refresh.returns(Promise.resolve(true));
server.plugins.elasticsearch.client.indices.refresh = refresh;
createPattern(server, indexName, userObj)
.then((resp) => {
chai.assert.isOk(create.calledOnce);
chai.assert.equal(create.args[0][0].index, '.kibana-testdefaultindex');
chai.assert.equal(create.args[0][0].type, 'index-pattern');
chai.assert.equal(create.args[0][0].id, 'abcdef*');
chai.assert.equal(create.args[0][0].body.title, 'abcdef*');
chai.assert.equal(create.args[0][0].body.timeFieldName, '@timestamp');
chai.assert.isOk(update.calledOnce);
chai.assert.equal(update.args[0][0].index, '.kibana-testdefaultindex');
chai.assert.equal(update.args[0][0].type, 'config');
chai.assert.equal(update.args[0][0].body.doc.defaultIndex, 'abcdef*');
chai.assert.isOk(refresh.called);
chai.assert.equal(refresh.args[0][0].index, '.kibana-testdefaultindex');
})
.then(done);
});
});
});

View File

@ -92,8 +92,6 @@ describe('plugins/fts-keystone', ()=> {
chai.expect(token).not.to.be.undefined;
chai.expect(token).to.be.eql(expectedToken);
console.log(yar.get.callCount);
chai.expect(yar.get.callCount).to.be.eq(2);
chai.expect(yar.set.calledOnce).not.to.be.ok;
chai.expect(

View File

@ -16,6 +16,7 @@ import Boom from 'boom';
import Promise from 'bluebird';
import kibanaIndex from '../kibana';
import defaultIndexPattern from '../kibana/defaultIndexPattern';
import userProjects from '../projects';
import {
@ -40,6 +41,7 @@ export default (server) => {
userProjects(server, session, userObj),
kibanaIndex(server, userObj)
])
.then(defaultIndexPattern(server, userObj))
.then(() => {
server.log(['status', 'info', 'keystone'], `User ${userObj.user.id} authorized with keystone`);
return token;

View File

@ -36,6 +36,22 @@ export default (server, indexName) => {
index: 'not_analyzed'
}
}
},
'index-pattern': {
properties: {
title: {
type: 'string'
},
timeFieldName: {
type: 'string'
},
notExpandable: {
type: 'boolean'
},
intervalName: {
type: 'string'
}
}
}
}
}

View File

@ -31,7 +31,7 @@ export function exists(server, indexName, status) {
waitForActiveShards: 1
};
if (status) {
opts.status = status;
opts.waitForStatus = status;
}
return es.cluster.health(opts);
}

View File

@ -0,0 +1,56 @@
/*
* Copyright 2016 FUJITSU LIMITED
*
* Licensed 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 Promise from 'bluebird';
export default (server, indexName, userObj) => {
server.log(['status', 'info', 'keystone'], `Creating default index pattern for ${indexName}`);
const client = server.plugins.elasticsearch.client;
const pattern = `${userObj.project.id}*`;
return client.create({
index: indexName,
type : 'index-pattern',
body : {
title: pattern,
timeFieldName : server.config().get('fts-keystone.defaultTimeField')
},
id : pattern
})
.then(() => {
return client.update({
index: indexName,
type: 'config',
id: server.config().get('pkg.version'),
body: {
doc: {
defaultIndex: pattern
}
}
});
})
.then(() => {
return client.indices.refresh({
index: indexName,
force: true
});
})
.then((response) => {
return Promise.resolve(response);
})
.catch((err)=> {
throw new Error(`Unable to setup index pattern, error is ${err}`);
});
};

View File

@ -0,0 +1,35 @@
/*
* Copyright 2016 FUJITSU LIMITED
*
* Licensed 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.
*/
export default (server, indexName) => {
const client = server.plugins.elasticsearch.client;
const options = {
index: indexName,
type : 'index-pattern',
ignoreUnavailable: true
};
server.log(['status', 'debug', 'keystone'],
`Checking if default index pattern for ${indexName} exists...`);
return client
.count(options)
.then((resp) => {
return resp.count > 0;
})
.catch((err)=> {
throw new Error(`Getting index-pattern for ${indexName} failed, error is ${err}`);
});
};

View File

@ -0,0 +1,35 @@
/*
* Copyright 2016 FUJITSU LIMITED
*
* Licensed 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 Promise from 'bluebird';
import defaultIndexExists from './_exists';
import createDefaultIndex from './_create';
import kibanaIndex from '../kibanaIndex';
export default (server, userObj) => {
return () => {
const indexName = kibanaIndex(server, userObj);
return defaultIndexExists(server, indexName)
.then((exists) => {
if (!exists) {
server.log(['status', 'warning', 'keystone'],
`Default index pattern for ${indexName} does not exist`);
return createDefaultIndex(server, indexName, userObj);
}
server.log(['status', 'debug', 'keystone'],
`Default index pattern for ${indexName} already exists`);
return Promise.resolve();
});
};
};