Skip to content

Commit

Permalink
Initial implementation of device tags.
Browse files Browse the repository at this point in the history
  • Loading branch information
mark-unwin committed Jan 29, 2024
1 parent ee9454c commit 27da1a6
Show file tree
Hide file tree
Showing 9 changed files with 150 additions and 4 deletions.
8 changes: 7 additions & 1 deletion app/Helpers/response_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,10 @@ function response_create($instance = null)
$received_data = $response->meta->received_data;
$response->meta->received_data = array();
}

if ($response->meta->collection === 'devices' and $response->meta->action === 'update' and !empty($response->meta->received_data->attributes->tags)) {
$received_data = $response->meta->received_data;
$response->meta->received_data = array();
}
if ($response->meta->collection === 'search' and $response->meta->action === 'create') {
$received_data = $response->meta->received_data;
$response->meta->received_data = array();
Expand Down Expand Up @@ -515,6 +518,9 @@ function response_create($instance = null)
if ($response->meta->collection === 'credentials' and $response->meta->action === 'create') {
$response->meta->received_data = $received_data;
}
if ($response->meta->collection === 'devices' and $response->meta->action === 'update' and !empty($received_data->attributes->tags)) {
$response->meta->received_data = $received_data;
}
// TODO - Why does enterprise return this as a string?
$response->meta->limit = intval($response->meta->limit);
if (!empty($response->errors)) {
Expand Down
28 changes: 27 additions & 1 deletion app/Models/DevicesModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ public function collection(object $resp): array
$joined_tables = array();
foreach ($resp->meta->filter as $filter) {
if (in_array($filter->operator, ['!=', '>=', '<=', '=', '>', '<'])) {
if ($filter->name === 'devices.tags' and $filter->operator === '=') {
$filter->function = 'like';
$filter->operator = '';
$filter->value = '"' . $filter->value . '"';
}
$this->builder->{$filter->function}($filter->name . ' ' . $filter->operator, $filter->value);
} else {
$this->builder->{$filter->function}($filter->name, $filter->value);
Expand All @@ -71,9 +76,10 @@ public function collection(object $resp): array
return array();
}
$result = $query->getResult();
$count = count($result);

if (isset($result[0]->type) and isset($result[0]->last_seen_by) and $instance->config->product !== 'community') {
for ($i=0; $i < count($result); $i++) {
for ($i=0; $i < $count; $i++) {
# BAD
if ($result[$i]->last_seen_by === 'nmap' and ($result[$i]->type === 'unclassified' or $result[$i]->type === 'unknown')) {
$result[$i]->audit_class = 'fa fa-times text-danger';
Expand Down Expand Up @@ -113,6 +119,18 @@ public function collection(object $resp): array
}
}
}

if (isset($result[0]->tags)) {
for ($i=0; $i < $count; $i++) {
if (!empty($result[$i]->tags)) {
$result[$i]->tags = @json_decode($result[$i]->tags);
}
if (empty($result[$i]->tags)) {
$result[$i]->tags = array();
}
}
}

return format_data($result, $resp->meta->collection);
}

Expand Down Expand Up @@ -591,6 +609,14 @@ public function read(int $id = 0): array
return array();
}
$device = $query->getResult();
if (isset($device[0]->tags)) {
if (!empty($device[0]->tags)) {
$device[0]->tags = @json_decode($device[0]->tags);
}
if (empty($device[0]->tags)) {
$device[0]->tags = array();
}
}
if (!empty($device[0]->instance_tags)) {
$device[0]->instance_tags = json_decode($device[0]->instance_tags);
}
Expand Down
12 changes: 12 additions & 0 deletions app/Models/db_upgrades/db_5.1.0.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@
log_message('info', (string)$db->getLastQuery());
}

if (!$db->fieldExists('tags', 'devices')) {
$sql = "ALTER TABLE `devices` ADD `tags` text NOT NULL DEFAULT '[]' AFTER kernel_version";
$query = $db->query($sql);
$output .= str_replace("\n", " ", (string)$db->getLastQuery()) . "\n\n";
log_message('info', (string)$db->getLastQuery());
}

$sql = "UPDATE `configuration` SET `value` = CONCAT(`value`, ',devices.tags') WHERE `name` = 'devices_default_retrieve_columns' AND `value` NOT LIKE '%devices.tags%'";
$db->query($sql);
$output .= str_replace("\n", " ", (string)$db->getLastQuery()) . "\n\n";
log_message('info', (string)$db->getLastQuery());

$sql = "DELETE FROM `dashboards` WHERE `name` = 'Summary Dashboard' and `description` = 'Summary Information'";
$db->query($sql);
$output .= str_replace("\n", " ", (string)$db->getLastQuery()) . "\n\n";
Expand Down
6 changes: 6 additions & 0 deletions app/Views/devicesCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,12 @@
}
} else if ($key === 'ip' and !empty($item->attributes->ip_padded)) {
echo " <td><span style=\"display:none;\">" . $item->attributes->ip_padded . "</span> " . $item->attributes->{$key} . " </td>\n";
} else if ($key === 'tags') {
echo "<td>";
foreach ($item->attributes->tags as $tag) {
echo ' <button type="button" class="btn btn-xs btn-primary rounded-pill" style="margin-right:20px;">&nbsp;&nbsp;<strong><a style="color:white;" href="' . url_to('devicesCollection') . '?devices.tags=' . $tag . '">' . $tag . '</a>&nbsp;&nbsp;</button>';
}
echo "</td>";
} else {
echo " <td><span class=\"float-start\"><button type=\"button\" class=\"btn btn-xs btn-light\" data-bs-container=\"body\" data-bs-toggle=\"popover\" data-bs-html=\"true\" data-bs-placement=\"right\" data-bs-content=\"<a href='" . $query_string . "devices." . $key . "=" . $item->attributes->{$key} . "'>" . __('Include') . "</a><br><a href='" . $query_string . "devices." . $key . "=!=" . $item->attributes->{$key} . "'>" . __('Exclude') . "</a>\"><span class=\"fa fa-filter fa-xs\"></span></button></span>&nbsp;" . $item->attributes->{$key} . "</td>\n";
}
Expand Down
29 changes: 29 additions & 0 deletions app/Views/devicesRead.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@
<?php if (!empty($resource->snmp_oid)) { ?>
<li class="list-group-item section_toggle" data-section="snmp_section"><img class="device-menu-icon" src="<?= base_url() ?>icons/snmp_details.svg" alt=""> <a href="#"><?= __('SNMP Details') ?></a></li>
<?php } ?>
<?php if ($config->product === 'enterprise') { ?>
<li class="list-group-item section_toggle" data-section="tags_section"><img class="device-menu-icon" src="<?= base_url() ?>icons/tags.svg" alt=""> <a href="#"><?= __('Tags') ?></a></li>
<?php } ?>
<?php if (!empty($included['windows'])) { ?>
<li class="list-group-item section_toggle" data-section="windows_section"><img class="device-menu-icon" src="<?= base_url() ?>icons/windows.svg" alt=""> <a href="#"><?= __('Windows') ?></a></li>
<?php } ?>
Expand Down Expand Up @@ -913,6 +916,25 @@
</div>
</div>

