Example #1
0
    def test_create(self):
        """ create headphones.SoftChroot """

        cf = SoftChroot('/tmp/')
        self.assertIsInstance(cf, SoftChroot)
        self.assertTrue(cf.isEnabled())
        self.assertEqual(cf.getRoot(), '/tmp/')
Example #2
0
    def test_create_disabled(self, empty_path):
        """ create DISABLED SoftChroot """

        cf = SoftChroot(empty_path)
        self.assertIsInstance(cf, SoftChroot)
        self.assertFalse(cf.isEnabled())
        self.assertIsNone(cf.getRoot())
Example #3
0
    def test_create_disabled(self, empty_path):
        """ create DISABLED SoftChroot """

        cf = SoftChroot(empty_path)
        self.assertIsInstance(cf, SoftChroot)
        self.assertFalse(cf.isEnabled())
        self.assertIsNone(cf.getRoot())
Example #4
0
    def test_create(self):
        """ create headphones.SoftChroot """

        cf = SoftChroot('/tmp/')
        self.assertIsInstance(cf, SoftChroot)
        self.assertTrue(cf.isEnabled())
        self.assertEqual(cf.getRoot(), '/tmp/')
    def test_actions_on_disabled(self, p):
        """ softchroot: disabled SC, apply and revoke should not change args """
        sc = SoftChroot(None)
        a = sc.apply(p)
        self.assertEqual(a, p)

        r = sc.revoke(p)
        self.assertEqual(r, p)
Example #6
0
    def test_actions_on_disabled(self, p):
        """ disabled SoftChroot should not change args on apply and revoke """
        sc = SoftChroot(None)
        a = sc.apply(p)
        self.assertEqual(a, p)

        r = sc.revoke(p)
        self.assertEqual(r, p)
    def test_apply_out_of_root(self, p, e):
        """ softchroot: apply to paths outside of the chroot """
        chroot = '/tmp/'
        sc = SoftChroot(chroot)
        a = sc.apply(p)

        # !!! Important thing for compatibility with non-soft-chroot:
        # !!! the path outside of chroot should be the same, but with chroot prefix!!
        self.assertEqual(a, e)
    def test_init_disabled(self, rt):
        """ softchroot: test_init_disabled """
        sc = SoftChroot(rt)

        enabled = sc.isEnabled()
        path = sc.getRoot()
        self.assertIsInstance(sc, SoftChroot)
        self.assertFalse(enabled)
        self.assertIsNone(path)
    def test_init_enabled(self, rt, exp_chroot):
        """ softchroot: test_init_enabled """
        sc = SoftChroot(rt)

        enabled = sc.isEnabled()
        path = sc.getRoot()
        self.assertIsInstance(sc, SoftChroot)
        self.assertTrue(enabled)
        self.assertEqual(exp_chroot, path)
Example #10
0
    def test_create_on_not_exists_dir(self):
        """ create SoftChroot on non existent dir """

        path = os.path.join('/tmp', 'notexist', 'asdf', '11', '12', 'np',
                            'itsssss')

        cf = None
        with self.assertRaises(SoftChrootError) as exc:
            cf = SoftChroot(path)
        self.assertIsNone(cf)

        self.assertRegexpMatches(str(exc.exception), r'No such directory')
        self.assertRegexpMatches(str(exc.exception), path)
Example #11
0
    def test_create_on_file(self, os_mock):
        """ create SoftChroot on file, not a directory """

        path = os.path.join('/tmp', 'notexist', 'asdf', '11', '12', 'np',
                            'itsssss')

        os_mock.path.sep = os.path.sep
        os_mock.path.isdir.side_effect = lambda x: x != path

        cf = None
        with self.assertRaises(SoftChrootError) as exc:
            cf = SoftChroot(path)
        self.assertIsNone(cf)

        self.assertTrue(os_mock.path.isdir.called)

        self.assertRegexpMatches(str(exc.exception), r'No such directory')
        self.assertRegexpMatches(str(exc.exception), path)
Example #12
0
 def test_apply(self, p, e):
     """ apply SoftChroot """
     sc = SoftChroot('/tmp/')
     a = sc.apply(p)
     self.assertEqual(a, e)
Example #13
0
 def test_apply_out_of_root(self, p):
     """ apply SoftChroot to paths outside of the chroot """
     sc = SoftChroot('/tmp/')
     a = sc.apply(p)
     self.assertEqual(a, '/')
Example #14
0
 def test_apply(self, p, e):
     """ softchroot: apply """
     sc = SoftChroot('/tmp/')
     a = sc.apply(p)
     self.assertEqual(a, e)
