Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filter out non-printable characters read from syseeprom #17

Merged
merged 1 commit into from
Jun 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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("") == "")