service_matcher-go/elasticsearch/elasticsearch.go

153 lines
3.2 KiB
Go
Raw Normal View History

2018-08-13 07:48:32 +00:00
package elasticsearch
import (
"bufio"
"encoding/json"
"strconv"
"strings"
2018-08-15 07:17:18 +00:00
osm "git.loafle.net/overflow/service_matcher-go"
2018-08-13 07:48:32 +00:00
)
type ElasticSearchMatcher struct {
2018-08-15 07:17:18 +00:00
osm.Matchers
2018-08-13 07:48:32 +00:00
}
2018-09-03 13:36:57 +00:00
func (m *ElasticSearchMatcher) Key() string {
2018-08-15 06:41:41 +00:00
return "ELASTICSEARCH"
2018-08-13 07:48:32 +00:00
}
2018-09-12 04:26:27 +00:00
func (m *ElasticSearchMatcher) Type() string {
return "SEARCH"
}
func (m *ElasticSearchMatcher) Vendor(matchCtx *osm.MatchCtx) string {
return "Elasticsearch"
}
func (m *ElasticSearchMatcher) Version(matchCtx *osm.MatchCtx) string {
return "UNKNOWN"
}
func (m *ElasticSearchMatcher) OsType(matchCtx *osm.MatchCtx) string {
return "UNKNOWN"
}
func (m *ElasticSearchMatcher) OsVersion(matchCtx *osm.MatchCtx) string {
return "UNKNOWN"
}
2018-09-12 04:26:27 +00:00
2018-09-03 13:36:57 +00:00
func (m *ElasticSearchMatcher) Name(matchCtx *osm.MatchCtx) string {
2018-08-13 07:48:32 +00:00
name := "ElasticSearch"
2018-09-03 13:36:57 +00:00
if v, ok := matchCtx.GetAttribute("number"); ok {
2018-09-03 13:52:01 +00:00
name = name + " (" + v + ")"
2018-08-13 07:48:32 +00:00
}
return name
}
2018-09-03 13:41:28 +00:00
func (m *ElasticSearchMatcher) IsPrePacket() bool {
2018-08-13 07:48:32 +00:00
return false
}
2018-09-03 13:36:57 +00:00
func (m *ElasticSearchMatcher) HasResponse(matchCtx *osm.MatchCtx, index int) bool {
2018-08-13 07:48:32 +00:00
return true
}
2018-09-03 13:36:57 +00:00
func (m *ElasticSearchMatcher) IsError(matchCtx *osm.MatchCtx, index int, packet *osm.Packet) bool {
2018-08-13 07:48:32 +00:00
return false
}
2018-09-03 13:36:57 +00:00
func (m *ElasticSearchMatcher) Match(matchCtx *osm.MatchCtx, index int, packet *osm.Packet) error {
2018-08-13 07:48:32 +00:00
2018-09-03 06:42:56 +00:00
if packet == nil || !packet.Valid() {
2018-08-15 07:17:18 +00:00
return osm.NoPacketReceivedError()
2018-08-13 07:48:32 +00:00
}
2018-09-03 06:42:56 +00:00
str := string(packet.Bytes())
2018-08-13 07:48:32 +00:00
hnb := strings.Split(str, "\r\n\r\n")
2018-09-03 14:44:50 +00:00
if nil == hnb || 2 > len(hnb) {
return osm.NotMatchedError()
}
2018-08-13 07:48:32 +00:00
header := hnb[0]
body := hnb[1]
lineNo := 0
scanner := bufio.NewScanner(strings.NewReader(header))
contentLen := 0
for scanner.Scan() {
line := scanner.Text()
if strings.Compare(line, "") == 0 {
continue
}
if lineNo == 0 && !strings.HasPrefix(line, "HTTP/") {
2018-08-15 07:17:18 +00:00
return osm.NotMatchedError()
2018-08-13 07:48:32 +00:00
}
if strings.Contains(line, ":") {
kv := strings.Split(line, ": ")
if kv[0] == "content-type" && !strings.Contains(kv[1], "application/json") {
2018-08-15 07:17:18 +00:00
return osm.NotMatchedError()
2018-08-13 07:48:32 +00:00
}
if kv[0] == "content-length" {
len, err := strconv.Atoi(kv[1])
if err != nil {
2018-08-15 07:17:18 +00:00
return osm.NotMatchedError()
2018-08-13 07:48:32 +00:00
}
contentLen = len
}
}
lineNo++
}
content := body[:contentLen]
if strings.HasPrefix(content, "{") && strings.HasSuffix(content, "}") {
2018-08-15 07:17:18 +00:00
return osm.NotMatchedError()
2018-08-13 07:48:32 +00:00
}
2018-09-03 13:36:57 +00:00
m.parseJson(matchCtx, content)
if _, ok := matchCtx.GetAttribute("cluster_name"); !ok {
2018-08-15 07:17:18 +00:00
return osm.NotMatchedError()
2018-08-13 07:48:32 +00:00
}
2018-09-03 13:36:57 +00:00
if _, ok := matchCtx.GetAttribute("cluster_uuid"); !ok {
2018-08-15 07:17:18 +00:00
return osm.NotMatchedError()
2018-08-13 07:48:32 +00:00
}
return nil
}
2018-09-03 13:36:57 +00:00
func (m *ElasticSearchMatcher) parseJson(matchCtx *osm.MatchCtx, jsonstr string) error {
2018-08-13 07:48:32 +00:00
jsonMap := make(map[string]interface{})
err := json.Unmarshal([]byte(jsonstr), &jsonMap)
if err != nil {
return err
}
2018-09-03 13:36:57 +00:00
m.dumpMap(matchCtx, jsonMap)
2018-08-13 07:48:32 +00:00
return nil
}
2018-09-03 13:36:57 +00:00
func (m *ElasticSearchMatcher) dumpMap(matchCtx *osm.MatchCtx, jsonMap map[string]interface{}) {
for k, v := range jsonMap {
2018-08-13 07:48:32 +00:00
if mv, ok := v.(map[string]interface{}); ok {
2018-09-03 13:36:57 +00:00
m.dumpMap(matchCtx, mv)
2018-08-13 07:48:32 +00:00
} else {
s, ok := v.(string)
if ok {
2018-09-03 13:36:57 +00:00
matchCtx.SetAttribute(k, s)
2018-08-13 07:48:32 +00:00
}
}
}
}
2018-08-15 07:17:18 +00:00
func NewMatcher() osm.Matcher {
2018-08-13 07:48:32 +00:00
m := &ElasticSearchMatcher{}
reqStr := "GET / HTTP/1.1\r\n\r\n"
byte := make([]byte, len(reqStr))
copy(byte[:], reqStr)
2018-08-15 07:17:18 +00:00
m.AddPacket(osm.NewPacket(byte, len(reqStr)))
2018-08-13 07:48:32 +00:00
return m
}