Skip to content

Commit

Permalink
Merge pull request #63 from github/escape_special_index_names
Browse files Browse the repository at this point in the history
Escape periods in index names as it interferes with gjson
  • Loading branch information
Nick Canzoneri authored Oct 7, 2019
2 parents 7d2e7ad + fe57e38 commit 77e4e6a
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 3 deletions.
6 changes: 3 additions & 3 deletions es.go
Original file line number Diff line number Diff line change
Expand Up @@ -991,7 +991,7 @@ func (c *Client) GetPrettyIndexSettings(index string) (string, error) {
return "", err
}

rawSettings := gjson.GetBytes(body, fmt.Sprintf("%s.settings.index", index)).Raw
rawSettings := gjson.GetBytes(body, fmt.Sprintf("%s.settings.index", escapeIndexName(index))).Raw

var prettyPrinted bytes.Buffer
err = json.Indent(&prettyPrinted, []byte(rawSettings), "", " ")
Expand All @@ -1012,7 +1012,7 @@ func (c *Client) GetIndexSettings(index string) ([]Setting, error) {
return nil, err
}

rawSettings := gjson.GetBytes(body, fmt.Sprintf("%s.settings.index", index)).Raw
rawSettings := gjson.GetBytes(body, fmt.Sprintf("%s.settings.index", escapeIndexName(index))).Raw

settings, err := settingsToStructs(rawSettings)

Expand All @@ -1029,7 +1029,7 @@ func (c *Client) SetIndexSetting(index, setting, value string) (string, string,
return "", "", err
}

currentValue := gjson.GetBytes(body, fmt.Sprintf("%s.settings.index.%s", index, setting)).Str
currentValue := gjson.GetBytes(body, fmt.Sprintf("%s.settings.index.%s", escapeIndexName(index), setting)).Str

agent := c.buildPutRequest(settingsPath).Set("Content-Type", "application/json").
Send(fmt.Sprintf(`{"index" : { "%s" : "%s"}}`, setting, value))
Expand Down
88 changes: 88 additions & 0 deletions es_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1308,6 +1308,28 @@ func TestGetPrettyIndexSettings(t *testing.T) {
}
}

func TestGetPrettyIndexSettings_SpecialName(t *testing.T) {
testSetup := &ServerSetup{
Method: "GET",
Path: "/.special/_settings",
Response: `{".special":{"settings":{"index":{"number_of_shards":"5","provided_name":"octocat","creation_date":"1535035072757","analysis":{"analyzer":{"my_custom_analyzer":{"filter":["lowercase","asciifolding"],"char_filter":["html_strip"],"type":"custom","tokenizer":"standard"}}},"number_of_replicas":"0","uuid":"Q_Jm1mD2Syy8JgMUiicqcw","version":{"created":"5061099"}}}}}`,
}

host, port, ts := setupTestServers(t, []*ServerSetup{testSetup})
defer ts.Close()
client := NewClient(host, port)

indexSettings, err := client.GetPrettyIndexSettings(".special")

if err != nil {
t.Errorf("Unexpected error, got %s", err)
}

if indexSettings == "" {
t.Error("Unexpected index settings, got empty string")
}
}

func TestGetIndexSettings(t *testing.T) {
testSetup := &ServerSetup{
Method: "GET",
Expand Down Expand Up @@ -1341,6 +1363,39 @@ func TestGetIndexSettings(t *testing.T) {
}
}

func TestGetIndexSettings_SpecialName(t *testing.T) {
testSetup := &ServerSetup{
Method: "GET",
Path: "/.special/_settings",
Response: `{".special":{"settings":{"index":{"number_of_shards":"5","provided_name":"octocat","creation_date":"1535035072757","analysis":{"analyzer":{"my_custom_analyzer":{"filter":["lowercase","asciifolding"],"char_filter":["html_strip"],"type":"custom","tokenizer":"standard"}}},"number_of_replicas":"0","uuid":"Q_Jm1mD2Syy8JgMUiicqcw","version":{"created":"5061099"}}}}}`,
}

host, port, ts := setupTestServers(t, []*ServerSetup{testSetup})
defer ts.Close()
client := NewClient(host, port)

settings, err := client.GetIndexSettings(".special")

if err != nil {
t.Errorf("Unexpected error, got %s", err)
}

if len(settings) != 11 {
t.Errorf("Unexpected number of settings, got %d", len(settings))
}

for i := range settings {
s := settings[i]
if s.Setting == "number_of_shards" && s.Value != "5" {
t.Errorf("Unexpected shards value, expected 5, got %s", s.Value)
}

if s.Setting == "number_of_replicas" && s.Value != "0" {
t.Errorf("Unexpected replicas value, expected 0, got %s", s.Value)
}
}
}

func TestSetIndexSetting(t *testing.T) {
getSetup := &ServerSetup{
Method: "GET",
Expand Down Expand Up @@ -1374,6 +1429,39 @@ func TestSetIndexSetting(t *testing.T) {
}
}

func TestSetIndexSetting_SpecialName(t *testing.T) {
getSetup := &ServerSetup{
Method: "GET",
Path: "/.special/_settings",
Response: `{".special":{"settings":{"index":{"number_of_shards":"5","provided_name":"octocat","creation_date":"1535035072757","analysis":{"analyzer":{"my_custom_analyzer":{"filter":["lowercase","asciifolding"],"char_filter":["html_strip"],"type":"custom","tokenizer":"standard"}}},"number_of_replicas":"0","uuid":"Q_Jm1mD2Syy8JgMUiicqcw","version":{"created":"5061099"}}}}}`,
}

updateSetup := &ServerSetup{
Method: "PUT",
Path: "/.special/_settings",
Body: `{"index":{"number_of_replicas":"2"}}`,
Response: `{"accepted": true}`,
}

host, port, ts := setupTestServers(t, []*ServerSetup{getSetup, updateSetup})
defer ts.Close()
client := NewClient(host, port)

previous, current, err := client.SetIndexSetting(".special", "number_of_replicas", "2")

if err != nil {
t.Errorf("Error setting index setting: %s", err)
}

if previous != "0" {
t.Errorf("Unexpected previous setting value, expected 0, got %s", previous)
}

if current != "2" {
t.Errorf("Unexpected current setting value, expected 2, got %s", current)
}
}

func TestGetPrettyIndexMappings(t *testing.T) {
testSetup := &ServerSetup{
Method: "GET",
Expand Down
4 changes: 4 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,7 @@ func combineErrors(errs []error) error {
}
return errors.New(strings.Join(errorText, "\n"))
}

func escapeIndexName(index string) string {
return strings.Replace(index, ".", "\\.", -1)
}
14 changes: 14 additions & 0 deletions util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,17 @@ Third error`
t.Errorf("Unexpected error message, got %s", combinedError.Error())
}
}

func TestEscapeIndexName(t *testing.T) {
if escapeIndexName(".secret-index") != "\\.secret-index" {
t.Errorf("Index name not escaped.")
}

if escapeIndexName(".multiple.periods.in.index") != "\\.multiple\\.periods\\.in\\.index" {
t.Errorf("Index name not escaped.")
}

if escapeIndexName("regular-index") != "regular-index" {
t.Errorf("Index name changed when it shouldn't have.")
}
}

0 comments on commit 77e4e6a

Please sign in to comment.