|
| 1 | +/* |
| 2 | + * Wazuh app - Integrity monitoring components |
| 3 | + * Copyright (C) 2015-2020 Wazuh, Inc. |
| 4 | + * |
| 5 | + * This program is free software; you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU General Public License as published by |
| 7 | + * the Free Software Foundation; either version 2 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * Find more information about this on the LICENSE file. |
| 11 | + */ |
| 12 | + |
| 13 | +import React, { Component } from 'react'; |
| 14 | +import { FlyoutDetail } from './states/flyout'; |
| 15 | +import { ModulesHelper } from '../../common/modules/modules-helper' |
| 16 | +import { EuiOverlayMask } from '@elastic/eui'; |
| 17 | + |
| 18 | + |
| 19 | +export class EventsFim extends Component { |
| 20 | + _isMount = false; |
| 21 | + state: { |
| 22 | + isFlyoutVisible: Boolean, |
| 23 | + currentFile: string, |
| 24 | + fetchStatus: 'loading' | 'complete', |
| 25 | + rows: number |
| 26 | + }; |
| 27 | + props!: { |
| 28 | + [key: string]: any |
| 29 | + } |
| 30 | + modulesHelper: ModulesHelper; |
| 31 | + fetchWatch!: any; |
| 32 | + |
| 33 | + constructor(props) { |
| 34 | + super(props); |
| 35 | + this.state = { |
| 36 | + isFlyoutVisible: false, |
| 37 | + currentFile: '', |
| 38 | + fetchStatus: 'loading', |
| 39 | + rows: 0 |
| 40 | + }; |
| 41 | + this.modulesHelper = ModulesHelper; |
| 42 | + this.getRowsField.bind(this); |
| 43 | + } |
| 44 | + |
| 45 | + async componentDidMount() { |
| 46 | + this._isMount = true; |
| 47 | + const scope = await this.modulesHelper.getDiscoverScope(); |
| 48 | + this.fetchWatch = scope.$watchCollection('fetchStatus', |
| 49 | + () => { |
| 50 | + const {fetchStatus} = this.state; |
| 51 | + if (fetchStatus !== scope.fetchStatus){ |
| 52 | + const rows = scope.fetchStatus === 'complete' ? scope.rows.length : 0; |
| 53 | + this._isMount && this.setState({fetchStatus: scope.fetchStatus, rows}) |
| 54 | + } |
| 55 | + }); |
| 56 | + } |
| 57 | + |
| 58 | + shouldComponentUpdate(nextProps, nextState) { |
| 59 | + const {fetchStatus, isFlyoutVisible, rows} = this.state; |
| 60 | + if (nextState.isFlyoutVisible !== isFlyoutVisible ){ |
| 61 | + return true; |
| 62 | + } |
| 63 | + if (nextState.fetchStatus !== fetchStatus){ |
| 64 | + return true; |
| 65 | + } |
| 66 | + if (nextState.rows !== rows) { |
| 67 | + return true; |
| 68 | + } |
| 69 | + return false; |
| 70 | + } |
| 71 | + |
| 72 | + componentDidUpdate() { |
| 73 | + const {fetchStatus, rows} = this.state; |
| 74 | + if(fetchStatus === 'complete' && rows){ |
| 75 | + this.getRowsField(); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + componentWillUnmount() { |
| 80 | + this._isMount = false; |
| 81 | + if (this.fetchWatch) this.fetchWatch(); |
| 82 | + } |
| 83 | + |
| 84 | + getRowsField = async () => { |
| 85 | + const indices: number[] = []; |
| 86 | + const { rows } = this.state; |
| 87 | + if (!rows) { |
| 88 | + this.setState({elements:[]}) |
| 89 | + return; |
| 90 | + } |
| 91 | + if (!document) |
| 92 | + this.getRowsField(); |
| 93 | + const cols = document.querySelectorAll(`.kbn-table thead th`); |
| 94 | + if (!(cols || []).length) { |
| 95 | + setTimeout(this.getRowsField, 10); |
| 96 | + } |
| 97 | + cols.forEach((col, idx) => { |
| 98 | + if (['syscheck.path', 'rule.id'].includes(col.textContent || '')) { |
| 99 | + indices.push(idx + 1); |
| 100 | + } |
| 101 | + }); |
| 102 | + let query = ''; |
| 103 | + indices.forEach((position, idx) => { |
| 104 | + query += `.kbn-table tbody tr td:nth-child(${position}) div` |
| 105 | + if (idx !== indices.length - 1) { |
| 106 | + query += ', '; |
| 107 | + } |
| 108 | + }); |
| 109 | + if (query){ |
| 110 | + const elements = document.querySelectorAll(query); |
| 111 | + elements.forEach((element, idx) => { |
| 112 | + const text = element.textContent; |
| 113 | + if (idx % 2){ |
| 114 | + element.childNodes.forEach(child => { |
| 115 | + if (child.nodeName === 'SPAN'){ |
| 116 | + const link = document.createElement('a') |
| 117 | + link.setAttribute('href', `#/manager/rules?tab=rules&redirectRule=${text}`) |
| 118 | + link.setAttribute('target', '_blank') |
| 119 | + link.setAttribute('style', 'minWidth: 55, display: "block"'); |
| 120 | + link.textContent = text |
| 121 | + child.replaceWith(link) |
| 122 | + } |
| 123 | + }) |
| 124 | + } else { |
| 125 | + element.childNodes.forEach(child => { |
| 126 | + if (child.nodeName === 'SPAN'){ |
| 127 | + const link = document.createElement('a') |
| 128 | + link.onclick = () => this.showFlyout(text); |
| 129 | + link.textContent = text |
| 130 | + child.replaceWith(link); |
| 131 | + } |
| 132 | + }) |
| 133 | + } |
| 134 | + }) |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + showFlyout(file) { |
| 139 | + if (file !== " - ") { |
| 140 | + //if a flyout is opened, we close it and open a new one, so the components are correctly updated on start. |
| 141 | + this.setState({ isFlyoutVisible: true, currentFile: file }); |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + closeFlyout() { |
| 146 | + this.setState({ isFlyoutVisible: false, currentFile: false }); |
| 147 | + } |
| 148 | + |
| 149 | + render() { |
| 150 | + return ( |
| 151 | + this.state.isFlyoutVisible && |
| 152 | + <EuiOverlayMask |
| 153 | + // @ts-ignore |
| 154 | + onClick={(e:Event) => {e.target.className === 'euiOverlayMask' && this.closeFlyout() }} > |
| 155 | + <FlyoutDetail |
| 156 | + fileName={this.state.currentFile} |
| 157 | + agentId={this.props.agent.id} |
| 158 | + closeFlyout={() => this.closeFlyout()} |
| 159 | + type='file' |
| 160 | + view='events' |
| 161 | + {...this.props}/> |
| 162 | + </EuiOverlayMask> |
| 163 | + ) |
| 164 | + } |
| 165 | +} |
0 commit comments