示例#1
0
    def test_disable_permissions_warnings(self):
        # Setup the config directory
        os.chmod(self.TEMP_CONFIG_DIR, 0o0755)

        self.assertNotEqual(os.stat(self.TEMP_CONFIG_DIR).st_mode & 0o7777, 0o0770)

        # Setup the config file
        os.chmod(self.TEMP_FILE_PATH, 0o664)

        self.assertNotEqual(os.stat(self.TEMP_FILE_PATH).st_mode & 0o777, 0o770)

        parser = CLIConfigParser(
            config_file_path=self.TEMP_FILE_PATH,
            validate_config_exists=True,
            validate_config_permissions=False,
        )
        parser.LOG = mock.Mock()

        result = parser.parse()  # noqa F841

        self.assertEqual(parser.LOG.info.call_count, 0)
        self.assertEqual(parser.LOG.warn.call_count, 0)

        # Make sure we left the file alone
        self.assertTrue(os.path.exists(self.TEMP_FILE_PATH))
        self.assertEqual(os.stat(self.TEMP_FILE_PATH).st_mode & 0o777, 0o664)

        self.assertTrue(os.path.exists(self.TEMP_CONFIG_DIR))
        self.assertEqual(os.stat(self.TEMP_CONFIG_DIR).st_mode & 0o7777, 0o0755)
示例#2
0
    def test_disable_permissions_warnings(self):
        # Setup the config directory
        os.chmod(self.TEMP_CONFIG_DIR, 0o0755)

        self.assertNotEqual(os.stat(self.TEMP_CONFIG_DIR).st_mode & 0o7777, 0o0770)

        # Setup the config file
        os.chmod(self.TEMP_FILE_PATH, 0o664)

        self.assertNotEqual(os.stat(self.TEMP_FILE_PATH).st_mode & 0o777, 0o770)

        parser = CLIConfigParser(config_file_path=self.TEMP_FILE_PATH,
                                 validate_config_exists=True,
                                 validate_config_permissions=False)
        parser.LOG = mock.Mock()

        result = parser.parse()  # noqa F841

        self.assertEqual(parser.LOG.info.call_count, 0)
        self.assertEqual(parser.LOG.warn.call_count, 0)

        # Make sure we left the file alone
        self.assertTrue(os.path.exists(self.TEMP_FILE_PATH))
        self.assertEqual(os.stat(self.TEMP_FILE_PATH).st_mode & 0o777, 0o664)

        self.assertTrue(os.path.exists(self.TEMP_CONFIG_DIR))
        self.assertEqual(os.stat(self.TEMP_CONFIG_DIR).st_mode & 0o7777, 0o0755)
示例#3
0
    def test_correct_permissions_emit_no_warnings(self):
        os.chmod(self.TEMP_CONFIG_DIR, 0o2770)

        self.assertEqual(os.stat(self.TEMP_CONFIG_DIR).st_mode & 0o7777, 0o2770)

        # Setup the config file
        os.chmod(self.TEMP_FILE_PATH, 0o660)

        self.assertEqual(os.stat(self.TEMP_FILE_PATH).st_mode & 0o777, 0o660)

        parser = CLIConfigParser(
            config_file_path=self.TEMP_FILE_PATH, validate_config_exists=True
        )
        parser.LOG = mock.Mock()

        result = parser.parse()  # noqa F841

        self.assertEqual(parser.LOG.warn.call_count, 0)

        # Make sure we left the file alone
        self.assertTrue(os.path.exists(self.TEMP_FILE_PATH))
        self.assertEqual(os.stat(self.TEMP_FILE_PATH).st_mode & 0o777, 0o660)

        self.assertTrue(os.path.exists(self.TEMP_CONFIG_DIR))
        self.assertEqual(os.stat(self.TEMP_CONFIG_DIR).st_mode & 0o7777, 0o2770)
示例#4
0
    def _parse_config_file(self, args):
        config_file_path = self._get_config_file_path(args=args)

        parser = CLIConfigParser(config_file_path=config_file_path,
                                 validate_config_exists=False)
        result = parser.parse()
        return result
示例#5
0
 def test_get_config_for_unicode_char(self):
     parser = CLIConfigParser(config_file_path=CONFIG_FILE_PATH_UNICODE,
                              validate_config_exists=False)
     config = parser.parse()
     print parser.config_file_path
     print config
     self.assertEqual(config['credentials']['password'], u'测试')
