気ままに

プログラム関連で困ったことを調べて気ままに投稿

【AWS】CloudFrontのIPアドレス範囲を調べる方法

今やっているサイトにCloudFrontを導入しようとした時に、検証用サーバではbasic認証を書けると、CloudFront側でも認証画面になり、 無限ループみたいになっていたので、IP制限でCloudFrontからのアクセスはbasic認証なしで見えるようにした話。

今回は、AWSからIPの一覧を取得するリンク(json形式)からIP取ってこれたので、このリストからCloudFrontのIPだけ抜き出そう。。。 膨大なので、手で抜き出すには限界があるので、jqコマンドにパイプしてお手軽IP取得をしてみた時のコマンド。

https://ip-ranges.amazonaws.com/ip-ranges.json

jqコマンドのインストール方法については、詳しくは書かないけどmacの場合brewで一発

$ brew install jq

jqのオプションは、下記の通り、よく使うのは、-cと-r

$ jq
Usage: jq [options] <jq filter> [file...]

    jq is a tool for processing JSON inputs, applying the
    given filter to its JSON text inputs and producing the
    filter's results as JSON on standard output.
    The simplest filter is ., which is the identity filter,
    copying jq's input to its output unmodified (except for
    formatting).
    For more advanced filters see the jq(1) manpage ("man jq")
    and/or https://stedolan.github.io/jq

    Some of the options include:
     -c     compact instead of pretty-printed output;
     -n     use `null` as the single input value;
     -e     set the exit status code based on the output;
     -s     read (slurp) all inputs into an array; apply filter to it;
     -r     output raw strings, not JSON texts;
     -R     read raw strings, not JSON texts;
     -C     colorize JSON;
     -M     monochrome (don't colorize JSON);
     -S     sort keys of objects on output;
     --tab  use tabs for indentation;
     --arg a v  set variable $a to value <v>;
     --argjson a v  set variable $a to JSON value <v>;
     --slurpfile a f    set variable $a to an array of JSON texts read from <f>;
    See the manpage for more options.

本題のIPのリストをjqでフィルタする方法

以下のコマンドで、serviceで何が使え使われているのか確認

$ curl https://ip-ranges.amazonaws.com/ip-ranges.json | jq -c '.prefixes[] | .service' | uniq
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  165k  100  165k    0     0   165k      0  0:00:01 --:--:--  0:00:01 1813k
"AMAZON"
"ROUTE53_HEALTHCHECKS"
"S3"
"EC2"
"ROUTE53"
"CLOUDFRONT"
"CODEBUILD"
"AMAZON_CONNECT"
"CLOUD9"

色々あるけど、今回は"CLOUDFRONT"に絞ったIPのリストを取得。取得するのか以下のコマンドを実行

$ curl https://ip-ranges.amazonaws.com/ip-ranges.json | jq -c '.prefixes[] | select(.service=="CLOUDFRONT") | .ip_prefix'

これであっという間に、CloudFrontのIP取得。これをNginxのbasic認証が書かれいてる部分に以下のように書くと、 CloudFrontからのアクセスはbasic認証なしに閲覧可能になる。

        location / {
                satisfy any;
                ### CloudFront
                allow 13.32.0.0/15;
                allow 13.35.0.0/16;
                allow 13.54.63.128/26;
                allow 13.59.250.0/26;
                allow 13.113.203.0/24;
                allow 13.124.199.0/24;
                allow 13.228.69.0/24;
                allow 18.216.170.128/25;
                ## 途中省略

               deny all;

                auth_basic "Restricted";                   # 認証時に表示されるメッセージ
                auth_basic_user_file /etc/nginx/.htpasswd; # .htpasswdファイルのパス
            }