Skip to content

Commit

Permalink
fix(reports): error generating group reports due to accessing to a pr…
Browse files Browse the repository at this point in the history
…operty of undefined

- Fixed an error due to accessing to a property of undefined when the API request to get the group configuration failed due to token expiration or another error.
- Changed styles of `Agents in groups` title
- Fixed a problem with missing agents in the table for a group report.
  Now it gets all the agents.
- Removed unnecessary API request
- Optimized API requests to get the agent information required to print
  the agents in group's table
- Removed `manager_host` deprecated field:
  - Removed of monitoring template mapping
  - Removed field meaning
  - Removed displaying its value reports if the field `manager` doesn't exist
  • Loading branch information
Desvelao committed Jul 21, 2022
1 parent 79661e4 commit 5554910
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 99 deletions.
1 change: 0 additions & 1 deletion common/csv-key-equivalence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ export const KeyEquivalence: {[key: string]: string} = {
architecture: 'Architecture',
node_name: 'Node',
dateAdd: 'Registration date',
manager_host: 'Manager',
manager: 'Manager',
lastKeepAlive: 'Last keep alive',
os: 'OS',
Expand Down
1 change: 0 additions & 1 deletion public/utils/monitoring-fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ export const FieldsMonitoring =[
{ "name": "configSum", "type": "string", "count": 0, "scripted": false, "searchable": true, "aggregatable": false, "readFromDocValues": false },
{ "name": "node_name", "type": "string", "count": 0, "scripted": false, "searchable": true, "aggregatable": false, "readFromDocValues": false },
{ "name": "manager", "type": "string", "count": 0, "scripted": false, "searchable": true, "aggregatable": false, "readFromDocValues": false },
{ "name": "manager_host", "type": "string", "count": 0, "scripted": false, "searchable": true, "aggregatable": false, "readFromDocValues": false },
{ "name": "name", "type": "string", "count": 0, "scripted": false, "searchable": true, "aggregatable": true, "readFromDocValues": true },
{ "name": "os.arch", "type": "string", "count": 0, "scripted": false, "searchable": true, "aggregatable": false, "readFromDocValues": false },
{ "name": "os.codename", "type": "string", "count": 0, "scripted": false, "searchable": true, "aggregatable": false, "readFromDocValues": false },
Expand Down
157 changes: 69 additions & 88 deletions server/controllers/wazuh-reporting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,16 +134,8 @@ export class WazuhReportingCtrl {
} else if (section === 'groupConfig') {
printer.addContent({
text: 'Agents in group',
style: { fontSize: 14, color: '#000' },
margin: [0, 20, 0, 0],
style: 'h1',
});
if (section === 'groupConfig' && !Object.keys(isAgents).length) {
printer.addContent({
text: 'There are still no agents in this group.',
style: { fontSize: 12, color: '#000' },
margin: [0, 10, 0, 0],
});
}
}
printer.addNewLine();
}
Expand All @@ -154,7 +146,7 @@ export class WazuhReportingCtrl {
printer,
isAgents,
apiId,
section === 'groupConfig' ? tab : false
section === 'groupConfig' ? tab : null
);
}

Expand Down Expand Up @@ -188,8 +180,6 @@ export class WazuhReportingCtrl {
style: 'standard',
});
}

