Skip to content

Commit

Permalink
fix: upgrade varbinds validation (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
wojtekzyla authored Aug 10, 2023
1 parent 6011b0f commit 9edebff
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 69 deletions.
10 changes: 5 additions & 5 deletions backend/SC4SNMP_UI_backend/common/backend_ui_conversions.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ def backend2ui(self, document: dict, **kwargs):
var_binds = []
for vb in backend_var_binds:
new_vb = {
"family": vb[0],
"category": vb[1] if len(vb) >= 2 else "",
"component": vb[0],
"object": vb[1] if len(vb) >= 2 else "",
"index": '.'.join(map(str, vb[2:])) if len(vb) >= 3 else "",
}
var_binds.append(new_vb)
Expand Down Expand Up @@ -168,9 +168,9 @@ def ui2backend(self, document: dict, **kwargs):
}
var_binds = []
for var_b in document['varBinds']:
single_var_bind = [var_b['family']]
if len(var_b['category']) > 0:
single_var_bind.append(var_b['category'])
single_var_bind = [var_b['component']]
if len(var_b['object']) > 0:
single_var_bind.append(var_b['object'])
if len(var_b['index']) > 0:
single_var_bind += var_b['index'].split(".")
var_binds.append(single_var_bind)
Expand Down
26 changes: 13 additions & 13 deletions backend/tests/common/test_backend_ui_conversions.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ def setUpClass(cls):
"patterns": [],
"conditions": []
},
"varBinds": [{"family": "IF-MIB", "category": "ifInDiscards", "index": "1.test.2"},
{"family": "IF-MIB", "category": "ifInDiscards", "index": "1"},
{"family": "IF-MIB", "category": "", "index": ""},
{"family": "IF-MIB", "category": "ifOutErrors", "index": ""}],
"varBinds": [{"component": "IF-MIB", "object": "ifInDiscards", "index": "1.test.2"},
{"component": "IF-MIB", "object": "ifInDiscards", "index": "1"},
{"component": "IF-MIB", "object": "", "index": ""},
{"component": "IF-MIB", "object": "ifOutErrors", "index": ""}],
"profileInInventory": True
}

Expand All @@ -43,9 +43,9 @@ def setUpClass(cls):
"patterns": [],
"conditions": []
},
"varBinds": [{"family": "IF-MIB", "category": "ifInDiscards", "index": "1"},
{"family": "IF-MIB", "category": "", "index": ""},
{"family": "IF-MIB", "category": "ifOutErrors", "index": ""}],
"varBinds": [{"component": "IF-MIB", "object": "ifInDiscards", "index": "1"},
{"component": "IF-MIB", "object": "", "index": ""},
{"component": "IF-MIB", "object": "ifOutErrors", "index": ""}],
"profileInInventory": False
}

Expand All @@ -59,9 +59,9 @@ def setUpClass(cls):
"patterns": [{"pattern": "^MIKROTIK"}, {"pattern": "^MIKROTIK2"}],
"conditions": []
},
"varBinds": [{"family": "IF-MIB", "category": "ifInDiscards", "index": "1"},
{"family": "IF-MIB", "category": "", "index": ""},
{"family": "IF-MIB", "category": "ifOutErrors", "index": ""}],
"varBinds": [{"component": "IF-MIB", "object": "ifInDiscards", "index": "1"},
{"component": "IF-MIB", "object": "", "index": ""},
{"component": "IF-MIB", "object": "ifOutErrors", "index": ""}],
"profileInInventory": True
}

Expand All @@ -80,9 +80,9 @@ def setUpClass(cls):
{"field": "field: IF-MIB.ifIndex", "operation": "greater than", "value": ["5"]}
]
},
"varBinds": [{"family": "IF-MIB", "category": "ifInDiscards", "index": "1"},
{"family": "IF-MIB", "category": "", "index": ""},
{"family": "IF-MIB", "category": "ifOutErrors", "index": ""}],
"varBinds": [{"component": "IF-MIB", "object": "ifInDiscards", "index": "1"},
{"component": "IF-MIB", "object": "", "index": ""},
{"component": "IF-MIB", "object": "ifOutErrors", "index": ""}],
"profileInInventory": False
}

Expand Down
12 changes: 6 additions & 6 deletions backend/tests/ui_handling/get_endpoints/test_get_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ def test_get_all_profiles_list(m_client, client):
"field": "",
"patterns": []
},
"varBinds": [{"family": "IF-MIB", "category": "ifInDiscards", "index": "1"},
{"family": "IF-MIB", "category": "", "index": ""},
{"family": "IF-MIB", "category": "ifOutErrors", "index": ""}],
"varBinds": [{"component": "IF-MIB", "object": "ifInDiscards", "index": "1"},
{"component": "IF-MIB", "object": "", "index": ""},
{"component": "IF-MIB", "object": "ifOutErrors", "index": ""}],
'profileInInventory': True,
}

