03 Initial Development
Architectural Updates
As I worked on migrating from K8s Ingress to the new Gateway API. I researched and introduced the Coraza WAF integration with Envoy Gateway. This integration fully changed my original view for this microservice.
Gateway API Migration and it's Coraza WAF integration are already documented on this site.
Originally, I had thought of the transaction lifecycle to originate from this blog itself. The SPA reaches out to this microservice and receives a 0 or a 1 from the Bloom Fitler lookup. Then the 404 delivery would still be handled by the SPA itself. With a modular WAF upfront now, I have realized that the SPA does not even need to know of this flow! With a 404 default page and a list of paths I can have Coraza serve all True Negatives before they even reach my webserver.
Now, creating the integration to call this microservice from Coraza will be a new challenge that will incurr a learning curve. But this is music to my ears and not a blocker, the end goal for all of this is to learn!
New flow
A major goal of this microservice is to warehouse and time out vulnerability crawlers / attackers based on load. With this new architecture we are not giving up on this goal. We will have two WAF rules, one at the front which will 403 banned IPs and the bloom filter rule at the end of our standard WAF rules.
Given this, an HTTP request's lifecycle will look something like this:
HTTP request ──► Coraza WAF
│ 1. IP on my ban list? ── yes ──► 403
│ 2. Standard Coraza WAF rules
│ 3. GRPC Evaluate(path, ip)
▼
┌──────────────────────────┐
│ cascadingBotFilters │
│ (N replicas) │
│ │
│ bloom filter cache (vN) ┼─────► 0/1 risk score
│ │
│ miss queue ─────────────┼──► worker:
│ LISTEN conn │ → window count ≥ threshold?
└──────────────────────────┘ → INSERT ban
Simple enough right? Bloom filters will be in charge of evaluating HTTP request's provided a path and it's bloom filter. Once evaluated and returned, asynchronously, it will record in PSQL the occurrence and the crawler's IP. If the IP has been recorded in the DB Table over a time based count threshold, add it to the ban IPs table. Biggest things to keep in mind here are:
- Crawler IP banning and warehousing must happen asynchronously after the bloom filter evaluation has been returned.
- Given a ban's time awareness, our application clock must be fully in sync with our environment.
- Adhering to Cloud Native principles. HTTP Request's must be recorded in a reliable point of truth, which will be our PSQL DB.
- Last fun little gotcha, a bloom filter miss, evaluating to 0, will provide a risk score of 1 and vice versa.
Asynchronous workflows
When working with multiple replicas and a time constraint, we have to be very cautious of each thread of compute. Regarding the risk score evaluation, it should occur at O( O(HashFunction) * x ) where x is our desired number of hashes decided at Bloom Filter creation and false positive rate. But this exactly brings the need for concurrency. The path's list and table will be freely changing as I add more pages to the site, which means Bloom Filter recreation must not block threads of compute. Additionally, we need to be notified on Bloom Filter update. Which will be delivered via NOTIFY statements from PSQL.
As mentioned, all banning will be happening after we have provided a risk score to Coraza. But additionally, all path requests will be recorded in PSQL for data warehousing and analysis. Once a ban has been observed and ban end time calculated, the Ban table will have to be updated. This will also have to use PSQL's NOTIFY to ensure a new list of banned IPs is provided to Coraza. Now, I really do not know how it is all going to look with the Coraza integration. If it will be another cached list within the microservice with another gRPC service or it will be a list placement onto Coraza itself. But we will get there when we get there.
SQL Schema and Partitioning
Last but not least, this data warehousing goal will become a problem of scale within our DB. Provided the wild west that is the internet now a days, if we do not have some sort of partitioning in our tables we will be harming this solution.
We are going to have partitioning by month, so True Negatives will be recorded and on a table for the current month. After that, we gotta think on how we warehouse it. Most likely, I will end up making an S3 backed per month storage to analyze in the future. If not S3, I would think using any open source columnar database.
Other than that, our schema is pretty simple, a table to record Bloom Filter misses, another to act as a point of truth for paths in our SPA and another for IP bans and times.
Open Sourcing
As I integrate this app further into my system open sourcing is becoming a much taller task. But I still plan on doing all I can to open source it. Maybe if anyone else is interested on this idea or project they will add more integrations for it to function in.