Example #15
0
def initialize(config_file):
    with INIT_LOCK:

        global CONFIG
        global SOFT_CHROOT
        global _INITIALIZED
        global CURRENT_VERSION
        global LATEST_VERSION
        global UMASK

        CONFIG = headphones.config.Config(config_file)

        assert CONFIG is not None

        if _INITIALIZED:
            return False

        if CONFIG.HTTP_PORT < 21 or CONFIG.HTTP_PORT > 65535:
            headphones.logger.warn(
                'HTTP_PORT out of bounds: 21 < %s < 65535', CONFIG.HTTP_PORT)
            CONFIG.HTTP_PORT = 8181

        if CONFIG.HTTPS_CERT == '':
            CONFIG.HTTPS_CERT = os.path.join(DATA_DIR, 'server.crt')
        if CONFIG.HTTPS_KEY == '':
            CONFIG.HTTPS_KEY = os.path.join(DATA_DIR, 'server.key')

        if not CONFIG.LOG_DIR:
            CONFIG.LOG_DIR = os.path.join(DATA_DIR, 'logs')

        if not os.path.exists(CONFIG.LOG_DIR):
            try:
                os.makedirs(CONFIG.LOG_DIR)
            except OSError:
                CONFIG.LOG_DIR = None

                if not QUIET:
                    sys.stderr.write("Unable to create the log directory. "
                                     "Logging to screen only.\n")

        # Start the logger, disable console if needed
        logger.initLogger(console=not QUIET, log_dir=CONFIG.LOG_DIR,
                          verbose=VERBOSE)

        try:
            SOFT_CHROOT = SoftChroot(str(CONFIG.SOFT_CHROOT))
            if SOFT_CHROOT.isEnabled():
                logger.info("Soft-chroot enabled for dir: %s", str(CONFIG.SOFT_CHROOT))
        except headphones.exceptions.SoftChrootError as e:
            logger.error("SoftChroot error: %s", e)
            raise e

        if not CONFIG.CACHE_DIR:
            # Put the cache dir in the data dir for now
            CONFIG.CACHE_DIR = os.path.join(DATA_DIR, 'cache')
        if not os.path.exists(CONFIG.CACHE_DIR):
            try:
                os.makedirs(CONFIG.CACHE_DIR)
            except OSError as e:
                logger.error("Could not create cache dir '%s': %s", DATA_DIR, e)

        # Sanity check for search interval. Set it to at least 6 hours
        if CONFIG.SEARCH_INTERVAL and CONFIG.SEARCH_INTERVAL < 360:
            logger.info("Search interval too low. Resetting to 6 hour minimum.")
            CONFIG.SEARCH_INTERVAL = 360

        # Initialize the database
        logger.info('Checking to see if the database has all tables....')
        try:
            dbcheck()
        except Exception as e:
            logger.error("Can't connect to the database: %s", e)

        # Get the currently installed version. Returns None, 'win32' or the git
        # hash.
        CURRENT_VERSION, CONFIG.GIT_BRANCH = versioncheck.getVersion()

        # Write current version to a file, so we know which version did work.
        # This allowes one to restore to that version. The idea is that if we
        # arrive here, most parts of Headphones seem to work.
        if CURRENT_VERSION:
            version_lock_file = os.path.join(DATA_DIR, "version.lock")

            try:
                with open(version_lock_file, "w") as fp:
                    fp.write(CURRENT_VERSION)
            except IOError as e:
                logger.error("Unable to write current version to file '%s': %s",
                             version_lock_file, e)

        # Check for new versions
        if CONFIG.CHECK_GITHUB and CONFIG.CHECK_GITHUB_ON_STARTUP:
            try:
                LATEST_VERSION = versioncheck.checkGithub()
            except:
                logger.exception("Unhandled exception")
                LATEST_VERSION = CURRENT_VERSION
        else:
            LATEST_VERSION = CURRENT_VERSION

        # Store the original umask
        UMASK = os.umask(0)
        os.umask(UMASK)

        _INITIALIZED = True
        return True
Example #16
0
 def test_apply_out_of_root(self, p):
     """ apply SoftChroot to paths outside of the chroot """
     sc = SoftChroot('/tmp/')
     a = sc.apply(p)
     self.assertEqual(a, '/')
Example #17
0
 def test_revoke(self, p, e):
     """ softchroot: revoke """
     sc = SoftChroot('/tmp/')
     a = sc.revoke(p)
     self.assertEqual(a, e)
