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)
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)
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)
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
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'测试')
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')
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
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")
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")
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)
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)
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)
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)
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)
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)
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)