Expand All @@ -73,9 +73,9 @@ def test_get_all_profiles_list(m_client, client):
"field": "SNMPv2-MIB.sysObjectID",
"patterns": [{"pattern": "^MIKROTIK"}, {"pattern": "^MIKROTIK2"}]
},
"varBinds": [{"family": "IF-MIB", "category": "ifInDiscards", "index": "1"},
{"family": "IF-MIB", "category": "", "index": ""},
{"family": "IF-MIB", "category": "ifOutErrors", "index": ""}],
"varBinds": [{"component": "IF-MIB", "object": "ifInDiscards", "index": "1"},
{"component": "IF-MIB", "object": "", "index": ""},
{"component": "IF-MIB", "object": "ifOutErrors", "index": ""}],
'profileInInventory': True,
}

Expand Down
10 changes: 5 additions & 5 deletions backend/tests/ui_handling/post_endpoints/test_post_profiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ def test_add_profile_record_success(m_insert, m_find, client):
"field": "",
"patterns": None
},
"varBinds": [{"family": "IF-MIB", "category": "ifInDiscards", "index": "1"},
{"family": "IF-MIB", "category": "", "index": ""},
{"family": "IF-MIB", "category": "ifOutErrors", "index": ""}]
"varBinds": [{"component": "IF-MIB", "object": "ifInDiscards", "index": "1"},
{"component": "IF-MIB", "object": "", "index": ""},
{"component": "IF-MIB", "object": "ifOutErrors", "index": ""}]
}
backend_prof = {
"profile_1": {
Expand Down Expand Up @@ -133,7 +133,7 @@ def test_update_profile_record_no_name_change_success(m_find, m_update, client):
"field": "SNMPv2-MIB.sysObjectID",
"patterns": [{"pattern": "^MIKROTIK"}, {"pattern": "^MIKROTIK2"}]
},
"varBinds": [{"family": "IF-MIB", "category": "ifInDiscards", "index": "1"}]
"varBinds": [{"component": "IF-MIB", "object": "ifInDiscards", "index": "1"}]
}

backend_prof_1_old = {
Expand Down Expand Up @@ -183,7 +183,7 @@ def test_update_profile_record_with_name_change_success(m_find, m_update, client
"field": "SNMPv2-MIB.sysObjectID",
"patterns": [{"pattern": "^MIKROTIK"}, {"pattern": "^MIKROTIK2"}]
},
"varBinds": [{"family": "IF-MIB", "category": "ifInDiscards", "index": "1"}]
"varBinds": [{"component": "IF-MIB", "object": "ifInDiscards", "index": "1"}]
}

backend_prof_1_new = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ function getExpansionRow(row) {
<Table.Cell>{/* Empty cell */}</Table.Cell>
<Table.Cell>
{row.varBinds.map((value) => (
<P style={{height: "20px", overflow: "hidden", whiteSpace: "nowrap", textOverflow: "ellipsis"}} key={createDOMID()}>{value.family}</P>
<P style={{height: "20px", overflow: "hidden", whiteSpace: "nowrap", textOverflow: "ellipsis"}} key={createDOMID()}>{value.component}</P>
))}
</Table.Cell>

<Table.Cell>
{row.varBinds.map((value) => (
<P style={{height: "20px", overflow: "hidden", whiteSpace: "nowrap", textOverflow: "ellipsis"}} key={createDOMID()}>{value.category}</P>
<P style={{height: "20px", overflow: "hidden", whiteSpace: "nowrap", textOverflow: "ellipsis"}} key={createDOMID()}>{value.object}</P>
))}
</Table.Cell>

Expand All @@ -50,8 +50,8 @@ function ProfilesList() {
{sortKey: 'profileName', label: 'Profile name'},
{sortKey: 'frequency', label: 'Frequency'},
{sortKey: 'profileType', label: 'Profile type'},
{sortKey: `mibFamily`, label: 'MIB family'},
{sortKey: `mibCategory`, label: 'MIB category'},
{sortKey: `mibComponent`, label: 'MIB Component'},
{sortKey: `mibObject`, label: 'MIB Object'},
{sortKey: `index`, label: 'MIB Index'},
{sortKey: `actions`, label: 'Actions'},
];
Expand Down
20 changes: 10 additions & 10 deletions frontend/packages/manager/src/components/profiles/VarBinds.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,15 @@ function VarBinds(props){
setReload((prev)=>{return !prev});
};

const handleItemValueFamily = (index, e) => {
const handleItemValueComponent = (index, e) => {
const varBindsCopy = ProfCtx.varBinds;
varBindsCopy[index].family = e.target.value
varBindsCopy[index].component = e.target.value
ProfCtx.setVarBinds(varBindsCopy);
}

const handleItemValueCategory = (index, e) => {
const handleItemValueObject = (index, e) => {
const varBindsCopy = ProfCtx.varBinds;
varBindsCopy[index].category = e.target.value
varBindsCopy[index].object = e.target.value
ProfCtx.setVarBinds(varBindsCopy);
}

Expand All @@ -74,7 +74,7 @@ function VarBinds(props){
const newIndex = rowItems.length;
const keyID = createDOMID();
const varBindsCopy = ProfCtx.varBinds;
varBindsCopy.push({family: "", category: "", index: ""});
varBindsCopy.push({component: "", object: "", index: ""});
indicesCopy[`${keyID}`] = newIndex;
setIndices(indicesCopy);
ProfCtx.setVarBinds(varBindsCopy);
Expand All @@ -93,13 +93,13 @@ function VarBinds(props){
<FormRows.Row data-test={`sc4snmp:form:varbind-row-${indexCopy}`} index={indexCopy} key={createDOMID()} onRequestRemove={handleRequestRemove}>
<ValidationGroup>
<div style={{display: 'flex'}}>
<Text data-test={`sc4snmp:form:varbind${indexCopy}-mib-family-input`} defaultValue={value.family} placeholder="MIB family"
onChange={e => handleItemValueFamily(newIndices[`${keyID}`], e)}
<Text data-test={`sc4snmp:form:varbind-mib-component-input-${indexCopy}`} defaultValue={value.component} placeholder="MIB component"
onChange={e => handleItemValueComponent(newIndices[`${keyID}`], e)}
error={((ValCtx.varBindsErrors && newIndices[`${keyID}`] in ValCtx.varBindsErrors))}/>
<Text data-test={`sc4snmp:form:varbind${indexCopy}-mib-category-input`} defaultValue={value.category} placeholder="MIB category"
onChange={e => handleItemValueCategory(newIndices[`${keyID}`], e)}
<Text data-test={`sc4snmp:form:varbind-mib-object-input-${indexCopy}`} defaultValue={value.object} placeholder="MIB object"
onChange={e => handleItemValueObject(newIndices[`${keyID}`], e)}
error={((ValCtx.varBindsErrors && newIndices[`${keyID}`] in ValCtx.varBindsErrors))}/>
<Text data-test={`sc4snmp:form:varbind${indexCopy}-mib-index-input`} defaultValue={value.index} placeholder="MIB index"
<Text data-test={`sc4snmp:form:varbind-mib-index-input-${indexCopy}`} defaultValue={value.index} placeholder="MIB index"
onChange={e => handleItemValueIndex(newIndices[`${keyID}`], e)}
error={((ValCtx.varBindsErrors && newIndices[`${keyID}`] in ValCtx.varBindsErrors))}/>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,9 @@ const validateProfiles = (validationObj) => {
const configuredVarBinds = {};
let varBindKey;
for (let i = 0; i < validationObj.varBinds.length; i++){
varBindKey = `${validationObj.varBinds[i].family}${validationObj.varBinds[i].category}${validationObj.varBinds[i].index}`
varBindKey = `${validationObj.varBinds[i].component}${validationObj.varBinds[i].object}${validationObj.varBinds[i].index}`
if (varBindKey in configuredVarBinds && varBindKey.length > 0){
message = "The same varbind has been already configured for this profile"
message = "The same varBind has been already configured for this profile"
if (i in errors.varBinds){
errors.varBinds[i].push(message);
}else{
Expand All @@ -234,7 +234,7 @@ const validateProfiles = (validationObj) => {
}else{
configuredVarBinds[varBindKey] = true
}
if (validationObj.varBinds[i].family.length === 0){
if (validationObj.varBinds[i].component.length === 0){
message = "MIB-Component is required";
if (i in errors.varBinds){
errors.varBinds[i].push(message);
Expand All @@ -243,9 +243,9 @@ const validateProfiles = (validationObj) => {
}
isValid = false;

}else if (!validationObj.varBinds[i].family.match(/^[a-zA-Z0-9_-]+$/)){
message = "MIB-Component can consist only of upper and lower english letters, " +
"numbers and two special characters: '-' and '_'. No spaces are allowed."
}else if (!validationObj.varBinds[i].component.match(/^[a-zA-Z0-9._-]+$/) || !isNaN(validationObj.varBinds[i].component)){
message = "MIB component can consist only of upper and lower english letters, " +
"numbers and three special characters: '.', '-' and '_'. No spaces are allowed. MIB component can't be a number."
if (i in errors.varBinds){
errors.varBinds[i].push(message);
}else{
Expand All @@ -254,10 +254,10 @@ const validateProfiles = (validationObj) => {
isValid = false;
}

if (validationObj.varBinds[i].category.length > 0){
if (!validationObj.varBinds[i].category.match(/^[a-zA-Z0-9_-]+$/)){
if (validationObj.varBinds[i].object.length > 0){
if (!validationObj.varBinds[i].object.match(/^[a-zA-Z0-9._-]+$/) || !isNaN(validationObj.varBinds[i].object)){
message = "MIB object can consist only of upper and lower english letters, " +
"numbers and two special characters: '-' and '_'. No spaces are allowed.";
"numbers and three special characters: '.', '-' and '_'. No spaces are allowed. MIB object can't be a number.";
if (i in errors.varBinds){
errors.varBinds[i].push(message);
}else{
Expand All @@ -268,7 +268,7 @@ const validateProfiles = (validationObj) => {
}

if (validationObj.varBinds[i].index.length > 0){
if (validationObj.varBinds[i].category.length === 0){
if (validationObj.varBinds[i].object.length === 0){
message = "MIB object is required when MIB index is specified";
if (i in errors.varBinds){
errors.varBinds[i].push(message);
Expand Down
Loading

0 comments on commit 9edebff

Please sign in to comment.