示例#6
0
    def test_get_config_for_unicode_char(self):
        parser = CLIConfigParser(config_file_path=CONFIG_FILE_PATH_UNICODE,
                                 validate_config_exists=False)
        config = parser.parse()

        if six.PY3:
            self.assertEqual(config['credentials']['password'], '密码')
        else:
            self.assertEqual(config['credentials']['password'], u'\u5bc6\u7801')
示例#7
0
    def _parse_config_file(self, args, validate_config_permissions=False):
        config_file_path = self._get_config_file_path(args=args)

        parser = CLIConfigParser(config_file_path=config_file_path,
                                 validate_config_exists=False,
                                 validate_config_permissions=validate_config_permissions,
                                 log=self.LOG)
        result = parser.parse()
        return result
示例#8
0
文件: base.py 项目: nzlosh/st2
    def _parse_config_file(self, args, validate_config_permissions=False):
        config_file_path = self._get_config_file_path(args=args)

        parser = CLIConfigParser(config_file_path=config_file_path,
                                 validate_config_exists=False,
                                 validate_config_permissions=validate_config_permissions,
                                 log=self.LOG)
        result = parser.parse()
        return result
示例#9
0
    def test_get_config_for_unicode_char(self):
        parser = CLIConfigParser(config_file_path=CONFIG_FILE_PATH_UNICODE,
                                 validate_config_exists=False)
        config = parser.parse()

        if six.PY3:
            self.assertEqual(config['credentials']['password'], '密码')
        else:
            self.assertEqual(config['credentials']['password'], u'\u5bc6\u7801')
示例#10
0
    def test_get_config_for_unicode_char(self):
        parser = CLIConfigParser(
            config_file_path=CONFIG_FILE_PATH_UNICODE, validate_config_exists=False
        )
        config = parser.parse()

        if six.PY3:
            self.assertEqual(config["credentials"]["password"], "密码")
        else:
            self.assertEqual(config["credentials"]["password"], "\u5bc6\u7801")
示例#11
0
    def test_get_config_for_unicode_char(self):
        # this test tests for config parameters with unicode characters and the % sign
        # the % sign interpolation is disabled since values are read with raw=True; See:
        # https://docs.python.org/3.8/library/configparser.html#configparser.ConfigParser
        parser = CLIConfigParser(
            config_file_path=CONFIG_FILE_PATH_UNICODE, validate_config_exists=False
        )
        config = parser.parse()

        if six.PY3:
            self.assertEqual(config["credentials"]["password"], "密码%")
        else:
            self.assertEqual(config["credentials"]["password"], "\u5bc6\u7801\u0025")
示例#12
0
    def test_constructor(self):
        parser = CLIConfigParser(config_file_path='doesnotexist',
                                 validate_config_exists=False)
        self.assertTrue(parser)

        self.assertRaises(ValueError,
                          CLIConfigParser,
                          config_file_path='doestnotexist',
                          validate_config_exists=True)
示例#13
0
    def test_warn_on_bad_config_permissions(self):
        # Setup the config directory
        os.chmod(self.TEMP_CONFIG_DIR, 0o0755)

        self.assertNotEqual(os.stat(self.TEMP_CONFIG_DIR).st_mode & 0o7777, 0o0770)

        # Setup the config file
        os.chmod(self.TEMP_FILE_PATH, 0o664)

        self.assertNotEqual(os.stat(self.TEMP_FILE_PATH).st_mode & 0o777, 0o770)

        parser = CLIConfigParser(
            config_file_path=self.TEMP_FILE_PATH, validate_config_exists=True
        )
        parser.LOG = mock.Mock()

        result = parser.parse()  # noqa F841

        self.assertEqual(parser.LOG.info.call_count, 1)

        self.assertEqual(
            "The SGID bit is not set on the StackStorm configuration directory.",
            parser.LOG.info.call_args_list[0][0][0],
        )

        self.assertEqual(parser.LOG.warn.call_count, 2)
        self.assertEqual(
            "The StackStorm configuration directory permissions are insecure "
            "(too permissive): others have access.",
            parser.LOG.warn.call_args_list[0][0][0],
        )

        self.assertEqual(
            "The StackStorm configuration file permissions are insecure: others have access.",
            parser.LOG.warn.call_args_list[1][0][0],
        )

        # Make sure we left the file alone
        self.assertTrue(os.path.exists(self.TEMP_FILE_PATH))
        self.assertEqual(os.stat(self.TEMP_FILE_PATH).st_mode & 0o777, 0o664)

        self.assertTrue(os.path.exists(self.TEMP_CONFIG_DIR))
        self.assertEqual(os.stat(self.TEMP_CONFIG_DIR).st_mode & 0o7777, 0o0755)
