Azure Load Testing で mTLS を有効化した Amazon API Gateway を対象に実行する方法

2022.11.28

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

いわさです。

Azure にはマネージド JMeter サービスの Azure Load Testing というものがあります。
以前ブログで紹介させて頂いたことがあります。

最近のアップデートでクライアント証明書の設定が出来るようになりました。

以前 mTLS を有効化した API Gateway を作成しており、その際に負荷テストで利用したかったのですがクライアント証明書の兼ね合いがあって見送っていました。
今回のアップデートで使えるようになっていそうなので試してみましょう。

Amazon API Gateway で mTLS を有効化する

上記を参考に mTLS を有効化した API Gateway を作成します。

% curl https://hogemtls.tak1wa.com/
curl: (35) LibreSSL SSL_connect: SSL_ERROR_SYSCALL in connection to hogemtls.tak1wa.com:443 
iwasa.takahito@HL01200 hoge1108mls % ls               
RootCA.key    RootCA.pem    my_client.csr    my_client.key    my_client.pem

% curl -i --key my_client.key --cert my_client.pem https://hogemtls.tak1wa.com/
HTTP/2 200 
x-amzn-requestid: d9ca505f-c0f2-44b0-8613-ab51f9e22603
x-amz-apigw-id: bRkUWFKAtjMFbMA=
content-type: application/json
content-length: 20
date: Tue, 08 Nov 2022 08:59:45 GMT
{
  "hoge":"iwasa"
}

良さそうですね。

クライアント証明書なしで Azure Load Testing を実行

まずは失敗するパターンです。
クライアント証明書を設定せずに負荷テストを設定し実行してみましょう。

以下のような結果となりました。

実行ログをダウンロードして確認してみます。

javax.net.ssl.SSLExceptionが発生していますね。

timeStamp,elapsed,label,responseCode,responseMessage,threadName,dataType,success,failureMessage,bytes,sentBytes,grpThreads,allThreads,URL,Latency,IdleTime,Connect
1667899732787,512,Homepage,Non HTTP response code: javax.net.ssl.SSLException,Non HTTP response message: Connection reset,172.18.1.4-Thread Group 1-2,text,false,,3789,0,5,5,https://hogemtls.tak1wa.com/,0,0,512
1667899732786,515,Homepage,Non HTTP response code: javax.net.ssl.SSLException,Non HTTP response message: Connection reset,172.18.1.4-Thread Group 1-5,text,false,,3789,0,5,5,https://hogemtls.tak1wa.com/,0,0,515
1667899732786,518,Homepage,Non HTTP response code: javax.net.ssl.SSLException,Non HTTP response message: Connection reset,172.18.1.4-Thread Group 1-1,text,false,,3789,0,5,5,https://hogemtls.tak1wa.com/,0,0,518
1667899732786,521,Homepage,Non HTTP response code: javax.net.ssl.SSLException,Non HTTP response message: Connection reset,172.18.1.4-Thread Group 1-3,text,false,,3789,0,5,5,https://hogemtls.tak1wa.com/,0,0,521
1667899732787,529,Homepage,Non HTTP response code: javax.net.ssl.SSLException,Non HTTP response message: Connection reset,172.18.1.4-Thread Group 1-4,text,false,,3789,0,5,5,https://hogemtls.tak1wa.com/,0,0,529
1667899733335,491,Homepage,Non HTTP response code: javax.net.ssl.SSLException,Non HTTP response message: Connection reset,172.18.1.4-Thread Group 1-5,text,false,,3789,0,5,5,https://hogemtls.tak1wa.com/,0,0,490
1667899733336,506,Homepage,Non HTTP response code: javax.net.ssl.SSLException,Non HTTP response message: Connection reset,172.18.1.4-Thread Group 1-4,text,false,,3789,0,5,5,https://hogemtls.tak1wa.com/,0,0,505

クライアント証明書ありで Azure Load Testing を実行

では、こちらの手順に従って先程の負荷テストにクライアント証明書を設定します。

% ls
RootCA.key	RootCA.pem	my_client.csr	my_client.key	my_client.pem

