From 27525fb205b70db40b1f8f1264028e20d56b7dfb Mon Sep 17 00:00:00 2001 From: Erica Portnoy Date: Thu, 15 Dec 2016 11:00:07 -0800 Subject: [PATCH] Use relative paths for livedir symlinks (#3914) * Use relative paths for livedir symlinks * switch directory back for the rest of the tests --- certbot/storage.py | 16 ++++++++++++++-- certbot/tests/cert_manager_test.py | 14 ++++++++++---- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/certbot/storage.py b/certbot/storage.py index 61ab69ff7..671922bee 100644 --- a/certbot/storage.py +++ b/certbot/storage.py @@ -198,6 +198,10 @@ def lineagename_for_filename(config_filename): "renewal config file name must end in .conf") return os.path.basename(config_filename[:-len(".conf")]) +def _relpath_from_file(archive_dir, from_file): + """Path to a directory from a file""" + return os.path.relpath(archive_dir, os.path.dirname(from_file)) + class RenewableCert(object): # pylint: disable=too-many-instance-attributes,too-many-public-methods @@ -305,6 +309,13 @@ class RenewableCert(object): return os.path.join( self.cli_config.default_archive_dir, self.lineagename) + def relative_archive_dir(self, from_file): + """Returns the default or specified archive directory as a relative path + + Used for creating symbolic links. + """ + return _relpath_from_file(self.archive_dir, from_file) + @property def is_test_cert(self): """Returns true if this is a test cert from a staging server.""" @@ -331,7 +342,8 @@ class RenewableCert(object): for kind in ALL_FOUR: link = getattr(self, kind) previous_link = get_link_target(link) - new_link = os.path.join(self.archive_dir, os.path.basename(previous_link)) + new_link = os.path.join(self.relative_archive_dir(link), + os.path.basename(previous_link)) os.unlink(link) os.symlink(new_link, link) @@ -856,7 +868,7 @@ class RenewableCert(object): target = dict([(kind, os.path.join(live_dir, kind + ".pem")) for kind in ALL_FOUR]) for kind in ALL_FOUR: - os.symlink(os.path.join(archive, kind + "1.pem"), + os.symlink(os.path.join(_relpath_from_file(archive, target[kind]), kind + "1.pem"), target[kind]) with open(target["cert"], "wb") as f: logger.debug("Writing certificate to %s.", target["cert"]) diff --git a/certbot/tests/cert_manager_test.py b/certbot/tests/cert_manager_test.py index f3569dc00..bffa5298f 100644 --- a/certbot/tests/cert_manager_test.py +++ b/certbot/tests/cert_manager_test.py @@ -99,10 +99,16 @@ class UpdateLiveSymlinksTest(BaseCertManagerTest): cert_manager.update_live_symlinks(self.cli_config) # check that symlinks go where they should - for domain in self.domains: - for kind in ALL_FOUR: - self.assertEqual(os.readlink(self.configs[domain][kind]), - archive_paths[domain][kind]) + prev_dir = os.getcwd() + try: + for domain in self.domains: + for kind in ALL_FOUR: + os.chdir(os.path.dirname(self.configs[domain][kind])) + self.assertEqual( + os.path.realpath(os.readlink(self.configs[domain][kind])), + os.path.realpath(archive_paths[domain][kind])) + finally: + os.chdir(prev_dir) class CertificatesTest(BaseCertManagerTest):