Exemple #1
0
    def test_config_migration(self):
        utilities.write_single_setting('CEPH_KEYRING_USER_FSID-WITH-MINUS',
                                       'dummy')
        self.assertEqual(
            utilities.read_single_setting('CEPH_KEYRING_USER_FSID-WITH-MINUS'),
            'dummy')

        utilities.write_single_setting('CEPH_KEYRING_USER_FSID_WITH_MINUS', '')

        obj = mock.Mock()
        ceph.librados._read_oa_settings(
            ConfigObj(ceph.librados.oa_settings.settings_file), obj)

        self.assertNotIn('CEPH_KEYRING_USER_FSID-WITH-MINUS', obj.__dict__)
        self.assertEqual(obj.CEPH_KEYRING_USER_FSID_WITH_MINUS, 'dummy')
        self.assertEqual(
            utilities.read_single_setting('CEPH_KEYRING_USER_FSID-WITH-MINUS'),
            'dummy')

        ceph.librados._write_oa_setting('CEPH_KEYRING_USER_FSID_WITH_MINUS',
                                        'dummy2')
        with self.assertRaises(KeyError):
            utilities.read_single_setting('CEPH_KEYRING_USER_FSID-WITH-MINUS')

        self.assertEqual(
            utilities.read_single_setting('CEPH_KEYRING_USER_FSID_WITH_MINUS'),
            'dummy2')
Exemple #2
0
def read_secret_from_config():
    """
    Try to read from our config file. Then try to read from old .secret.txt file. Then
    generate a new secret.

    :return: tuple. First element is the secret, second element is a boolean telling if we should
     write the secret later on. We cannot write the secret here, because django is not yet fully
     functional.
    """
    try:
        secret = read_single_setting('DJANGO_SECRET')
        if secret:
            return secret, False
    except KeyError:
        pass

    # Pre 3.6.1: we have used a dedicated file to store the django secret.
    try:
        secret = open(join(PROJECT_ROOT, '.secret.txt')).read().strip()
        if secret:
            return secret, True
    except IOError:
        pass
    except Exception:
        logging.exception('failed')

    import string
    from django.utils.crypto import get_random_string

    secret = get_random_string(
        50, string.ascii_letters + string.digits + string.punctuation)

    return secret, True
Exemple #3
0
    def test_new_secret(self):
        utilities.write_single_setting('DJANGO_SECRET', '')
        if exists(SettingsTest.secret_file_path):
            os.remove(SettingsTest.secret_file_path)

        secret_1, should_write_1 = settings.read_secret_from_config()
        self.assertTrue(should_write_1)
        settings.write_secret_to_config(secret_1)

        secret_2, should_write_2 = settings.read_secret_from_config()
        self.assertEqual(secret_1, secret_2)
        self.assertFalse(should_write_2)

        self.assertEqual(utilities.read_single_setting('DJANGO_SECRET'),
                         secret_2)
Exemple #4
0
    def test_secret_migration_read(self):

        with open(SettingsTest.secret_file_path, 'w') as f:
            f.write('mysecret')

        utilities.write_single_setting('DJANGO_SECRET', '')

        secret, should_write_secret = settings.read_secret_from_config()
        self.assertTrue(should_write_secret)
        self.assertEqual(secret, 'mysecret')

        settings.write_secret_to_config('mysecret')
        self.assertEqual(utilities.read_single_setting('DJANGO_SECRET'),
                         'mysecret')
        self.assertFalse(exists(SettingsTest.secret_file_path))