Skip to content

Commit

Permalink
[Fleet] Enforce port in fleet server url even for default http|https …
Browse files Browse the repository at this point in the history
…port (#98957)
  • Loading branch information
nchaulet authored and kibanamachine committed May 1, 2021
1 parent 719b380 commit 926e45c
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
21 changes: 20 additions & 1 deletion x-pack/plugins/fleet/server/services/settings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import { savedObjectsClientMock } from 'src/core/server/mocks';

import { appContextService } from './app_context';
import { getCloudFleetServersHosts, settingsSetup } from './settings';
import { getCloudFleetServersHosts, normalizeFleetServerHost, settingsSetup } from './settings';

jest.mock('./app_context');

Expand Down Expand Up @@ -205,3 +205,22 @@ describe('settingsSetup', () => {
expect(soClientMock.update).not.toBeCalled();
});
});

describe('normalizeFleetServerHost', () => {
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 = normalizeFleetServerHost(scenario.sourceUrl);

expect(url).toEqual(scenario.expectedUrl);
});
}
});
31 changes: 30 additions & 1 deletion x-pack/plugins/fleet/server/services/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,46 @@ export async function settingsSetup(soClient: SavedObjectsClientContract) {
}
}

function getPortForURL(url: URL) {
if (url.port !== '') {
return url.port;
}

if (url.protocol === 'http:') {
return '80';
}

if (url.protocol === 'https:') {
return '443';
}
}

export function normalizeFleetServerHost(host: string) {
// Fleet server is not using default port for http|https https://github.com/elastic/beats/issues/25420
const fleetServerURL = 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 `${fleetServerURL.protocol}//${fleetServerURL.hostname}:${getPortForURL(fleetServerURL)}${
fleetServerURL.pathname === '/' ? '' : fleetServerURL.pathname
}`;
}

export async function saveSettings(
soClient: SavedObjectsClientContract,
newData: Partial<Omit<Settings, 'id'>>
): Promise<Partial<Settings> & Pick<Settings, 'id'>> {
try {
const settings = await getSettings(soClient);

const data = { ...newData };
if (data.fleet_server_hosts) {
data.fleet_server_hosts = data.fleet_server_hosts.map(normalizeFleetServerHost);
}

const res = await soClient.update<SettingsSOAttributes>(
GLOBAL_SETTINGS_SAVED_OBJECT_TYPE,
settings.id,
newData
data
);

return {
Expand Down

0 comments on commit 926e45c

Please sign in to comment.