2016年12月23日金曜日

GAEのOutbound IP address確認方法

概要

GoogleAppEngine(GAE)上に構築したアプリケーションからのアクセスをwhitelist形式で許可したい場合など、接続元IPでアクセス制限を行う際のIPアドレス(の範囲)を調べる方法
ちなみに、公式ドキュメントにも明記されていますが、今のところGAEからの外部へのアクセス時のIPアドレス(Outboud IP addres)を固定化は出来ない(らしい)ため、GAE全体のIPアドレスのレンジとしてしか調べられません。
※ 第三者の悪質なGAEアプリケーションも同じIPアドレス範囲を共有するため、根本的にはIPアドレス以外の方法でアクセス制限が推奨されています。

https://cloud.google.com/appengine/kb/#static-ip


確認方法

公式ドキュメントの内容を雑にスクリトに落としただけですが、 以下のスクリプトを実行してもらえればIP Addressのレンジを取得できます。
#!/bin/bash
includes=`nslookup -q=TXT _cloud-netblocks.googleusercontent.com 8.8.8.8 \
| tr ' ' '\n' | grep include | sed "s/include:\(.*\)/\1/g"`
for include in $includes; do
  nslookup -q=TXT $include 8.8.8.8 | tr ' ' '\n' | grep ip | sed "s/ip[4,6]:\(.*\)/\1/g"
done

2016年12月22日木曜日

ElasticSearchで禁止ワードチェックしてみた

Elasticsearchを使っていてpercolator機能で何か面白い使い方できないかなぁということで遊んでみたのでメモを。

0. 環境

  • ElasticSearch 1.0.1
  • elasticsearch-analysis-kuromoji 2.0.0

1. 下準備

index,mappingを作成します. messageのindex_analyzerは調べたいテキストを解析するanalyzeするanalyzer、 search_analyzerはNGワードの検索クエリを解析するanalyzerになる点に注意して下さい。
$ curl -XPUT  http://localhost:9200/filter/ -d '
{
    "index":{
        "analysis":{
            "analyzer" : {
                "kuromoji" : {
                    "tokenizer" : "kuromoji_tokenizer"
                }
            }
        }
    }
}
'
{"acknowledged":true}
$ curl -XPUT localhost:9200/filter/message/_mapping -d '
{
    "message": {
        "_id": {},
        "properties": {
            "message": {
                "type": "string",
                "index_analyzer": "kuromoji",
                "search_analyzer": "whitespace"
            },
            "filter_id": {
                "type": "long",
                "include_in_all": false
            }
        }
    }
}
'
{"acknowledged":true}
$ curl -XPUT localhost:9200/filter/.percolator/_mapping -d '
{
  ".percolator": {
    "_id": {
        "index": "not_analyzed",
        "path" : "filter_id"
    },
    "properties": {
        "query": {
            "enabled": false,
            "type": "object"
        },
        "filter_id": {
            "type": "long"
        }
    }
  }
}
'
{"acknowledged":true}

2. 禁止ワード登録

$ curl -XPOST localhost:9200/filter/.percolator/ -d '
{
    "query": {
        "match": {
            "message": "売春 殺す 死ね"
        }
    },
    "filter_id" : 1
}
'
{"_index":"filter","_type":".percolator","_id":"1","_version":1,"created":true}
$  curl -XPOST localhost:9200/filter/.percolator/ -d '
{
    "query": {
        "match": {
            "message": "ブタ"
        }
    },
    "filter_id" : 2
}
'
{"_index":"filter","_type":".percolator","_id":"2","_version":1,"created":true}

3. NGチェック

禁止ワードが含まれているかどうかの判定だけではなく、 highlight指定してやると該当箇所を伏字(ニ○ニ○動画風)にし易い形式でも取得することが可能です。
禁止ワードの有無を判定
$ curl -XGET localhost:9200/filter/message/_percolate?pretty -d '
{
    "doc": {
        "message": "nobuta@github死ね"
    }
}
'
{
  "took" : 1,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "total" : 1,
  "matches" : [ {
    "_index" : "filter",
    "_id" : "1"
  } ]
}
禁止ワード部分の抽出
$ curl -XGET localhost:9200/filter/message/_percolate?pretty -d '
{
    "highlight": {
        "fields": {
            "message": {
                "pre_tags": [ "(" ],
                "post_tags": [ ")" ]
            }
        }
    },
    "size": 10,
    "doc": {
        "message": "nobuta@github死ね"
    }
}
'
{
  "took" : 30,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "total" : 1,
  "matches" : [ {
    "_index" : "filter",
    "_id" : "1",
    "highlight" : {
      "message" : [ "nobuta@github(死ね)" ]
    }
  } ]
禁止ワード部分の抽出(複数のパターン)
$ curl -XGET localhost:9200/filter/message/_percolate?pretty -d '
{
    "highlight": {
        "fields": {
            "message": {
                "pre_tags": [ "(" ],
                "post_tags": [ ")" ]
            }
        }
    },
    "size": 10,
    "doc": {
        "message": "ノブタ@github殺す"
    }
}
'
{
  "took" : 3,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "total" : 2,
  "matches" : [ {
    "_index" : "filter",
    "_id" : "1",
    "highlight" : {
      "message" : [ "ノブタ@github(殺す)" ]
    }
  }, {
    "_index" : "filter",
    "_id" : "2",
    "highlight" : {
      "message" : [ "ノ(ブタ)@github殺す" ]
    }
  } ]
}
ElasticSearchといえば全文検索であったりKibanaが主流ですが、こんな使い方もできるんじゃないだろうかってことで紹介をしてみました。
他にもこんな使い方してるよって方いましたら是非教えてください。