示例#14
0
    def test_parse(self):
        # File doesn't exist
        parser = CLIConfigParser(config_file_path='doesnotexist',
                                 validate_config_exists=False)
        result = parser.parse()

        self.assertEqual(CONFIG_DEFAULT_VALUES, result)

        # File exists - all the options specified
        expected = {
            'general': {
                'base_url': 'http://127.0.0.1',
                'api_version': 'v1',
                'cacert': 'cacartpath',
                'silence_ssl_warnings': False,
                'silence_schema_output': True
            },
            'cli': {
                'debug': True,
                'cache_token': False,
                'timezone': 'UTC'
            },
            'credentials': {
                'username': '******',
                'password': '******',
                'api_key': None
            },
            'api': {
                'url': 'http://127.0.0.1:9101/v1'
            },
            'auth': {
                'url': 'http://127.0.0.1:9100/'
            },
            'stream': {
                'url': 'http://127.0.0.1:9102/v1/stream'
            }
        }
        parser = CLIConfigParser(config_file_path=CONFIG_FILE_PATH_FULL,
                                 validate_config_exists=False)
        result = parser.parse()
        self.assertEqual(expected, result)

        # File exists - missing options, test defaults
        parser = CLIConfigParser(config_file_path=CONFIG_FILE_PATH_PARTIAL,
                                 validate_config_exists=False)
        result = parser.parse()
        self.assertTrue(result['cli']['cache_token'], True)
示例#15
0
    def test_parse(self):
        # File doesn't exist
        parser = CLIConfigParser(config_file_path="doesnotexist", validate_config_exists=False)
        result = parser.parse()

        self.assertEqual(CONFIG_DEFAULT_VALUES, result)

        # File exists - all the options specified
        expected = {
            "general": {
                "base_url": "http://localhost",
                "api_version": "v1",
                "cacert": "cacartpath",
                "silence_ssl_warnings": False,
            },
            "cli": {"debug": True, "cache_token": False},
            "credentials": {"username": "******", "password": "******"},
            "api": {"url": "http://localhost:9101/v1"},
            "auth": {"url": "http://localhost:9100/"},
        }
        parser = CLIConfigParser(config_file_path=CONFIG_FILE_PATH_FULL, validate_config_exists=False)
        result = parser.parse()
        self.assertEqual(expected, result)

        # File exists - missing options, test defaults
        parser = CLIConfigParser(config_file_path=CONFIG_FILE_PATH_PARTIAL, validate_config_exists=False)
        result = parser.parse()
        self.assertTrue(result["cli"]["cache_token"], True)
示例#16
0
    def test_warn_on_bad_config_permissions(self):
        # Setup the config directory
        os.chmod(self.TEMP_CONFIG_DIR, 0o0755)

        self.assertNotEqual(os.stat(self.TEMP_CONFIG_DIR).st_mode & 0o7777, 0o0770)

        # Setup the config file
        os.chmod(self.TEMP_FILE_PATH, 0o664)

        self.assertNotEqual(os.stat(self.TEMP_FILE_PATH).st_mode & 0o777, 0o770)

        parser = CLIConfigParser(config_file_path=self.TEMP_FILE_PATH, validate_config_exists=True)
        parser.LOG = mock.Mock()

        result = parser.parse()  # noqa F841

        self.assertEqual(parser.LOG.info.call_count, 1)

        self.assertEqual(
            "The SGID bit is not set on the StackStorm configuration directory.",
            parser.LOG.info.call_args_list[0][0][0])

        self.assertEqual(parser.LOG.warn.call_count, 2)
        self.assertEqual(
            "The StackStorm configuration directory permissions are insecure "
            "(too permissive): others have access.",
            parser.LOG.warn.call_args_list[0][0][0])

        self.assertEqual(
            "The StackStorm configuration file permissions are insecure: others have access.",
            parser.LOG.warn.call_args_list[1][0][0])

        # Make sure we left the file alone
        self.assertTrue(os.path.exists(self.TEMP_FILE_PATH))
        self.assertEqual(os.stat(self.TEMP_FILE_PATH).st_mode & 0o777, 0o664)

        self.assertTrue(os.path.exists(self.TEMP_CONFIG_DIR))
        self.assertEqual(os.stat(self.TEMP_CONFIG_DIR).st_mode & 0o7777, 0o0755)
