def test_select_saved_query(self): graylog_api = self._mock_graylog_api_saved_query(self.saved_queries) self._mock_prompt_stream(0) query, fields = CliInterface.select_saved_query(graylog_api) self.assertEqual('*', query) self.assertEqual(['timestamp', 'level', 'message'], fields) self._mock_prompt_stream(1) query, fields = CliInterface.select_saved_query(graylog_api) self.assertEqual('level:DEBUG', query) self.assertEqual(['timestamp', 'message'], fields)
def test_select_stream_with_admin_user_and_all_streams_options_chosen( self): graylog_api = self._mock_graylog_api_streams(stream_list=self.streams, user=self.admin_user) self._mock_prompt_stream(2) stream_filter = CliInterface.select_stream(graylog_api, None) self.assertEqual(None, stream_filter)
def get_graylog_api(cfg, environment, host, password, port, proxy, no_tls, username, keyring, insecure_https, store_session): gl_api = None scheme = "https" if no_tls: scheme = "http" if scheme == "https" and port is None: port = 443 port = port or 80 proxies = {scheme: proxy} if proxy else None if environment is not None: gl_api = GraylogAPIFactory.api_from_config(cfg, environment, port, proxies, no_tls, username, insecure_https, store_session) else: if host is not None: if username is None: username = CliInterface.prompt_username(scheme, host, port) gl_api = GraylogAPIFactory.api_from_host( host=host, port=port, username=username, password=password, scheme=scheme, proxies=proxies, tls=no_tls, insecure_https=insecure_https, store_session=store_session) else: if cfg.has_section("environment:default"): gl_api = GraylogAPIFactory.api_from_config(cfg, "default", port, proxies, no_tls, username) else: cli_error("Error: No host or environment configuration specified and no default found.") if not password and keyring: password = get_password_from_keyring(gl_api.host, gl_api.username) if not password and not gl_api.session_id: password = CliInterface.prompt_password(scheme, gl_api.host, gl_api.port, gl_api.username, gl_api.api_path) gl_api.password = password if keyring: store_password_in_keyring(gl_api.host, gl_api.username, password) return gl_api
def auth_session(self): must_authenticate = True if self.is_session_active(): must_authenticate = False if must_authenticate: if not self.password: self.password = CliInterface.prompt_password(self.scheme, self.host, self.port, self.username, self.api_path) result = self.post(url="system/sessions", data=dict(username=self.username,password=self.password,host=""), skip_auth=True) self.session_id = result["session_id"] if self.store_session: self.store_current_session() self.update_host_timezone(self.user_info().get('timezone'))
def api_from_config(cfg, env_name, port, proxies, no_tls, username, insecure_https, store_session): section_name = "environment:" + env_name host = None if cfg.has_option(section_name, utils.HOST): host = cfg.get(section_name, utils.HOST) else: cli_error("'host' option is not available in section [%(section_name)s]" % {'section_name': section_name}) if not port and cfg.has_option(section_name, utils.PORT): port = cfg.get(section_name, utils.PORT) if cfg.has_option(section_name, utils.API_PATH): api_path = cfg.get(section_name, utils.API_PATH) else: api_path = utils.DEFAULT_API_PATH scheme = "https" if no_tls: scheme = "http" if not username and cfg.has_option(section_name, utils.USERNAME): username = cfg.get(section_name, utils.USERNAME) elif not username: username = CliInterface.prompt_username(scheme, host, port) if cfg.has_section("sessions") and cfg.get("sessions", host): session_id = cfg.get("sessions", host) else: session_id = None if not proxies and cfg.has_option(section_name, utils.PROXY): proxies = {scheme: cfg.get(section_name, utils.PROXY)} default_stream = None if cfg.has_option(section_name, utils.DEFAULT_STREAM): default_stream = cfg.get(section_name, utils.DEFAULT_STREAM) return GraylogAPI( host=host, port=port, api_path=api_path, username=username, default_stream=default_stream, scheme=scheme, proxies=proxies, insecure_https=insecure_https, session_id=session_id, store_session=store_session )
def run(version, host, environment, saved_query, port, no_tls, username, password, keyring, search_from, search_to, mode, fields, output, follow, limit, latency, stream, sort, asc, proxy, format_template, no_color, config, insecure_https, store_session, query): cfg = get_config(config_file_path=config) if version: click.echo(get_pygray_version()) exit() if search_from and follow: click.echo( "-f (follow) and -@ (search from) are conflicting options, please choose one of them." ) exit() if cfg.has_option(section='environment:%s' % environment, option='port') and port is None: port = cfg.get(section='environment:%s' % environment, option='port') if search_from is None: search_from = "5 minutes ago" if insecure_https is None: if cfg.has_option(section='environment:%s' % environment, option='insecure_https'): insecure_https = cfg.get(section='environment:%s' % environment, option='insecure_https') else: insecure_https = False if store_session is None: if cfg.has_option(section='environment:%s' % environment, option='store_session'): store_session = cfg.get(section='environment:%s' % environment, option='store_session') else: store_session = False graylog_api = GraylogAPIFactory.get_graylog_api(cfg, environment, host, password, port, proxy, no_tls, username, keyring, insecure_https, store_session) if not graylog_api.session_id or not graylog_api.is_session_active(): graylog_api.auth_session() graylog_api.user_info() graylog_api.update_host_timezone(graylog_api.user_info().get('timezone')) sr = SearchRange(from_time=search_from, to_time=search_to) if follow: limit = None sort = None sr.from_time = arrow.now().replace(seconds=-latency - 1) sr.to_time = arrow.now().replace(seconds=-latency) limit = None if limit is None or limit <= 0 else limit fields = fields if mode == 'dump' else utils.extract_fields_from_format( cfg, format_template) color = get_color_option(cfg, format_template, no_color) stream_filter = CliInterface.select_stream(graylog_api, stream) if saved_query: query, fields = CliInterface.select_saved_query(graylog_api) q = SearchQuery(search_range=sr, query=query, limit=limit, filter=stream_filter, fields=fields, sort=sort, ascending=asc) formatter = FormatterFactory.get_formatter(mode, cfg, format_template, fields, color) LogPrinter().run_logprint(graylog_api, q, formatter, follow, output)
def test_select_saved_query_no_queries_found_for_user(self): graylog_api = self._mock_graylog_api_saved_query({'searches': []}) self._mock_prompt_stream(0) with self.assertRaises(SystemExit): CliInterface.select_saved_query(graylog_api)
def test_select_stream_with_regular_user(self): graylog_api = self._mock_graylog_api_streams(stream_list=self.streams, user=self.regular_user) self._mock_prompt_stream(1) stream_filter = CliInterface.select_stream(graylog_api, None) self.assertEqual('streams:456', stream_filter)
def test_select_stream_with_no_stream_set_and_admin_user(self): graylog_api = self._mock_graylog_api_streams(user=self.admin_user) stream_filter = CliInterface.select_stream(graylog_api, '*') self.assertEqual(None, stream_filter)
def test_select_stream_with_default_stream_set(self): graylog_api = self._mock_graylog_api_streams(user=self.admin_user, default_stream='abc') stream_filter = CliInterface.select_stream(graylog_api, None) self.assertEqual('streams:abc', stream_filter)
def test_select_stream_given_stream_passed(self): graylog_api = self._mock_graylog_api_streams(user=self.admin_user) stream_filter = CliInterface.select_stream(graylog_api, '123456') self.assertEqual('streams:123456', stream_filter)