2018-01-29 11:37:29 +00:00
const app = require ( 'ui/modules' ) . get ( 'app/wazuh' , [ ] ) ;
2018-01-15 15:11:45 +00:00
const beautifier = require ( 'plugins/wazuh/utils/json-beautifier' ) ;
2017-01-16 12:29:48 +00:00
2018-01-08 14:55:25 +00:00
app . controller ( 'agentsController' ,
2018-01-29 11:37:29 +00:00
function ( $scope , $location , $q , $rootScope , appState , genericReq , apiReq , AgentsAutoComplete , errorHandler ) {
2018-01-09 18:53:42 +00:00
$rootScope . page = 'agents' ;
2017-11-28 16:37:31 +00:00
$scope . extensions = appState . getExtensions ( ) . extensions ;
2018-01-09 18:53:42 +00:00
$scope . agentsAutoComplete = AgentsAutoComplete ;
2017-10-20 19:04:22 +00:00
2018-02-20 12:47:04 +00:00
// Check the url hash and retrieve the tabView information
2018-01-09 18:53:42 +00:00
if ( $location . search ( ) . tabView ) {
$scope . tabView = $location . search ( ) . tabView ;
2018-02-20 12:47:04 +00:00
} else { // If tabView doesn't exist, default it to 'panels'
2018-01-09 18:53:42 +00:00
$scope . tabView = "panels" ;
$location . search ( "tabView" , "panels" ) ;
}
2017-10-20 19:04:22 +00:00
2018-01-15 11:44:37 +00:00
// Check the url hash and retrivew the tab information
2018-01-09 18:53:42 +00:00
if ( $location . search ( ) . tab ) {
$scope . tab = $location . search ( ) . tab ;
2018-02-20 12:47:04 +00:00
} else { // If tab doesn't exist, default it to 'general'
2018-01-09 18:53:42 +00:00
$scope . tab = "general" ;
$location . search ( "tab" , "general" ) ;
2017-11-28 16:37:31 +00:00
// Now we initialize the implicitFilter
$rootScope . currentImplicitFilter = "" ;
2018-01-09 18:53:42 +00:00
}
2018-02-26 11:25:02 +00:00
// Metrics Audit
let watcher8 , watcher9 , watcher10 , watcher11 ;
$scope . auditNewFiles = '' ;
$scope . auditReadFiles = '' ;
$scope . auditModifiedFiles = '' ;
$scope . auditRemovedFiles = '' ;
const assignWatcher8 = ( ) => {
watcher8 = $scope . $watch ( ( ) => {
return $ ( '#Wazuh-App-Agents-Audit-New-files-metric > visualize > visualization > div > div > div > div > div.metric-value.ng-binding > span' ) . text ( ) ;
} , ( newVal , oldVal ) => {
if ( newVal !== oldVal ) {
$scope . auditNewFiles = newVal ;
if ( ! $scope . $$phase ) $scope . $digest ( ) ;
}
} ) ;
}
const assignWatcher9 = ( ) => {
watcher9 = $scope . $watch ( ( ) => {
return $ ( '#Wazuh-App-Agents-Audit-Read-files-metric > visualize > visualization > div > div > div > div > div.metric-value.ng-binding > span' ) . text ( ) ;
} , ( newVal , oldVal ) => {
if ( newVal !== oldVal ) {
$scope . auditReadFiles = newVal ;
if ( ! $scope . $$phase ) $scope . $digest ( ) ;
}
} ) ;
}
const assignWatcher10 = ( ) => {
watcher10 = $scope . $watch ( ( ) => {
return $ ( '#Wazuh-App-Agents-Audit-Modified-files-metric > visualize > visualization > div > div > div > div > div.metric-value.ng-binding > span' ) . text ( ) ;
} , ( newVal , oldVal ) => {
if ( newVal !== oldVal ) {
$scope . auditModifiedFiles = newVal ;
if ( ! $scope . $$phase ) $scope . $digest ( ) ;
}
} ) ;
}
const assignWatcher11 = ( ) => {
watcher11 = $scope . $watch ( ( ) => {
return $ ( '#Wazuh-App-Agents-Audit-Removed-files-metric > visualize > visualization > div > div > div > div > div.metric-value.ng-binding > span' ) . text ( ) ;
} , ( newVal , oldVal ) => {
if ( newVal !== oldVal ) {
$scope . auditRemovedFiles = newVal ;
if ( ! $scope . $$phase ) $scope . $digest ( ) ;
}
} ) ;
}
const assignAuditMetrics = ( ) => {
assignWatcher8 ( ) ;
assignWatcher9 ( ) ;
assignWatcher10 ( ) ;
assignWatcher11 ( ) ;
}
const destroyAuditMetrics = ( ) => {
watcher8 ( ) ;
watcher9 ( ) ;
watcher10 ( ) ;
watcher11 ( ) ;
watcher8 = null ;
watcher9 = null ;
watcher10 = null ;
watcher11 = null ;
}
if ( $scope . tab === 'audit' && $scope . tabView === 'panels' && ! watcher8 ) {
assignAuditMetrics ( ) ;
}
2018-01-09 18:53:42 +00:00
$rootScope . tabVisualizations = {
2018-01-29 11:37:29 +00:00
general : 7 ,
fim : 8 ,
pm : 4 ,
2018-01-30 15:19:42 +00:00
vuls : 7 ,
2018-01-29 11:37:29 +00:00
oscap : 13 ,
audit : 15 ,
pci : 3 ,
virustotal : 6 ,
configuration : 0
2018-01-09 18:53:42 +00:00
} ;
2018-01-15 11:44:37 +00:00
2018-01-09 18:53:42 +00:00
// Object for matching nav items and Wazuh groups
2018-01-29 11:37:29 +00:00
const tabFilters = {
general : { group : '' } ,
fim : { group : 'syscheck' } ,
pm : { group : 'rootcheck' } ,
2018-01-30 10:05:54 +00:00
vuls : { group : 'vulnerability-detector' } ,
2018-01-29 11:37:29 +00:00
oscap : { group : 'oscap' } ,
audit : { group : 'audit' } ,
pci : { group : 'pci_dss' } ,
virustotal : { group : 'virustotal' }
2018-01-09 18:53:42 +00:00
} ;
2017-08-13 19:54:56 +00:00
2017-11-20 18:20:27 +00:00
// Switch subtab
2018-02-26 11:25:02 +00:00
$scope . switchSubtab = subtab => {
$scope . tabView = subtab ;
if ( $scope . tab === 'audit' && subtab === 'panels' && ! watcher8 ) {
assignAuditMetrics ( ) ;
} else if ( watcher8 ) {
destroyAuditMetrics ( ) ;
}
}
2018-01-29 11:37:29 +00:00
$scope . switchTab = tab => {
2018-01-15 15:11:45 +00:00
2018-01-09 18:53:42 +00:00
if ( $scope . tab === tab ) return ;
2018-02-26 11:25:02 +00:00
if ( tab === 'audit' && $scope . tabView === 'panels' && ! watcher8 ) {
assignAuditMetrics ( ) ;
} else if ( watcher8 ) {
destroyAuditMetrics ( ) ;
}
2018-01-15 15:11:45 +00:00
if ( $rootScope . ownHandlers ) {
for ( let h of $rootScope . ownHandlers ) {
h . _scope . $destroy ( ) ;
}
2018-01-08 15:27:37 +00:00
}
$rootScope . ownHandlers = [ ] ;
2018-01-09 18:53:42 +00:00
2017-11-28 16:37:31 +00:00
// Deleting app state traces in the url
2017-11-17 16:01:16 +00:00
$location . search ( '_a' , null ) ;
2018-01-09 18:53:42 +00:00
$scope . tabView = 'panels' ;
2017-11-17 16:01:16 +00:00
2018-01-09 18:53:42 +00:00
$rootScope . loadedVisualizations = [ ] ;
} ;
2017-11-17 16:01:16 +00:00
2018-01-09 18:53:42 +00:00
// Watchers
2017-11-17 16:01:16 +00:00
2018-01-09 18:53:42 +00:00
// We watch the resultState provided by the discover
$scope . $watch ( 'tabView' , ( ) => $location . search ( 'tabView' , $scope . tabView ) ) ;
2017-11-27 16:27:07 +00:00
$scope . $watch ( 'tab' , ( ) => {
2018-01-09 18:53:42 +00:00
$location . search ( 'tab' , $scope . tab ) ;
2018-01-15 15:11:45 +00:00
2017-11-17 16:01:16 +00:00
// Update the implicit filter
2018-01-15 15:11:45 +00:00
if ( typeof tabFilters [ $scope . tab ] !== 'undefined' && tabFilters [ $scope . tab ] . group === "" ) $rootScope . currentImplicitFilter = "" ;
else $rootScope . currentImplicitFilter = ( typeof tabFilters [ $scope . tab ] !== 'undefined' ) ? tabFilters [ $scope . tab ] . group : '' ;
if ( $scope . tab === 'configuration' ) {
firstLoad ( ) ;
}
2017-11-27 16:27:07 +00:00
} ) ;
2017-08-13 19:54:56 +00:00
2017-11-28 16:37:31 +00:00
// Agent data
2018-01-09 18:53:42 +00:00
$scope . getAgentStatusClass = ( agentStatus ) => agentStatus === "Active" ? "green" : "red" ;
$scope . formatAgentStatus = ( agentStatus ) => {
return [ 'Active' , 'Disconnected' ] . includes ( agentStatus ) ? agentStatus : 'Never connected' ;
} ;
const calculateMinutes = ( start , end ) => {
let time = new Date ( start ) ;
let endTime = new Date ( end ) ;
let minutes = ( ( endTime - time ) / 1000 ) / 60 ;
return minutes ;
}
const validateRootCheck = ( ) => {
$scope . agent . rootcheck . duration = 'Unknown' ;
if ( $scope . agent . rootcheck . end && $scope . agent . rootcheck . start ) {
$scope . agent . rootcheck . duration = ( ( new Date ( $scope . agent . rootcheck . end ) - new Date ( $scope . agent . rootcheck . start ) ) / 1000 ) / 60 ;
$scope . agent . rootcheck . duration = Math . round ( $scope . agent . rootcheck . duration * 100 ) / 100 ;
if ( $scope . agent . rootcheck . duration <= 0 ) {
$scope . agent . rootcheck . inProgress = true ;
}
} else {
if ( ! $scope . agent . rootcheck . end ) {
$scope . agent . rootcheck . end = 'Unknown' ;
2018-01-15 11:44:37 +00:00
}
2018-01-09 18:53:42 +00:00
if ( ! $scope . agent . rootcheck . start ) {
$scope . agent . rootcheck . start = 'Unknown' ;
}
2018-01-15 11:44:37 +00:00
}
2018-01-09 18:53:42 +00:00
}
const validateSysCheck = ( ) => {
$scope . agent . syscheck . duration = 'Unknown' ;
if ( $scope . agent . syscheck . end && $scope . agent . syscheck . start ) {
$scope . agent . syscheck . duration = ( ( new Date ( $scope . agent . syscheck . end ) - new Date ( $scope . agent . syscheck . start ) ) / 1000 ) / 60 ;
$scope . agent . syscheck . duration = Math . round ( $scope . agent . syscheck . duration * 100 ) / 100 ;
if ( $scope . agent . syscheck . duration <= 0 ) {
$scope . agent . syscheck . inProgress = true ;
}
} else {
if ( ! $scope . agent . syscheck . end ) {
$scope . agent . syscheck . end = 'Unknown' ;
2018-01-15 11:44:37 +00:00
}
2018-01-09 18:53:42 +00:00
if ( ! $scope . agent . syscheck . start ) {
$scope . agent . syscheck . start = 'Unknown' ;
}
}
}
2018-01-29 11:37:29 +00:00
$scope . getAgent = async newAgentId => {
try {
if ( $scope . tab === 'configuration' ) {
return $scope . getAgentConfig ( newAgentId ) ;
}
let id = null ;
2018-01-09 18:53:42 +00:00
2018-01-29 11:37:29 +00:00
// They passed an id
if ( newAgentId ) {
id = newAgentId ;
2018-01-09 18:53:42 +00:00
$location . search ( 'agent' , id ) ;
2018-01-29 11:37:29 +00:00
} else {
2018-02-22 10:30:30 +00:00
if ( $location . search ( ) . agent && ! $rootScope . globalAgent ) { // There's one in the url
2018-01-29 11:37:29 +00:00
id = $location . search ( ) . agent ;
} else { // We pick the one in the rootScope
id = $rootScope . globalAgent ;
$location . search ( 'agent' , id ) ;
delete $rootScope . globalAgent ;
}
2018-01-09 18:53:42 +00:00
}
2018-01-29 11:37:29 +00:00
if ( id === '000' && $scope . tab === 'configuration' ) {
$scope . tab = 'general' ;
$scope . switchTab ( 'general' ) ;
}
2018-01-30 10:03:52 +00:00
2018-01-29 11:37:29 +00:00
const data = await Promise . all ( [
apiReq . request ( 'GET' , ` /agents/ ${ id } ` , { } ) ,
apiReq . request ( 'GET' , ` /syscheck/ ${ id } /last_scan ` , { } ) ,
apiReq . request ( 'GET' , ` /rootcheck/ ${ id } /last_scan ` , { } )
] ) ;
2018-01-15 11:44:37 +00:00
2018-01-09 18:53:42 +00:00
// Agent
$scope . agent = data [ 0 ] . data . data ;
if ( $scope . agent . os ) {
$scope . agentOS = $scope . agent . os . name + ' ' + $scope . agent . os . version ;
}
2018-01-29 11:37:29 +00:00
else { $scope . agentOS = 'Unkwnown' } ;
2018-01-09 18:53:42 +00:00
// Syscheck
$scope . agent . syscheck = data [ 1 ] . data . data ;
2018-01-15 11:44:37 +00:00
validateSysCheck ( ) ;
2018-01-29 11:37:29 +00:00
2018-01-09 18:53:42 +00:00
// Rootcheck
$scope . agent . rootcheck = data [ 2 ] . data . data ;
2018-01-15 11:44:37 +00:00
validateRootCheck ( ) ;
2018-01-09 18:53:42 +00:00
2018-01-26 10:21:50 +00:00
if ( ! $scope . $$phase ) $scope . $digest ( ) ;
2018-01-29 11:37:29 +00:00
return ;
} catch ( error ) {
2018-01-29 14:39:21 +00:00
errorHandler . handle ( error , 'Agents' ) ;
2018-01-29 12:16:59 +00:00
if ( ! $rootScope . $$phase ) $rootScope . $digest ( ) ;
2018-01-29 11:37:29 +00:00
}
2018-01-09 18:53:42 +00:00
} ;
$scope . goGroups = agent => {
$rootScope . globalAgent = agent ;
$rootScope . comeFrom = 'agents' ;
$location . search ( 'tab' , 'groups' ) ;
2018-01-15 11:44:37 +00:00
$location . path ( '/manager' ) ;
2018-01-09 18:53:42 +00:00
} ;
2017-12-11 08:33:43 +00:00
2018-01-29 11:37:29 +00:00
$scope . analizeAgents = async search => {
try {
$scope . agentsAutoComplete . filters = [ ] ;
await $scope . agentsAutoComplete . addFilter ( 'search' , search ) ;
2017-12-12 16:27:29 +00:00
2018-01-29 11:37:29 +00:00
if ( ! $scope . $$phase ) $scope . $digest ( ) ;
return $scope . agentsAutoComplete . items ;
} catch ( error ) {
2018-01-29 14:39:21 +00:00
errorHandler . handle ( error , 'Agents' ) ;
2018-01-29 12:16:59 +00:00
if ( ! $rootScope . $$phase ) $rootScope . $digest ( ) ;
2018-01-29 11:37:29 +00:00
}
2017-12-12 16:27:29 +00:00
}
2018-01-09 18:53:42 +00:00
//Load
try {
2018-01-15 15:11:45 +00:00
if ( $scope . tab !== 'configuration' ) {
$scope . getAgent ( ) ;
}
2018-01-09 18:53:42 +00:00
$scope . agentsAutoComplete . nextPage ( '' ) ;
} catch ( e ) {
2018-01-29 14:39:21 +00:00
errorHandler . handle ( 'Unexpected exception loading controller' , 'Agents' ) ;
2018-01-29 12:16:59 +00:00
if ( ! $rootScope . $$phase ) $rootScope . $digest ( ) ;
2018-01-09 18:53:42 +00:00
}
//Destroy
$scope . $on ( "$destroy" , ( ) => {
$scope . agentsAutoComplete . reset ( ) ;
2018-01-29 11:37:29 +00:00
if ( $rootScope . ownHandlers ) {
for ( let h of $rootScope . ownHandlers ) {
h . _scope . $destroy ( ) ;
}
2018-01-08 15:27:37 +00:00
}
2018-02-26 11:25:02 +00:00
if ( watcher8 ) destroyAuditMetrics ( ) ;
2018-01-08 15:27:37 +00:00
$rootScope . ownHandlers = [ ] ;
2018-01-09 18:53:42 +00:00
} ) ;
2018-01-15 11:44:37 +00:00
2018-01-09 18:53:42 +00:00
//PCI tab
let tabs = [ ] ;
genericReq . request ( 'GET' , '/api/wazuh-api/pci/all' )
. then ( ( data ) => {
for ( let key in data . data ) {
tabs . push ( {
"title" : key ,
"content" : data . data [ key ]
} ) ;
}
} )
2018-01-29 12:16:59 +00:00
. catch ( error => {
2018-01-29 14:39:21 +00:00
errorHandler . handle ( error , 'Agents' ) ;
2018-01-29 12:16:59 +00:00
if ( ! $rootScope . $$phase ) $rootScope . $digest ( ) ;
} ) ;
2018-01-09 18:53:42 +00:00
$scope . tabs = tabs ;
$scope . selectedIndex = 0 ;
2018-01-15 15:11:45 +00:00
/** Agent configuration */
2018-01-17 16:29:49 +00:00
$scope . switchConfigTab = selected => {
2018-01-18 15:55:49 +00:00
2018-01-17 16:29:49 +00:00
} ;
2018-01-15 15:11:45 +00:00
$scope . isArray = angular . isArray ;
const getAgent = newAgentId => {
// They passed an id
if ( newAgentId ) {
$location . search ( 'agent' , newAgentId ) ;
2018-01-23 11:52:02 +00:00
}
2018-01-15 15:11:45 +00:00
} ;
$scope . getAgentConfig = newAgentId => {
getAgent ( newAgentId ) ;
firstLoad ( ) ;
}
$scope . goGroup = ( ) => {
$rootScope . globalAgent = $scope . agent ;
$rootScope . comeFrom = 'agents' ;
$location . search ( 'tab' , 'groups' ) ;
$location . path ( '/manager' ) ;
} ;
2018-01-23 11:52:02 +00:00
2018-01-15 15:11:45 +00:00
const firstLoad = async ( ) => {
try {
$scope . configurationError = false ;
$scope . load = true ;
let id ;
2018-02-22 10:30:30 +00:00
if ( $location . search ( ) . agent && ! $rootScope . globalAgent ) { // There's one in the url
2018-01-15 15:11:45 +00:00
id = $location . search ( ) . agent ;
} else { // We pick the one in the rootScope
id = $rootScope . globalAgent ;
$location . search ( 'agent' , id ) ;
delete $rootScope . globalAgent ;
}
let data = await apiReq . request ( 'GET' , ` /agents/ ${ id } ` , { } ) ;
$scope . agent = data . data . data ;
$scope . groupName = $scope . agent . group ;
if ( ! $scope . groupName ) {
2018-01-23 11:52:02 +00:00
2018-01-15 15:11:45 +00:00
$scope . configurationError = true ;
$scope . load = false ;
if ( $scope . agent . id === '000' ) {
$scope . configurationError = false ;
$scope . tab = 'general' ;
$scope . switchTab ( 'general' ) ;
}
if ( ! $scope . $$phase ) $scope . $digest ( ) ;
return ;
}
data = await apiReq . request ( 'GET' , ` /agents/groups/ ${ $scope . groupName } /configuration ` , { } ) ;
$scope . groupConfiguration = data . data . data . items [ 0 ] ;
$scope . rawJSON = beautifier . prettyPrint ( $scope . groupConfiguration ) ;
data = await Promise . all ( [
apiReq . request ( 'GET' , ` /agents/groups?search= ${ $scope . groupName } ` , { } ) ,
apiReq . request ( 'GET' , ` /agents/groups/ ${ $scope . groupName } ` , { } )
] ) ;
2018-01-23 11:52:02 +00:00
2018-01-15 15:11:45 +00:00
let filtered = data [ 0 ] . data . data . items . filter ( item => item . name === $scope . groupName ) ;
$scope . groupMergedSum = ( filtered . length ) ? filtered [ 0 ] . merged _sum : 'Unknown' ;
filtered = data [ 1 ] . data . data . items . filter ( item => item . id === $scope . agent . id ) ;
$scope . agentMergedSum = ( filtered . length ) ? filtered [ 0 ] . merged _sum : 'Unknown' ;
$scope . isSynchronized = ( ( $scope . agentMergedSum === $scope . groupMergedSum ) && ! ( [ $scope . agentMergedSum , $scope . groupMergedSum ] . includes ( 'Unknown' ) ) ) ? true : false ;
$scope . load = false ;
if ( ! $scope . $$phase ) $scope . $digest ( ) ;
return ;
} catch ( error ) {
2018-01-29 14:39:21 +00:00
errorHandler . handle ( error , 'Agents' ) ;
2018-01-29 12:16:59 +00:00
if ( ! $rootScope . $$phase ) $rootScope . $digest ( ) ;
2018-01-15 15:11:45 +00:00
}
}
/** End of agent configuration */
2018-01-15 11:44:37 +00:00
} ) ;