From 2d02b69bcb568d4cb023d042b9806403439c9fef Mon Sep 17 00:00:00 2001 From: Nick Canzoneri Date: Sun, 6 Oct 2019 19:00:39 -0400 Subject: [PATCH 1/2] Escape periods in index names as it interferes with gjson --- es.go | 6 ++-- es_test.go | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++++ util.go | 4 +++ util_test.go | 14 +++++++++ 4 files changed, 109 insertions(+), 3 deletions(-) diff --git a/es.go b/es.go index eb4ca28..d54860a 100644 --- a/es.go +++ b/es.go @@ -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), "", " ") @@ -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) @@ -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)) diff --git a/es_test.go b/es_test.go index c9bc5e8..3e84648 100644 --- a/es_test.go +++ b/es_test.go @@ -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", @@ -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", @@ -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", diff --git a/util.go b/util.go index ee16718..60ff87f 100644 --- a/util.go +++ b/util.go @@ -63,3 +63,7 @@ func combineErrors(errs []error) error { } return errors.New(strings.Join(errorText, "\n")) } + +func escapeIndexName(index string) string { + return strings.ReplaceAll(index, ".", "\\.") +} diff --git a/util_test.go b/util_test.go index dd9e0ea..27a1ddd 100644 --- a/util_test.go +++ b/util_test.go @@ -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.") + } +} From fe57e38cb638b148f02747f4c7fa8b678291a09a Mon Sep 17 00:00:00 2001 From: Nick Canzoneri Date: Sun, 6 Oct 2019 19:07:13 -0400 Subject: [PATCH 2/2] `strings.ReplaceAll` only availabe in 1.12+, so using old method for compatability --- util.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/util.go b/util.go index 60ff87f..2b1c8c7 100644 --- a/util.go +++ b/util.go @@ -65,5 +65,5 @@ func combineErrors(errs []error) error { } func escapeIndexName(index string) string { - return strings.ReplaceAll(index, ".", "\\.") + return strings.Replace(index, ".", "\\.", -1) }