return;
} catch (error) {
log('reporting:renderHeader', error.message || error);
return Promise.reject(error);
Expand All @@ -201,57 +191,44 @@ export class WazuhReportingCtrl {
* @param {Array<Strings>} ids ids of agents
* @param {String} apiId API id
*/
private async buildAgentsTable(context, printer: ReportPrinter, agentIDs: string[], apiId: string, multi = false) {
private async buildAgentsTable(context, printer: ReportPrinter, agentIDs: string[], apiId: string, groupID: string | null = null) {
const dateFormat = await context.core.uiSettings.client.get('dateFormat');
if (!agentIDs || !agentIDs.length) return;
if ((!agentIDs || !agentIDs.length) && !groupID) return;
log('reporting:buildAgentsTable', `${agentIDs.length} agents for API ${apiId}`, 'info');
try {
let agentRows = [];
if (multi) {
try {
const agentsResponse = await context.wazuh.api.client.asCurrentUser.request(
let agentsData = [];
if (groupID) {
let totalAgentsInGroup = null;
do{
const { data: { data: { affected_items, total_affected_items } } } = await context.wazuh.api.client.asCurrentUser.request(
'GET',
`/groups/${multi}/agents`,
{},
`/groups/${groupID}/agents`,
{
params: {
offset: agentsData.length,
select: 'dateAdd,id,ip,lastKeepAlive,manager,name,os.name,os.version,version',
}
},
{ apiHostID: apiId }
);
const agentsData =
agentsResponse &&
agentsResponse.data &&
agentsResponse.data.data &&
agentsResponse.data.data.affected_items;
agentRows = (agentsData || []).map((agent) => ({
...agent,
manager: agent.manager || agent.manager_host,
os:
agent.os && agent.os.name && agent.os.version
? `${agent.os.name} ${agent.os.version}`
: '',
}));
} catch (error) {
log(
'reporting:buildAgentsTable',
`Skip agent due to: ${error.message || error}`,
'debug'
);
}
!totalAgentsInGroup && (totalAgentsInGroup = total_affected_items);
agentsData = [...agentsData, ...affected_items];
}while(agentsData.length < totalAgentsInGroup);
} else {
for (const agentID of agentIDs) {
try {
const agentResponse = await context.wazuh.api.client.asCurrentUser.request(
const { data: { data: { affected_items: [agent] } } } = await context.wazuh.api.client.asCurrentUser.request(
'GET',
`/agents`,
{ params: { q: `id=${agentID}` } },
{
params: {
q: `id=${agentID}`,
select: 'dateAdd,id,ip,lastKeepAlive,manager,name,os.name,os.version,version',
}
},
{ apiHostID: apiId }
);
const [agent] = agentResponse.data.data.affected_items;
agentRows.push({
...agent,
manager: agent.manager || agent.manager_host,
os: (agent.os && agent.os.name && agent.os.version) ? `${agent.os.name} ${agent.os.version}` : '',
lastKeepAlive: moment(agent.lastKeepAlive).format(dateFormat),
dateAdd: moment(agent.dateAdd).format(dateFormat)
});
agentsData.push(agent);
} catch (error) {
log(
'reporting:buildAgentsTable',
Expand All @@ -261,19 +238,37 @@ export class WazuhReportingCtrl {
}
}
}
printer.addSimpleTable({
columns: [
{ id: 'id', label: 'ID' },
{ id: 'name', label: 'Name' },
{ id: 'ip', label: 'IP' },
{ id: 'version', label: 'Version' },
{ id: 'manager', label: 'Manager' },
{ id: 'os', label: 'OS' },
{ id: 'dateAdd', label: 'Registration date' },
{ id: 'lastKeepAlive', label: 'Last keep alive' },
],
items: agentRows,
});

if(agentsData.length){
// Print a table with agent/s information
printer.addSimpleTable({
columns: [
{ id: 'id', label: 'ID' },
{ id: 'name', label: 'Name' },
{ id: 'ip', label: 'IP' },
{ id: 'version', label: 'Version' },
{ id: 'manager', label: 'Manager' },
{ id: 'os', label: 'OS' },
{ id: 'dateAdd', label: 'Registration date' },
{ id: 'lastKeepAlive', label: 'Last keep alive' },
],
items: agentsData.map((agent) => {
return {
...agent,
os: (agent.os && agent.os.name && agent.os.version) ? `${agent.os.name} ${agent.os.version}` : '',
lastKeepAlive: moment(agent.lastKeepAlive).format(dateFormat),
dateAdd: moment(agent.dateAdd).format(dateFormat)
}
}),
});
}else if(!agentsData.length && groupID){
// For group reports when there is no agents in the group
printer.addContent({
text: 'There are still no agents in this group.',
style: { fontSize: 12, color: '#000' },
});
}

} catch (error) {
log('reporting:buildAgentsTable', error.message || error);
return Promise.reject(error);
Expand Down Expand Up @@ -1249,19 +1244,15 @@ export class WazuhReportingCtrl {
style: 'h1',
});

// Group configuration
if (components['0']) {
let configuration = {};
try {
const configurationResponse = await context.wazuh.api.client.asCurrentUser.request(
'GET',
`/groups/${groupID}/configuration`,
{},
{ apiHostID: apiId }
);
configuration = configurationResponse.data.data;
} catch (error) {
log('reporting:createReportsGroups', error.message || error, 'debug');
}

const { data: { data: configuration } } = await context.wazuh.api.client.asCurrentUser.request(
'GET',
`/groups/${groupID}/configuration`,
{},
{ apiHostID: apiId }
);

if (
configuration.affected_items.length > 0 &&
Expand Down Expand Up @@ -1441,25 +1432,15 @@ export class WazuhReportingCtrl {
});
}
}

// Agents in group
if (components['1']) {
let agentsInGroup = [];
try {
const agentsInGroupResponse = await context.wazuh.api.client.asCurrentUser.request(
'GET',
`/groups/${groupID}/agents`,
{},
{ apiHostID: apiId }
);
agentsInGroup = agentsInGroupResponse.data.data.affected_items;
} catch (error) {
log('reporting:report', error.message || error, 'debug');
}
await this.renderHeader(
context,
printer,
'groupConfig',
groupID,
(agentsInGroup || []).map((x) => x.id),
[],
apiId
);
}
Expand Down
9 changes: 0 additions & 9 deletions server/integration-files/monitoring-known-fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,15 +174,6 @@ export const monitoringKnownFields = [
aggregatable: false,
readFromDocValues: false
},
{
name: 'manager_host',
type: 'string',
count: 0,
scripted: false,
searchable: true,
aggregatable: false,
readFromDocValues: false
},
{
name: 'name',
type: 'string',
Expand Down

0 comments on commit 5554910

Please sign in to comment.