<?php if ($config->product === 'enterprise') { ?>
<div style="margin-bottom:20px; display:none;" class="card" id="tags_section">
<?= device_panel('tags', $user->toolbar_style, 0, base_url() . "icons/tags.svg", $update); ?>
<div class="card-body">
<?php foreach ($resource->tags as $tag) {
$othertags = array();
foreach ($resource->tags as $mytag) {
if ($mytag !== $tag) {
$othertags[] = $mytag;
}
}
$othertags = json_encode($othertags);
echo '<button type="button" class="btn btn-primary rounded-pill" style="margin-right:20px;"><strong><a style="color:white;" href="' . url_to('devicesCollection') . '?devices.tags=' . $tag . '">' . $tag . '</a>&nbsp;&nbsp;&nbsp;</strong><a href="#" class="delete_tags" style="color:white;" data-tags=\'' . $othertags . '\'><span class="badge text-bg-secondary"><i class="close white-text fas fa-times"></i></span></a></button>';
}
?>
</div>
</div>
<?php } ?>


<div style="margin-bottom:20px; display:none;" class="card" id="windows_section">
<?= device_panel('Windows', $user->toolbar_style, $resource->id, '', $update); ?>
Expand Down Expand Up @@ -2397,5 +2419,12 @@
$("#oa_panel_buttons").append('<form style="padding-right:4px;" id="componentsCreate" method="post" action="<?= url_to('componentsCreate') ?>"><?= $form_contents ?><button id="componentsCreateButton" class="btn btn-light mb-2" type="submit" title="<?= __('Discover') ?>"><?= __('Discover') ?></button></form>');
<?php } ?>

