-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Fleet] Add default http|https port to ES hosts (#99240)
- Loading branch information
Showing
8 changed files
with
159 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { normalizeHostsForAgents } from './hosts_utils'; | ||
|
||
describe('normalizeHostsForAgents', () => { | ||
const scenarios = [ | ||
{ sourceUrl: 'http://test.fr', expectedUrl: 'http://test.fr:80' }, | ||
{ sourceUrl: 'http://test.fr/test/toto', expectedUrl: 'http://test.fr:80/test/toto' }, | ||
{ sourceUrl: 'https://test.fr', expectedUrl: 'https://test.fr:443' }, | ||
{ sourceUrl: 'https://test.fr/test/toto', expectedUrl: 'https://test.fr:443/test/toto' }, | ||
{ sourceUrl: 'https://test.fr:9243', expectedUrl: 'https://test.fr:9243' }, | ||
{ sourceUrl: 'https://test.fr:9243/test/toto', expectedUrl: 'https://test.fr:9243/test/toto' }, | ||
]; | ||
|
||
for (const scenario of scenarios) { | ||
it(`should transform ${scenario.sourceUrl} correctly`, () => { | ||
const url = normalizeHostsForAgents(scenario.sourceUrl); | ||
|
||
expect(url).toEqual(scenario.expectedUrl); | ||
}); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
function getPortForURL(url: URL) { | ||
if (url.port !== '') { | ||
return url.port; | ||
} | ||
|
||
if (url.protocol === 'http:') { | ||
return '80'; | ||
} | ||
|
||
if (url.protocol === 'https:') { | ||
return '443'; | ||
} | ||
} | ||
|
||
export function normalizeHostsForAgents(host: string) { | ||
// Elastic Agent is not using default port for http|https for Fleet server and ES https://github.com/elastic/beats/issues/25420 | ||
const hostURL = new URL(host); | ||
|
||
// We are building the URL manualy as url format will not include the port if the port is 80 or 443 | ||
return `${hostURL.protocol}//${hostURL.hostname}:${getPortForURL(hostURL)}${ | ||
hostURL.pathname === '/' ? '' : hostURL.pathname | ||
}`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import expect from '@kbn/expect'; | ||
import { FtrProviderContext } from '../../../api_integration/ftr_provider_context'; | ||
import { skipIfNoDockerRegistry } from '../../helpers'; | ||
import { setupFleetAndAgents } from '../agents/services'; | ||
|
||
export default function (providerContext: FtrProviderContext) { | ||
const { getService } = providerContext; | ||
const supertest = getService('supertest'); | ||
const esArchiver = getService('esArchiver'); | ||
|
||
describe('fleet_output_crud', async function () { | ||
skipIfNoDockerRegistry(providerContext); | ||
before(async () => { | ||
await esArchiver.load('fleet/empty_fleet_server'); | ||
}); | ||
setupFleetAndAgents(providerContext); | ||
|
||
let defaultOutputId: string; | ||
|
||
before(async function () { | ||
const { body: getOutputsRes } = await supertest.get(`/api/fleet/outputs`).expect(200); | ||
|
||
const defaultOutput = getOutputsRes.items.find((item: any) => item.is_default); | ||
if (!defaultOutput) { | ||
throw new Error('default output not set'); | ||
} | ||
|
||
defaultOutputId = defaultOutput.id; | ||
}); | ||
|
||
after(async () => { | ||
await esArchiver.unload('fleet/empty_fleet_server'); | ||
}); | ||
|
||
it('GET /outputs should list the default output', async () => { | ||
const { body: getOutputsRes } = await supertest.get(`/api/fleet/outputs`).expect(200); | ||
|
||
expect(getOutputsRes.items.length).to.eql(1); | ||
}); | ||
|
||
it('GET /outputs/{defaultOutputId} should return the default output', async () => { | ||
const { body: getOutputRes } = await supertest | ||
.get(`/api/fleet/outputs/${defaultOutputId}`) | ||
.expect(200); | ||
|
||
expect(getOutputRes.item).to.have.keys('id', 'name', 'type', 'is_default', 'hosts'); | ||
}); | ||
|
||
it('PUT /output/{defaultOutputId} should explicitly set port on ES hosts', async function () { | ||
await supertest | ||
.put(`/api/fleet/outputs/${defaultOutputId}`) | ||
.set('kbn-xsrf', 'xxxx') | ||
.send({ hosts: ['https://test.fr'] }) | ||
.expect(200); | ||
|
||
const { body: getSettingsRes } = await supertest | ||
.get(`/api/fleet/outputs/${defaultOutputId}`) | ||
.expect(200); | ||
expect(getSettingsRes.item.hosts).to.eql(['https://test.fr:443']); | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
export default function loadTests({ loadTestFile }) { | ||
describe('Output Endpoints', () => { | ||
loadTestFile(require.resolve('./crud')); | ||
}); | ||
} |