iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🚪

knockrd: Dynamically Update IP Address Restrictions - fujiwara-ware 2024 Day 19

に公開

This article is the 19th day of the fujiwara-ware advent calendar 2024.

What is knockrd

https://github.com/fujiwara/knockrd

knockrd is a tool for dynamically updating IP address restrictions for accessing sites. It is pronounced "knock-r-d".

knockrd operates as a web application. When it receives an HTTP request, it adds the source IP address of that request to an allowlist. IP addresses added to the allowlist are automatically deleted after a certain period of time. By authenticating the initial access request using an appropriate method, you can dynamically add only the IP addresses used by authenticated individuals to the allowlist.

Why I made it

Even in the Reiwa era, the practice of IP address restriction does not easily disappear. While it might seem that such antiquated restrictions would be unnecessary if zero trust were properly implemented, there are cases where that is difficult.

Specifically, in my case, I needed to allow access only from development applications to a development environment for a mobile app (game). Since the access is via HTTPS, it is possible to introduce authentication like OIDC, but passing OIDC authentication from a game application built with Unity is quite challenging. Furthermore, the application doesn't require source authentication in production, so it's a significant effort to implement a special mechanism just for this purpose.

IP address restriction is the simplest way to allow access from locations with a fixed IP address. However, as this coincided with the COVID-19 pandemic, it was difficult to rely on fixed addresses from offices and similar locations. Therefore, I created knockrd as a mechanism to dynamically update the allowlist.

How it works

knockrd operates as an HTTP server. When it receives an HTTP request, it adds the source IP address of that request to a DynamoDB table. (Authenticating this request separately using an appropriate method is the most important part.)

A Lambda function triggered by DynamoDB Streams reflects the added IP address in Security Groups or WAF IP Sets.

Using the DynamoDB TTL feature, IP addresses are deleted once a certain amount of time has passed. The Lambda function triggered by the Stream also removes them from the Security Groups or WAF IP Sets.

The following sequence diagram shows the integration with WAF IP Set.

It is also possible to dynamically update IP address restrictions using Security Groups or the nginx auth request mechanism. For more details, please refer to the README.

Operation Method

knockrd incorporates ridge, which was introduced on day 10 of this advent calendar, so it can be run on AWS Lambda. When running on Lambda, server operation is not necessary.

https://zenn.dev/fujiwara/articles/fujiwara-ware-2024-ridge

The initial request to knockrd must be authenticated and authorized separately using an appropriate method. For example, Basic authentication or OIDC.

However, knockrd itself does not have a feature for request authentication and authorization. You need to provide authentication and authorization separately. I mainly used OIDC authentication on an ALB (Application Load Balancer) and invoked the Lambda from the ALB.

In addition to the Lambda that accepts HTTP access, a Lambda triggered by DynamoDB Streams is required. This Lambda is responsible for reflecting IP addresses added to or deleted from the DynamoDB table into Security Groups or WAF IP Sets. Both the Lambda for the web application and the Lambda triggered by DynamoDB Streams operate simply by placing a single knockrd release binary named bootstrap, making deployment easy.

Since it is entirely contained within Lambda and DynamoDB, which are only executed rarely (only for requests to update access permissions) during operation, once it is introduced, it should be possible to operate with almost no fixed costs.

Summary

knockrd is a tool for dynamically updating IP address restrictions. It runs on AWS Lambda and works with DynamoDB to add and remove IP addresses.

When you maintain a fixed IP address list, it is very time-consuming to update it frequently in response to requests and further keep it secure by deleting addresses that are no longer needed. By using knockrd, you should be able to significantly reduce that effort.

If you have such a need, please give knockrd a try.

Stay tuned for tomorrow's post!

Reference Materials

https://speakerdeck.com/fujiwara3/kichijojipm23

Discussion