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

[Fleet] add support for fleet server urls #94364

Merged
merged 16 commits into from
Mar 26, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions x-pack/plugins/fleet/common/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export interface FleetConfigType {
host?: string;
ca_sha256?: string;
};
fleetServerUrls?: string[];
nchaulet marked this conversation as resolved.
Show resolved Hide resolved
agentPolicyRolloutRateLimitIntervalMs: number;
agentPolicyRolloutRateLimitRequestPerInterval: number;
};
Expand Down
10 changes: 7 additions & 3 deletions x-pack/plugins/fleet/common/types/models/agent_policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,13 @@ export interface FullAgentPolicy {
[key: string]: any;
};
};
fleet?: {
kibana: FullAgentPolicyKibanaConfig;
};
fleet?:
| {
hosts: string[];
}
| {
kibana: FullAgentPolicyKibanaConfig;
};
inputs: FullAgentPolicyInput[];
revision?: number;
agent?: {
Expand Down
4 changes: 3 additions & 1 deletion x-pack/plugins/fleet/common/types/models/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ import type { SavedObjectAttributes } from 'src/core/public';
export interface BaseSettings {
agent_auto_upgrade: boolean;
package_auto_upgrade: boolean;
has_seen_add_data_notice?: boolean;
fleet_server_urls: string[];
// TODO remove as part of https://github.com/elastic/kibana/issues/94303
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great, we should add it in more place to make the cleanup easier and not forget things.

kibana_urls: string[];
kibana_ca_sha256?: string;
has_seen_add_data_notice?: boolean;
}

export interface Settings extends BaseSettings {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { FormattedMessage } from '@kbn/i18n/react';
import { EnrollmentAPIKey } from '../../../types';

interface Props {
fleetServerUrls: string[];
kibanaUrl: string;
apiKey: EnrollmentAPIKey;
kibanaCASha256?: string;
Expand All @@ -23,14 +24,32 @@ const CommandCode = styled.pre({
overflow: 'scroll',
});

function getFleetServerUrlsEnrollArgs(apiKey: EnrollmentAPIKey, fleetServerUrls: string[]) {
return `--url=${fleetServerUrls[0]} --enrollment-token=${apiKey.api_key}`;
}

function getKibanaUrlEnrollArgs(
apiKey: EnrollmentAPIKey,
kibanaUrl: string,
kibanaCASha256?: string
) {
return `--kibana-url=${kibanaUrl} --enrollment-token=${apiKey.api_key}${
kibanaCASha256 ? ` --ca_sha256=${kibanaCASha256}` : ''
}`;
}

export const ManualInstructions: React.FunctionComponent<Props> = ({
kibanaUrl,
apiKey,
kibanaCASha256,
fleetServerUrls,
}) => {
const enrollArgs = `--kibana-url=${kibanaUrl} --enrollment-token=${apiKey.api_key}${
kibanaCASha256 ? ` --ca_sha256=${kibanaCASha256}` : ''
}`;
const fleetServerUrlsNotEmpty = fleetServerUrls.length > 0;

const enrollArgs = fleetServerUrlsNotEmpty
? getFleetServerUrlsEnrollArgs(apiKey, fleetServerUrls)
: // TODO remove as part of https://github.com/elastic/kibana/issues/94303
getKibanaUrlEnrollArgs(apiKey, kibanaUrl, kibanaCASha256);

const linuxMacCommand = `./elastic-agent install -f ${enrollArgs}`;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,30 @@ function useSettingsForm(outputId: string | undefined, onSuccess: () => void) {
];
}
});
const fleetServerUrlsInput = useComboInput([], (value) => {
if (value.length === 0) {
return [
i18n.translate('xpack.fleet.settings.fleetServerUrlsEmptyError', {
defaultMessage: 'At least one URL is required',
}),
];
}
if (value.some((v) => !v.match(URL_REGEX))) {
return [
i18n.translate('xpack.fleet.settings.fleetServerUrlsError', {
defaultMessage: 'Invalid URL',
}),
];
}
if (isDiffPathProtocol(value)) {
return [
i18n.translate('xpack.fleet.settings.fleetServerUrlsDifferentPathOrProtocolError', {
defaultMessage: 'Protocol and path must be the same for each URL',
}),
];
}
});

const elasticsearchUrlInput = useComboInput([], (value) => {
if (value.some((v) => !v.match(URL_REGEX))) {
return [
Expand Down Expand Up @@ -98,6 +122,7 @@ function useSettingsForm(outputId: string | undefined, onSuccess: () => void) {
onSubmit: async () => {
if (
!kibanaUrlsInput.validate() ||
!fleetServerUrlsInput.validate() ||
!elasticsearchUrlInput.validate() ||
!additionalYamlConfigInput.validate()
) {
Expand All @@ -118,6 +143,7 @@ function useSettingsForm(outputId: string | undefined, onSuccess: () => void) {
}
const settingsResponse = await sendPutSettings({
kibana_urls: kibanaUrlsInput.value,
fleet_server_urls: fleetServerUrlsInput.value,
});
if (settingsResponse.error) {
throw settingsResponse.error;
Expand All @@ -137,6 +163,7 @@ function useSettingsForm(outputId: string | undefined, onSuccess: () => void) {
}
},
inputs: {
fleetServerUrls: fleetServerUrlsInput,
kibanaUrls: kibanaUrlsInput,
elasticsearchUrl: elasticsearchUrlInput,
additionalYamlConfig: additionalYamlConfigInput,
Expand Down Expand Up @@ -165,6 +192,7 @@ export const SettingFlyout: React.FunctionComponent<Props> = ({ onClose }) => {
useEffect(() => {
if (settings) {
inputs.kibanaUrls.setValue(settings.kibana_urls);
inputs.fleetServerUrls.setValue(settings.fleet_server_urls);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [settings]);
Expand Down Expand Up @@ -254,6 +282,18 @@ export const SettingFlyout: React.FunctionComponent<Props> = ({ onClose }) => {
/>
</EuiText>
<EuiSpacer size="m" />
<EuiFormRow>
<EuiFormRow
label={i18n.translate('xpack.fleet.settings.fleetServerUrlsLabel', {
defaultMessage: 'Fleet Server URL',
})}
{...inputs.fleetServerUrls.formRowProps}
>
<EuiComboBox noSuggestions {...inputs.fleetServerUrls.props} />
</EuiFormRow>
</EuiFormRow>
<EuiSpacer size="m" />
{/* // TODO remove as part of https://github.com/elastic/kibana/issues/94303 */}
<EuiFormRow>
<EuiFormRow
label={i18n.translate('xpack.fleet.settings.kibanaUrlLabel', {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export const ManagedInstructions = React.memo<Props>(({ agentPolicies }) => {
apiKey={apiKey.data.item}
kibanaUrl={kibanaUrl}
kibanaCASha256={kibanaCASha256}
fleetServerUrls={settings.data?.item?.fleet_server_urls || []}
/>
),
},
Expand Down
4 changes: 3 additions & 1 deletion x-pack/plugins/fleet/server/saved_objects/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,11 @@ const getSavedObjectTypes = (
properties: {
agent_auto_upgrade: { type: 'keyword' },
package_auto_upgrade: { type: 'keyword' },
fleet_server_urls: { type: 'keyword' },
has_seen_add_data_notice: { type: 'boolean', index: false },
// TODO remove as part of https://github.com/elastic/kibana/issues/94303
kibana_urls: { type: 'keyword' },
kibana_ca_sha256: { type: 'keyword' },
has_seen_add_data_notice: { type: 'boolean', index: false },
},
},
migrations: {
Expand Down
17 changes: 12 additions & 5 deletions x-pack/plugins/fleet/server/services/agent_policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -706,12 +706,19 @@ class AgentPolicyService {
} catch (error) {
throw new Error('Default settings is not setup');
}
if (!settings.kibana_urls || !settings.kibana_urls.length)
throw new Error('kibana_urls is missing');
if (settings.fleet_server_urls && settings.fleet_server_urls.length) {
fullAgentPolicy.fleet = {
hosts: settings.fleet_server_urls,
};
} // TODO remove as part of https://github.com/elastic/kibana/issues/94303
else {
if (!settings.kibana_urls || !settings.kibana_urls.length)
throw new Error('kibana_urls is missing');

fullAgentPolicy.fleet = {
kibana: getFullAgentPolicyKibanaConfig(settings.kibana_urls),
};
fullAgentPolicy.fleet = {
kibana: getFullAgentPolicyKibanaConfig(settings.kibana_urls),
};
}
}
return fullAgentPolicy;
}
Expand Down
4 changes: 4 additions & 0 deletions x-pack/plugins/fleet/server/services/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export async function getSettings(soClient: SavedObjectsClientContract): Promise
return {
id: settingsSo.id,
...settingsSo.attributes,
fleet_server_urls: settingsSo.attributes.fleet_server_urls || [],
};
}

Expand Down Expand Up @@ -81,9 +82,12 @@ export function createDefaultSettings(): BaseSettings {
pathname: basePath.serverBasePath,
});

const fleetServerUrls = appContextService.getConfig()?.agents?.fleetServerUrls || [];

return {
agent_auto_upgrade: true,
package_auto_upgrade: true,
kibana_urls: [cloudUrl || flagsUrl || defaultUrl].flat(),
fleet_server_urls: fleetServerUrls,
};
}
9 changes: 9 additions & 0 deletions x-pack/plugins/fleet/server/types/rest_spec/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ export const PutSettingsRequestSchema = {
body: schema.object({
agent_auto_upgrade: schema.maybe(schema.boolean()),
package_auto_upgrade: schema.maybe(schema.boolean()),
fleet_server_urls: schema.maybe(
schema.arrayOf(schema.uri({ scheme: ['http', 'https'] }), {
validate: (value) => {
if (isDiffPathProtocol(value)) {
return 'Protocol and path must be the same for each URL';
}
},
})
),
kibana_urls: schema.maybe(
schema.arrayOf(schema.uri({ scheme: ['http', 'https'] }), {
validate: (value) => {
Expand Down