mirror of
https://github.com/ansible/awx.git
synced 2026-03-02 09:18:48 -03:30
HTTP logger overrides kwargs with settings
This commit is contained in:
@@ -251,8 +251,25 @@ register(
|
|||||||
register(
|
register(
|
||||||
'LOG_AGGREGATOR_USERNAME',
|
'LOG_AGGREGATOR_USERNAME',
|
||||||
field_class=fields.CharField,
|
field_class=fields.CharField,
|
||||||
label=_('Logging Aggregator Receiver Username'),
|
label=_('Logging Aggregator Username to Authenticate With'),
|
||||||
help_text=_('Username for Logstash or others'),
|
help_text=_('Username for Logstash or others (basic auth)'),
|
||||||
|
category=_('Logging'),
|
||||||
|
category_slug='logging',
|
||||||
|
)
|
||||||
|
register(
|
||||||
|
'LOG_AGGREGATOR_PASSWORD',
|
||||||
|
field_class=fields.CharField,
|
||||||
|
label=_('Logging Aggregator Password to Authenticate With'),
|
||||||
|
help_text=_('Password for Logstash or others (basic auth)'),
|
||||||
|
category=_('Logging'),
|
||||||
|
category_slug='logging',
|
||||||
|
)
|
||||||
|
register(
|
||||||
|
'LOG_AGGREGATOR_LOGGERS',
|
||||||
|
field_class=fields.StringListField,
|
||||||
|
default=['awx', 'activity_stream', 'job_events', 'packages', 'services', 'ansible'],
|
||||||
|
label=_(''),
|
||||||
|
help_text=_(''),
|
||||||
category=_('Logging'),
|
category=_('Logging'),
|
||||||
category_slug='logging',
|
category_slug='logging',
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ from logstash import formatter
|
|||||||
|
|
||||||
# custom
|
# custom
|
||||||
from requests.auth import HTTPBasicAuth
|
from requests.auth import HTTPBasicAuth
|
||||||
|
from django.conf import settings as django_settings
|
||||||
|
|
||||||
|
|
||||||
ENABLED_LOGS = ['ansible']
|
ENABLED_LOGS = ['ansible']
|
||||||
@@ -52,28 +53,42 @@ class TCPLogstashHandler(logging.handlers.SocketHandler, object):
|
|||||||
return self.formatter.format(record) + b'\n'
|
return self.formatter.format(record) + b'\n'
|
||||||
|
|
||||||
|
|
||||||
# loggly
|
# techniquest borrowed from the loggly library
|
||||||
# https://github.com/varshneyjayant/loggly-python-handler
|
# https://github.com/varshneyjayant/loggly-python-handler
|
||||||
|
# MIT License
|
||||||
|
|
||||||
session = FuturesSession()
|
# Translation of parameter names to names in Django settings
|
||||||
|
PARAM_NAMES = {
|
||||||
|
'host': 'LOG_AGGREGATOR_HOST',
|
||||||
|
'port': 'LOG_AGGREGATOR_PORT',
|
||||||
|
'message_type': 'LOG_AGGREGATOR_TYPE',
|
||||||
|
'username': 'LOG_AGGREGATOR_USERNAME',
|
||||||
|
'password': 'LOG_AGGREGATOR_PASSWORD',
|
||||||
|
}
|
||||||
|
# TODO: figure out what to do with LOG_AGGREGATOR_LOGGERS (if anything)
|
||||||
|
|
||||||
|
|
||||||
def bg_cb(sess, resp):
|
def bg_cb(sess, resp):
|
||||||
""" Don't do anything with the response """
|
""" Don't do anything with the response """
|
||||||
pass
|
pass
|
||||||
|
|
||||||
# add port for a generic handler
|
|
||||||
class HTTPSHandler(logging.Handler):
|
class HTTPSHandler(logging.Handler):
|
||||||
def __init__(self, host, fqdn=False, **kwargs):
|
def __init__(self, fqdn=False, **kwargs):
|
||||||
super(HTTPSHandler, self).__init__()
|
super(HTTPSHandler, self).__init__()
|
||||||
self.host_saved = host
|
|
||||||
self.fqdn = fqdn
|
self.fqdn = fqdn
|
||||||
for fd in ['port', 'message_type', 'username', 'password']:
|
for fd in PARAM_NAMES:
|
||||||
if fd in kwargs:
|
# settings values take precedence over the input params
|
||||||
|
settings_name = PARAM_NAMES[fd]
|
||||||
|
settings_val = getattr(django_settings, settings_name, None)
|
||||||
|
if settings_val:
|
||||||
|
setattr(self, fd, settings_val)
|
||||||
|
elif fd in kwargs:
|
||||||
attr_name = fd
|
attr_name = fd
|
||||||
if fd == 'username':
|
setattr(self, fd, kwargs[fd])
|
||||||
attr_name = 'user'
|
else:
|
||||||
setattr(self, attr_name, kwargs[fd])
|
setattr(self, fd, None)
|
||||||
|
self.session = FuturesSession()
|
||||||
|
self.add_auth_information()
|
||||||
|
|
||||||
def get_full_message(self, record):
|
def get_full_message(self, record):
|
||||||
if record.exc_info:
|
if record.exc_info:
|
||||||
@@ -81,18 +96,20 @@ class HTTPSHandler(logging.Handler):
|
|||||||
else:
|
else:
|
||||||
return record.getMessage()
|
return record.getMessage()
|
||||||
|
|
||||||
def add_auth_information(self, kwargs):
|
def add_auth_information(self):
|
||||||
if self.message_type == 'logstash':
|
if self.message_type == 'logstash':
|
||||||
if not self.user:
|
if not self.username:
|
||||||
# Logstash authentication not enabled
|
# Logstash authentication not enabled
|
||||||
return kwargs
|
return kwargs
|
||||||
logstash_auth = HTTPBasicAuth(self.user, self.password)
|
logstash_auth = HTTPBasicAuth(self.username, self.password)
|
||||||
kwargs['auth'] = logstash_auth
|
self.session.auth = logstash_auth
|
||||||
elif self.message_type == 'splunk':
|
elif self.message_type == 'splunk':
|
||||||
|
## Auth used by Splunk logger library
|
||||||
|
# self.session.auth = ('x', self.access_token)
|
||||||
|
# self.session.headers.update({'Content-Encoding': 'gzip'})
|
||||||
auth_header = "Splunk %s" % self.token
|
auth_header = "Splunk %s" % self.token
|
||||||
headers = dict(Authorization=auth_header)
|
headers = dict(Authorization=auth_header)
|
||||||
kwargs['headers'] = headers
|
self.session.headers.update(headers)
|
||||||
return kwargs
|
|
||||||
|
|
||||||
def emit(self, record):
|
def emit(self, record):
|
||||||
try:
|
try:
|
||||||
@@ -108,14 +125,13 @@ class HTTPSHandler(logging.Handler):
|
|||||||
break
|
break
|
||||||
if st_type not in ENABLED_LOGS:
|
if st_type not in ENABLED_LOGS:
|
||||||
return
|
return
|
||||||
host = self.host_saved
|
host = self.host
|
||||||
if not host.startswith('http'):
|
if not host.startswith('http'):
|
||||||
host = 'http://%s' % self.host_saved
|
host = 'http://%s' % self.host
|
||||||
if self.port != 80:
|
if self.port != 80:
|
||||||
host = '%s:%s' % (host, str(self.port))
|
host = '%s:%s' % (host, str(self.port))
|
||||||
bare_kwargs = dict(data=payload, background_callback=bg_cb)
|
kwargs = dict(data=payload, background_callback=bg_cb)
|
||||||
kwargs = self.add_auth_information(bare_kwargs)
|
self.session.post(host, **kwargs)
|
||||||
session.post(host, **kwargs)
|
|
||||||
except (KeyboardInterrupt, SystemExit):
|
except (KeyboardInterrupt, SystemExit):
|
||||||
raise
|
raise
|
||||||
except:
|
except:
|
||||||
|
|||||||
Reference in New Issue
Block a user