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

Adding system API module #8097

Merged
merged 5 commits into from
Sep 1, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 3 additions & 0 deletions src/core_plugins/kibana/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import ingest from './server/routes/api/ingest';
import search from './server/routes/api/search';
import settings from './server/routes/api/settings';
import scripts from './server/routes/api/scripts';
import * as systemApi from './server/lib/system_api';

module.exports = function (kibana) {
const kbnBaseUrl = '/app/kibana';
Expand Down Expand Up @@ -92,6 +93,8 @@ module.exports = function (kibana) {
search(server);
settings(server);
scripts(server);

server.expose('systemApi', systemApi);
}
});

Expand Down
22 changes: 22 additions & 0 deletions src/core_plugins/kibana/server/lib/__tests__/system_api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import expect from 'expect.js';
import { isSystemApiRequest } from '../system_api';

describe('system_api', () => {
describe('#isSystemApiRequest', () => {
it ('returns true for a system API HTTP request', () => {
const mockHapiRequest = {
headers: {
'kbn-system-api': true
}
};
expect(isSystemApiRequest(mockHapiRequest)).to.be(true);
});

it ('returns false for a non-system API HTTP request', () => {
const mockHapiRequest = {
headers: {}
};
expect(isSystemApiRequest(mockHapiRequest)).to.be(false);
});
});
});
11 changes: 11 additions & 0 deletions src/core_plugins/kibana/server/lib/system_api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const SYSTEM_API_HEADER_NAME = 'kbn-system-api';

/**
* Checks on the *server-side*, if an HTTP request is a system API request
*
* @param request HAPI request object
* @return true if request is a system API request; false, otherwise
*/
export function isSystemApiRequest(request) {
return !!request.headers[SYSTEM_API_HEADER_NAME];
}
6 changes: 6 additions & 0 deletions src/ui/public/chrome/directives/kbn_chrome.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import $ from 'jquery';
import { remove } from 'lodash';

import './kbn_chrome.less';
import UiModules from 'ui/modules';
import { isSystemApiRequest } from 'ui/system_api';

export default function (chrome, internals) {

Expand Down Expand Up @@ -41,6 +43,10 @@ export default function (chrome, internals) {
$rootScope.$on('$routeUpdate', onRouteChange);
onRouteChange();

const allPendingHttpRequests = () => $http.pendingRequests;
const removeSystemApiRequests = (pendingHttpRequests = []) => remove(pendingHttpRequests, isSystemApiRequest);
$scope.$watchCollection(allPendingHttpRequests, removeSystemApiRequests);

// and some local values
chrome.httpActive = $http.pendingRequests;
$scope.notifList = require('ui/notify')._notifs;
Expand Down
38 changes: 38 additions & 0 deletions src/ui/public/system_api/__tests__/system_api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import expect from 'expect.js';
import ngMock from 'ng_mock';
import { addSystemApiHeader, isSystemApiRequest } from '../system_api';

describe('system_api', () => {
describe('#addSystemApiHeader', () => {
it ('adds the correct system API header', () => {
const headers = {
'kbn-version': '4.6.0'
};
const newHeaders = addSystemApiHeader(headers);

expect(newHeaders).to.have.property('kbn-system-api');
expect(newHeaders['kbn-system-api']).to.be(true);

expect(newHeaders).to.have.property('kbn-version');
expect(newHeaders['kbn-version']).to.be('4.6.0');
});
});

describe('#isSystemApiRequest', () => {
it ('returns true for a system API HTTP request', () => {
const mockRequest = {
headers: {
'kbn-system-api': true
}
};
expect(isSystemApiRequest(mockRequest)).to.be(true);
});

it ('returns false for a non-system API HTTP request', () => {
const mockRequest = {
headers: {}
};
expect(isSystemApiRequest(mockRequest)).to.be(false);
});
});
});
26 changes: 26 additions & 0 deletions src/ui/public/system_api/system_api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const SYSTEM_API_HEADER_NAME = 'kbn-system-api';

/**
* Adds a custom header designating request as system API
* @param originalHeaders Object representing set of headers
* @return Object representing set of headers, with system API header added in
*/
export function addSystemApiHeader(originalHeaders) {
const systemApiHeaders = {
[SYSTEM_API_HEADER_NAME]: true
};
return {
...originalHeaders,
...systemApiHeaders
};
}

/**
* Returns true if request is a system API request; false otherwise
*
* @param request Object Request object created by $http service
* @return true if request is a system API request; false otherwise
*/
export function isSystemApiRequest(request) {
return !!request.headers[SYSTEM_API_HEADER_NAME];
}