示例#17
0
    def test_correct_permissions_emit_no_warnings(self):
        os.chmod(self.TEMP_CONFIG_DIR, 0o2770)

        self.assertEqual(os.stat(self.TEMP_CONFIG_DIR).st_mode & 0o7777, 0o2770)

        # Setup the config file
        os.chmod(self.TEMP_FILE_PATH, 0o660)

        self.assertEqual(os.stat(self.TEMP_FILE_PATH).st_mode & 0o777, 0o660)

        parser = CLIConfigParser(config_file_path=self.TEMP_FILE_PATH, validate_config_exists=True)
        parser.LOG = mock.Mock()

        result = parser.parse()  # noqa F841

        self.assertEqual(parser.LOG.warn.call_count, 0)

        # Make sure we left the file alone
        self.assertTrue(os.path.exists(self.TEMP_FILE_PATH))
        self.assertEqual(os.stat(self.TEMP_FILE_PATH).st_mode & 0o777, 0o660)

        self.assertTrue(os.path.exists(self.TEMP_CONFIG_DIR))
        self.assertEqual(os.stat(self.TEMP_CONFIG_DIR).st_mode & 0o7777, 0o2770)
示例#18
0
    def test_parse(self):
        # File doesn't exist
        parser = CLIConfigParser(
            config_file_path="doesnotexist", validate_config_exists=False
        )
        result = parser.parse()

        self.assertEqual(CONFIG_DEFAULT_VALUES, result)

        # File exists - all the options specified
        expected = {
            "general": {
                "base_url": "http://127.0.0.1",
                "api_version": "v1",
                "cacert": "cacartpath",
                "silence_ssl_warnings": False,
                "silence_schema_output": True,
            },
            "cli": {"debug": True, "cache_token": False, "timezone": "UTC"},
            "credentials": {"username": "******", "password": "******", "api_key": None},
            "api": {"url": "http://127.0.0.1:9101/v1"},
            "auth": {"url": "http://127.0.0.1:9100/"},
            "stream": {"url": "http://127.0.0.1:9102/v1/stream"},
        }
        parser = CLIConfigParser(
            config_file_path=CONFIG_FILE_PATH_FULL, validate_config_exists=False
        )
        result = parser.parse()
        self.assertEqual(expected, result)

        # File exists - missing options, test defaults
        parser = CLIConfigParser(
            config_file_path=CONFIG_FILE_PATH_PARTIAL, validate_config_exists=False
        )
        result = parser.parse()
        self.assertTrue(result["cli"]["cache_token"], True)
示例#19
0
    def test_parse(self):
        # File doesn't exist
        parser = CLIConfigParser(config_file_path='doesnotexist', validate_config_exists=False)
        result = parser.parse()

        self.assertEqual(CONFIG_DEFAULT_VALUES, result)

        # File exists - all the options specified
        expected = {
            'general': {
                'base_url': 'http://127.0.0.1',
                'api_version': 'v1',
                'cacert': 'cacartpath',
                'silence_ssl_warnings': False,
                'silence_schema_output': True
            },
            'cli': {
                'debug': True,
                'cache_token': False,
                'timezone': 'UTC'
            },
            'credentials': {
                'username': '******',
                'password': '******',
                'api_key': None
            },
            'api': {
                'url': 'http://127.0.0.1:9101/v1'
            },
            'auth': {
                'url': 'http://127.0.0.1:9100/'
            },
            'stream': {
                'url': 'http://127.0.0.1:9102/v1/stream'
            }
        }
        parser = CLIConfigParser(config_file_path=CONFIG_FILE_PATH_FULL,
                                 validate_config_exists=False)
        result = parser.parse()
        self.assertEqual(expected, result)

        # File exists - missing options, test defaults
        parser = CLIConfigParser(config_file_path=CONFIG_FILE_PATH_PARTIAL,
                                 validate_config_exists=False)
        result = parser.parse()
        self.assertTrue(result['cli']['cache_token'], True)
示例#20
0
文件: shell.py 项目: Bala96/st2
    def _parse_config_file(self, args):
        config_file_path = self._get_config_file_path(args=args)

        parser = CLIConfigParser(config_file_path=config_file_path, validate_config_exists=False)
        result = parser.parse()
        return result