Debian: Fix VIM API calls

On python 2 struct.pack returns a string, and therefore a
payload of type 'string' can be appended to it, before
being converted to bytes for writing to tcp.

On python 3 struct.pack returns bytes, and therefore a
payload of type 'string' can not be appended to it.
The payload needs to be converted to bytes first.  The
conversion of bytes to bytes (writing to tcp) is a no-op.

This change has no impact on python2 code.

Test Plan:
  PASS: "sw-manager patch-strategy show" does not generate
       exceptions in nfv-vim or nfv-vim-api logs on Debian.

Closes-Bug: 1971171
Signed-off-by: Al Bailey <al.bailey@windriver.com>
Change-Id: Ia4fc56ca20f1e013ad1432802c4f18793ee3bb26
This commit is contained in:
Al Bailey 2022-05-02 15:24:43 +00:00
parent bb991828c2
commit 985739f7fb
1 changed files with 10 additions and 2 deletions

View File

@ -8,6 +8,7 @@ import errno
import hashlib
import hmac
import select
import six
import socket
import struct
@ -119,20 +120,27 @@ class TCPConnection(object):
"""
Send a message into the TCP connection, assumes the following
messaging format: | length (4-bytes) | string of bytes |
struct.pack returns string in python2 and bytes in python3
"""
bytes_sent = 0
if self._socket is not None:
if self._auth_key is None:
msg = struct.pack('!L', socket.htonl(len(payload)))
msg += payload
if six.PY3:
msg += bytes(payload, 'utf-8')
else:
msg += payload
else:
auth_vector = hmac.new(self._auth_key, msg=payload,
digestmod=hashlib.sha512).digest()
msg_len = len(auth_vector) + len(payload)
msg = struct.pack('!L', socket.htonl(msg_len))
msg += auth_vector[:self.AUTH_VECTOR_MAX_SIZE]
msg += payload
if six.PY3:
msg += bytes(payload, 'utf-8')
else:
msg += payload
bytes_sent = self._socket.send(bytes(msg))
return bytes_sent