WEBVTT

00:00:01.540 --> 00:00:05.550
Now that the audit logs are set up to be produced in the Kafka cluster,

00:00:05.680 --> 00:00:07.960
we can do a lot of interesting things with them.

00:00:08.840 --> 00:00:10.280
We can, for example,

00:00:10.300 --> 00:00:13.460
set up an alerting mechanism if someone creates a lot of

00:00:13.460 --> 00:00:18.300
resources in a short amount of time, by effectively spamming our server.

00:00:18.300 --> 00:00:22.250
To set this sort of mechanism up, we can use ksqlDB.

00:00:22.250 --> 00:00:28.060
First, we need to give our data some structure using the CREATE STREAM statement.

00:00:28.940 --> 00:00:29.600
Basically,

00:00:29.610 --> 00:00:33.560
we are telling ksqlDB that the log events produced in the

00:00:33.570 --> 00:00:36.630
audit‑logs topic have a DELIMITED structure.

00:00:36.630 --> 00:00:40.150
All the fields, date and time; identifier,

00:00:40.160 --> 00:00:44.550
represented by username and IP; resourceType; action; and

00:00:44.550 --> 00:00:47.160
resourceName are delimited by a comma.

00:00:48.140 --> 00:00:50.070
Once we've defined the data structure,

00:00:50.080 --> 00:00:54.560
we can start defining the SPAM resource creation detection mechanism.

00:00:55.340 --> 00:00:57.440
This query might look a bit intimidating,

00:00:57.440 --> 00:01:00.760
but it will be pretty easy to understand once we go through it.

00:01:01.340 --> 00:01:03.560
So, how is this mechanism going to work?

00:01:04.440 --> 00:01:07.490
Well, for each time interval of 60 seconds,

00:01:07.500 --> 00:01:11.360
we calculate the number of entries where a resource was created.

00:01:12.140 --> 00:01:17.150
If the user creates 3 or more resources in that time interval of 60 seconds,

00:01:17.540 --> 00:01:20.930
then an alert mentioning the username and the account will

00:01:20.930 --> 00:01:24.100
be sent in the audit‑logs‑spam topic.

00:01:24.100 --> 00:01:26.060
By pressing Enter,

00:01:26.070 --> 00:01:28.660
we're launching a process in the background that

00:01:28.660 --> 00:01:30.350
will start the analysis process.

00:01:31.040 --> 00:01:33.170
So, how can we check if it's working?

00:01:33.170 --> 00:01:38.690
Well, we can inspect if any alerts are produced in the audit‑logs‑spam topic.

00:01:38.690 --> 00:01:43.560
Just like in the previous demo, we can easily do this using the PRINT statement.

00:01:44.840 --> 00:01:46.860
No alert has been published so far,

00:01:46.880 --> 00:01:49.540
but that's because no one has created more than 3

00:01:49.540 --> 00:01:54.680
resources in a 60‑second time interval, so let's do just that.

00:01:54.680 --> 00:01:59.830
First, I need to open a new terminal, because in order to create three resources,

00:02:00.000 --> 00:02:04.850
we need to execute three HTTP requests to the audit logger using curl.

00:02:05.640 --> 00:02:10.860
I'm going to copy and paste the first example request and execute it 3 times.

00:02:11.440 --> 00:02:14.160
This should definitely trigger our alerting mechanism.

00:02:14.940 --> 00:02:16.890
If we switch back to the ksqlDB CLI,

00:02:16.890 --> 00:02:20.660
we see that something has already been published.

00:02:21.340 --> 00:02:25.130
We have the key, which is our identifier represented by the key,

00:02:25.140 --> 00:02:27.620
and a little bit of the IP.

00:02:27.620 --> 00:02:31.590
The other two large numbers represents the time window timestamps,

00:02:31.870 --> 00:02:35.000
meaning when it started and when it ended.

00:02:35.000 --> 00:02:39.560
Finally, we also have the value that represents the number of resources created.
