mirror of
https://github.com/jokob-sk/NetAlertX.git
synced 2026-04-04 09:11:34 -07:00
review fixes
This commit is contained in:
@@ -83,7 +83,8 @@
|
|||||||
}
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{ "function": "SET_ALWAYS",
|
{
|
||||||
|
"function": "SET_ALWAYS",
|
||||||
"type": {
|
"type": {
|
||||||
"dataType": "array",
|
"dataType": "array",
|
||||||
"elements": [
|
"elements": [
|
||||||
|
|||||||
@@ -383,7 +383,69 @@
|
|||||||
"string": "Retrieve only devices that are reachable."
|
"string": "Retrieve only devices that are reachable."
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
},
|
||||||
|
{
|
||||||
|
"function": "SET_ALWAYS",
|
||||||
|
"type": {
|
||||||
|
"dataType": "array",
|
||||||
|
"elements": [
|
||||||
|
{
|
||||||
|
"elementType": "select",
|
||||||
|
"elementOptions": [{ "multiple": "true", "orderable": "true" }],
|
||||||
|
"transformers": []
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"default_value": ["devMac", "devLastIP"],
|
||||||
|
"options": [
|
||||||
|
"devMac",
|
||||||
|
"devLastIP"
|
||||||
|
],
|
||||||
|
"localized": ["name", "description"],
|
||||||
|
"name": [
|
||||||
|
{
|
||||||
|
"language_code": "en_us",
|
||||||
|
"string": "Set always columns"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"description": [
|
||||||
|
{
|
||||||
|
"language_code": "en_us",
|
||||||
|
"string": "These columns are always overwritten by this plugin, unless the user locks or overwrites the value."
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"function": "SET_EMPTY",
|
||||||
|
"type": {
|
||||||
|
"dataType": "array",
|
||||||
|
"elements": [
|
||||||
|
{
|
||||||
|
"elementType": "select",
|
||||||
|
"elementOptions": [{ "multiple": "true", "orderable": "true" }],
|
||||||
|
"transformers": []
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"default_value": [],
|
||||||
|
"options": [
|
||||||
|
"devMac",
|
||||||
|
"devLastIP"
|
||||||
|
],
|
||||||
|
"localized": ["name", "description"],
|
||||||
|
"name": [
|
||||||
|
{
|
||||||
|
"language_code": "en_us",
|
||||||
|
"string": "Set empty columns"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"description": [
|
||||||
|
{
|
||||||
|
"language_code": "en_us",
|
||||||
|
"string": "These columns are only overwritten if they are empty or set to NEWDEV."
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
],
|
],
|
||||||
"database_column_definitions": [
|
"database_column_definitions": [
|
||||||
{
|
{
|
||||||
@@ -568,68 +630,6 @@
|
|||||||
"string": "Статус"
|
"string": "Статус"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
},
|
|
||||||
{
|
|
||||||
"function": "SET_ALWAYS",
|
|
||||||
"type": {
|
|
||||||
"dataType": "array",
|
|
||||||
"elements": [
|
|
||||||
{
|
|
||||||
"elementType": "select",
|
|
||||||
"elementOptions": [{ "multiple": "true", "orderable": "true" }],
|
|
||||||
"transformers": []
|
|
||||||
}
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"default_value": ["devMac", "devLastIP"],
|
|
||||||
"options": [
|
|
||||||
"devMac",
|
|
||||||
"devLastIP"
|
|
||||||
],
|
|
||||||
"localized": ["name", "description"],
|
|
||||||
"name": [
|
|
||||||
{
|
|
||||||
"language_code": "en_us",
|
|
||||||
"string": "Set always columns"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"description": [
|
|
||||||
{
|
|
||||||
"language_code": "en_us",
|
|
||||||
"string": "These columns are always overwritten by this plugin, unless the user locks or overwrites the value."
|
|
||||||
}
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"function": "SET_EMPTY",
|
|
||||||
"type": {
|
|
||||||
"dataType": "array",
|
|
||||||
"elements": [
|
|
||||||
{
|
|
||||||
"elementType": "select",
|
|
||||||
"elementOptions": [{ "multiple": "true", "orderable": "true" }],
|
|
||||||
"transformers": []
|
|
||||||
}
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"default_value": [],
|
|
||||||
"options": [
|
|
||||||
"devMac",
|
|
||||||
"devLastIP"
|
|
||||||
],
|
|
||||||
"localized": ["name", "description"],
|
|
||||||
"name": [
|
|
||||||
{
|
|
||||||
"language_code": "en_us",
|
|
||||||
"string": "Set empty columns"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"description": [
|
|
||||||
{
|
|
||||||
"language_code": "en_us",
|
|
||||||
"string": "These columns are only overwritten if they are empty or set to NEWDEV."
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
@@ -216,36 +216,6 @@
|
|||||||
}
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
|
||||||
"function": "SET_ALWAYS",
|
|
||||||
"type": {
|
|
||||||
"dataType": "array",
|
|
||||||
"elements": [
|
|
||||||
{
|
|
||||||
"elementType": "select",
|
|
||||||
"elementOptions": [{ "multiple": "true"}],
|
|
||||||
"transformers": []
|
|
||||||
}
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"default_value": ["devLastIP"],
|
|
||||||
"options": [
|
|
||||||
"devLastIP"
|
|
||||||
],
|
|
||||||
"localized": ["name", "description"],
|
|
||||||
"name": [
|
|
||||||
{
|
|
||||||
"language_code": "en_us",
|
|
||||||
"string": "Set always columns"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"description": [
|
|
||||||
{
|
|
||||||
"language_code": "en_us",
|
|
||||||
"string": "These columns are always overwritten by this plugin, unless the user locks or overwrites the value."
|
|
||||||
}
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
"function": "SET_EMPTY",
|
"function": "SET_EMPTY",
|
||||||
"type": {
|
"type": {
|
||||||
@@ -309,39 +279,6 @@
|
|||||||
}
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
|
||||||
"function": "SET_EMPTY",
|
|
||||||
"type": {
|
|
||||||
"dataType": "array",
|
|
||||||
"elements": [
|
|
||||||
{
|
|
||||||
"elementType": "select",
|
|
||||||
"elementOptions": [{ "multiple": "true"}],
|
|
||||||
"transformers": []
|
|
||||||
}
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"default_value": ["devName", "devVendor"],
|
|
||||||
"options": [
|
|
||||||
"devMac",
|
|
||||||
"devLastIP",
|
|
||||||
"devName",
|
|
||||||
"devVendor"
|
|
||||||
],
|
|
||||||
"localized": ["name", "description"],
|
|
||||||
"name": [
|
|
||||||
{
|
|
||||||
"language_code": "en_us",
|
|
||||||
"string": "Set empty columns"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"description": [
|
|
||||||
{
|
|
||||||
"language_code": "en_us",
|
|
||||||
"string": "These columns are only overwritten if they are empty or set to NEWDEV."
|
|
||||||
}
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
"function": "WATCH",
|
"function": "WATCH",
|
||||||
"type": {
|
"type": {
|
||||||
|
|||||||
@@ -40,8 +40,6 @@
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
"params": [],
|
"params": [],
|
||||||
}
|
|
||||||
],
|
|
||||||
"settings": [
|
"settings": [
|
||||||
{
|
{
|
||||||
"function": "RUN",
|
"function": "RUN",
|
||||||
|
|||||||
@@ -497,7 +497,7 @@ def api_device_field_lock(mac, payload=None):
|
|||||||
else:
|
else:
|
||||||
return jsonify(result), 400
|
return jsonify(result), 400
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
mylog("error", f"Error locking field {field_name} for {mac}: {str(e)}")
|
mylog("none", f"Error locking field {field_name} for {mac}: {str(e)}")
|
||||||
return jsonify({"success": False, "error": str(e)}), 500
|
return jsonify({"success": False, "error": str(e)}), 500
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -275,7 +275,7 @@ class UpdateDeviceColumnRequest(BaseModel):
|
|||||||
|
|
||||||
class LockDeviceFieldRequest(BaseModel):
|
class LockDeviceFieldRequest(BaseModel):
|
||||||
"""Request to lock/unlock a device field."""
|
"""Request to lock/unlock a device field."""
|
||||||
fieldName: str = Field(..., description="Field name to lock/unlock (devMac, devName, devLastIP, etc.)")
|
fieldName: Optional[str] = Field(None, description="Field name to lock/unlock (devMac, devName, devLastIP, etc.)")
|
||||||
lock: bool = Field(True, description="True to lock the field, False to unlock")
|
lock: bool = Field(True, description="True to lock the field, False to unlock")
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -595,6 +595,30 @@ class DeviceInstance:
|
|||||||
cur.execute(sql, values)
|
cur.execute(sql, values)
|
||||||
conn.commit()
|
conn.commit()
|
||||||
|
|
||||||
|
if data.get("createNew", False):
|
||||||
|
# Initialize source-tracking fields on device creation.
|
||||||
|
# We always mark devMacSource as NEWDEV, and mark other tracked fields
|
||||||
|
# as NEWDEV only if the create payload provides a non-empty value.
|
||||||
|
initial_sources = {FIELD_SOURCE_MAP["devMac"]: "NEWDEV"}
|
||||||
|
for field_name, source_field in FIELD_SOURCE_MAP.items():
|
||||||
|
if field_name == "devMac":
|
||||||
|
continue
|
||||||
|
field_value = data.get(field_name)
|
||||||
|
if field_value is None:
|
||||||
|
continue
|
||||||
|
if isinstance(field_value, str) and not field_value.strip():
|
||||||
|
continue
|
||||||
|
initial_sources[source_field] = "NEWDEV"
|
||||||
|
|
||||||
|
if initial_sources:
|
||||||
|
# Apply source updates in a single statement for the newly inserted row.
|
||||||
|
set_clause = ", ".join([f"{col}=?" for col in initial_sources.keys()])
|
||||||
|
source_values = list(initial_sources.values())
|
||||||
|
source_values.append(normalized_mac)
|
||||||
|
source_sql = f"UPDATE Devices SET {set_clause} WHERE devMac = ?"
|
||||||
|
cur.execute(source_sql, source_values)
|
||||||
|
conn.commit()
|
||||||
|
|
||||||
# Enforce source tracking on user updates
|
# Enforce source tracking on user updates
|
||||||
# User-updated fields should have their *Source set to "USER"
|
# User-updated fields should have their *Source set to "USER"
|
||||||
user_updated_fields = {k: v for k, v in data.items() if k in FIELD_SOURCE_MAP}
|
user_updated_fields = {k: v for k, v in data.items() if k in FIELD_SOURCE_MAP}
|
||||||
@@ -683,26 +707,30 @@ class DeviceInstance:
|
|||||||
if field_name not in FIELD_SOURCE_MAP:
|
if field_name not in FIELD_SOURCE_MAP:
|
||||||
return {"success": False, "error": f"Field {field_name} does not support locking"}
|
return {"success": False, "error": f"Field {field_name} does not support locking"}
|
||||||
|
|
||||||
|
mac_normalized = normalize_mac(mac)
|
||||||
|
conn = get_temp_db_connection()
|
||||||
try:
|
try:
|
||||||
conn = get_temp_db_connection()
|
lock_field(mac_normalized, field_name, conn)
|
||||||
lock_field(mac, field_name, conn)
|
|
||||||
conn.close()
|
|
||||||
return {"success": True, "message": f"Field {field_name} locked"}
|
return {"success": True, "message": f"Field {field_name} locked"}
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return {"success": False, "error": str(e)}
|
return {"success": False, "error": str(e)}
|
||||||
|
finally:
|
||||||
|
conn.close()
|
||||||
|
|
||||||
def unlockDeviceField(self, mac, field_name):
|
def unlockDeviceField(self, mac, field_name):
|
||||||
"""Unlock a device field so plugins can overwrite it again."""
|
"""Unlock a device field so plugins can overwrite it again."""
|
||||||
if field_name not in FIELD_SOURCE_MAP:
|
if field_name not in FIELD_SOURCE_MAP:
|
||||||
return {"success": False, "error": f"Field {field_name} does not support unlocking"}
|
return {"success": False, "error": f"Field {field_name} does not support unlocking"}
|
||||||
|
|
||||||
|
mac_normalized = normalize_mac(mac)
|
||||||
|
conn = get_temp_db_connection()
|
||||||
try:
|
try:
|
||||||
conn = get_temp_db_connection()
|
unlock_field(mac_normalized, field_name, conn)
|
||||||
unlock_field(mac, field_name, conn)
|
|
||||||
conn.close()
|
|
||||||
return {"success": True, "message": f"Field {field_name} unlocked"}
|
return {"success": True, "message": f"Field {field_name} unlocked"}
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return {"success": False, "error": str(e)}
|
return {"success": False, "error": str(e)}
|
||||||
|
finally:
|
||||||
|
conn.close()
|
||||||
|
|
||||||
def copyDevice(self, mac_from, mac_to):
|
def copyDevice(self, mac_from, mac_to):
|
||||||
"""Copy a device entry from one MAC to another."""
|
"""Copy a device entry from one MAC to another."""
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ from models.device_instance import DeviceInstance
|
|||||||
from scan.name_resolution import NameResolver
|
from scan.name_resolution import NameResolver
|
||||||
from scan.device_heuristics import guess_icon, guess_type
|
from scan.device_heuristics import guess_icon, guess_type
|
||||||
from db.db_helper import sanitize_SQL_input, list_to_where, safe_int
|
from db.db_helper import sanitize_SQL_input, list_to_where, safe_int
|
||||||
|
from helper import format_ip_long
|
||||||
|
|
||||||
# Make sure log level is initialized correctly
|
# Make sure log level is initialized correctly
|
||||||
Logger(get_setting_value("LOG_LEVEL"))
|
Logger(get_setting_value("LOG_LEVEL"))
|
||||||
@@ -544,6 +545,12 @@ def create_new_devices(db):
|
|||||||
# Derive primary IP family values
|
# Derive primary IP family values
|
||||||
cur_IP = str(cur_IP).strip() if cur_IP else ""
|
cur_IP = str(cur_IP).strip() if cur_IP else ""
|
||||||
cur_IP_normalized = check_IP_format(cur_IP) if ":" not in cur_IP else cur_IP
|
cur_IP_normalized = check_IP_format(cur_IP) if ":" not in cur_IP else cur_IP
|
||||||
|
|
||||||
|
# Validate IPv6 addresses using format_ip_long for consistency
|
||||||
|
if ":" in cur_IP_normalized:
|
||||||
|
validated_ipv6 = format_ip_long(cur_IP_normalized)
|
||||||
|
cur_IP_normalized = validated_ipv6 if validated_ipv6 else ""
|
||||||
|
|
||||||
primary_ipv4 = cur_IP_normalized if cur_IP_normalized and ":" not in cur_IP_normalized else ""
|
primary_ipv4 = cur_IP_normalized if cur_IP_normalized and ":" not in cur_IP_normalized else ""
|
||||||
primary_ipv6 = cur_IP_normalized if cur_IP_normalized and ":" in cur_IP_normalized else ""
|
primary_ipv6 = cur_IP_normalized if cur_IP_normalized and ":" in cur_IP_normalized else ""
|
||||||
|
|
||||||
|
|||||||
@@ -46,16 +46,16 @@ def cleanup_test_device(test_mac):
|
|||||||
# Clean before test
|
# Clean before test
|
||||||
try:
|
try:
|
||||||
device_handler.deleteDeviceByMAC(test_mac)
|
device_handler.deleteDeviceByMAC(test_mac)
|
||||||
except Exception:
|
except Exception as e:
|
||||||
pass
|
pytest.fail(f"Pre-test cleanup failed for {test_mac}: {e}")
|
||||||
|
|
||||||
yield
|
yield
|
||||||
|
|
||||||
# Clean after test
|
# Clean after test
|
||||||
try:
|
try:
|
||||||
device_handler.deleteDeviceByMAC(test_mac)
|
device_handler.deleteDeviceByMAC(test_mac)
|
||||||
except Exception:
|
except Exception as e:
|
||||||
pass
|
pytest.fail(f"Post-test cleanup failed for {test_mac}: {e}")
|
||||||
|
|
||||||
|
|
||||||
class TestDeviceFieldLock:
|
class TestDeviceFieldLock:
|
||||||
@@ -269,6 +269,34 @@ class TestDeviceFieldLock:
|
|||||||
class TestFieldLockIntegration:
|
class TestFieldLockIntegration:
|
||||||
"""Integration tests for field locking with plugin overwrites."""
|
"""Integration tests for field locking with plugin overwrites."""
|
||||||
|
|
||||||
|
def test_lock_unlock_normalizes_mac(self, test_mac):
|
||||||
|
"""Lock/unlock should normalize MAC addresses before DB updates."""
|
||||||
|
device_handler = DeviceInstance()
|
||||||
|
|
||||||
|
create_result = device_handler.setDeviceData(
|
||||||
|
test_mac,
|
||||||
|
{
|
||||||
|
"devName": "Original Name",
|
||||||
|
"devLastIP": "192.168.1.100",
|
||||||
|
"createNew": True,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
assert create_result.get("success") is True
|
||||||
|
|
||||||
|
mac_variant = "aa-bb-cc-dd-ee-ff"
|
||||||
|
|
||||||
|
lock_result = device_handler.lockDeviceField(mac_variant, "devName")
|
||||||
|
assert lock_result.get("success") is True
|
||||||
|
|
||||||
|
device_data = device_handler.getDeviceData(test_mac)
|
||||||
|
assert device_data.get("devNameSource") == "LOCKED"
|
||||||
|
|
||||||
|
unlock_result = device_handler.unlockDeviceField(mac_variant, "devName")
|
||||||
|
assert unlock_result.get("success") is True
|
||||||
|
|
||||||
|
device_data = device_handler.getDeviceData(test_mac)
|
||||||
|
assert device_data.get("devNameSource") != "LOCKED"
|
||||||
|
|
||||||
def test_locked_field_blocks_plugin_overwrite(self, test_mac, auth_headers):
|
def test_locked_field_blocks_plugin_overwrite(self, test_mac, auth_headers):
|
||||||
"""Verify locked fields prevent plugin source overwrites."""
|
"""Verify locked fields prevent plugin source overwrites."""
|
||||||
device_handler = DeviceInstance()
|
device_handler = DeviceInstance()
|
||||||
|
|||||||
Reference in New Issue
Block a user