先程作成した証明書は上記のようになっています。
Azure Load Testing でサポートされている形式は PFX となりますので設定する前に変換が必要です。

Azure Load Testing では、PKCS12 証明書のみがサポートされます。 クライアント証明書を PFX ファイル形式でアップロードします。

openssl pkcs12で export します。

% openssl pkcs12 -inkey my_client.key -in my_client.pem -export -out my_client.pfx
Enter Export Password:
Verifying - Enter Export Password:
% ls
RootCA.key	RootCA.pem	my_client.csr	my_client.key	my_client.pem	my_client.pfx

変換に成功しました。
次に、Azure Load Testing で利用するにあたって Key Vault への格納が必要です。

Key Vault にインポート

以下の公式ドキュメントを参考に証明書をインポートします。

作成した Key Vault にてGenerate/Importを選択します。

変換した PFX ファイルを選択します。

Certificate Identifier が後ほど必要になるので記録しておきます。

次に、Azure Load Testing から Key Vault へアクセスを許可するためにアクセスポリシーを設定していきます。

今回の場合だと Secret Permissions の Get 権限が必要です。

プリンシパルには Azure Load Testing のマネージド ID を指定します。

負荷テストでクライアント証明書を利用する

ここまでで下準備は OK です。
最後にテストの設定でクライアント証明書を指定します。

テストのパラメータで証明書が指定出来るので、ここで先程の Certificate Identifier を入力します。

あとは通常どおりテストを実行するだけです。
実行してみると先程はエラーレートが 100 % でしたが、0 % になりましたね。

先程と同様に実行ログも確認してみます。

timeStamp,elapsed,label,responseCode,responseMessage,threadName,dataType,success,failureMessage,bytes,sentBytes,grpThreads,allThreads,URL,Latency,IdleTime,Connect
1667902765189,909,Homepage,200,OK,172.18.25.5-Thread Group 1-4,text,true,,218,137,5,5,https://hogemtls.tak1wa.com/,906,0,723
1667902765190,911,Homepage,200,OK,172.18.25.5-Thread Group 1-2,text,true,,218,137,5,5,https://hogemtls.tak1wa.com/,911,0,733
1667902765189,913,Homepage,200,OK,172.18.25.5-Thread Group 1-5,text,true,,218,137,5,5,https://hogemtls.tak1wa.com/,913,0,733
1667902765189,915,Homepage,200,OK,172.18.25.5-Thread Group 1-1,text,true,,218,137,5,5,https://hogemtls.tak1wa.com/,915,0,744
1667902765189,925,Homepage,200,OK,172.18.25.5-Thread Group 1-3,text,true,,218,137,5,5,https://hogemtls.tak1wa.com/,925,0,752
1667902766129,168,Homepage,200,OK,172.18.25.5-Thread Group 1-3,text,true,,218,137,5,5,https://hogemtls.tak1wa.com/,168,0,0
1667902766130,171,Homepage,200,OK,172.18.25.5-Thread Group 1-1,text,true,,218,137,5,5,https://hogemtls.tak1wa.com/,171,0,0
1667902766129,174,Homepage,200,OK,172.18.25.5-Thread Group 1-4,text,true,,218,137,5,5,https://hogemtls.tak1wa.com/,174,0,0
1667902766129,175,Homepage,200,OK,172.18.25.5-Thread Group 1-5,text,true,,218,137,5,5,https://hogemtls.tak1wa.com/,175,0,0
1667902766129,175,Homepage,200,OK,172.18.25.5-Thread Group 1-2,text,true,,218,137,5,5,https://hogemtls.tak1wa.com/,175,0,0
1667902766306,168,Homepage,200,OK,172.18.25.5-Thread Group 1-2,text,true,,218,137,5,5,https://hogemtls.tak1wa.com/,168,0,0

全てステータス 200 となっていることが確認出来ました。

さいごに

本日は Azure Load Testing でクライアント証明書認証が使えるようになったので mTLS の Amazon API Gateway を対象に実行してみました。

まだ Azure Load Testing 自体がパブリックプレビューではありますが、またサポートされる API の認証パターンが増えたということでより使いやすくなったと思います。
GA が待ち遠しいところです。