LoginSignup
6
5

More than 5 years have passed since last update.

Amazon Elasticsearch Service用の認証プロキシを作った

Last updated at Posted at 2017-02-19

Amazon Elasticsearch Serviceを使うときに、グローバルに大公開というのもイヤだし、でも認証かけるとブラウザやらcurlからやらその他認証未対応のツールから使いたいときとかに困るので、認証部分を代理で行ってくれるプロキシサーバーを作りました。

実はaws-auth-proxyというのがあって使おうかと思ったのですが、自分の欲しい機能が足りないこと、またせっかくなのでgoでAWSSDKを使った何かを作りたいということもあり、ゼロから作ってます。

インストール

amzn-es-proxy:releaseから対応のバイナリをダウンロードするかビルドして利用してください。
ビルドする際は依存管理にdepを使っているのでご注意ください。

使い方

EC2のIAMロール、aws configureで作成できる認証情報ファイルのどちらにも対応しています。

Amazon Elasticsearch Serviceの設定

以下のようなアクセスポリシーが必要なので設定してください。
※[]で囲んである部分は書き換えて利用

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "AWS": [
          "arn:aws:iam::[account id]:role/[rolename]",
          "arn:aws:iam::[account id]:user/[username]",
        ]
      },
      "Action": "es:*",
      "Resource": "arn:aws:es:ap-northeast-1:[account id:domain/[domain]/*"
    }
  ]
}

IAMユーザー/ロールの設定

サーバー起動時に--domainオプション(後述)を利用する場合のみ以下の指定が必要です。

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "Stmt1487408882000",
            "Effect": "Allow",
            "Action": [
                "es:DescribeElasticsearchDomain"
            ],
            "Resource": [
                "*"
            ]
        }
    ]
}

サーバーの起動

オプション

  • --domain ESのドメイン、ESを作成するときにDomain Nameとして指定するもの(--endpointとどちらか必須)
  • --endpoint ESの接続先ホスト名、起動後自動的に付与される値(--domainとどちらか必須)
  • --region 接続先のESの起動しているリージョン
    • 環境変数 AWS_REGIONでも指定可能
  • --profile 利用するプロファイル、EC2のIAMロールもしくはデフォルトの場合は指定不要
    • 環境変数 AWS_PROFILEでも指定可能
  • --listen LISTENするホスト/ポートの指定。デフォルトではローカルからのみ
$ nohup amzn-es-proxy --region=ap-northeast-1 --profile=[profile] --domain=[domain] &
Using endpoint search-[domain]-xxxxxxxxxxxxxxx-northeast-1.es.amazonaws.com
Listen 127.0.0.1:9200

ESへのアクセス

直接の場合、以下のように認証エラーになってしまうような認証必須のエンドポイントでも

$ curl search-[domain]-xxxxxxxxxxxx-northeast-1.es.amazonaws.com
{"Message":"User: anonymous is not authorized to perform: es:ESHttpGet on resource: test"}

プロキシを通すとアクセスできるようになります。

$ curl localhost:9200
{
  "name" : "rSKb5Pf",
  "cluster_name" : "247280120152:test",
  "cluster_uuid" : "xMoECUJ2SNux0MILlPBRRw",
  "version" : {
    "number" : "5.1.1",
    "build_hash" : "5395e21",
    "build_date" : "2016-12-15T22:47:19.049Z",
    "build_snapshot" : false,
    "lucene_version" : "6.3.0"
  },
  "tagline" : "You Know, for Search"
}

kibanaもここまでは表示できた(POSTしてる場所もある)のでおそらく一通りは大丈夫ではないかと思います。

Kibana.png

大容量データの通信を試していないので、そこがとても不安です。

6
5
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
6
5