Example #18
0
 def test_apply_to_chroot_without_slash(self, chroot_root, path, exp):
     """ softchroot: apply to the chroot path without trailing slash """
     sc = SoftChroot(chroot_root)
     a = sc.apply(path)
     self.assertEqual(a, exp)
Example #19
0
 def test_revoke(self, p, e):
     """ revoke SoftChroot """
     sc = SoftChroot('/tmp/')
     a = sc.revoke(p)
     self.assertEqual(a, e)
Example #20
0
def initialize(config_file):
    with INIT_LOCK:

        global CONFIG
        global SOFT_CHROOT
        global _INITIALIZED
        global CURRENT_VERSION
        global LATEST_VERSION
        global UMASK

        CONFIG = headphones.config.Config(config_file)

        assert CONFIG is not None

        if _INITIALIZED:
            return False

        if CONFIG.HTTP_PORT < 21 or CONFIG.HTTP_PORT > 65535:
            headphones.logger.warn('HTTP_PORT out of bounds: 21 < %s < 65535',
                                   CONFIG.HTTP_PORT)
            CONFIG.HTTP_PORT = 8181

        if CONFIG.HTTPS_CERT == '':
            CONFIG.HTTPS_CERT = os.path.join(DATA_DIR, 'server.crt')
        if CONFIG.HTTPS_KEY == '':
            CONFIG.HTTPS_KEY = os.path.join(DATA_DIR, 'server.key')

        if not CONFIG.LOG_DIR:
            CONFIG.LOG_DIR = os.path.join(DATA_DIR, 'logs')

        if not os.path.exists(CONFIG.LOG_DIR):
            try:
                os.makedirs(CONFIG.LOG_DIR)
            except OSError:
                CONFIG.LOG_DIR = None

                if not QUIET:
                    sys.stderr.write("Unable to create the log directory. "
                                     "Logging to screen only.\n")

        # Start the logger, disable console if needed
        logger.initLogger(console=not QUIET,
                          log_dir=CONFIG.LOG_DIR,
                          verbose=VERBOSE)

        try:
            SOFT_CHROOT = SoftChroot(str(CONFIG.SOFT_CHROOT))
            if SOFT_CHROOT.isEnabled():
                logger.info("Soft-chroot enabled for dir: %s",
                            str(CONFIG.SOFT_CHROOT))
        except headphones.exceptions.SoftChrootError as e:
            logger.error("SoftChroot error: %s", e)
            raise e

        if not CONFIG.CACHE_DIR:
            # Put the cache dir in the data dir for now
            CONFIG.CACHE_DIR = os.path.join(DATA_DIR, 'cache')
        if not os.path.exists(CONFIG.CACHE_DIR):
            try:
                os.makedirs(CONFIG.CACHE_DIR)
            except OSError as e:
                logger.error("Could not create cache dir '%s': %s", DATA_DIR,
                             e)

        # Sanity check for search interval. Set it to at least 6 hours
        if CONFIG.SEARCH_INTERVAL and CONFIG.SEARCH_INTERVAL < 360:
            logger.info(
                "Search interval too low. Resetting to 6 hour minimum.")
            CONFIG.SEARCH_INTERVAL = 360

        # Initialize the database
        logger.info('Checking to see if the database has all tables....')
        try:
            dbcheck()
        except Exception as e:
            logger.error("Can't connect to the database: %s", e)

        # Get the currently installed version. Returns None, 'win32' or the git
        # hash.
        CURRENT_VERSION, CONFIG.GIT_BRANCH = versioncheck.getVersion()

        # Write current version to a file, so we know which version did work.
        # This allowes one to restore to that version. The idea is that if we
        # arrive here, most parts of Headphones seem to work.
        if CURRENT_VERSION:
            version_lock_file = os.path.join(DATA_DIR, "version.lock")

            try:
                with open(version_lock_file, "w") as fp:
                    fp.write(CURRENT_VERSION)
            except IOError as e:
                logger.error(
                    "Unable to write current version to file '%s': %s",
                    version_lock_file, e)

        # Check for new versions
        if CONFIG.CHECK_GITHUB and CONFIG.CHECK_GITHUB_ON_STARTUP:
            try:
                LATEST_VERSION = versioncheck.checkGithub()
            except:
                logger.exception("Unhandled exception")
                LATEST_VERSION = CURRENT_VERSION
        else:
            LATEST_VERSION = CURRENT_VERSION

        # Store the original umask
        UMASK = os.umask(0)
        os.umask(UMASK)

        _INITIALIZED = True
        return True