Skip to content

Instantly share code, notes, and snippets.

@rksk
Last active March 9, 2022 12:30
Show Gist options
  • Save rksk/ea0b9060d7cd7f336e79e57b3450a2c6 to your computer and use it in GitHub Desktop.
Save rksk/ea0b9060d7cd7f336e79e57b3450a2c6 to your computer and use it in GitHub Desktop.
Publishing WSO2 Identity Server analytics events directly to a database

Tested with WSO2 Identity Server 5.10.0

Enable event publishing in the deployment.toml with following config

[identity_mgt.analytics_login_data_publisher]
enable=true

Define a new datasource for event publishing in the deployment.toml

[[datasource]]
id="WSO2_AUTH_EVENTS_DB"
url = "jdbc:mysql://localhost:3306/IAMtest?useSSL=false"
username = "root"
password = "root"
driver = "com.mysql.jdbc.Driver"
pool_options.maxActive = "80"
pool_options.maxWait = "60000"
pool_options.minIdle = "5"
pool_options.testOnBorrow = true
pool_options.validationQuery="SELECT 1"
pool_options.validationInterval="30000"
pool_options.defaultAutoCommit=false

Configure the event publisher file at <IS_HOME>/repository/deployment/server/eventpublishers/IsAnalytics-Publisher-wso2event-AuthenticationData.xml as below

<?xml version="1.0" encoding="UTF-8"?>
<eventPublisher
  name="IsAnalytics-Publisher-wso2event-AuthenticationData"
  statistics="disable" trace="disable" xmlns="http://wso2.org/carbon/eventpublisher">
  <from streamName="org.wso2.is.analytics.stream.OverallAuthentication" version="1.0.0"/>
  <mapping customMapping="disable" type="map"/>
  <to eventAdapterType="rdbms">
    <property name="datasource.name">WSO2_AUTH_EVENTS_DB</property>
    <property name="table.name">AuthenticationData</property>
    <property name="execution.mode">insert</property>
  </to>
</eventPublisher>

Now it will automatically create a table called AuthenticationData in the previously defined database and insert all the authentication data.

The table gets created according to the paramters and their data types defined in <IS_HOME>/repository/deployment/server/eventstreams/org.wso2.is.analytics.stream.OverallAuthentication_1.0.0.json file

CREATE TABLE `AuthenticationData` (
  `_TIMESTAMP` bigint(20) DEFAULT NULL,
  `AUTHSTEPSUCCESS` tinyint(1) DEFAULT NULL,
  `AUTHENTICATIONSTEP` varchar(255) DEFAULT NULL,
  `AUTHENTICATIONSUCCESS` tinyint(1) DEFAULT NULL,
  `CONTEXTID` varchar(255) DEFAULT NULL,
  `EVENTID` varchar(255) DEFAULT NULL,
  `EVENTTYPE` varchar(255) DEFAULT NULL,
  `FORCEAUTHENABLED` tinyint(1) DEFAULT NULL,
  `IDENTITYPROVIDER` varchar(255) DEFAULT NULL,
  `IDENTITYPROVIDERTYPE` varchar(255) DEFAULT NULL,
  `INBOUNDAUTHTYPE` varchar(255) DEFAULT NULL,
  `ISFIRSTLOGIN` tinyint(1) DEFAULT NULL,
  `LOCALUSERNAME` varchar(255) DEFAULT NULL,
  `META_TENANTID` int(11) DEFAULT NULL,
  `PASSIVEAUTHENABLED` tinyint(1) DEFAULT NULL,
  `REGION` varchar(255) DEFAULT NULL,
  `REMEMBERMEENABLED` tinyint(1) DEFAULT NULL,
  `REMOTEIP` varchar(255) DEFAULT NULL,
  `ROLESCOMMASEPARATED` varchar(255) DEFAULT NULL,
  `SERVICEPROVIDER` varchar(255) DEFAULT NULL,
  `STEPAUTHENTICATOR` varchar(255) DEFAULT NULL,
  `TENANTDOMAIN` varchar(255) DEFAULT NULL,
  `USERSTOREDOMAIN` varchar(255) DEFAULT NULL,
  `USERNAME` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8

Note:

  1. These insert operations are done synchornously. Therefore, the authentication flow will be delayed if the insert operations are slow.
  2. It is required to clean this table up to avoid accumilating a large amount of data and cause slownesses while inserting.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment