Skip to content

Commit

Permalink
[Fleet] Set fleet server host in cloud (#98070) (#98140)
Browse files Browse the repository at this point in the history
Co-authored-by: Nicolas Chaulet <nicolas.chaulet@elastic.co>
  • Loading branch information
kibanamachine and nchaulet authored Apr 23, 2021
1 parent 9baab80 commit adae134
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 2 deletions.
3 changes: 3 additions & 0 deletions x-pack/plugins/cloud/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ import { CoreSetup, Logger, Plugin, PluginInitializerContext } from 'src/core/se
import { CloudConfigType } from './config';
import { registerCloudUsageCollector } from './collectors';
import { getIsCloudEnabled } from '../common/is_cloud_enabled';
import { parseDeploymentIdFromDeploymentUrl } from './utils';

interface PluginsSetup {
usageCollection?: UsageCollectionSetup;
}

export interface CloudSetup {
cloudId?: string;
deploymentId?: string;
isCloudEnabled: boolean;
apm: {
url?: string;
Expand All @@ -40,6 +42,7 @@ export class CloudPlugin implements Plugin<CloudSetup> {

return {
cloudId: this.config.id,
deploymentId: parseDeploymentIdFromDeploymentUrl(this.config.deployment_url),
isCloudEnabled,
apm: {
url: this.config.apm?.url,
Expand Down
20 changes: 20 additions & 0 deletions x-pack/plugins/cloud/server/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* 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 { parseDeploymentIdFromDeploymentUrl } from './utils';

describe('parseDeploymentIdFromDeploymentUrl', () => {
it('should return undefined if there is no deploymentUrl configured', () => {
expect(parseDeploymentIdFromDeploymentUrl()).toBeUndefined();
});

it('should return the deploymentId if this is a valid deployment url', () => {
expect(parseDeploymentIdFromDeploymentUrl('deployments/uuid-deployment-1')).toBe(
'uuid-deployment-1'
);
});
});
13 changes: 13 additions & 0 deletions x-pack/plugins/cloud/server/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* 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 function parseDeploymentIdFromDeploymentUrl(deploymentUrl?: string) {
if (!deploymentUrl) {
return;
}
return deploymentUrl.split('/').pop();
}
35 changes: 35 additions & 0 deletions x-pack/plugins/fleet/server/services/settings.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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 { appContextService } from './app_context';
import { getCloudFleetServersHosts } from './settings';

jest.mock('./app_context');

const mockedAppContextService = appContextService as jest.Mocked<typeof appContextService>;

describe('getCloudFleetServersHosts', () => {
it('should return undefined if cloud is not setup', () => {
expect(getCloudFleetServersHosts()).toBeUndefined();
});

it('should return fleet server hosts if cloud is correctly setup', () => {
mockedAppContextService.getCloud.mockReturnValue({
cloudId:
'dXMtZWFzdC0xLmF3cy5mb3VuZC5pbyRjZWM2ZjI2MWE3NGJmMjRjZTMzYmI4ODExYjg0Mjk0ZiRjNmMyY2E2ZDA0MjI0OWFmMGNjN2Q3YTllOTYyNTc0Mw==',
isCloudEnabled: true,
deploymentId: 'deployment-id-1',
apm: {},
});

expect(getCloudFleetServersHosts()).toMatchInlineSnapshot(`
Array [
"https://deployment-id-1.fleet.us-east-1.aws.found.io",
]
`);
});
});
20 changes: 18 additions & 2 deletions x-pack/plugins/fleet/server/services/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import Boom from '@hapi/boom';
import type { SavedObjectsClientContract } from 'kibana/server';

import { GLOBAL_SETTINGS_SAVED_OBJECT_TYPE } from '../../common';
import { decodeCloudId, GLOBAL_SETTINGS_SAVED_OBJECT_TYPE } from '../../common';
import type { SettingsSOAttributes, Settings, BaseSettings } from '../../common';

import { appContextService } from './app_context';
Expand Down Expand Up @@ -65,9 +65,25 @@ export async function saveSettings(
}

export function createDefaultSettings(): BaseSettings {
const fleetServerHosts = appContextService.getConfig()?.agents?.fleet_server?.hosts ?? [];
const configFleetServerHosts = appContextService.getConfig()?.agents?.fleet_server?.hosts;
const cloudFleetServerHosts = getCloudFleetServersHosts();

const fleetServerHosts = configFleetServerHosts ?? cloudFleetServerHosts ?? [];

return {
fleet_server_hosts: fleetServerHosts,
};
}

export function getCloudFleetServersHosts() {
const cloudSetup = appContextService.getCloud();
if (cloudSetup && cloudSetup.isCloudEnabled && cloudSetup.cloudId && cloudSetup.deploymentId) {
const res = decodeCloudId(cloudSetup.cloudId);
if (!res) {
return;
}

// Fleet Server url are formed like this `https://<deploymentId>.fleet.<host>
return [`https://${cloudSetup.deploymentId}.fleet.${res.host}`];
}
}

0 comments on commit adae134

Please sign in to comment.