$("#device_panel_tags").append('<div id="tags_control" class="btn-group" style="padding-left:20px; height:40px; display:none;" role="group"><div class="input-group"><input type="text" data-tags=\'<?= json_encode($resource->tags) ?>\' id="tags_add" class="form-control form-control-sm"><div class="float-end" style="padding-left:4px;"><button data-attribute="tags" class="btn btn-outline-success submit" title="Submit" style=""><span style="font-size: 1.2rem;" class="fa fa-check"></span></button></div></div></div>');

$(document).on('click', '#add_tags', function (e) {
$("#tags_control").css('display', 'block');
});


}
</script>
9 changes: 9 additions & 0 deletions app/Views/shared/read_functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,15 @@ function device_panel(string $name = '', string $toolbar = '', int $device_id =
$panel_add_button = "<a role=\"button\" class=\"btn btn-light mb-2\" tabindex=0 title=\"" . __("Add") . "\" href=\"" . url_to('componentsCreateForm', $name, $device_id) . "\">" . __("Add") . "</a>";
}
}
if ($name === 'tags') {
if ($toolbar === 'icontext') {
$panel_add_button = "<a role=\"button\" class=\"btn btn-light mb-2\" tabindex=0 title=\"" . __("Add") . "\" href=\"#\" id=\"add_tags\"><span style=\"margin-right:6px;\" class=\"fa fa-plus text-oa-success\"></span>" . __("Add") . "</a>";
} else if ($toolbar === 'icon') {
$panel_add_button = "<a role=\"button\" class=\"btn btn-light mb-2\" tabindex=0 title=\"" . __("Add") . "\" href=\"#\" id=\"add_tags\"><span class=\"fa fa-plus text-oa-success\"></span></a>";
} else {
$panel_add_button = "<a role=\"button\" class=\"btn btn-light mb-2\" tabindex=0 title=\"" . __("Add") . "\" href=\"#\" id=\"add_tags\">" . __("Add") . "</a>";
}
}
}

