Skip to content

Commit fd98c11

Browse files
authored
Replace deprecated _xpack endpoints (#9656)
In 7.0.0, Elasticsearch is deprecating most `_xpack/*` endpoints. See elastic/elasticsearch#35958. This PR updates the Beats codebase, except test fixtures, with the appropriate replacements for the deprecated endpoints.
1 parent b038211 commit fd98c11

File tree

13 files changed

+121
-62
lines changed

13 files changed

+121
-62
lines changed

CHANGELOG.next.asciidoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
216216
- Add support to read ILM policy from external JSON file. {pull}10347[10347]
217217
- Add `overwrite` and `check_exists` settings to ILM support. {pull}10347[10347]
218218
- Generate Kibana index pattern on demand instead of using a local file. {pull}10478[10478]
219+
- Calls to Elasticsearch X-Pack APIs made by Beats won't cause deprecation logs in Elasticsearch logs. {9656}9656[9656]
219220

220221
*Auditbeat*
221222

filebeat/docs/fields.asciidoc

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4381,19 +4381,10 @@ The name of the action that was executed
43814381
*`elasticsearch.audit.url.params`*::
43824382
+
43834383
--
4384-
type: keyword
4385-
43864384
example: {username=jacknich2}
43874385
43884386
REST URI parameters
43894387
4390-
*`elasticsearch.audit.url.params.text`*::
4391-
+
4392-
--
4393-
type: text
4394-
4395-
--
4396-
43974388
--
43984389
43994390
*`elasticsearch.audit.indices`*::

filebeat/module/elasticsearch/audit/_meta/fields.yml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,6 @@
2929
- name: url.params
3030
description: "REST URI parameters"
3131
example: "{username=jacknich2}"
32-
type: keyword
33-
multi_fields:
34-
- name: text
35-
type: text
3632
- name: indices
3733
description: "Indices accessed by action"
3834
example: [ "foo-2019.01.04", "foo-2019.01.03", "foo-2019.01.06" ]

filebeat/module/elasticsearch/fields.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

filebeat/tests/system/test_ml.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from elasticsearch import Elasticsearch
88
import logging
99
from parameterized import parameterized
10+
import semver
1011

1112

1213
class Test(BaseTest):
@@ -48,23 +49,34 @@ def _run_ml_test(self, modules_flag):
4849

4950
from elasticsearch import AuthorizationException
5051

52+
es_info = self.es.info()
53+
version = semver.parse(es_info["version"]["number"])
54+
if version["major"] < 7:
55+
start_trial_api_url = "/_xpack/license/start_trial?acknowledge=true"
56+
ml_datafeeds_url = "/_xpack/ml/datafeeds/"
57+
ml_anomaly_detectors_url = "/_xpack/ml/anomaly_detectors/"
58+
else:
59+
start_trial_api_url = "/_license/start_trial?acknowledge=true"
60+
ml_datafeeds_url = "/_ml/datafeeds/"
61+
ml_anomaly_detectors_url = "/_ml/anomaly_detectors/"
62+
5163
try:
52-
output = self.es.transport.perform_request("POST", "/_xpack/license/start_trial?acknowledge=true")
64+
output = self.es.transport.perform_request("POST", start_trial_api_url)
5365
except AuthorizationException:
5466
print("License already enabled")
5567

5668
print("Test modules_flag: {}".format(modules_flag))
5769

5870
# Clean any previous state
59-
for df in self.es.transport.perform_request("GET", "/_xpack/ml/datafeeds/")["datafeeds"]:
71+
for df in self.es.transport.perform_request("GET", ml_datafeeds_url)["datafeeds"]:
6072
if df["datafeed_id"] == 'filebeat-nginx-access-response_code':
6173
self.es.transport.perform_request(
62-
"DELETE", "/_xpack/ml/datafeeds/" + df["datafeed_id"])
74+
"DELETE", "/_ml/datafeeds/" + df["datafeed_id"])
6375

64-
for df in self.es.transport.perform_request("GET", "/_xpack/ml/anomaly_detectors/")["jobs"]:
76+
for df in self.es.transport.perform_request("GET", ml_anomaly_detectors_url)["jobs"]:
6577
if df["job_id"] == 'datafeed-filebeat-nginx-access-response_code':
6678
self.es.transport.perform_request(
67-
"DELETE", "/_xpack/ml/anomaly_detectors/" + df["job_id"])
79+
"DELETE", ml_anomaly_detectors_url + df["job_id"])
6880

