Skip to content

Ensure source and destination path conversion to absolute is cwd safe. #89

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

Closed
wants to merge 1 commit into from
Closed
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
54 changes: 43 additions & 11 deletions mig/shared/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
import sys

from mig.shared.defaults import default_http_port, default_https_port, \
mig_user, mig_group, default_source, default_destination, \
MIG_BASE, mig_user, mig_group, default_source, default_destination, \
auth_openid_mig_db, auth_openid_ext_db, STRONG_TLS_CIPHERS, \
STRONG_TLS_CURVES, STRONG_SSH_KEXALGOS, STRONG_SSH_LEGACY_KEXALGOS, \
STRONG_SSH_CIPHERS, STRONG_SSH_LEGACY_CIPHERS, STRONG_SSH_MACS, \
Expand All @@ -74,6 +74,15 @@ def _override_apache_initd(template_name, user_dict):
return "%s-%s" % (file_name, user_dict['__MIG_USER__'])


def abspath(path, start):
"""Generate an absolute path as per os.path.abspath()
from an explicit starting cwf if necessary.
"""
if os.path.isabs(path):
return path
return os.path.normpath(os.path.join(start, path))


def determine_timezone(_environ=os.environ, _path_exists=os.path.exists, _print=print):
"""Attempt to detect the timezone in various known portable ways."""

Expand Down Expand Up @@ -255,7 +264,11 @@ def template_remove(template_file, remove_pattern):
'group',
'user',
'timezone',
'_getcwd',
'_getpwnam',
'_prepare',
'_writefiles',
'_instructions',
]


Expand Down Expand Up @@ -485,34 +498,53 @@ def generate_confs(
ca_fqdn='',
ca_user='mig-ca',
ca_smtp='localhost',
_getcwd=os.getcwd,
_getpwnam=pwd.getpwnam,
_prepare=None,
_writefiles=None,
_instructions=None,
):
"""Generate Apache and MiG server confs with specified variables"""

# TODO: override in signature as a non-functional follow-up change
if _prepare is None:
_prepare = _generate_confs_prepare
if _writefiles is None:
_writefiles = _generate_confs_writefiles
if _instructions is None:
_instructions = _generate_confs_instructions

# Read out dictionary of args with defaults and overrides

thelocals = locals()
expanded = {k: v for k, v in thelocals.items() if k not in
_GENERATE_CONFS_NOFORWARD_KEYS}

# expand any directory path specific as "auto" relative to CWD
thecwd = _getcwd()

if source == keyword_auto:
template_path = os.path.dirname(sys.argv[0])
# use the templates from this copy of the code tree
template_dir = os.path.join(MIG_BASE, "mig/install")
else:
template_path = source
# construct a path using the supplied value made absolute
template_dir = abspath(source, start=thecwd)

if destination == keyword_auto:
destination_link = os.path.dirname(sys.argv[0])
# write output into a confs folder within the CWD
destination = os.path.join(thecwd, 'confs')
else:
destination_link = destination
# construct a path from the supplied value made absolute
destination = abspath(destination, start=thecwd)

# expand any user information marked as "auto" based on the environment
if user == keyword_auto:
user = pwd.getpwuid(os.getuid())[0]
if group == keyword_auto:
group = grp.getgrgid(os.getgid())[0]

# finalize a destination path up-front
# finalize destination paths up-front
destination_link = destination
destination_dir = "%s%s" % (destination, destination_suffix)

# Backwards compatibility with old name
Expand All @@ -528,16 +560,16 @@ def generate_confs(
'command_line': generateconfs_command,
'destination_dir': destination_dir,
'destination_link': destination_link,
'template_dir': template_path,
'template_dir': template_dir,
'timezone': timezone,
'user_gid': user_pw_info.pw_gid,
'user_group': group,
'user_uid': user_pw_info.pw_uid,
'user_uname': user,
}
user_dict = _generate_confs_prepare(options, **expanded)
_generate_confs_write(options, user_dict)
_generate_confs_instructions(options, user_dict)
user_dict = _prepare(options, **expanded)
_writefiles(options, user_dict)
_instructions(options, user_dict)
return options


Expand Down Expand Up @@ -2131,7 +2163,7 @@ def _generate_confs_prepare(
return user_dict


def _generate_confs_write(options, user_dict, insert_list=[], cleanup_list=[]):
def _generate_confs_writefiles(options, user_dict, insert_list=[], cleanup_list=[]):
"""Actually write generated confs"""
assert os.path.isabs(options['destination_dir'])
assert os.path.isabs(options['template_dir'])
Expand Down
65 changes: 63 additions & 2 deletions tests/test_mig_shared_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@

sys.path.append(os.path.realpath(os.path.join(os.path.dirname(__file__), ".")))

from support import MigTestCase, testmain, temppath, cleanpath, fixturepath
from support import MIG_BASE, MigTestCase, testmain, temppath, cleanpath, fixturepath

from mig.shared.defaults import keyword_auto
from mig.shared.install import determine_timezone, generate_confs


Expand All @@ -56,7 +57,9 @@ def create_dummy_gpwnam(pw_uid, pw_gid):


def noop(*args, **kwargs):
pass
if (len(args) > 0):
return args[0]
return None


class MigSharedInstall__determine_timezone(MigTestCase):
Expand Down Expand Up @@ -161,6 +164,64 @@ def test_creates_output_directory_containing_a_standard_local_configuration(self
expected_file = os.path.join(fixture_dir, file_name)
self.assertFileContentIdentical(actual_file, expected_file)

def test_options_for_source_auto(self):
options = generate_confs(
source=keyword_auto,
_getcwd=lambda: '/some/arbitrary/path',
_getpwnam=create_dummy_gpwnam(4321, 1234),
_prepare=noop,
_writefiles=noop,
_instructions=noop,
)
expected_template_dir = os.path.join(MIG_BASE, 'mig/install')

self.assertEqual(options['template_dir'], expected_template_dir)

def test_options_for_source_relative(self):
options = generate_confs(
source='.',
_getcwd=lambda: '/current/working/directory/mig/install',
_getpwnam=create_dummy_gpwnam(4321, 1234),
_prepare=noop,
_writefiles=noop,
_instructions=noop,
)

self.assertEqual(options['template_dir'],
'/current/working/directory/mig/install')

def test_options_for_destination_auto(self):
options = generate_confs(
destination=keyword_auto,
destination_suffix='_suffix',
_getcwd=lambda: '/some/arbitrary/path',
_getpwnam=create_dummy_gpwnam(4321, 1234),
_prepare=noop,
_writefiles=noop,
_instructions=noop,
)

self.assertEqual(options['destination_link'],
'/some/arbitrary/path/confs')
self.assertEqual(options['destination_dir'],
'/some/arbitrary/path/confs_suffix')

def test_options_for_destination_relative(self):
options = generate_confs(
destination='generate-confs',
destination_suffix='_suffix',
_getcwd=lambda: '/current/working/directory',
_getpwnam=create_dummy_gpwnam(4321, 1234),
_prepare=noop,
_writefiles=noop,
_instructions=noop,
)

self.assertEqual(options['destination_link'],
'/current/working/directory/generate-confs')
self.assertEqual(options['destination_dir'],
'/current/working/directory/generate-confs_suffix')


if __name__ == '__main__':
testmain()
Loading