Add support for Python3

* remove relative imports
* use six for cookies and url parsing
* don't mix strings and bytes

Change-Id: I329100063d9f47c878be134a28308c5bea0c1dc3
This commit is contained in:
Radoslav Gerganov 2017-02-15 16:05:29 +02:00
parent 9c6fd65e87
commit 86b4c11ff0
5 changed files with 20 additions and 17 deletions

View File

@ -14,14 +14,14 @@
# under the License. # under the License.
import base64 import base64
import Cookie
import hashlib import hashlib
import json import json
import os import os
import socket import socket
import ssl import ssl
import urlparse
from six.moves import http_cookies as Cookie
import six.moves.urllib.parse as urlparse
import websockify import websockify
@ -36,7 +36,7 @@ VMAD_CONNECT_CMD = "CONNECT"
def expect(sock, code): def expect(sock, code):
line = sock.recv(1024) line = sock.recv(1024).decode('ascii')
recv_code, msg = line.split()[0:2] recv_code, msg = line.split()[0:2]
recv_code = int(recv_code) recv_code = int(recv_code)
if code != recv_code: if code != recv_code:
@ -55,16 +55,18 @@ def handshake(host, port, ticket, cfg_file, thumbprint):
h.update(cert) h.update(cert)
if thumbprint != h.hexdigest(): if thumbprint != h.hexdigest():
raise Exception("Server thumbprint doesn't match") raise Exception("Server thumbprint doesn't match")
sock.write("%s %s\r\n" % (VMAD_USER_CMD, ticket)) sock.sendall(("%s %s\r\n" % (VMAD_USER_CMD, ticket)).encode('ascii'))
expect(sock, VMAD_NEEDPASSWD) expect(sock, VMAD_NEEDPASSWD)
sock.write("%s %s\r\n" % (VMAD_PASS_CMD, ticket)) sock.sendall(("%s %s\r\n" % (VMAD_PASS_CMD, ticket)).encode('ascii'))
expect(sock, VMAD_LOGINOK) expect(sock, VMAD_LOGINOK)
rand = os.urandom(12) rand = os.urandom(12)
rand = base64.b64encode(rand) rand_b = base64.b64encode(rand)
sock.write("%s %s\r\n" % (VMAD_THUMB_CMD, rand)) rand_s = rand_b.decode('ascii')
sock.sendall(("%s %s\r\n" % (VMAD_THUMB_CMD, rand_s)).encode('ascii'))
thumbprint2 = expect(sock, VMAD_OK) thumbprint2 = expect(sock, VMAD_OK)
thumbprint2 = thumbprint2.replace(':', '').lower() thumbprint2 = thumbprint2.replace(':', '').lower()
sock.write("%s %s mks\r\n" % (VMAD_CONNECT_CMD, cfg_file)) sock.sendall(
("%s %s mks\r\n" % (VMAD_CONNECT_CMD, cfg_file)).encode('ascii'))
expect(sock, VMAD_OK) expect(sock, VMAD_OK)
sock2 = ssl.wrap_socket(sock) sock2 = ssl.wrap_socket(sock)
cert2 = sock2.getpeercert(binary_form=True) cert2 = sock2.getpeercert(binary_form=True)
@ -72,7 +74,7 @@ def handshake(host, port, ticket, cfg_file, thumbprint):
h.update(cert2) h.update(cert2)
if thumbprint2 != h.hexdigest(): if thumbprint2 != h.hexdigest():
raise Exception("Second thumbprint doesn't match") raise Exception("Second thumbprint doesn't match")
sock2.write(rand) sock2.sendall(rand_b)
return sock2 return sock2

View File

@ -16,8 +16,8 @@ import os
import sys import sys
import argparse import argparse
import authd
from novaclient import client from novaclient import client
from novaproxy import authd
import websockify import websockify
import logging import logging

View File

@ -33,10 +33,10 @@ class AuthdRequestHandler(testtools.TestCase):
'200 OK'] '200 OK']
def fake_recv(len): def fake_recv(len):
return msgs.pop(0) return msgs.pop(0).encode('ascii')
def fake_getpeercert(binary_form=True): def fake_getpeercert(binary_form=True):
return 'fake-certificate' return 'fake-certificate'.encode('ascii')
sock = mock.MagicMock() sock = mock.MagicMock()
sock.recv = fake_recv sock.recv = fake_recv
@ -56,10 +56,10 @@ class AuthdRequestHandler(testtools.TestCase):
'200 OK'] '200 OK']
def fake_recv(len): def fake_recv(len):
return msgs.pop(0) return msgs.pop(0).encode('ascii')
def fake_getpeercert(binary_form=True): def fake_getpeercert(binary_form=True):
return 'fake-certificate' return 'fake-certificate'.encode('ascii')
sock = mock.MagicMock() sock = mock.MagicMock()
sock.recv = fake_recv sock.recv = fake_recv
@ -79,10 +79,10 @@ class AuthdRequestHandler(testtools.TestCase):
'200 OK'] '200 OK']
def fake_recv(len): def fake_recv(len):
return msgs.pop(0) return msgs.pop(0).encode('ascii')
def fake_getpeercert(binary_form=True): def fake_getpeercert(binary_form=True):
return 'fake-certificate' return 'fake-certificate'.encode('ascii')
sock = mock.MagicMock() sock = mock.MagicMock()
sock.recv = fake_recv sock.recv = fake_recv

View File

@ -1,2 +1,3 @@
python-novaclient python-novaclient
six
websockify websockify

View File

@ -1,5 +1,5 @@
[tox] [tox]
envlist = py27,pep8 envlist = py35,py27,pep8
skipsdist = True skipsdist = True
[testenv] [testenv]