iTranslated by AI

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

Combating Crypto Miners: Using Go with Azure Functions Linux Consumption Plan

に公開

About a month ago, I was targeted by a Pull Request on GitHub Actions that was attempting to mine cryptocurrency.

https://twitter.com/yskszk63/status/1380778347437821960

When a Pull Request arrives, the GitHub Actions jobs triggered by it end up running calculations endlessly across about 20 parallel instances.
While I cancel them as soon as I notice, GitHub's Free plan has a limit of 2,000 minutes/month, so it leaves me feeling quite frustrated.

Since I hate this, I created a GitHub App to cancel such GitHub Actions jobs.

https://github.com/yskszk63/cancel-workflow-run

Strategy

The idea is to hook into the startup of GitHub Actions jobs and cancel them if the job is defined by a new Pull Request. For my case, this is sufficient.

Initially, I built the backend using Python on Heroku's Free plan.
However, GitHub Webhooks are recorded as Timed Out if they don't return a response within 10 seconds.
Since Heroku's Free plan goes to sleep quickly and is usually asleep when a Webhook arrives, the history ends up filled with Timed Out errors, which feels messy.

Therefore, I wanted to build a backend that wakes up quickly without costing money, so I tried Go + Azure Functions.

What is the Azure Functions Linux Consumption Plan?

It is Azure's FaaS (Function as a Service).
You might associate it with Windows + C#, but it can also run various languages on Linux.
I don't own a Windows machine and can't write C#, yet I was able to build something that works.

Initially, Azure Functions Consumption plans were only for Windows, but the Linux Consumption plan is now GA (Generally Available).
The detailed mechanism explained in this article (Japanese) was very helpful.

Azure Functions offers 1 million requests and 400,000 GB-seconds of resource consumption for free always.

A mechanism called Custom Handlers recently went GA, making it possible to implement functions in any language, not just C#, JavaScript, Java, or Python.

What are Custom Handlers?

First, let's review Azure Functions.
Azure Functions is a system that kicks off functions based on triggers.

In addition to triggers, you can bind associated inputs and outputs to the function.

  • Triggers
    • HTTP Trigger ... Starts the function based on an HTTP request. The URL, method, headers, and body of the HTTP request are bound as function arguments.
    • Timer Trigger ... Starts the function periodically based on a cron-like time specification.
    • EventGrid Trigger ... Starts the function based on event delivery from EventGrid (a Pub/Sub service).
    • etc.
  • Inputs
    • Blob ... Binds a specified Blob in Azure Storage as a function argument.
    • CosmosDB ... Binds a specified record in CosmosDB as a function argument.
    • etc.
  • Outputs
    • HTTP ... Binds the response to the HTTP trigger request as the function return value.
    • EventGrid ... Binds the function return value as an event publication to EventGrid.
    • Blob ... Stores the function return value into a specified Blob in Azure Storage.
    • CosmosDB ... Stores the function return value into a specified record in CosmosDB.
    • etc.

You can get a sense of it by looking around here.

The Functions Host, which is part of the service, handles these bindings.
Previously, function calls from the Functions Host used mechanisms specialized for each supported language.
Custom Handlers are designed to be used generically by using HTTP requests/responses, allowing functions to be implemented in various languages.

Furthermore, when only HTTP triggers are used, HTTP requests/responses can be bypassed, allowing you to implement functions with the feeling of developing a normal API server.

Local Development

You can run it locally with Azure Functions Core Tools. While some situations require a bit of extra effort when binding services, it makes things easier since you can verify behavior without having to deploy every time.

Things I Noticed and Stuck Points During Implementation

CGO

Simply running go build will link dynamic libraries.

$ go build -o app
$ ldd app
	linux-vdso.so.1 (0x00007ffc733ca000)
	libpthread.so.0 => /usr/lib/libpthread.so.0 (0x00007f7e2ae79000)
	libc.so.6 => /usr/lib/libc.so.6 (0x00007f7e2acac000)
	/lib64/ld-linux-x86-64.so.2 => /usr/lib64/ld-linux-x86-64.so.2 (0x00007f7e2aeb6000)
$ file app
app: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, Go BuildID=F3hqAn1f_eQ3ixd2paU0/L-Z6juymmivPuIHPXFPg/lYGTqgl_U_LogSimNKOf/n5eybwLJ6tJ27j10m8PQ, not stripped

Since my environment is Arch Linux, glibc is relatively new. Therefore, if I deploy and run it on Azure Functions, it fails.
To avoid this, I specify the following to ensure it's compiled as a static binary.

$ CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o app
$ ldd app
	not a dynamic executable
$ file app
app: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, Go BuildID=e0bWgJUij4gee1Pz8_Px/OlgtQa0T9BqCawWYIm9z/fl_6voTAytl1LRrRcqxF/5CcVo8_9lNEeDTq1Q6E6, not stripped

I used to think Go was always compiled into a single static binary. (I've only just started writing Go.)
If anyone knows why dynamic libraries get linked, I'd appreciate it if you could let me know!

Ensuring it "Takes Off"

After deploying to Azure Functions, if it crashes before starting to listen for HTTP, the logs go missing.
When that happens, tracking down what occurred is painful.
For example, if you implement "Fail Fast" logic (like failing when environment variables are missing at process startup), you lose visibility and might only realize something went wrong much later.
It seems better to implement it so that it stops as little as possible until it successfully starts listening for HTTP.

Logs

There are two ways to output logs:

  • ① Output to standard output
  • ② Embed in the HTTP response as the function's return value

Method ② is very easy to track as it's tied to the function call. However, if the HTTP response cannot be returned, all logs disappear.
Also, if you use the HTTP request/response bypass method, you cannot output logs this way.

Following this article, I chose the method of streaming structured logs to standard output.
However, querying the logs becomes complicated. I hope this area improves in the future.

Deployment

For the Linux Consumption plan, it seems Kudu is not available.
As a result, there are various restrictions on deployment compared to other plans.

You can reflect changes in Azure Functions by specifying the URL of a zip file containing the entire directory structure in the WEBSITE_RUN_FROM_PACKAGE application setting (environment variable) and then calling /admin/host/synctriggers?<master_key>.

I set WEBSITE_RUN_FROM_PACKAGE to point to a GitHub release and configured GitHub Actions to call /admin/host/synctriggers?<master_key> so that it deploys to the test environment on every push.

GitHubで編集を提案

Discussion