0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Kong Gatewayのログ系プラグインで出力されるログの内容をマスクする

Last updated at Posted at 2025-07-04

小ネタ。
Kong GatewayでAPIリクエストを中継した時、そのアクセスログをHTTP Log PluginFile Log Pluginなどで詳細化して出力することが出来る。

出力例(クリックして表示)
{
	"latencies": {
		"kong": 0,
		"request": 8,
		"receive": 0,
		"proxy": 8
	},
	"source": "upstream",
	"started_at": 1751603220086,
	"request": {
		"method": "GET",
		"uri": "/httpbin/headers",
		"url": "http://localhost:8000/httpbin/headers",
		"size": 93,
		"headers": {
			"user-agent": "curl/7.81.0",
			"host": "localhost:8000",
			"accept": "*/*",
			"apikey": " mykey"
		},
		"querystring": {},
		"id": "7c0815404e0bb37490d7132e31dd2d3e"
	},
	"route": {
		"paths": [
			"/httpbin"
		],
		"preserve_host": false,
		"name": "httpbin-route",
		"path_handling": "v0",
		"service": {
			"id": "4578a848-ceb0-4db6-a143-b2ebea9d79df"
		},
		"regex_priority": 0,
		"protocols": [
			"http",
			"https"
		],
		"id": "aa3a4476-d322-4d8e-850b-33d87596502b",
		"created_at": 1745394934,
		"ws_id": "0dc6f45b-8f8d-40d2-a504-473544ee190b",
		"updated_at": 1745395362,
		"request_buffering": true,
		"response_buffering": true,
		"priority": 0,
		"strip_path": true,
		"https_redirect_status_code": 426
	},
	"workspace_name": "default",
	"tries": [
		{
			"balancer_start": 1751603220086,
			"balancer_start_ns": 1751603220087000000,
			"target_id": "unknown",
			"balancer_latency_ns": 22016,
			"balancer_latency": 0,
			"ip": "34.107.88.45",
			"keepalive": true,
			"port": 443
		}
	],
	"workspace": "0dc6f45b-8f8d-40d2-a504-473544ee190b",
	"upstream_uri": "/headers",
	"response": {
		"size": 760,
		"headers": {
			"date": "Fri, 04 Jul 2025 04:27:00 GMT",
			"content-length": "381",
			"access-control-allow-origin": "*",
			"connection": "close",
			"content-type": "application/json",
			"x-kong-upstream-latency": "8",
			"x-kong-request-id": "7c0815404e0bb37490d7132e31dd2d3e",
			"via": "1.1 kong/3.10.0.0-enterprise-edition",
			"x-kong-proxy-latency": "0",
			"access-control-allow-credentials": "true",
			"server": "gunicorn/19.9.0"
		},
		"status": 200
	},
	"upstream_status": "200",
	"service": {
		"enabled": true,
		"connect_timeout": 60000,
		"name": "httpbin-dummy-app",
		"updated_at": 1749625258,
		"id": "4578a848-ceb0-4db6-a143-b2ebea9d79df",
		"retries": 5,
		"read_timeout": 60000,
		"created_at": 1745394917,
		"write_timeout": 60000,
		"ws_id": "0dc6f45b-8f8d-40d2-a504-473544ee190b",
		"protocol": "https",
		"port": 443,
		"path": "/",
		"host": "httpbin.org"
	},
	"client_ip": "172.20.0.1"
}

この時、Request Transformer PluginでAPIキーを追加する場合など、一部の情報をログには残したくないことがあると思う。
こういう場合、各ログ系プラグインが持つcustom_fields_by_luaという設定項目で削除することが出来る。
ここではRequest Transformer Pluginでヘッダに"apikey: mykey"を設定した際に通信先には適切にヘッダが届き、ログには"apikey: Masked!"として値が見えないようにしてみる。

custom_fields_by_luaの仕組み

この設定値は少し特殊であり、Luaのコードを書くことでログの内容を上書きする。
ログ系プラグインはログの内容をJSON形式で出力するが、JSON内のキーとバリューを指定する形となり、バリューの部分にLuaのコードを書く。

書き換えの実践

今は以下のようにヘッダがログ出力されている。

{
	"request": {
		"headers": {
			"user-agent": "curl/7.81.0",
			"host": "localhost:8000",
			"accept": "*/*",
			"apikey": " mykey"
		}
}

この場合、request.headers.apikeyの値を設定することで、値の書き換えが可能となる。
また、値はLuaで書くため、return "文字列"のような形で指定する。
これらを踏まえると、設定値は以下のような感じになる。
image.png

これを設定してログを改めて取得する。
ログは以下となり、マスクされていることが分かる。

{
	"request": {
		"headers": {
			"user-agent": "curl/7.81.0",
			"host": "localhost:8000",
			"accept": "*/*",
			"apikey": "Masked!"
		},
        :(省略)

アクセス先から見たヘッダ情報は以下となった。

{
  "headers": {
    "Accept": "*/*",
    "Apikey": "mykey",
    "Host": "httpbin.org",
    "User-Agent": "curl/7.81.0",
    "X-Amzn-Trace-Id": "Root=1-68675ed5-319449cf3106663577566e0d",
    "X-Forwarded-Host": "localhost",
    "X-Forwarded-Path": "/httpbin/headers",
    "X-Forwarded-Prefix": "/httpbin",
    "X-Kong-Request-Id": "d954b84d226ac8559bc77918c6c58f31"
  }
}

apikeyは適切に送られ、ログだけマスクされていることが確認できた。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?