Skip to content
Draft
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
21 changes: 13 additions & 8 deletions ister.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@
from urllib.parse import urlparse
from contextlib import closing
import netifaces
import pycryptsetup
import gi
gi.require_version("BlockDev", "2.0")
from gi.repository import BlockDev as bd

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh fun, gobject introspection. Well I guess that's making good use of the gnome stack.


LOG = None

Expand Down Expand Up @@ -391,11 +393,15 @@ def create_filesystems(template):
if "encryption" in fst:
encr = fst["encryption"]
c_dev = "{0}{1}".format(dev, fst["partition"])
crs = pycryptsetup.CryptSetup(device=c_dev)
crs.luksFormat(cipher="aes", cipherMode="xts-plain64",
keysize=512, hashMode="sha256")
crs.addKeyByPassphrase(encr["passphrase"], encr["passphrase"])
crs.activate(name=encr["name"], passphrase=encr["passphrase"])

if not bd.crypto_luks_format(c_dev, "aes-xts-plain64", 512,
encr["passphrase"]):
LOG.error("Could not format LUKS device %s" % encr["name"])

if not bd.crypto_luks_open(c_dev, encr["name"],
encr["passphrase"]):
LOG.error("Could not open LUKS device %s" % encr["name"])

command = "{0}{1} /dev/mapper/{2}".format(fsu["cmd"], opts,
encr["name"])
run_command(command)
Expand Down Expand Up @@ -964,8 +970,7 @@ def cleanup(args, template, target_dir, raise_exception=True):
raise_exception=raise_exception)
for dev_entry in template['PartitionMountPoints']:
if 'encryption' in dev_entry:
crs = pycryptsetup.CryptSetup(name=dev_entry['encryption']['name'])
crs.deactivate()
bd.crypto_luks_close(dev_entry['encryption']['name'])


def get_template_location(path):
Expand Down
68 changes: 30 additions & 38 deletions ister_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,9 @@
import netifaces
import traceback
import types
try:
import pycryptsetup
except:
pycrypts = types.ModuleType("pycryptsetup")
pycrypts.CryptSetup = types.new_class("CryptSetup")
sys.modules["pycryptsetup"] = pycrypts
import pycryptsetup
import gi
gi.require_version("BlockDev", "2.0")
from gi.repository import BlockDev as bd

import ister
import ister_gui
Expand Down Expand Up @@ -145,47 +141,46 @@ def good_template_string_partitions():
"SoftwareManager": "swupd"}'


def cryptsetup_wrapper(func):
def blockdev_wrapper(func):
"""Wrapprt for test whose function use pycryptsetup"""
@functools.wraps(func)
def wrapper():
"""CryptSetup Mock Class"""
class mock_CryptSetup:
def __init__(self, device=None, name=None):
"""BlockDev Mock Class"""
class mock_BlockDev:
@staticmethod
def crypto_luks_open(device=None, name=None):
if device:
COMMAND_RESULTS.append(device)
if name:
COMMAND_RESULTS.append(name)

def luksFormat(self, cipher=None, cipherMode=None,
keysize=None, hashMode=None):
@staticmethod
def crypto_luks_format(device=None, cipher=None, keySize=None,
passphrase=None):
if device:
COMMAND_RESULTS.append(device)
if cipher:
COMMAND_RESULTS.append(cipher)
if cipherMode:
COMMAND_RESULTS.append(cipherMode)
if keysize:
COMMAND_RESULTS.append(keysize)
if hashMode:
COMMAND_RESULTS.append(hashMode)

def addKeyByPassphrase(self, passphrase1, passphrase2):
COMMAND_RESULTS.append(
"{0} - {1}".format(passphrase1, passphrase2))
if keySize:
COMMAND_RESULTS.append(keySize)
if passphrase:
COMMAND_RESULTS.append(passphrase)

def activate(self, name='', passphrase=''):
COMMAND_RESULTS.append("activating")
@staticmethod
def crypto_luks_close(device=None)
if device:
COMMAND_RESULTS(device)

def deactivate(self):
COMMAND_RESULTS.append("deactivating")
orig_bd = bd
bd = mock_BlockDev

pycrypt = pycryptsetup.CryptSetup
pycryptsetup.CryptSetup = mock_CryptSetup
try:
func()
except Exception as e:
raise e
finally:
pycryptsetup.CryptSetup = pycrypt
bd = orig_bd

return wrapper


Expand Down Expand Up @@ -912,7 +907,7 @@ def mock_check_output(cmd):
raise Exception("Result was {}, expected None".format(res))


@cryptsetup_wrapper
@blockdev_wrapper
@run_command_wrapper
def create_filesystems_encrypted_good():
"""Create filesystems without options"""
Expand Down Expand Up @@ -950,10 +945,7 @@ def mock_listdir(directory):
"mkswap /dev/sdb2",
"swapon /dev/sdb2",
False,
'/dev/sdb3', 'aes',
'xts-plain64', 512, 'sha256',
'abc@123 - abc@123',
'activating',
'/dev/sdb3', 'aes-xts-plain64', "abc@123",
'mkfs.xfs -f /dev/mapper/mapper_name'
]
os.listdir = mock_listdir
Expand Down Expand Up @@ -1231,7 +1223,7 @@ def args():
raise Exception("Failed to detect open failure")


@cryptsetup_wrapper
@blockdev_wrapper
@run_command_wrapper
def setup_mounts_encryption_good():
"""Setup mount points for install"""
Expand Down Expand Up @@ -2228,7 +2220,7 @@ def post_install_chroot_shell_good():
commands_compare_helper(commands)


@cryptsetup_wrapper
@blockdev_wrapper
@run_command_wrapper
def cleanup_physical_encrypted_good():
"""Test cleanup of physical device"""
Expand Down Expand Up @@ -2260,7 +2252,7 @@ def args():
"umount -R /not-writable/place",
"rm -fr /not-writable/place",
"mapper_name",
'deactivating']
"mapper_name"]
ister.cleanup(args, template, "/not-writable/place")
os.path.isdir = backup_isdir
commands_compare_helper(commands)
Expand Down