2016-10-17 18:55:03 +00:00
|
|
|
import React, { Component, PropTypes } from 'react';
|
2016-11-17 17:12:41 +00:00
|
|
|
import AceEditor from 'react-ace';
|
2016-10-17 18:55:03 +00:00
|
|
|
import { connect } from 'react-redux';
|
2016-12-05 15:21:03 +00:00
|
|
|
import { push } from 'react-router-redux';
|
2017-01-13 01:18:23 +00:00
|
|
|
import { orderBy, sortBy } from 'lodash';
|
2016-10-17 18:55:03 +00:00
|
|
|
|
2016-11-17 17:12:41 +00:00
|
|
|
import entityGetter from 'redux/utilities/entityGetter';
|
|
|
|
import hostActions from 'redux/nodes/entities/hosts/actions';
|
|
|
|
import labelActions from 'redux/nodes/entities/labels/actions';
|
|
|
|
import labelInterface from 'interfaces/label';
|
|
|
|
import HostDetails from 'components/hosts/HostDetails';
|
|
|
|
import hostInterface from 'interfaces/host';
|
|
|
|
import HostSidePanel from 'components/side_panels/HostSidePanel';
|
2016-11-21 16:26:58 +00:00
|
|
|
import HostsTable from 'components/hosts/HostsTable';
|
2016-12-22 19:26:18 +00:00
|
|
|
import Icon from 'components/icons/Icon';
|
2016-11-17 17:12:41 +00:00
|
|
|
import osqueryTableInterface from 'interfaces/osquery_table';
|
2016-12-12 16:48:50 +00:00
|
|
|
import paths from 'router/paths';
|
2017-01-06 00:01:17 +00:00
|
|
|
import QueryForm from 'components/forms/queries/QueryForm';
|
2016-11-17 17:12:41 +00:00
|
|
|
import QuerySidePanel from 'components/side_panels/QuerySidePanel';
|
2016-11-21 16:26:58 +00:00
|
|
|
import Rocker from 'components/buttons/Rocker';
|
2016-11-17 17:12:41 +00:00
|
|
|
import { selectOsqueryTable } from 'redux/nodes/components/QueryPages/actions';
|
2016-12-12 16:48:50 +00:00
|
|
|
import { setDisplay } from 'redux/nodes/components/ManageHostsPage/actions';
|
2016-12-15 14:44:45 +00:00
|
|
|
import iconClassForLabel from 'utilities/icon_class_for_label';
|
2016-10-17 18:55:03 +00:00
|
|
|
|
2016-12-05 15:21:03 +00:00
|
|
|
const NEW_LABEL_HASH = '#new_label';
|
2016-12-15 14:44:45 +00:00
|
|
|
const baseClass = 'manage-hosts';
|
2016-12-05 15:21:03 +00:00
|
|
|
|
2016-11-17 17:12:41 +00:00
|
|
|
export class ManageHostsPage extends Component {
|
2016-10-17 18:55:03 +00:00
|
|
|
static propTypes = {
|
|
|
|
dispatch: PropTypes.func,
|
2016-11-21 16:26:58 +00:00
|
|
|
display: PropTypes.oneOf(['Grid', 'List']),
|
2016-10-21 23:13:41 +00:00
|
|
|
hosts: PropTypes.arrayOf(hostInterface),
|
2016-12-05 15:21:03 +00:00
|
|
|
isAddLabel: PropTypes.bool,
|
2017-01-06 00:01:17 +00:00
|
|
|
labelErrors: PropTypes.shape({
|
|
|
|
base: PropTypes.string,
|
|
|
|
}),
|
2016-11-17 17:12:41 +00:00
|
|
|
labels: PropTypes.arrayOf(labelInterface),
|
|
|
|
selectedLabel: labelInterface,
|
|
|
|
selectedOsqueryTable: osqueryTableInterface,
|
2016-10-17 18:55:03 +00:00
|
|
|
};
|
|
|
|
|
2016-11-21 16:26:58 +00:00
|
|
|
static defaultProps = {
|
|
|
|
display: 'Grid',
|
|
|
|
};
|
|
|
|
|
2016-11-17 17:12:41 +00:00
|
|
|
constructor (props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
labelQueryText: '',
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-10-17 18:55:03 +00:00
|
|
|
componentWillMount () {
|
2016-11-17 17:12:41 +00:00
|
|
|
const {
|
|
|
|
dispatch,
|
|
|
|
hosts,
|
|
|
|
labels,
|
|
|
|
} = this.props;
|
|
|
|
|
2016-10-19 20:22:18 +00:00
|
|
|
if (!hosts.length) {
|
|
|
|
dispatch(hostActions.loadAll());
|
|
|
|
}
|
2016-10-17 18:55:03 +00:00
|
|
|
|
2016-11-17 17:12:41 +00:00
|
|
|
if (!labels.length) {
|
|
|
|
dispatch(labelActions.loadAll());
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
onCancelAddLabel = () => {
|
2016-12-05 15:21:03 +00:00
|
|
|
const { dispatch } = this.props;
|
|
|
|
|
|
|
|
dispatch(push('/hosts/manage'));
|
2016-11-17 17:12:41 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
onAddLabelClick = (evt) => {
|
|
|
|
evt.preventDefault();
|
|
|
|
|
2016-12-05 15:21:03 +00:00
|
|
|
const { dispatch } = this.props;
|
|
|
|
|
|
|
|
dispatch(push(`/hosts/manage${NEW_LABEL_HASH}`));
|
2016-11-17 17:12:41 +00:00
|
|
|
|
2016-10-17 18:55:03 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
onHostDetailActionClick = (type) => {
|
|
|
|
return (host) => {
|
|
|
|
return (evt) => {
|
|
|
|
evt.preventDefault();
|
|
|
|
|
|
|
|
console.log(type, host);
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-11-17 17:12:41 +00:00
|
|
|
onLabelClick = (selectedLabel) => {
|
|
|
|
return (evt) => {
|
|
|
|
evt.preventDefault();
|
|
|
|
|
|
|
|
const { dispatch } = this.props;
|
2016-12-12 16:48:50 +00:00
|
|
|
const { MANAGE_HOSTS } = paths;
|
|
|
|
const { slug } = selectedLabel;
|
|
|
|
const nextLocation = slug === 'all-hosts' ? MANAGE_HOSTS : `${MANAGE_HOSTS}/${slug}`;
|
2016-11-17 17:12:41 +00:00
|
|
|
|
2016-12-12 16:48:50 +00:00
|
|
|
dispatch(push(nextLocation));
|
2016-11-17 17:12:41 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
onOsqueryTableSelect = (tableName) => {
|
|
|
|
const { dispatch } = this.props;
|
|
|
|
|
|
|
|
dispatch(selectOsqueryTable(tableName));
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
onSaveAddLabel = (formData) => {
|
|
|
|
const { dispatch } = this.props;
|
|
|
|
|
|
|
|
return dispatch(labelActions.create(formData))
|
|
|
|
.then(() => {
|
2016-12-05 15:21:03 +00:00
|
|
|
dispatch(push('/hosts/manage'));
|
2016-11-17 17:12:41 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-12-16 15:54:49 +00:00
|
|
|
onToggleDisplay = (val) => {
|
|
|
|
const { dispatch } = this.props;
|
2016-11-21 16:26:58 +00:00
|
|
|
|
2016-12-16 15:54:49 +00:00
|
|
|
dispatch(setDisplay(val));
|
2016-11-21 16:26:58 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-01-13 01:18:23 +00:00
|
|
|
sortHosts = (hosts) => {
|
|
|
|
const alphaHosts = sortBy(hosts, (h) => { return h.hostname; });
|
|
|
|
const orderedHosts = orderBy(alphaHosts, 'status', 'desc');
|
|
|
|
|
|
|
|
return orderedHosts;
|
|
|
|
}
|
|
|
|
|
2017-01-11 19:08:27 +00:00
|
|
|
renderQuery = () => {
|
|
|
|
const { selectedLabel } = this.props;
|
|
|
|
const { label_type: labelType, query } = selectedLabel;
|
|
|
|
|
|
|
|
if (!query || labelType === 1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<AceEditor
|
|
|
|
editorProps={{ $blockScrolling: Infinity }}
|
|
|
|
mode="kolide"
|
|
|
|
minLines={1}
|
|
|
|
maxLines={20}
|
|
|
|
name="label-header"
|
|
|
|
readOnly
|
|
|
|
setOptions={{ wrap: true }}
|
|
|
|
showGutter={false}
|
|
|
|
showPrintMargin={false}
|
|
|
|
theme="kolide"
|
|
|
|
value={query}
|
|
|
|
width="100%"
|
|
|
|
fontSize={14}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-11-17 17:12:41 +00:00
|
|
|
renderHeader = () => {
|
2017-01-11 19:08:27 +00:00
|
|
|
const { renderQuery } = this;
|
2016-12-05 15:21:03 +00:00
|
|
|
const { display, isAddLabel, selectedLabel } = this.props;
|
2016-11-17 17:12:41 +00:00
|
|
|
|
|
|
|
if (!selectedLabel || isAddLabel) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-01-11 19:08:27 +00:00
|
|
|
const { count, description, display_text: displayText } = selectedLabel;
|
2016-11-21 16:26:58 +00:00
|
|
|
const { onToggleDisplay } = this;
|
|
|
|
const buttonOptions = {
|
2016-12-16 15:54:49 +00:00
|
|
|
rightIcon: 'grid-select',
|
|
|
|
rightText: 'Grid',
|
|
|
|
leftIcon: 'list-select',
|
|
|
|
leftText: 'List',
|
2016-11-21 16:26:58 +00:00
|
|
|
};
|
2016-11-17 17:12:41 +00:00
|
|
|
|
|
|
|
return (
|
2016-12-15 14:44:45 +00:00
|
|
|
<div className={`${baseClass}__header`}>
|
|
|
|
<h1 className={`${baseClass}__title`}>
|
|
|
|
<Icon name={iconClassForLabel(selectedLabel)} />
|
|
|
|
<span>{displayText}</span>
|
|
|
|
</h1>
|
|
|
|
|
2017-01-11 19:08:27 +00:00
|
|
|
{ renderQuery() }
|
2016-12-15 14:44:45 +00:00
|
|
|
|
2017-01-11 19:08:27 +00:00
|
|
|
{description &&
|
|
|
|
<div className={`${baseClass}__description`}>
|
|
|
|
<h2>Description</h2>
|
|
|
|
<p>{description}</p>
|
|
|
|
</div>
|
|
|
|
}
|
2016-12-15 14:44:45 +00:00
|
|
|
|
|
|
|
<div className={`${baseClass}__topper`}>
|
|
|
|
<p className={`${baseClass}__host-count`}>{count} Hosts Total</p>
|
2016-11-21 16:26:58 +00:00
|
|
|
<Rocker
|
2016-12-16 15:54:49 +00:00
|
|
|
onChange={onToggleDisplay}
|
2016-11-21 16:26:58 +00:00
|
|
|
options={buttonOptions}
|
|
|
|
value={display}
|
|
|
|
/>
|
|
|
|
</div>
|
2016-11-17 17:12:41 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-11-21 16:26:58 +00:00
|
|
|
renderHosts = () => {
|
2016-12-05 15:21:03 +00:00
|
|
|
const { display, hosts, isAddLabel } = this.props;
|
2017-01-13 01:18:23 +00:00
|
|
|
const { onHostDetailActionClick, sortHosts } = this;
|
2016-11-21 16:26:58 +00:00
|
|
|
|
|
|
|
if (isAddLabel) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-01-13 01:18:23 +00:00
|
|
|
const sortedHosts = sortHosts(hosts);
|
|
|
|
|
2016-11-21 16:26:58 +00:00
|
|
|
if (display === 'Grid') {
|
2017-01-13 01:18:23 +00:00
|
|
|
return sortedHosts.map((host) => {
|
2016-11-21 16:26:58 +00:00
|
|
|
return (
|
|
|
|
<HostDetails
|
|
|
|
host={host}
|
|
|
|
key={`host-${host.id}-details`}
|
|
|
|
onDisableClick={onHostDetailActionClick('disable')}
|
|
|
|
onQueryClick={onHostDetailActionClick('query')}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-01-13 01:18:23 +00:00
|
|
|
return <HostsTable hosts={sortedHosts} />;
|
2016-11-21 16:26:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
renderForm = () => {
|
2017-01-06 00:01:17 +00:00
|
|
|
const { isAddLabel, labelErrors } = this.props;
|
2016-11-17 17:12:41 +00:00
|
|
|
const {
|
|
|
|
onCancelAddLabel,
|
2017-01-06 00:01:17 +00:00
|
|
|
onOsqueryTableSelect,
|
2016-11-17 17:12:41 +00:00
|
|
|
onSaveAddLabel,
|
|
|
|
} = this;
|
2017-01-06 00:01:17 +00:00
|
|
|
const queryStub = { description: '', name: '', query: '' };
|
2016-11-17 17:12:41 +00:00
|
|
|
|
|
|
|
if (isAddLabel) {
|
|
|
|
return (
|
2017-01-10 19:20:36 +00:00
|
|
|
<div className="body-wrap">
|
|
|
|
<QueryForm
|
|
|
|
key="query-composer"
|
|
|
|
onCancel={onCancelAddLabel}
|
|
|
|
onOsqueryTableSelect={onOsqueryTableSelect}
|
|
|
|
handleSubmit={onSaveAddLabel}
|
|
|
|
queryType="label"
|
|
|
|
query={queryStub}
|
|
|
|
serverErrors={labelErrors}
|
|
|
|
/>
|
|
|
|
</div>
|
2016-11-17 17:12:41 +00:00
|
|
|
);
|
|
|
|
}
|
2016-10-17 18:55:03 +00:00
|
|
|
|
2016-11-21 16:26:58 +00:00
|
|
|
return false;
|
2016-10-17 18:55:03 +00:00
|
|
|
}
|
|
|
|
|
2016-11-17 17:12:41 +00:00
|
|
|
renderSidePanel = () => {
|
|
|
|
let SidePanel;
|
|
|
|
const {
|
2016-12-05 15:21:03 +00:00
|
|
|
isAddLabel,
|
2016-11-17 17:12:41 +00:00
|
|
|
labels,
|
|
|
|
selectedLabel,
|
|
|
|
selectedOsqueryTable,
|
|
|
|
} = this.props;
|
|
|
|
const { onAddLabelClick, onLabelClick, onOsqueryTableSelect } = this;
|
|
|
|
|
|
|
|
if (isAddLabel) {
|
|
|
|
SidePanel = (
|
|
|
|
<QuerySidePanel
|
|
|
|
key="query-side-panel"
|
|
|
|
onOsqueryTableSelect={onOsqueryTableSelect}
|
|
|
|
selectedOsqueryTable={selectedOsqueryTable}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
SidePanel = (
|
|
|
|
<HostSidePanel
|
|
|
|
key="hosts-side-panel"
|
|
|
|
labels={labels}
|
|
|
|
onAddLabelClick={onAddLabelClick}
|
|
|
|
onLabelClick={onLabelClick}
|
|
|
|
selectedLabel={selectedLabel}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-01-06 15:36:51 +00:00
|
|
|
return SidePanel;
|
2016-11-17 17:12:41 +00:00
|
|
|
}
|
|
|
|
|
2016-10-17 18:55:03 +00:00
|
|
|
render () {
|
2016-11-21 16:26:58 +00:00
|
|
|
const { renderForm, renderHeader, renderHosts, renderSidePanel } = this;
|
2016-12-21 18:39:40 +00:00
|
|
|
const { display, isAddLabel } = this.props;
|
2016-10-17 18:55:03 +00:00
|
|
|
|
|
|
|
return (
|
2016-12-15 14:44:45 +00:00
|
|
|
<div className="has-sidebar">
|
2016-12-21 18:39:40 +00:00
|
|
|
{renderForm()}
|
|
|
|
{!isAddLabel &&
|
|
|
|
<div className={`${baseClass} body-wrap`}>
|
|
|
|
{renderHeader()}
|
|
|
|
<div className={`${baseClass}__list ${baseClass}__list--${display.toLowerCase()}`}>
|
|
|
|
{renderHosts()}
|
|
|
|
</div>
|
2016-12-15 14:44:45 +00:00
|
|
|
</div>
|
2016-12-21 18:39:40 +00:00
|
|
|
}
|
2016-12-15 14:44:45 +00:00
|
|
|
|
2016-11-17 17:12:41 +00:00
|
|
|
{renderSidePanel()}
|
2016-10-17 18:55:03 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-12 16:48:50 +00:00
|
|
|
const mapStateToProps = (state, { location, params }) => {
|
|
|
|
const activeLabelSlug = params.active_label || 'all-hosts';
|
|
|
|
const { display } = state.components.ManageHostsPage;
|
2016-10-17 18:55:03 +00:00
|
|
|
const { entities: hosts } = entityGetter(state).get('hosts');
|
2016-12-12 16:48:50 +00:00
|
|
|
const labelEntities = entityGetter(state).get('labels');
|
|
|
|
const { entities: labels } = labelEntities;
|
2016-12-05 15:21:03 +00:00
|
|
|
const isAddLabel = location.hash === NEW_LABEL_HASH;
|
2016-12-12 16:48:50 +00:00
|
|
|
const selectedLabel = labelEntities.findBy(
|
|
|
|
{ slug: activeLabelSlug },
|
|
|
|
{ ignoreCase: true },
|
|
|
|
);
|
2016-11-17 17:12:41 +00:00
|
|
|
const { selectedOsqueryTable } = state.components.QueryPages;
|
2017-01-06 00:01:17 +00:00
|
|
|
const labelErrors = state.entities.labels.errors;
|
2016-10-17 18:55:03 +00:00
|
|
|
|
2016-11-17 17:12:41 +00:00
|
|
|
return {
|
2016-11-21 16:26:58 +00:00
|
|
|
display,
|
2016-11-17 17:12:41 +00:00
|
|
|
hosts,
|
2016-12-05 15:21:03 +00:00
|
|
|
isAddLabel,
|
2017-01-06 00:01:17 +00:00
|
|
|
labelErrors,
|
2016-11-17 17:12:41 +00:00
|
|
|
labels,
|
|
|
|
selectedLabel,
|
|
|
|
selectedOsqueryTable,
|
|
|
|
};
|
2016-10-17 18:55:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default connect(mapStateToProps)(ManageHostsPage);
|