Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[7.x] [Fleet] Enforce port in fleet server url even for default http|https port (#98957) #99002

Merged
merged 1 commit into from
May 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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