if (!in_array(strtolower($export_name), ['audit_log', 'bios', 'certificate', 'change_log', 'discovery_log', 'disk', 'dns', 'edit_log', 'file', 'ip', 'log', 'memory', 'module', 'monitor', 'motherboard', 'netstat', 'network', 'nmap', 'optical', 'pagefile', 'partition', 'policy', 'print_queue', 'processor', 'radio', 'route', 'san', 'scsi', 'server', 'server_item', 'service', 'share', 'software', 'software_key', 'sound', 'task', 'usb', 'user', 'user_group', 'variable', 'video', 'vm', 'windows']) or empty($device_id)) {
Expand Down
3 changes: 2 additions & 1 deletion other/open-audit.sql
Original file line number Diff line number Diff line change
Expand Up @@ -891,7 +891,7 @@ INSERT INTO `configuration` VALUES (NULL,'delete_noncurrent_windows','n','bool',
INSERT INTO `configuration` VALUES (NULL,'device_auto_delete','n','bool','y','system','2000-01-01 00:00:00','Should we delete the device data completely from the database when the device status is set to Deleted.');
INSERT INTO `configuration` VALUES (NULL,'devices_default_display_columns','devices.id,devices.icon,devices.type,devices.name,devices.ip,devices.dns_fqdn,devices.identification,devices.description,devices.manufacturer,devices.os_family,devices.status','text','y','system','2000-01-01 00:00:00','When requesting a list of devices, display these columns.');
INSERT INTO `configuration` VALUES (NULL,'devices_default_group_columns','devices.id,devices.icon,devices.type,devices.name,devices.ip,devices.dns_fqdn,devices.identification,devices.description,devices.manufacturer,devices.os_family,devices.status,devices.org_id','text','y','system','2000-01-01 00:00:00','When requesting a group of devices, retrieve and display these columns.');
INSERT INTO `configuration` VALUES (NULL,'devices_default_retrieve_columns','devices.id,devices.icon,devices.type,devices.name,devices.ip,devices.uuid,devices.hostname,devices.dns_hostname,devices.domain,devices.dns_domain,devices.dbus_identifier,devices.fqdn,devices.dns_fqdn,devices.description,devices.os_group,devices.os_family,devices.os_name,devices.os_version,devices.manufacturer,devices.model,devices.serial,devices.form_factor,devices.status,devices.environment,devices.class,devices.function,devices.org_id,devices.location_id,devices.snmp_oid,devices.sysDescr,devices.sysObjectID,devices.sysUpTime,devices.sysContact,devices.sysName,devices.sysLocation,devices.first_seen,devices.last_seen,devices.last_seen_by,devices.identification','text','y','system','2000-01-01 00:00:00','When requesting a list of devices, provide these columns.');
INSERT INTO `configuration` VALUES (NULL,'devices_default_retrieve_columns','devices.id,devices.icon,devices.type,devices.name,devices.ip,devices.uuid,devices.hostname,devices.dns_hostname,devices.domain,devices.dns_domain,devices.dbus_identifier,devices.fqdn,devices.dns_fqdn,devices.description,devices.os_group,devices.os_family,devices.os_name,devices.os_version,devices.manufacturer,devices.model,devices.serial,devices.form_factor,devices.status,devices.environment,devices.class,devices.function,devices.org_id,devices.location_id,devices.snmp_oid,devices.sysDescr,devices.sysObjectID,devices.sysUpTime,devices.sysContact,devices.sysName,devices.sysLocation,devices.first_seen,devices.last_seen,devices.last_seen_by,devices.identification,devices.tags','text','y','system','2000-01-01 00:00:00','When requesting a list of devices, provide these columns.');
INSERT INTO `configuration` VALUES (NULL,'discovery_default_scan_option','1','number','y','system','2000-01-01 00:00:00','The default discovery options for Nmap.');
INSERT INTO `configuration` VALUES (NULL,'discovery_ip_exclude','','text','y','system','2000-01-01 00:00:00','Populate this list with ip addresses to be excluded from discovery. IPs should be separated by a space.');
INSERT INTO `configuration` VALUES (NULL,'discovery_limit','20','number','y','system','2000-01-01 00:00:00','The maximum number of concurrent discoveries we should run.');
Expand Down Expand Up @@ -1133,6 +1133,7 @@ CREATE TABLE `devices` (
`os_version` varchar(200) NOT NULL DEFAULT '',
`os_cpe` varchar(200) NOT NULL DEFAULT '',
`kernel_version` varchar(200) NOT NULL DEFAULT '',
`tags` text NOT NULL DEFAULT '[]',
`attached_device_id` int(10) unsigned DEFAULT NULL,
`manufacturer` varchar(100) NOT NULL DEFAULT '',
`manufacturer_code` varchar(200) NOT NULL DEFAULT '',
Expand Down
Loading

0 comments on commit 27da1a6

Please sign in to comment.