2016-11-21 20:22:14 +00:00
|
|
|
import { appendTargetTypeToTargets } from 'redux/nodes/entities/targets/helpers';
|
|
|
|
import Base from 'kolide/base';
|
|
|
|
import endpoints from 'kolide/endpoints';
|
|
|
|
import helpers from 'kolide/helpers';
|
2016-12-21 17:07:13 +00:00
|
|
|
import local from 'utilities/local';
|
2016-09-13 19:50:37 +00:00
|
|
|
|
2016-09-16 13:55:46 +00:00
|
|
|
class Kolide extends Base {
|
2016-12-21 17:25:54 +00:00
|
|
|
addLabelToPack = (packID, labelID) => {
|
|
|
|
const path = `/v1/kolide/packs/${packID}/labels/${labelID}`;
|
|
|
|
|
|
|
|
return this.authenticatedPost(this.endpoint(path));
|
|
|
|
}
|
|
|
|
|
|
|
|
addQueryToPack = ({ packID, queryID }) => {
|
|
|
|
const endpoint = `/v1/kolide/packs/${packID}/queries/${queryID}`;
|
|
|
|
|
|
|
|
return this.authenticatedPost(this.endpoint(endpoint));
|
|
|
|
}
|
|
|
|
|
2016-11-17 17:12:41 +00:00
|
|
|
createLabel = ({ description, name, query }) => {
|
|
|
|
const { LABELS } = endpoints;
|
|
|
|
|
|
|
|
return this.authenticatedPost(this.endpoint(LABELS), JSON.stringify({ description, name, query }))
|
|
|
|
.then((response) => {
|
2016-12-12 16:48:50 +00:00
|
|
|
const { label } = response;
|
|
|
|
|
2016-11-17 17:12:41 +00:00
|
|
|
return {
|
2016-12-12 16:48:50 +00:00
|
|
|
...label,
|
|
|
|
slug: helpers.labelSlug(label),
|
2016-11-17 17:12:41 +00:00
|
|
|
type: 'custom',
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-12-21 17:25:54 +00:00
|
|
|
createPack = ({ name, description }) => {
|
|
|
|
const { PACKS } = endpoints;
|
|
|
|
|
|
|
|
return this.authenticatedPost(this.endpoint(PACKS), JSON.stringify({ description, name }))
|
|
|
|
.then((response) => { return response.pack; });
|
|
|
|
}
|
|
|
|
|
2016-11-07 16:42:39 +00:00
|
|
|
createQuery = ({ description, name, query }) => {
|
|
|
|
const { QUERIES } = endpoints;
|
|
|
|
|
|
|
|
return this.authenticatedPost(this.endpoint(QUERIES), JSON.stringify({ description, name, query }))
|
|
|
|
.then((response) => { return response.query; });
|
|
|
|
}
|
|
|
|
|
2016-12-21 17:25:54 +00:00
|
|
|
createScheduledQuery = ({ interval, logging_type: loggingType, pack_id: packID, platform, query_id: queryID, version }) => {
|
|
|
|
const removed = loggingType === 'differential';
|
|
|
|
const snapshot = loggingType === 'snapshot';
|
|
|
|
|
|
|
|
const formData = {
|
|
|
|
interval: Number(interval),
|
|
|
|
pack_id: Number(packID),
|
|
|
|
platform,
|
|
|
|
query_id: Number(queryID),
|
|
|
|
removed,
|
|
|
|
snapshot,
|
|
|
|
version,
|
|
|
|
};
|
|
|
|
|
|
|
|
return this.authenticatedPost(this.endpoint('/v1/kolide/schedule'), JSON.stringify(formData))
|
|
|
|
.then(response => response.scheduled);
|
|
|
|
}
|
|
|
|
|
2017-01-06 20:26:58 +00:00
|
|
|
destroyQuery = ({ id }) => {
|
|
|
|
const { QUERIES } = endpoints;
|
|
|
|
const endpoint = `${this.endpoint(QUERIES)}/${id}`;
|
|
|
|
|
|
|
|
return this.authenticatedDelete(endpoint);
|
|
|
|
}
|
|
|
|
|
2017-01-03 20:56:50 +00:00
|
|
|
destroyPack = ({ id }) => {
|
|
|
|
const { PACKS } = endpoints;
|
|
|
|
const endpoint = `${this.endpoint(PACKS)}/${id}`;
|
|
|
|
|
|
|
|
return this.authenticatedDelete(endpoint);
|
|
|
|
}
|
|
|
|
|
2016-12-21 17:25:54 +00:00
|
|
|
destroyScheduledQuery = ({ id }) => {
|
|
|
|
const endpoint = `${this.endpoint('/v1/kolide/schedule')}/${id}`;
|
|
|
|
|
|
|
|
return this.authenticatedDelete(endpoint);
|
|
|
|
}
|
|
|
|
|
2016-12-29 20:27:43 +00:00
|
|
|
createUser = (formData) => {
|
|
|
|
const { USERS } = endpoints;
|
|
|
|
|
|
|
|
return this.authenticatedPost(this.endpoint(USERS), JSON.stringify(formData))
|
|
|
|
.then((response) => { return response.user; });
|
|
|
|
}
|
|
|
|
|
2016-09-19 23:43:35 +00:00
|
|
|
forgotPassword ({ email }) {
|
|
|
|
const { FORGOT_PASSWORD } = endpoints;
|
|
|
|
const forgotPasswordEndpoint = this.baseURL + FORGOT_PASSWORD;
|
|
|
|
|
2016-10-21 23:13:41 +00:00
|
|
|
return Base.post(forgotPasswordEndpoint, JSON.stringify({ email }));
|
2016-09-19 23:43:35 +00:00
|
|
|
}
|
|
|
|
|
2016-10-05 23:43:37 +00:00
|
|
|
getConfig = () => {
|
|
|
|
const { CONFIG } = endpoints;
|
|
|
|
|
2016-12-23 18:40:16 +00:00
|
|
|
return this.authenticatedGet(this.endpoint(CONFIG));
|
2016-10-05 23:43:37 +00:00
|
|
|
}
|
|
|
|
|
2016-10-14 21:08:57 +00:00
|
|
|
getInvites = () => {
|
|
|
|
const { INVITES } = endpoints;
|
|
|
|
|
|
|
|
return this.authenticatedGet(this.endpoint(INVITES))
|
2016-12-15 15:00:44 +00:00
|
|
|
.then((response) => {
|
|
|
|
const { invites } = response;
|
|
|
|
|
|
|
|
return invites.map((invite) => {
|
|
|
|
return helpers.addGravatarUrlToResource(invite);
|
|
|
|
});
|
|
|
|
});
|
2016-10-14 21:08:57 +00:00
|
|
|
}
|
|
|
|
|
2016-10-17 18:55:03 +00:00
|
|
|
getHosts = () => {
|
|
|
|
const { HOSTS } = endpoints;
|
|
|
|
|
|
|
|
return this.authenticatedGet(this.endpoint(HOSTS))
|
2016-10-21 23:13:41 +00:00
|
|
|
.then((response) => { return response.hosts; });
|
2016-10-17 18:55:03 +00:00
|
|
|
}
|
|
|
|
|
2016-10-27 16:14:30 +00:00
|
|
|
getLabelHosts = (labelID) => {
|
|
|
|
const { LABEL_HOSTS } = endpoints;
|
|
|
|
console.log(LABEL_HOSTS(labelID));
|
|
|
|
|
|
|
|
const stubbedResponse = {
|
|
|
|
hosts: [
|
|
|
|
{
|
|
|
|
detail_updated_at: '2016-10-25T16:24:27.679472917-04:00',
|
|
|
|
hostname: 'jmeller-mbp.local',
|
|
|
|
id: 1,
|
|
|
|
ip: '192.168.1.10',
|
|
|
|
mac: '10:11:12:13:14:15',
|
|
|
|
memory: 4145483776,
|
|
|
|
os_version: 'Mac OS X 10.11.6',
|
|
|
|
osquery_version: '2.0.0',
|
|
|
|
platform: 'darwin',
|
|
|
|
status: 'online',
|
|
|
|
updated_at: '0001-01-01T00:00:00Z',
|
|
|
|
uptime: 3600000000000,
|
|
|
|
uuid: '1234-5678-9101',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
detail_updated_at: '2016-10-25T16:24:27.679472917-04:00',
|
|
|
|
hostname: 'Jason Meller\'s Windows Note',
|
|
|
|
id: 2,
|
|
|
|
ip: '192.168.1.11',
|
|
|
|
mac: '0C-BA-8D-45-FD-B9',
|
|
|
|
memory: 4145483776,
|
|
|
|
os_version: 'Windows Vista 0.0.1',
|
|
|
|
osquery_version: '2.0.0',
|
|
|
|
platform: 'windows',
|
|
|
|
status: 'offline',
|
|
|
|
updated_at: '0001-01-01T00:00:00Z',
|
|
|
|
uptime: 3600000000000,
|
|
|
|
uuid: '1234-5678-9101',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
return Promise.resolve(stubbedResponse)
|
|
|
|
.then((response) => { return response.hosts; });
|
|
|
|
}
|
|
|
|
|
2016-12-21 17:25:54 +00:00
|
|
|
getPack = (packID) => {
|
|
|
|
const { PACKS } = endpoints;
|
|
|
|
const getPackEndpoint = `${this.baseURL}${PACKS}/${packID}`;
|
|
|
|
|
|
|
|
return this.authenticatedGet(getPackEndpoint)
|
|
|
|
.then((response) => { return response.pack; });
|
|
|
|
}
|
|
|
|
|
2016-11-07 16:42:39 +00:00
|
|
|
getQuery = (queryID) => {
|
|
|
|
const { QUERIES } = endpoints;
|
|
|
|
const getQueryEndpoint = `${this.baseURL}${QUERIES}/${queryID}`;
|
|
|
|
|
|
|
|
return this.authenticatedGet(getQueryEndpoint)
|
|
|
|
.then((response) => { return response.query; });
|
|
|
|
}
|
|
|
|
|
2016-12-21 17:25:54 +00:00
|
|
|
getQueries = () => {
|
|
|
|
const { QUERIES } = endpoints;
|
|
|
|
|
|
|
|
return this.authenticatedGet(this.endpoint(QUERIES))
|
|
|
|
.then((response) => { return response.queries; });
|
|
|
|
}
|
|
|
|
|
2016-11-09 18:08:00 +00:00
|
|
|
getTargets = (query, selected = { hosts: [], labels: [] }) => {
|
|
|
|
const { TARGETS } = endpoints;
|
2016-10-27 16:14:30 +00:00
|
|
|
|
2016-11-09 18:08:00 +00:00
|
|
|
return this.authenticatedPost(this.endpoint(TARGETS), JSON.stringify({ query, selected }))
|
2016-11-14 17:32:13 +00:00
|
|
|
.then((response) => {
|
|
|
|
const { targets } = response;
|
|
|
|
|
|
|
|
return {
|
|
|
|
...response,
|
|
|
|
targets: [
|
|
|
|
...appendTargetTypeToTargets(targets.hosts, 'hosts'),
|
|
|
|
...appendTargetTypeToTargets(targets.labels, 'labels'),
|
|
|
|
],
|
|
|
|
};
|
|
|
|
});
|
2016-10-27 16:14:30 +00:00
|
|
|
}
|
|
|
|
|
2016-11-17 17:12:41 +00:00
|
|
|
getLabels = () => {
|
|
|
|
const { LABELS } = endpoints;
|
|
|
|
|
|
|
|
return this.authenticatedGet(this.endpoint(LABELS))
|
|
|
|
.then((response) => {
|
2016-12-12 16:48:50 +00:00
|
|
|
const labelTypeForDisplayText = {
|
|
|
|
'All Hosts': 'all',
|
|
|
|
'MS Windows': 'platform',
|
|
|
|
'CentOS Linux': 'platform',
|
|
|
|
'Mac OS X': 'platform',
|
|
|
|
'Ubuntu Linux': 'platform',
|
|
|
|
};
|
2016-11-17 17:12:41 +00:00
|
|
|
const labels = response.labels.map((label) => {
|
2016-12-12 16:48:50 +00:00
|
|
|
return {
|
|
|
|
...label,
|
|
|
|
slug: helpers.labelSlug(label),
|
|
|
|
type: labelTypeForDisplayText[label.display_text] || 'custom',
|
|
|
|
};
|
2016-11-17 17:12:41 +00:00
|
|
|
});
|
|
|
|
const stubbedLabels = [
|
2016-12-21 17:07:13 +00:00
|
|
|
{ id: 40, display_text: 'ONLINE', type: 'status', count: 20 },
|
|
|
|
{ id: 50, display_text: 'OFFLINE', type: 'status', count: 2 },
|
|
|
|
{ id: 55, display_text: 'MIA', description: '(offline > 30 days)', type: 'status', count: 3 },
|
2016-11-17 17:12:41 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
return labels.concat(stubbedLabels);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-11-21 19:49:36 +00:00
|
|
|
getPacks = () => {
|
|
|
|
const { PACKS } = endpoints;
|
|
|
|
|
|
|
|
return this.authenticatedGet(this.endpoint(PACKS))
|
|
|
|
.then((response) => { return response.packs; });
|
|
|
|
}
|
|
|
|
|
2016-12-21 17:25:54 +00:00
|
|
|
getScheduledQueries = (pack) => {
|
|
|
|
const { SCHEDULED_QUERIES } = endpoints;
|
|
|
|
const scheduledQueryPath = SCHEDULED_QUERIES(pack);
|
|
|
|
|
|
|
|
return this.authenticatedGet(this.endpoint(scheduledQueryPath))
|
|
|
|
.then(response => response.scheduled);
|
|
|
|
}
|
|
|
|
|
2016-10-03 17:54:22 +00:00
|
|
|
getUsers = () => {
|
|
|
|
const { USERS } = endpoints;
|
|
|
|
|
|
|
|
return this.authenticatedGet(this.endpoint(USERS))
|
2016-12-15 15:00:44 +00:00
|
|
|
.then((response) => {
|
|
|
|
const { users } = response;
|
|
|
|
|
|
|
|
return users.map((user) => {
|
|
|
|
return helpers.addGravatarUrlToResource(user);
|
|
|
|
});
|
|
|
|
});
|
2016-10-03 17:54:22 +00:00
|
|
|
}
|
|
|
|
|
2016-10-11 15:34:52 +00:00
|
|
|
inviteUser = (formData) => {
|
|
|
|
const { INVITES } = endpoints;
|
|
|
|
|
2016-10-14 21:08:57 +00:00
|
|
|
return this.authenticatedPost(this.endpoint(INVITES), JSON.stringify(formData))
|
2016-12-15 15:00:44 +00:00
|
|
|
.then((response) => {
|
|
|
|
const { invite } = response;
|
|
|
|
|
|
|
|
return helpers.addGravatarUrlToResource(invite);
|
|
|
|
});
|
2016-10-11 15:34:52 +00:00
|
|
|
}
|
|
|
|
|
2016-09-13 19:50:37 +00:00
|
|
|
loginUser ({ username, password }) {
|
|
|
|
const { LOGIN } = endpoints;
|
2016-09-16 13:55:46 +00:00
|
|
|
const loginEndpoint = this.baseURL + LOGIN;
|
2016-09-13 19:50:37 +00:00
|
|
|
|
2016-12-15 15:00:44 +00:00
|
|
|
return Base.post(loginEndpoint, JSON.stringify({ username, password }))
|
|
|
|
.then((response) => {
|
|
|
|
const { user } = response;
|
|
|
|
const userWithGravatarUrl = helpers.addGravatarUrlToResource(user);
|
|
|
|
|
|
|
|
return {
|
|
|
|
...response,
|
|
|
|
user: userWithGravatarUrl,
|
|
|
|
};
|
|
|
|
});
|
2016-09-16 13:55:46 +00:00
|
|
|
}
|
2016-09-13 19:50:37 +00:00
|
|
|
|
2016-09-19 23:43:35 +00:00
|
|
|
logout () {
|
|
|
|
const { LOGOUT } = endpoints;
|
|
|
|
const logoutEndpoint = this.baseURL + LOGOUT;
|
2016-09-13 19:50:37 +00:00
|
|
|
|
2016-09-19 23:43:35 +00:00
|
|
|
return this.authenticatedPost(logoutEndpoint);
|
|
|
|
}
|
|
|
|
|
|
|
|
me () {
|
|
|
|
const { ME } = endpoints;
|
|
|
|
const meEndpoint = this.baseURL + ME;
|
|
|
|
|
2016-12-15 15:00:44 +00:00
|
|
|
return this.authenticatedGet(meEndpoint)
|
|
|
|
.then((response) => {
|
|
|
|
const { user } = response;
|
|
|
|
|
|
|
|
return helpers.addGravatarUrlToResource(user);
|
|
|
|
});
|
2016-09-13 19:50:37 +00:00
|
|
|
}
|
2016-09-19 15:35:38 +00:00
|
|
|
|
|
|
|
resetPassword (formData) {
|
|
|
|
const { RESET_PASSWORD } = endpoints;
|
|
|
|
const resetPasswordEndpoint = this.baseURL + RESET_PASSWORD;
|
|
|
|
|
2016-10-21 23:13:41 +00:00
|
|
|
return Base.post(resetPasswordEndpoint, JSON.stringify(formData));
|
2016-09-19 15:35:38 +00:00
|
|
|
}
|
2016-10-03 17:54:22 +00:00
|
|
|
|
2017-01-06 00:13:28 +00:00
|
|
|
revokeInvite = ({ id }) => {
|
2016-10-14 21:08:57 +00:00
|
|
|
const { INVITES } = endpoints;
|
2017-01-06 00:13:28 +00:00
|
|
|
const endpoint = `${this.endpoint(INVITES)}/${id}`;
|
2016-10-14 21:08:57 +00:00
|
|
|
|
|
|
|
return this.authenticatedDelete(endpoint);
|
|
|
|
}
|
|
|
|
|
2016-12-21 17:07:13 +00:00
|
|
|
runQuery = ({ query, selected }) => {
|
|
|
|
const { RUN_QUERY } = endpoints;
|
|
|
|
|
|
|
|
return this.authenticatedPost(this.endpoint(RUN_QUERY), JSON.stringify({ query, selected }))
|
|
|
|
.then(response => response.campaign);
|
|
|
|
}
|
|
|
|
|
|
|
|
runQueryWebsocket = (campaignID) => {
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
const socket = new global.WebSocket(`${this.websocketBaseURL}/v1/kolide/results/${campaignID}`);
|
|
|
|
|
|
|
|
socket.onopen = () => {
|
|
|
|
socket.send(JSON.stringify({ type: 'auth', data: { token: local.getItem('auth_token') } }));
|
|
|
|
};
|
|
|
|
|
|
|
|
return resolve(socket);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-11-21 20:22:14 +00:00
|
|
|
setup = (formData) => {
|
|
|
|
const { SETUP } = endpoints;
|
|
|
|
const setupData = helpers.setupData(formData);
|
|
|
|
|
|
|
|
return Base.post(this.endpoint(SETUP), JSON.stringify(setupData));
|
|
|
|
}
|
|
|
|
|
2016-12-23 18:40:16 +00:00
|
|
|
updateConfig = (formData) => {
|
|
|
|
const { CONFIG } = endpoints;
|
|
|
|
const configData = helpers.formatConfigDataForServer(formData);
|
|
|
|
|
|
|
|
return this.authenticatedPatch(this.endpoint(CONFIG), JSON.stringify(configData));
|
|
|
|
}
|
|
|
|
|
2017-01-03 20:56:50 +00:00
|
|
|
updatePack = ({ id: packID }, updateParams) => {
|
|
|
|
const { PACKS } = endpoints;
|
|
|
|
const updatePackEndpoint = `${this.baseURL}${PACKS}/${packID}`;
|
|
|
|
|
|
|
|
return this.authenticatedPatch(updatePackEndpoint, JSON.stringify(updateParams))
|
|
|
|
.then((response) => { return response.pack; });
|
|
|
|
}
|
|
|
|
|
2016-11-08 14:23:25 +00:00
|
|
|
updateQuery = ({ id: queryID }, updateParams) => {
|
|
|
|
const { QUERIES } = endpoints;
|
|
|
|
const updateQueryEndpoint = `${this.baseURL}${QUERIES}/${queryID}`;
|
|
|
|
|
|
|
|
return this.authenticatedPatch(updateQueryEndpoint, JSON.stringify(updateParams))
|
|
|
|
.then((response) => { return response.query; });
|
|
|
|
}
|
|
|
|
|
2016-10-03 17:54:22 +00:00
|
|
|
updateUser = (user, formData) => {
|
|
|
|
const { USERS } = endpoints;
|
|
|
|
const updateUserEndpoint = `${this.baseURL}${USERS}/${user.id}`;
|
|
|
|
|
2016-10-11 16:22:11 +00:00
|
|
|
return this.authenticatedPatch(updateUserEndpoint, JSON.stringify(formData))
|
2016-12-15 15:00:44 +00:00
|
|
|
.then((response) => {
|
|
|
|
const { user: updatedUser } = response;
|
|
|
|
|
|
|
|
return helpers.addGravatarUrlToResource(updatedUser);
|
|
|
|
});
|
2016-10-03 17:54:22 +00:00
|
|
|
}
|
2017-01-06 22:38:39 +00:00
|
|
|
|
|
|
|
requirePasswordReset = (user, { require }) => {
|
|
|
|
const { USERS } = endpoints;
|
|
|
|
const requirePasswordResetEndpoint = this.endpoint(`${USERS}/${user.id}/require_password_reset`);
|
|
|
|
|
|
|
|
return this.authenticatedPost(requirePasswordResetEndpoint, JSON.stringify({ require }))
|
|
|
|
.then((response) => {
|
|
|
|
const { user: updatedUser } = response;
|
|
|
|
return helpers.addGravatarUrlToResource(updatedUser);
|
|
|
|
});
|
|
|
|
}
|
2016-09-13 19:50:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default new Kolide();
|