<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Logging &#8211; privacyID3A</title>
	<atom:link href="https://www.privacyidea.org/tag/logging/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.privacyidea.org</link>
	<description>flexible, Open Source Multi Factor Authentication (2FA)</description>
	<lastBuildDate>Thu, 14 Nov 2024 11:40:31 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://www.privacyidea.org/wp-content/uploads/2016/06/cropped-only-logo-white-background-32x32.png</url>
	<title>Logging &#8211; privacyID3A</title>
	<link>https://www.privacyidea.org</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Event-based logging with privacyIDEA and Logstash</title>
		<link>https://www.privacyidea.org/event-based-logging-with-privacyidea-and-logstash/</link>
					<comments>https://www.privacyidea.org/event-based-logging-with-privacyidea-and-logstash/#comments</comments>
		
		<dc:creator><![CDATA[Henning Hollermann]]></dc:creator>
		<pubDate>Sun, 05 Apr 2020 23:17:25 +0000</pubDate>
				<category><![CDATA[Howto]]></category>
		<category><![CDATA[Event Handler]]></category>
		<category><![CDATA[Logging]]></category>
		<category><![CDATA[logstash]]></category>
		<category><![CDATA[privacyIDEA]]></category>
		<guid isPermaLink="false">https://www.privacyidea.org/?p=1900</guid>

					<description><![CDATA[Keeping track of the system logs in a complex environment with many hosts and even more services can be challenging. 
A sophisticated logging system with aggregation and filtering capabilities helps the administrator to stay ahead of things. 
This article shows how to integrate privacyIDEA in the centralized logging system provided by the elastic stack]]></description>
										<content:encoded><![CDATA[
<p>Keeping track of the system logs in a complex environment with many hosts and even more services can be challenging. A sophisticated logging system with aggregation and filtering capabilities helps the administrator to stay ahead of things. Popular examples are <a href="https://www.splunk.com/">Splunk</a> and <a href="https://www.elastic.co/logstash">Logstash</a>. Both are intrinsically supported by privacyIDEA, since the software builds on the standard python logging library. This article shows how to integrate privacyIDEA in the centralized logging system provided by the elastic stack which consists of the software components Logstash, Elasticsearch and Kibana.</p>



<p>We will use the privacyIDEA logging facility to let the server not only store its system logs and audit locally but also feed them to a remote Logstash server. We will also show how to use the new Logging module of the Event Handler, introduced in privacyIDEA 3.3, to customize the logged information. The usual path of information is displayed in the following picture.</p>



<figure class="wp-block-image alignwide size-large"><img fetchpriority="high" decoding="async" width="1024" height="163" src="https://www.privacyidea.org/wp-content/uploads/2020/03/privacyIDEA-ELK_2000-1024x163.png" alt="" class="wp-image-1904" srcset="https://www.privacyidea.org/wp-content/uploads/2020/03/privacyIDEA-ELK_2000-1024x163.png 1024w, https://www.privacyidea.org/wp-content/uploads/2020/03/privacyIDEA-ELK_2000-300x48.png 300w, https://www.privacyidea.org/wp-content/uploads/2020/03/privacyIDEA-ELK_2000-768x122.png 768w, https://www.privacyidea.org/wp-content/uploads/2020/03/privacyIDEA-ELK_2000-1536x245.png 1536w, https://www.privacyidea.org/wp-content/uploads/2020/03/privacyIDEA-ELK_2000.png 1997w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<h3 class="wp-block-heading">Setup the Base System</h3>



<p>As the installation Logstash, Elasticsearch and Kibana is documented at the <a href="https://www.elastic.co/guide/en/elastic-stack/current/installing-elastic-stack.html">vendors website</a>, we will not go into detail here. In any case, you need a java runtime environment. For Ubuntu you can use the package <code>default-jre</code>. Once you have the elastic stack up and running, turn towards privacyIDEA.</p>



<p>The installation of privacyIDEA is documented at <a href="https://privacyidea.readthedocs.io/en/latest/">privacyidea.readthedocs.io</a>. For a quick start, there is a community package repository for Ubuntu 18.04 LTS available. Install the privacyIDEA server and become a little familiar to the WebUI, which is the primary management interface.</p>



<p>The base configuration of privacyIDEA is set in the <a href="https://privacyidea.readthedocs.io/en/latest/installation/system/inifile.html">configuration file <code>pi.cfg</code></a> and the dedicated <a href="https://privacyidea.readthedocs.io/en/latest/installation/system/logging.html#advanced-logging">logging configuration file <code>logging.cfg</code> or <code>logging.yml</code></a>. In the default Ubuntu 18.04 package installation, those are located in /etc/privacyidea/. To be able to view the audit logs in the WebUI and send them at the same time to the python logger, the <a href="https://privacyidea.readthedocs.io/en/latest/audit/#container-audit">ContainerAudit</a> module is used.</p>



<pre class="wp-block-preformatted has-small-font-size"># /etc/privacyidea/pi.cfg
PI_AUDIT_MODULE = 'privacyidea.lib.auditmodules.containeraudit'
PI_AUDIT_CONTAINER_WRITE = ['privacyidea.lib.auditmodules.sqlaudit','privacyidea.lib.auditmodules.loggeraudit']
PI_AUDIT_CONTAINER_READ = 'privacyidea.lib.auditmodules.sqlaudit'
PI_AUDIT_LOGGER_QUALNAME = 'pi-audit'
PI_LOGCONFIG = '/etc/privacyidea/logging.cfg'</pre>



<p>Note that we use a custom audit logger name &#8220;pi-audit&#8221; in the above configuration. See the documentation of the <a href="https://privacyidea.readthedocs.io/en/latest/audit/index.html#logger-audit">Logger Audit</a>.</p>



<h3 class="wp-block-heading">Send privacyIDEA logs to Logstash</h3>



<p>The logging module <code>privacyidea.lib.auditmodules.loggeraudit</code> sends the audit messages to the python logging system and makes it available to the configuration by <code>logging.yml</code>. To send both the privacyIDEA server logs and the audit log to Logstash, the <a href="https://github.com/eht16/python-logstash-async">module <code>python-logstash-async</code></a> comes in handy. It can be installed through pip by</p>



<pre class="wp-block-preformatted has-small-font-size">~$ pip install python-logstash-async</pre>



<p>The module can be used in a <code>logging.cfg</code> or <code>logging.yml</code> in YAML and INI format respectively. <a href="https://gist.github.com/laclaro/a9ba8b6f1f1ed8f9de23d1383f71666c">Minimal examples for the configuration of the logstash-async module</a> are found on Github Gist. A <a href="https://gist.github.com/laclaro/6cbd3f51bba0041d5307569644a575fc">more detailed YAML configuration file</a> is also available, which provides a good basis for this test case.</p>



<p>Restart privacyidea for the changes to have effect. If you used the extended configuration from gist, you should now see the audit log in /var/log/privacyidea/audit.log.</p>



<h3 class="wp-block-heading">Receive privacyIDEA logs with Logstash</h3>



<p>On the other end, Logstash is configured to listen on port 5959 and to forward the logs to Elasticsearch using different indices for the qualnames pi-audit, pi-eventlog and all the rest (privacyidea.*).</p>



<pre class="wp-block-preformatted has-small-font-size"># /etc/logstash/conf.d/privacyidea_elasticsearch.conf
# privacyIDEA input is logged by the python-logstash-async module
input {
   tcp {
      port =&gt; 5959
      codec =&gt; json
      tags =&gt; ["privacyidea"]
   }
}
# filter adds metadata field according to logger to
# separate the privacyIDEA audit log from the rest
filter {
   if [extra][logger_name] == "pi-audit" or [extra][logger_name] == "privacyidea.lib.auditmodules.loggeraudit" {
      mutate { add_field =&gt; { "[@metadata][indexPrefix]" =&gt; "pi-audit" } }
   } else if [extra][logger_name] == "pi-eventlog" {
      mutate { add_field =&gt; { "[@metadata][indexPrefix]" =&gt; "pi-eventlog" } }
   } else {
      mutate { add_field =&gt; { "[@metadata][indexPrefix]" =&gt; "privacyidea" } }
   }
}
# Logs are sent to elasticsearch using the indexPrefix
output {
   elasticsearch {
      index =&gt; "%{[@metadata][indexPrefix]}-%{+YYYY.MM.dd}"
   }
   # additional output to syslog
   stdout {
      codec =&gt; rubydebug
   }
}</pre>



<p>Restart logstash afterwards. The output section contains an additional part for logging to stdout. On a systemd-driven system (check <code>~$ ps -p 1</code>), it can be viewed by</p>



<pre class="wp-block-preformatted has-small-font-size">~# journalctl -f -u logstash</pre>



<p>Once you interact with the privacyIDEA server, you should see the incoming audit log messages in json format in the journalctl ountput on the logstash machine. The example below is the audit message for viewing the audit log in the privacyIDEA WebUI:</p>



<pre class="wp-block-preformatted has-small-font-size">{
          "extra" =&gt; {
                    "logger_name" =&gt; "pi-audit",
                   "process_name" =&gt; "MainProcess",
                    "thread_name" =&gt; "MainThread",
                           "line" =&gt; 85,
         "logstash_async_version" =&gt; "1.6.4",
                           "path" =&gt; "/opt/privacyidea/privacyidea/lib/auditmodules/loggeraudit.py",
                      "func_name" =&gt; "finalize_log",
                    "interpreter" =&gt; "/opt/privacyidea/venv/bin/python",
            "interpreter_version" =&gt; "3.6.9"
     },
          "level" =&gt; "INFO",
        "program" =&gt; "/opt/privacyidea/pi-manage",
           "port" =&gt; 47962,
            "pid" =&gt; 10047,
      "logsource" =&gt; "myhost",
     "@timestamp" =&gt; 2020-03-25T15:32:42.748Z,
       "@version" =&gt; "1",
           "type" =&gt; "python-logstash",
        "message" =&gt; "{'success': True, 'serial': '<strong>', 'user': '</strong>', 'realm': '**', 'resolver': '', 'token_type': '', 'client': '127.0.0.1', 'client_user_agent': 'firefox', 'privacyidea_server': 'localhost:5000', 'action': 'GET /audit/', 'action_detail': '', 'info': '', 'administrator': 'admin', 'policies': '', 'timestamp': datetime.datetime(2020, 3, 25, 15, 32, 42, 748526)}",
           "host" =&gt; "henning-t470"
 }</pre>



<h3 class="wp-block-heading">Display privacyIDEA logs with Kibana</h3>



<p>The logs received by Logstash are sent to Elasticsearch which talks to the Kibana instance. The Elasticsearch indices should appear in Kibana&#8217;s index management, available from the home screen.</p>



<figure class="wp-block-image size-large is-resized"><img decoding="async" width="273" height="154" src="https://www.privacyidea.org/wp-content/uploads/2020/03/kibana-index-patterns-button-1.png" alt="" class="wp-image-1917" style="width:205px;height:116px"/></figure>



<p>The privacyIDEA indices will look like shown below.</p>


<div class="wp-block-image is-style-default">
<figure class="aligncenter size-large is-resized"><a href="https://www.privacyidea.org/wp-content/uploads/2020/03/kibana-indices.png"><img decoding="async" width="1024" height="437" src="https://www.privacyidea.org/wp-content/uploads/2020/03/kibana-indices-1024x437.png" alt="" class="wp-image-1911" style="width:768px;height:328px" srcset="https://www.privacyidea.org/wp-content/uploads/2020/03/kibana-indices-1024x437.png 1024w, https://www.privacyidea.org/wp-content/uploads/2020/03/kibana-indices-300x128.png 300w, https://www.privacyidea.org/wp-content/uploads/2020/03/kibana-indices-768x328.png 768w, https://www.privacyidea.org/wp-content/uploads/2020/03/kibana-indices.png 1082w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></figure>
</div>


<p>Note: the yellow health status is due to a default index setting <code>"index.number_of_replicas": "1"</code>. Changing it to zero will result in a green status. Under &#8220;Data Views&#8221; create a new data view with the index-pattern &#8220;privacyidea*,pi-audit-*,pi-event*&#8221;.</p>



<figure class="wp-block-gallery alignwide has-nested-images columns-default wp-block-gallery-1 is-layout-flex wp-block-gallery-is-layout-flex">
<figure class="wp-block-image size-full"><a href="https://www.privacyidea.org/wp-content/uploads/2024/11/Create-Data-View.png"><img loading="lazy" decoding="async" width="816" height="568" data-id="2362" src="https://www.privacyidea.org/wp-content/uploads/2024/11/Create-Data-View.png" alt="" class="wp-image-2362" srcset="https://www.privacyidea.org/wp-content/uploads/2024/11/Create-Data-View.png 816w, https://www.privacyidea.org/wp-content/uploads/2024/11/Create-Data-View-300x209.png 300w, https://www.privacyidea.org/wp-content/uploads/2024/11/Create-Data-View-768x535.png 768w" sizes="auto, (max-width: 816px) 100vw, 816px" /></a></figure>



<figure class="wp-block-image size-large"><a href="https://www.privacyidea.org/wp-content/uploads/2024/11/Logs_View.png"><img loading="lazy" decoding="async" width="1024" height="498" data-id="2363" src="https://www.privacyidea.org/wp-content/uploads/2024/11/Logs_View-1024x498.png" alt="" class="wp-image-2363" srcset="https://www.privacyidea.org/wp-content/uploads/2024/11/Logs_View-1024x498.png 1024w, https://www.privacyidea.org/wp-content/uploads/2024/11/Logs_View-300x146.png 300w, https://www.privacyidea.org/wp-content/uploads/2024/11/Logs_View-768x374.png 768w, https://www.privacyidea.org/wp-content/uploads/2024/11/Logs_View.png 1042w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></figure>
</figure>



<p>In the Logs view, select the privacyIDEA data view you can select the log columns (&#8220;selected fields&#8221;) to be shown. The privacyIDEA log messages are now nicely display<em>ed.</em></p>



<figure class="wp-block-image alignwide size-large"><a href="https://www.privacyidea.org/wp-content/uploads/2024/11/Bild3.png"><img loading="lazy" decoding="async" width="1024" height="498" src="https://www.privacyidea.org/wp-content/uploads/2024/11/Bild3-1024x498.png" alt="" class="wp-image-2364" srcset="https://www.privacyidea.org/wp-content/uploads/2024/11/Bild3-1024x498.png 1024w, https://www.privacyidea.org/wp-content/uploads/2024/11/Bild3-300x146.png 300w, https://www.privacyidea.org/wp-content/uploads/2024/11/Bild3-768x374.png 768w, https://www.privacyidea.org/wp-content/uploads/2024/11/Bild3.png 1042w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></figure>



<p>Don&#8217;t forget to save!</p>



<h3 class="wp-block-heading">Event-based logging from privacyIDEA to Logstash</h3>



<p>New in <a href="https://www.privacyidea.org/privacyidea-3-3-with-webauthn/">privacyIDEA 3.3</a> is the Event Handler module &#8220;Logging&#8221;. With this module, custom logging messages can be bound to any event. This opens the door to a whole new world of monitoring possibilities in privacyIDEA. To demonstrate the feature, we simply log whenever a token is disabled &#8212; a silly example, of course. The Event Handler is created as shown below.</p>


<div class="wp-block-image">
<figure class="aligncenter size-large is-resized"><a href="https://www.privacyidea.org/wp-content/uploads/2020/03/LoggingNotification4.png"><img loading="lazy" decoding="async" width="930" height="942" src="https://www.privacyidea.org/wp-content/uploads/2020/03/LoggingNotification4.png" alt="" class="wp-image-1928" style="width:698px;height:707px" srcset="https://www.privacyidea.org/wp-content/uploads/2020/03/LoggingNotification4.png 930w, https://www.privacyidea.org/wp-content/uploads/2020/03/LoggingNotification4-296x300.png 296w, https://www.privacyidea.org/wp-content/uploads/2020/03/LoggingNotification4-768x778.png 768w" sizes="auto, (max-width: 930px) 100vw, 930px" /></a></figure>
</div>


<p>For the Logging module, only one action is available. The log level, the name of the logger and a custom message are required. The message field supports variables known from the user notification module (see <a href="https://privacyidea.readthedocs.io/en/latest/eventhandler/logginghandler.html">documentation</a>). Note, that it depends on the context of the RESTful API event if a certain variable is available or not. </p>



<p>The chosen logger name has to be added as a logger in <code>logging.yml</code> to send it to Logstash.</p>



<pre class="wp-block-preformatted has-small-font-size">loggers:
  pi-eventlog:
    handlers:
      - logfile
      - logstash_async
    level: DEBUG</pre>



<p>Restart privacyIDEA to apply the changes in the config file <code>logging.yml</code>. After triggering the event by disabling a token in privacyIDEA, Kibana shows the notification.</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1024" height="25" src="https://www.privacyidea.org/wp-content/uploads/2020/03/LoggingNotification3-1024x25.png" alt="" class="wp-image-1927" srcset="https://www.privacyidea.org/wp-content/uploads/2020/03/LoggingNotification3-1024x25.png 1024w, https://www.privacyidea.org/wp-content/uploads/2020/03/LoggingNotification3-300x7.png 300w, https://www.privacyidea.org/wp-content/uploads/2020/03/LoggingNotification3-768x18.png 768w, https://www.privacyidea.org/wp-content/uploads/2020/03/LoggingNotification3.png 1081w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure>



<p>Of course, you can use the logging event handler for more reasonable purposes like not to send the full audit log to logstash but to single-out the important validate-check events. You can even apply some more conditions, if you like making use of the powerful condition properties of the privacyIDEA Event Handlers. This not only spares bandwidth and storage space but prevents important information to be buried by other data. The message field in privacyIDEA can be used for a custom log message with contextual information. The configurable logger name (e.g. <em>pi-validate-check</em>) provides an additional identifier. In the case of suspicious behavior or a security incident, all the information is there to quickly track down the threat.</p>



<h3 class="wp-block-heading">Conclusion</h3>



<p>With this demonstration of the logging facility, privacyIDEA proves again to be extraordinarily scalable. It integrates well with logging systems like Logstash and Splunk since the privacyIDEA server builds on the standard python logging library. For Logstash, this article showed the detailed steps how to integrate privacyIDEA via the loggeraudit and a small third-party python module called <em>python-logstash-async</em>.</p>



<p>In privacyIDEA 3.3 the logging capabilities have been further extended by an Event Handler module which enables to conditionally log arbitrary events to the python logging system. We showed that also these messages can easily be passed to Logstash and open a vast playground custom logging.</p>



<p>The solution shown here is only one possible approach. Since privacyIDEA is available as open source an licensed under the AGPL, another possibility would be of course to write your very own logger module to do whatever <em>you</em> want. privacyIDEA is and will be always open source and therefore it will <em>always stay</em> in <em>your</em> hands.</p>



<p>If you would like to have a custom logger module, but have no time to  implement it yourself, you can always request a quote from <a href="https://netknights.it/en/">Netknights</a>, the company which drives the privacyIDEA innovation via <a href="https://github.com/privacyidea/">Github</a>. They also provide professional support for privacyIDEA, including enterprise repositories for Ubuntu and CentOS/RHEL containing the server and a number of additional components and tools.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.privacyidea.org/event-based-logging-with-privacyidea-and-logstash/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
	</channel>
</rss>