6981
shutil.rmtree(os.path.join(self.working_dir,
7082
"modules.d"), ignore_errors=True)
@@ -109,10 +121,10 @@ def _run_ml_test(self, modules_flag):
109121
# Check result
110122
self.wait_until(lambda: "filebeat-nginx-access-response_code" in
111123
(df["job_id"] for df in self.es.transport.perform_request(
112-
"GET", "/_xpack/ml/anomaly_detectors/")["jobs"]),
124+
"GET", ml_anomaly_detectors_url)["jobs"]),
113125
max_timeout=60)
114126
self.wait_until(lambda: "datafeed-filebeat-nginx-access-response_code" in
115-
(df["datafeed_id"] for df in self.es.transport.perform_request("GET", "/_xpack/ml/datafeeds/")["datafeeds"]))
127+
(df["datafeed_id"] for df in self.es.transport.perform_request("GET", ml_datafeeds_url)["datafeeds"]))
116128

117129
beat.kill()
118130

libbeat/docs/security/basic-auth.asciidoc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ You can create roles from the **Management / Roles** UI in {kib} or through the
2929
ifeval::["{beatname_lc}"!="filebeat"]
3030
["source","sh",subs="attributes,callouts"]
3131
---------------------------------------------------------------
32-
POST _xpack/security/role/{beat_default_index_prefix}_writer
32+
POST _security/role/{beat_default_index_prefix}_writer
3333
{
3434
"cluster": ["manage_index_templates","monitor"],
3535
"indices": [
@@ -46,7 +46,7 @@ endif::[]
4646
ifeval::["{beatname_lc}"=="filebeat"]
4747
["source","sh",subs="attributes,callouts"]
4848
---------------------------------------------------------------
49-
POST _xpack/security/role/{beat_default_index_prefix}_writer
49+
POST _security/role/{beat_default_index_prefix}_writer
5050
{
5151
"cluster": ["manage_index_templates","monitor","manage_ingest_pipelines"], <1>
5252
"indices": [
@@ -81,7 +81,7 @@ named ++{beat_default_index_prefix}_internal++ that has the
8181
--
8282
["source","sh",subs="attributes,callouts"]
8383
---------------------------------------------------------------
84-
POST /_xpack/security/user/{beat_default_index_prefix}_internal
84+
POST /_security/user/{beat_default_index_prefix}_internal
8585
{
8686
"password" : "{pwd}",
8787
"roles" : [ "{beat_default_index_prefix}_writer","kibana_user"],

libbeat/docs/security/user-access.asciidoc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ You can create roles from the **Management > Roles** UI in {kib} or through the
1616
--
1717
["source","sh",subs="attributes,callouts"]
1818
---------------------------------------------------------------
19-
POST _xpack/security/role/{beat_default_index_prefix}_reader
19+
POST _security/role/{beat_default_index_prefix}_reader
2020
{
2121
"indices": [
2222
{
@@ -43,7 +43,7 @@ following request grants ++{beat_default_index_prefix}_user++ the
4343
--
4444
["source", "sh", subs="attributes,callouts"]
4545
---------------------------------------------------------------
46-
POST /_xpack/security/user/{beat_default_index_prefix}_user
46+
POST /_security/user/{beat_default_index_prefix}_user
4747
{
4848
"password" : "{pwd}",
4949
"roles" : [ "{beat_default_index_prefix}_reader","kibana_user"],

libbeat/ml-importer/importer.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ import (
3535
)
3636

3737
var (
38-
esDataFeedURL = "/_xpack/ml/datafeeds/datafeed-%s"
39-
esJobURL = "/_xpack/ml/anomaly_detectors/%s"
40-
kibanaGetModuleURL = "/api/ml/modules/get_module/%s"
41-
kibanaRecognizeURL = "/api/ml/modules/recognize/%s"
42-
kibanaSetupModuleURL = "/api/ml/modules/setup/%s"
38+
esMLDataFeedURLSuffix = "/datafeeds/datafeed-%s"
39+
esMLJobURLSuffix = "/anomaly_detectors/%s"
40+
kibanaGetModuleURL = "/api/ml/modules/get_module/%s"
41+
kibanaRecognizeURL = "/api/ml/modules/recognize/%s"
42+
kibanaSetupModuleURL = "/api/ml/modules/setup/%s"
4343
)
4444

4545
// MLConfig contains the required configuration for loading one job and the associated
@@ -121,11 +121,11 @@ func readJSONFile(path string) (common.MapStr, error) {
121121

122122
// ImportMachineLearningJob uploads the job and datafeed configuration to ES/xpack.
123123
func ImportMachineLearningJob(esClient MLLoader, cfg *MLConfig) error {
124-
jobURL := fmt.Sprintf(esJobURL, cfg.ID)
125-
datafeedURL := fmt.Sprintf(esDataFeedURL, cfg.ID)
124+
esVersion := esClient.GetVersion()
125+
jobURL := makeMLURLPerESVersion(esVersion, fmt.Sprintf(esMLJobURLSuffix, cfg.ID))
126+
datafeedURL := makeMLURLPerESVersion(esVersion, fmt.Sprintf(esMLDataFeedURLSuffix, cfg.ID))
126127

127128
if len(cfg.MinVersion) > 0 {
128-
esVersion := esClient.GetVersion()
129129
if !esVersion.IsValid() {
130130
return errors.New("Invalid Elasticsearch version")
131131
}
@@ -275,3 +275,10 @@ func checkResponse(r []byte) error {
275275

276276
return errs.Err()
277277
}
278+
279+
func makeMLURLPerESVersion(esVersion common.Version, mlURLPathSuffix string) string {
280+
if esVersion.Major < 7 {
281+
return "_xpack/ml/" + mlURLPathSuffix
282+
}
283+
return "_ml" + mlURLPathSuffix
284+
}

libbeat/ml-importer/importer_integration_test.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,15 @@ func TestImportJobs(t *testing.T) {
128128
err = ImportMachineLearningJob(client, &mlconfig)
129129
assert.NoError(t, err)
130130

131-
// check by GETing back
131+
var mlBaseURL string
132+
if client.GetVersion().Major < 7 {
133+
mlBaseURL = "/_xpack/ml"
134+
} else {
135+
mlBaseURL = "/_ml"
136+
}
132137

133-
status, response, err := client.Request("GET", "/_xpack/ml/anomaly_detectors", "", nil, nil)
138+
// check by GETing back
139+
status, response, err := client.Request("GET", mlBaseURL+"/anomaly_detectors", "", nil, nil)
134140
assert.NoError(t, err)
135141
assert.Equal(t, 200, status)
136142

@@ -157,7 +163,7 @@ func TestImportJobs(t *testing.T) {
157163
}
158164
assert.True(t, found)
159165

160-
status, response, err = client.Request("GET", "/_xpack/ml/datafeeds", "", nil, nil)
166+
status, response, err = client.Request("GET", mlBaseURL+"/datafeeds", "", nil, nil)
161167
assert.NoError(t, err)
162168
assert.Equal(t, 200, status)
163169

metricbeat/module/elasticsearch/elasticsearch.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,20 @@ func GetLicense(http *helper.HTTP, resetURI string) (*License, error) {
211211
// First, check the cache
212212
license := licenseCache.get()
213213

214+
info, err := GetInfo(http, resetURI)
215+
if err != nil {
216+
return nil, err
217+
}
218+
var licensePath string
219+
if info.Version.Number.Major < 7 {
220+
licensePath = "_xpack/license"
221+
} else {
222+
licensePath = "_license"
223+
}
224+
214225
// Not cached, fetch license from Elasticsearch
215226
if license == nil {
216-
content, err := fetchPath(http, resetURI, "_xpack/license", "")
227+
content, err := fetchPath(http, resetURI, licensePath, "")
217228
if err != nil {
218229
return nil, err
219230
}

metricbeat/module/elasticsearch/elasticsearch_integration_test.go

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -66,17 +66,22 @@ func TestFetch(t *testing.T) {
6666
err := createIndex(host)
6767
assert.NoError(t, err)
6868

69-
err = enableTrialLicense(host)
69+
version, err := getElasticsearchVersion(host)
70+
if err != nil {
71+
t.Fatal("getting elasticsearch version", err)
72+
}
73+
74+
err = enableTrialLicense(host, version)
7075
assert.NoError(t, err)
7176

72-
err = createMLJob(host)
77+
err = createMLJob(host, version)
7378
assert.NoError(t, err)
7479

7580
err = createCCRStats(host)
7681
assert.NoError(t, err)
7782

7883
for _, metricSet := range metricSets {
79-
checkSkip(t, metricSet, host)
84+
checkSkip(t, metricSet, version)
8085
t.Run(metricSet, func(t *testing.T) {
8186
f := mbtest.NewReportingMetricSetV2(t, getConfig(metricSet))
8287
events, errs := mbtest.ReportingFetchV2(f)
@@ -95,8 +100,14 @@ func TestData(t *testing.T) {
95100
compose.EnsureUp(t, "elasticsearch")
96101

97102
host := net.JoinHostPort(getEnvHost(), getEnvPort())
103+
104+
version, err := getElasticsearchVersion(host)
105+
if err != nil {
106+
t.Fatal("getting elasticsearch version", err)
107+
}
108+
98109
for _, metricSet := range metricSets {
99-
checkSkip(t, metricSet, host)
110+
checkSkip(t, metricSet, version)
100111
t.Run(metricSet, func(t *testing.T) {
101112
f := mbtest.NewReportingMetricSetV2(t, getConfig(metricSet))
102113
err := mbtest.WriteEventsReporterV2(f, t, metricSet)
@@ -164,10 +175,15 @@ func createIndex(host string) error {
164175
}
165176

166177
// createIndex creates and elasticsearch index in case it does not exit yet
167-
func enableTrialLicense(host string) error {
178+
func enableTrialLicense(host string, version *common.Version) error {
168179
client := &http.Client{}
169180

170-
enableXPackURL := "/_xpack/license/start_trial?acknowledge=true"
181+
var enableXPackURL string
182+
if version.Major < 7 {
183+
enableXPackURL = "/_xpack/license/start_trial?acknowledge=true"
184+
} else {
185+
enableXPackURL = "/_license/start_trial?acknowledge=true"
186+
}
171187

172188
req, err := http.NewRequest("POST", "http://"+host+enableXPackURL, nil)
173189
if err != nil {
@@ -191,14 +207,19 @@ func enableTrialLicense(host string) error {
191207
return nil
192208
}
193209

194-
func createMLJob(host string) error {
210+
func createMLJob(host string, version *common.Version) error {
195211

196212
mlJob, err := ioutil.ReadFile("ml_job/_meta/test/test_job.json")
197213
if err != nil {
198214
return err
199215
}
200216

201-
jobURL := "/_xpack/ml/anomaly_detectors/total-requests"
217+
var jobURL string
218+
if version.Major < 7 {
219+
jobURL = "/_xpack/ml/anomaly_detectors/total-requests"
220+
} else {
221+
jobURL = "/_ml/anomaly_detectors/total-requests"
222+
}
202223

203224
if checkExists("http://" + host + jobURL) {
204225
return nil
@@ -279,16 +300,11 @@ func checkExists(url string) bool {
279300
return false
280301
}
281302

282-
func checkSkip(t *testing.T, metricset string, host string) {
303+
func checkSkip(t *testing.T, metricset string, version *common.Version) {
283304
if metricset != "ccr" {
284305
return
285306
}
286307

287-
version, err := getElasticsearchVersion(host)
288-
if err != nil {
289-
t.Fatal("getting elasticsearch version", err)
290-
}
291-
292308
isCCRStatsAPIAvailable := elastic.IsFeatureAvailable(version, elasticsearch.CCRStatsAPIAvailableVersion)
293309

294310
if !isCCRStatsAPIAvailable {

metricbeat/module/elasticsearch/ml_job/ml_job.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func init() {
3333
}
3434

3535
const (
36-
jobPath = "/_xpack/ml/anomaly_detectors/_all/_stats"
36+
jobPathSuffix = "/anomaly_detectors/_all/_stats"
3737
)
3838

3939
// MetricSet for ml job
@@ -45,10 +45,11 @@ type MetricSet struct {
4545
// any MetricSet specific configuration options if there are any.
4646
func New(base mb.BaseMetricSet) (mb.MetricSet, error) {
4747
// Get the stats from the local node
48-
ms, err := elasticsearch.NewMetricSet(base, jobPath)
48+
ms, err := elasticsearch.NewMetricSet(base, "") // servicePath will be set in Fetch() based on ES version
4949
if err != nil {
5050
return nil, err
5151
}
52+
5253
return &MetricSet{MetricSet: ms}, nil
5354
}
5455

@@ -74,6 +75,12 @@ func (m *MetricSet) Fetch(r mb.ReporterV2) {
7475
return
7576
}
7677

78+
if info.Version.Number.Major < 7 {
79+
m.SetServiceURI("/_xpack/ml" + jobPathSuffix)
80+
} else {
81+
m.SetServiceURI("/_ml" + jobPathSuffix)
82+
}
83+
7784
content, err := m.HTTP.FetchContent()
7885
if err != nil {
7986
elastic.ReportAndLogError(err, r, m.Log)

0 commit comments

Comments
 (0)