Merge pull request #6696 from ryanpetrello/rsyslog-splunk-extras

add a few minor logging changes to accomodate Splunk's API

Reviewed-by: https://github.com/apps/softwarefactory-project-zuul
This commit is contained in:
softwarefactory-project-zuul[bot]
2020-04-14 16:40:19 +00:00
committed by GitHub
3 changed files with 31 additions and 4 deletions

View File

@@ -158,3 +158,17 @@ def test_rsyslog_conf_template(enabled, type, host, port, protocol, expected_con
# check validity of created template # check validity of created template
assert expected_config in tmpl assert expected_config in tmpl
def test_splunk_auth():
mock_settings, _ = _mock_logging_defaults()
# Set test settings
logging_defaults = getattr(settings, 'LOGGING')
setattr(mock_settings, 'LOGGING', logging_defaults)
setattr(mock_settings, 'LOG_AGGREGATOR_ENABLED', True)
setattr(mock_settings, 'LOG_AGGREGATOR_TYPE', 'splunk')
setattr(mock_settings, 'LOG_AGGREGATOR_HOST', 'example.org')
setattr(mock_settings, 'LOG_AGGREGATOR_PASSWORD', 'SECRET-TOKEN')
tmpl = construct_rsyslog_conf_template(mock_settings)
assert 'httpheaderkey="Authorization" httpheadervalue="Splunk SECRET-TOKEN"' in tmpl

View File

@@ -60,11 +60,21 @@ def construct_rsyslog_conf_template(settings=settings):
params.append(f'restpath="{path}"') params.append(f'restpath="{path}"')
username = getattr(settings, 'LOG_AGGREGATOR_USERNAME', '') username = getattr(settings, 'LOG_AGGREGATOR_USERNAME', '')
password = getattr(settings, 'LOG_AGGREGATOR_PASSWORD', '') password = getattr(settings, 'LOG_AGGREGATOR_PASSWORD', '')
if username: if getattr(settings, 'LOG_AGGREGATOR_TYPE', None) == 'splunk':
# splunk has a weird authorization header <shrug>
if password:
# from omhttp docs:
# https://www.rsyslog.com/doc/v8-stable/configuration/modules/omhttp.html
# > Currently only a single additional header/key pair is
# > configurable, further development is needed to support
# > arbitrary header key/value lists.
params.append('httpheaderkey="Authorization"')
params.append(f'httpheadervalue="Splunk {password}"')
elif username:
params.append(f'uid="{username}"') params.append(f'uid="{username}"')
if username and password: if password:
# you can only have a basic auth password if there's a username # you can only have a basic auth password if there's a username
params.append(f'pwd="{password}"') params.append(f'pwd="{password}"')
params = ' '.join(params) params = ' '.join(params)
parts.extend(['module(load="omhttp")', f'action({params})']) parts.extend(['module(load="omhttp")', f'action({params})'])
elif protocol and host and port: elif protocol and host and port:

View File

@@ -244,4 +244,7 @@ class LogstashFormatter(LogstashFormatterBase):
if record.exc_info: if record.exc_info:
message.update(self.get_debug_fields(record)) message.update(self.get_debug_fields(record))
if settings.LOG_AGGREGATOR_TYPE == 'splunk':
# splunk messages must have a top level "event" key
message = {'event': message}
return self.serialize(message) return self.serialize(message)