Skip to content

Commit

Permalink
fix: handle unicode and byte encoding for power environment variables
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Styk <[email protected]>
  • Loading branch information
StykMartin committed Oct 28, 2024
1 parent 69b28c8 commit 9cb36a8
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 5 deletions.
25 changes: 20 additions & 5 deletions LabController/src/bkr/labcontroller/provision.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,13 +199,28 @@ def find_power_script(power_type):
return pkg_resources.resource_filename('bkr.labcontroller', resource)
raise ValueError('Invalid power type %r' % power_type)


def _decode(value):
# Decode if we are running python2 and value is unicode
if six.PY2 and isinstance(value, six.text_type):
return value.encode("utf8")
return value


def build_power_env(command):
env = dict(os.environ)
env['power_address'] = (command['power'].get('address') or u'').encode('utf8')
env['power_id'] = (command['power'].get('id') or u'').encode('utf8')
env['power_user'] = (command['power'].get('user') or u'').encode('utf8')
env['power_pass'] = (command['power'].get('passwd') or u'').encode('utf8')
env['power_mode'] = command['action'].encode('utf8')
power_mapping = {
"address": "power_address",
"id": "power_id",
"user": "power_user",
"passwd": "power_pass",
}

for k, v in six.iteritems(power_mapping):
env[v] = _decode(command["power"].get(k, ""))

env["power_mode"] = _decode(command["action"])

return env

def handle_clear_logs(conf, command):
Expand Down
54 changes: 54 additions & 0 deletions LabController/src/bkr/labcontroller/test_provision.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Copyright Contributors to the Beaker project.
# SPDX-License-Identifier: GPL-2.0-or-later

import unittest

import six

from bkr.common.helpers import SensitiveUnicode
from bkr.labcontroller.provision import build_power_env


class TestBuildPowerEnv(unittest.TestCase):
def test_build_power_env(self):
t_command = {
"power": {
"address": u"192.168.1.1",
"id": u"42",
"user": u"root",
"passwd": SensitiveUnicode(u"toor"),
},
"action": u"reboot",
}

expected = {
"power_address": "192.168.1.1",
"power_id": "42",
"power_user": "root",
"power_pass": "toor",
"power_mode": "reboot",
}

actual = build_power_env(t_command)

for key, value in six.iteritems(expected):
self.assertEqual(expected[key], actual[key])

def test_build_power_env_with_missing_fields(self):
t_command = {
"power": {"address": u"192.168.1.1", "passwd": SensitiveUnicode(u"toor")},
"action": u"reboot",
}

expected = {
"power_address": "192.168.1.1",
"power_id": "",
"power_user": "",
"power_pass": "toor",
"power_mode": "reboot",
}

actual = build_power_env(t_command)

for key, value in six.iteritems(expected):
self.assertEqual(expected[key], actual[key])
2 changes: 2 additions & 0 deletions beaker.spec
Original file line number Diff line number Diff line change
Expand Up @@ -229,10 +229,12 @@ BuildRequires: pkgconfig(systemd)
BuildRequires: python3-gevent
BuildRequires: python3-lxml
BuildRequires: python3-werkzeug
BuildRequires: python3-daemon
%else
# python2-gevent112 is a special build created for labcontroller. It includes backports to ensure compatibility with py2.7.9 SSL backport.
BuildRequires: python2-gevent112
BuildRequires: python-lxml
BuildRequires: python-daemon
%endif

# Syslinux is only available on x86_64. This package is used to provide pxelinux.0, which is then copied to the TFTP directory.
Expand Down

0 comments on commit 9cb36a8

Please sign in to comment.