Tanium Connect: Deduplicating Data in SQL Server to Accurately Capture Current State
The capability to send data collected by Tanium to a Microsoft SQL Server database has been part of Connect’s feature set for a number of years. Many customers have used this ability to gather their data into a centralized location to build reports on everything from vulnerabilities to patching status.
Once a customer configures Connect to send data to a database, they quickly realize that Connect does a basic INSERT of the data to the table, which can lead to duplicate data in the table. This could be useful when you want to maintain a historic record of data state, and build reports that show how that data changes over time. On the other hand, some customers want the data to be deduplicated based off of a field so that they only see the latest state of the data they have gathered.
This post shows how to configure a SQL Server destination in Connect with a Stored Procedure that deduplicates the data in the SQL table. This translates to cleaner and easier reporting for ephemeral data like open ports or CVEs for application vulnerabilities.
Background
Unfortunately, there is no easy UPSERT (a combination of INSERT and UPDATE, depending on if the data exists in the table already, available in some other database engines) functionality in MS SQL. As such, we have to get creative when we want to ensure the data in our table is fresh. However, Connect does have the ability to execute a pre-run or post-run SQL Stored Procedure, which can be used to manipulate the data in the database.
These fields are only shown in the Connect user interface when the SQL user used to connect to the database server has access to any stored procedures within the selected database.
In the example below, we will create a table called “inventory” that we want to send Computer Name data to. We will first configure our destination table in our SQL Server, and then we will configure Connect to send over the desired data and execute our Stored Procedure.
One important thing to keep in mind is that Computer Name alone is almost never unique in an enterprise environment, so this example is overly simplistic. If you are looking to enrich a true CMDB for your enterprise, Tanium Asset is a much better solution that offers automated deduplication of data by computer by examining a number of different aspects of each endpoint in your environment.
That being said, this method of deduplicating SQL data is excellent for working with data that you would not want to store in a CMDB product. Ephemeral data, like open ports or CVEs for application vulnerabilities, is a much better use case for this procedure.
Part I: SQL Configuration
Since we are sending a relatively simple data set to SQL, specifically just computer names, we only need one field to hold that data. I called that column [computerName] in the “inventory” table. We also need a way to tell what the newest data is in our table, so we will create another column called [insertTime] and give it a default value of getDate(), as seen below:
After saving the table, we need to create a Stored Procedure that can automatically deduplicate the data within that table. Below is a simple Stored Procedure that will extract the newest data based off of the [insertTime] column, copy it into a temp table, remove the data from our “inventory” table, and re-insert the deduplicated data back into the original table.
In a production environment, you would probably want a more robust Stored Procedure that would do things like verify the temp table doesn’t exist before starting, adding logging, etc.
Now that our “inventory” table has been created and we have a Stored Procedure called “sp_dedupe” that eliminates the stale data in the table, we are ready to configure Connect to send our data to the database.
Part 2: Connect Configuration
After creating our saved question of “Get Computer Name from all machines” in Interact, we can create a new connection that uses this saved question as the data source. Since there are other articles describing that process in detail, we will focus on the Connect destination configuration to ensure our data deduplication project is successful.
While creating your new connection in Connect, select SQL Server as your destination. Fill in the server name and user credentials and hit the Connect button. After successfully authenticating to the SQL Server, a list of databases will appear. Select the database you created the “inventory” table in. Next, choose the correct schema and the “inventory” table from the drop-down lists.
At this point, if we were to save the connection and run it, our first set of data would go to the table and there would be no duplicates. On the next run of the connection, the question results would be sent again, and we would likely start to see duplicate computer names in the table. Rather than doing that, we need to configure this connection to run the Post-run Stored Procedure created above, as shown below:
Furthermore, we need to configure the columns on our connection so that only the Computer Name field is sent to our table. The [insertDate] column will be automatically populated by SQL Server with the current time/date stamp when the data is inserted.
After the column configuration is completed, we can finish creating the connection and run it. On the first run, the Stored Procedure will execute, but there will be no duplicates. We might see something like this result set upon completion:
After running the connection a second time, we will see the same [computerName] in the table, but with a newer [insertTime], as shown below:
This is proof that the Stored Procedure executed and deleted the older entry in our table. Now you can build similar Stored Procedures to deduplicate the data in your SQL Server destinations!