Skip to content

Commit

Permalink
Filter out non-printable characters read from syseeprom (#17)
Browse files Browse the repository at this point in the history
Signed-off-by: Rajendra Dendukuri <rajendra.dendukuri@broadcom.com>
  • Loading branch information
rajendra-dendukuri committed Jul 6, 2020
1 parent 374c9e8 commit c93fb6d
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/usr/lib/python3/dist-packages/ztp/DecodeSysEeprom.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import sys
import os

from ztp.ZTPLib import runCommand
from ztp.ZTPLib import runCommand, printable

class DecodeSysEeprom:

Expand Down Expand Up @@ -64,7 +64,7 @@ def __read_sys_eeprom(self, option):
if not rc == 0 or len(cmd_stdout) != 1:
return 'N.A'
else:
return cmd_stdout[0].rstrip()
return printable(cmd_stdout[0].rstrip())

## Global instance of the class
sysEeprom = DecodeSysEeprom()
15 changes: 15 additions & 0 deletions src/usr/lib/python3/dist-packages/ztp/ZTPLib.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import time
import shlex
import subprocess
from curses.ascii import isprint
import ztp.ZTPCfg
import os.path
from ztp.defaults import *
Expand Down Expand Up @@ -272,3 +273,17 @@ def systemReboot():
os.system('reboot -y')
else:
os.system('reboot')

def printable(input):
'''!
Filter out non-printable characters from an input string
@param input (str) Input string
@return String with non-printable characters removed
None if invalid input data type
'''
if input is not None and isString(input):
return ''.join(char for char in input if isprint(char))
else:
return None
4 changes: 2 additions & 2 deletions src/usr/lib/ztp/ztp-profile.sh
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ ztp_config_create()
DEST=${TMP_ZTP_CONFIG_DB_JSON}
fi

PRODUCT_NAME=$(decode-syseeprom -p)
SERIAL_NO=$(decode-syseeprom -s)
PRODUCT_NAME=$(decode-syseeprom -p | tr -dc '[[:print:]]')
SERIAL_NO=$(decode-syseeprom -s | tr -dc '[[:print:]]')
sonic-cfggen -H -k ${HW_KEY} -a "{\"ZTP_INBAND\": \"$(get_feature inband)\", \
\"ZTP_IPV4\": \"$(get_feature ipv4)\", \"ZTP_IPV6\": \"$(get_feature ipv6)\", \
\"PRODUCT_NAME\": \"${PRODUCT_NAME}\", \"SERIAL_NO\": \"${SERIAL_NO}\"}" \
Expand Down
10 changes: 9 additions & 1 deletion tests/test_ZTPLib.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import stat
import pytest

from ztp.ZTPLib import runCommand, getField, getCfg
from ztp.ZTPLib import runCommand, getField, getCfg, printable
sys.path.append(getCfg('plugins-dir'))

class TestClass(object):
Expand Down Expand Up @@ -114,3 +114,11 @@ def test_getField(self):

data = dict({'key': {'subkey':10} })
assert (getField(data, 'key', dict, None).get('subkey') == 10)

def test_misc(self):
assert(printable("Test-/\=$!()*#!_?><,.][{}+String1234567890") == "Test-/\=$!()*#!_?><,.][{}+String1234567890")
assert(printable("Te\u20ACst\u20AC") == "Test")
assert(printable("\u20AC\u20AC") == "")
assert(printable(None) == None)
assert(printable({"k": "v"}) == None)
assert(printable("") == "")

0 comments on commit c93fb6d

Please sign in to comment.