From 4dcc3e9476afea8fed2c7ad9a6a58a25cdbc9fc2 Mon Sep 17 00:00:00 2001 From: Jen Huang Date: Mon, 23 Sep 2024 11:56:28 -0700 Subject: [PATCH 01/10] Return download source proxy from enrollment settings API --- .../fleet_settings_enrollment_response.yaml | 2 ++ .../fleet/common/types/rest_spec/settings.ts | 1 + .../settings/enrollment_settings_handler.ts | 13 +++++++++++ .../fleet/server/types/rest_spec/settings.ts | 23 ++++--------------- 4 files changed, 20 insertions(+), 19 deletions(-) diff --git a/x-pack/plugins/fleet/common/openapi/components/schemas/fleet_settings_enrollment_response.yaml b/x-pack/plugins/fleet/common/openapi/components/schemas/fleet_settings_enrollment_response.yaml index 5c9204e5f35a3..5ba8e5df028bd 100644 --- a/x-pack/plugins/fleet/common/openapi/components/schemas/fleet_settings_enrollment_response.yaml +++ b/x-pack/plugins/fleet/common/openapi/components/schemas/fleet_settings_enrollment_response.yaml @@ -38,5 +38,7 @@ properties: - has_active download_source: $ref: ./download_sources.yaml + download_source_proxy: + $ref: ./proxies.yaml required: - fleet_server diff --git a/x-pack/plugins/fleet/common/types/rest_spec/settings.ts b/x-pack/plugins/fleet/common/types/rest_spec/settings.ts index 6961a0254562f..75908ae3a5621 100644 --- a/x-pack/plugins/fleet/common/types/rest_spec/settings.ts +++ b/x-pack/plugins/fleet/common/types/rest_spec/settings.ts @@ -45,6 +45,7 @@ export interface GetEnrollmentSettingsResponse { host_proxy?: FleetProxy; }; download_source?: DownloadSource; + download_source_proxy?: FleetProxy; } export interface PutSpaceSettingsRequest { body: { diff --git a/x-pack/plugins/fleet/server/routes/settings/enrollment_settings_handler.ts b/x-pack/plugins/fleet/server/routes/settings/enrollment_settings_handler.ts index 73a7d03a14592..5bf49e9851733 100644 --- a/x-pack/plugins/fleet/server/routes/settings/enrollment_settings_handler.ts +++ b/x-pack/plugins/fleet/server/routes/settings/enrollment_settings_handler.ts @@ -69,6 +69,19 @@ export const getEnrollmentSettingsHandler: FleetRequestHandler< settingsResponse.download_source = undefined; } + // Get download source proxy + // ignore errors if the download source proxy is not found + try { + if (settingsResponse.download_source?.proxy_id) { + settingsResponse.download_source_proxy = await getFleetProxy( + soClient, + settingsResponse.download_source.proxy_id + ); + } + } catch (e) { + settingsResponse.download_source_proxy = undefined; + } + // Get associated fleet server host, or default one if it doesn't exist // `getFleetServerHostsForAgentPolicy` errors if there is no default, so catch it try { diff --git a/x-pack/plugins/fleet/server/types/rest_spec/settings.ts b/x-pack/plugins/fleet/server/types/rest_spec/settings.ts index 6553d2e976bed..81383298dc8dc 100644 --- a/x-pack/plugins/fleet/server/types/rest_spec/settings.ts +++ b/x-pack/plugins/fleet/server/types/rest_spec/settings.ts @@ -9,6 +9,8 @@ import { schema } from '@kbn/config-schema'; import { isDiffPathProtocol } from '../../../common/services'; +import { FleetProxySchema } from './fleet_proxies'; + export const GetSettingsRequestSchema = {}; export const PutSettingsRequestSchema = { @@ -115,25 +117,7 @@ export const GetEnrollmentSettingsResponseSchema = schema.object({ proxy_id: schema.maybe(schema.oneOf([schema.literal(null), schema.string()])), }) ), - host_proxy: schema.maybe( - schema.object({ - id: schema.string(), - proxy_headers: schema.maybe( - schema.recordOf( - schema.string(), - schema.oneOf([schema.string(), schema.number(), schema.boolean()]) - ) - ), - name: schema.string(), - url: schema.string(), - certificate_authorities: schema.maybe( - schema.oneOf([schema.literal(null), schema.string()]) - ), - certificate: schema.maybe(schema.oneOf([schema.literal(null), schema.string()])), - certificate_key: schema.maybe(schema.oneOf([schema.literal(null), schema.string()])), - is_preconfigured: schema.boolean(), - }) - ), + host_proxy: schema.maybe(FleetProxySchema), }), download_source: schema.maybe( schema.object({ @@ -154,4 +138,5 @@ export const GetEnrollmentSettingsResponseSchema = schema.object({ ), }) ), + download_source_proxy: schema.maybe(FleetProxySchema), }); From 8e9039c8b93341a19c59b0886054a1c234d5dd43 Mon Sep 17 00:00:00 2001 From: Jen Huang Date: Mon, 23 Sep 2024 11:59:05 -0700 Subject: [PATCH 02/10] Add proxy args to managed agent enrollment instructions --- .../agent_enrollment_flyout/index.tsx | 10 ++++- .../steps/compute_steps.tsx | 2 + .../agent_enrollment_flyout/types.ts | 1 + .../enrollment_instructions/manual/index.tsx | 37 ++++++++++++++++--- .../use_fleet_server_hosts_for_policy.ts | 2 + 5 files changed, 45 insertions(+), 7 deletions(-) diff --git a/x-pack/plugins/fleet/public/components/agent_enrollment_flyout/index.tsx b/x-pack/plugins/fleet/public/components/agent_enrollment_flyout/index.tsx index 1b4e6565efc01..5ccdf37951703 100644 --- a/x-pack/plugins/fleet/public/components/agent_enrollment_flyout/index.tsx +++ b/x-pack/plugins/fleet/public/components/agent_enrollment_flyout/index.tsx @@ -77,8 +77,13 @@ export const AgentEnrollmentFlyout: React.FunctionComponent = ({ const { agentPolicyWithPackagePolicies } = useAgentPolicyWithPackagePolicies(selectedPolicyId); - const { fleetServerHost, fleetProxy, downloadSource, isLoadingInitialRequest } = - useFleetServerHostsForPolicy(agentPolicyWithPackagePolicies); + const { + fleetServerHost, + fleetProxy, + downloadSource, + isLoadingInitialRequest, + downloadSourceProxy, + } = useFleetServerHostsForPolicy(agentPolicyWithPackagePolicies); const selectedPolicy = agentPolicyWithPackagePolicies ? agentPolicyWithPackagePolicies @@ -196,6 +201,7 @@ export const AgentEnrollmentFlyout: React.FunctionComponent = ({ fleetServerHost={fleetServerHost} fleetProxy={fleetProxy} downloadSource={downloadSource} + downloadSourceProxy={downloadSourceProxy} setSelectedPolicyId={setSelectedPolicyId} agentPolicy={agentPolicy} selectedPolicy={selectedPolicy} diff --git a/x-pack/plugins/fleet/public/components/agent_enrollment_flyout/steps/compute_steps.tsx b/x-pack/plugins/fleet/public/components/agent_enrollment_flyout/steps/compute_steps.tsx index ea31b163fb368..553c6145f7b40 100644 --- a/x-pack/plugins/fleet/public/components/agent_enrollment_flyout/steps/compute_steps.tsx +++ b/x-pack/plugins/fleet/public/components/agent_enrollment_flyout/steps/compute_steps.tsx @@ -143,6 +143,7 @@ export const ManagedSteps: React.FunctionComponent = ({ fleetServerHost, fleetProxy, downloadSource, + downloadSourceProxy, refreshAgentPolicies, mode, setMode, @@ -173,6 +174,7 @@ export const ManagedSteps: React.FunctionComponent = ({ fleetServerHost, fleetProxy, downloadSource, + downloadSourceProxy, agentVersion: agentVersion || '', gcpProjectId, gcpOrganizationId, diff --git a/x-pack/plugins/fleet/public/components/agent_enrollment_flyout/types.ts b/x-pack/plugins/fleet/public/components/agent_enrollment_flyout/types.ts index 0eefe62229193..a58feeda65617 100644 --- a/x-pack/plugins/fleet/public/components/agent_enrollment_flyout/types.ts +++ b/x-pack/plugins/fleet/public/components/agent_enrollment_flyout/types.ts @@ -83,4 +83,5 @@ export interface InstructionProps extends BaseProps { fleetServerHost: string; fleetProxy?: FleetProxy; downloadSource?: DownloadSource; + downloadSourceProxy?: FleetProxy; } diff --git a/x-pack/plugins/fleet/public/components/enrollment_instructions/manual/index.tsx b/x-pack/plugins/fleet/public/components/enrollment_instructions/manual/index.tsx index e5733983dd754..d5e49bf92bfef 100644 --- a/x-pack/plugins/fleet/public/components/enrollment_instructions/manual/index.tsx +++ b/x-pack/plugins/fleet/public/components/enrollment_instructions/manual/index.tsx @@ -28,6 +28,7 @@ export const ManualInstructions = ({ fleetServerHost, fleetProxy, downloadSource, + downloadSourceProxy, agentVersion: agentVersion, gcpProjectId = '', gcpOrganizationId = '', @@ -37,6 +38,7 @@ export const ManualInstructions = ({ fleetServerHost: string; fleetProxy?: FleetProxy; downloadSource?: DownloadSource; + downloadSourceProxy?: FleetProxy; agentVersion: string; gcpProjectId?: string; gcpOrganizationId?: string; @@ -54,27 +56,52 @@ export const ManualInstructions = ({ const k8sCommand = 'kubectl apply -f elastic-agent-managed-kubernetes.yml'; - const linuxCommand = `curl -L -O ${downloadBaseUrl}/beats/elastic-agent/elastic-agent-${agentVersion}-linux-x86_64.tar.gz + const windowsDownloadSourceProxyArgs = `${ + downloadSourceProxy?.url ? `-Proxy "${downloadSourceProxy.url}"` : '' + } ${ + downloadSourceProxy?.proxy_headers + ? `@{${Object.entries(downloadSourceProxy.proxy_headers) + .reduce((acc, [proxyKey, proyVal]) => { + acc.push(`"${proxyKey}"="${proyVal}"`); + return acc; + }, [] as string[]) + .join(';')}` + : '' + }}`.trim(); + const curlDownloadSourceProxyArgs = `${ + downloadSourceProxy?.url ? `--proxy ${downloadSourceProxy.url}` : '' + } ${ + downloadSourceProxy?.proxy_headers + ? Object.entries(downloadSourceProxy.proxy_headers) + .reduce((acc, [proxyKey, proyVal]) => { + acc.push(`--proxy-header ${proxyKey}=${proyVal}`); + return acc; + }, [] as string[]) + .join(' ') + : '' + }`.trim(); + + const linuxCommand = `curl -L -O ${downloadBaseUrl}/beats/elastic-agent/elastic-agent-${agentVersion}-linux-x86_64.tar.gz ${curlDownloadSourceProxyArgs} tar xzvf elastic-agent-${agentVersion}-linux-x86_64.tar.gz cd elastic-agent-${agentVersion}-linux-x86_64 sudo ./elastic-agent install ${enrollArgs}`; - const macCommand = `curl -L -O ${downloadBaseUrl}/beats/elastic-agent/elastic-agent-${agentVersion}-darwin-aarch64.tar.gz + const macCommand = `curl -L -O ${downloadBaseUrl}/beats/elastic-agent/elastic-agent-${agentVersion}-darwin-aarch64.tar.gz ${curlDownloadSourceProxyArgs} tar xzvf elastic-agent-${agentVersion}-darwin-aarch64.tar.gz cd elastic-agent-${agentVersion}-darwin-aarch64 sudo ./elastic-agent install ${enrollArgs}`; const windowsCommand = `$ProgressPreference = 'SilentlyContinue' -Invoke-WebRequest -Uri ${downloadBaseUrl}/beats/elastic-agent/elastic-agent-${agentVersion}-windows-x86_64.zip -OutFile elastic-agent-${agentVersion}-windows-x86_64.zip +Invoke-WebRequest -Uri ${downloadBaseUrl}/beats/elastic-agent/elastic-agent-${agentVersion}-windows-x86_64.zip -OutFile elastic-agent-${agentVersion}-windows-x86_64.zip ${windowsDownloadSourceProxyArgs} Expand-Archive .\\elastic-agent-${agentVersion}-windows-x86_64.zip -DestinationPath . cd elastic-agent-${agentVersion}-windows-x86_64 .\\elastic-agent.exe install ${enrollArgs}`; - const linuxDebCommand = `curl -L -O ${downloadBaseUrl}/beats/elastic-agent/elastic-agent-${agentVersion}-amd64.deb + const linuxDebCommand = `curl -L -O ${downloadBaseUrl}/beats/elastic-agent/elastic-agent-${agentVersion}-amd64.deb ${curlDownloadSourceProxyArgs} sudo dpkg -i elastic-agent-${agentVersion}-amd64.deb sudo elastic-agent enroll ${enrollArgs} \nsudo systemctl enable elastic-agent \nsudo systemctl start elastic-agent`; - const linuxRpmCommand = `curl -L -O ${downloadBaseUrl}/beats/elastic-agent/elastic-agent-${agentVersion}-x86_64.rpm + const linuxRpmCommand = `curl -L -O ${downloadBaseUrl}/beats/elastic-agent/elastic-agent-${agentVersion}-x86_64.rpm ${curlDownloadSourceProxyArgs} sudo rpm -vi elastic-agent-${agentVersion}-x86_64.rpm sudo elastic-agent enroll ${enrollArgs} \nsudo systemctl enable elastic-agent \nsudo systemctl start elastic-agent`; diff --git a/x-pack/plugins/fleet/public/hooks/use_fleet_server_hosts_for_policy.ts b/x-pack/plugins/fleet/public/hooks/use_fleet_server_hosts_for_policy.ts index 8ca0d8a6871c3..0dd627603cb3f 100644 --- a/x-pack/plugins/fleet/public/hooks/use_fleet_server_hosts_for_policy.ts +++ b/x-pack/plugins/fleet/public/hooks/use_fleet_server_hosts_for_policy.ts @@ -27,6 +27,7 @@ export function useFleetServerHostsForPolicy(agentPolicy?: AgentPolicy | null) { fleetServerHost: enrollmentSettings?.fleet_server.host?.host_urls[0] || '', fleetProxy: enrollmentSettings?.fleet_server.host_proxy, downloadSource: enrollmentSettings?.download_source, + downloadSourceProxy: enrollmentSettings?.download_source_proxy, }), [ isLoading, @@ -34,6 +35,7 @@ export function useFleetServerHostsForPolicy(agentPolicy?: AgentPolicy | null) { enrollmentSettings?.fleet_server.host, enrollmentSettings?.fleet_server.host_proxy, enrollmentSettings?.download_source, + enrollmentSettings?.download_source_proxy, ] ); } From c66c8cf40bd5e58ae047ae8bbe5fd698bc6574e7 Mon Sep 17 00:00:00 2001 From: Jen Huang Date: Tue, 24 Sep 2024 09:34:47 -0700 Subject: [PATCH 03/10] Add custom download source uri and proxy args to standalone agent install snippets --- .../steps/compute_steps.tsx | 10 ++- .../enrollment_instructions/manual/index.tsx | 66 +++++++++++-------- .../standalone/index.tsx | 27 ++++++-- 3 files changed, 67 insertions(+), 36 deletions(-) diff --git a/x-pack/plugins/fleet/public/components/agent_enrollment_flyout/steps/compute_steps.tsx b/x-pack/plugins/fleet/public/components/agent_enrollment_flyout/steps/compute_steps.tsx index 553c6145f7b40..660aafa339b47 100644 --- a/x-pack/plugins/fleet/public/components/agent_enrollment_flyout/steps/compute_steps.tsx +++ b/x-pack/plugins/fleet/public/components/agent_enrollment_flyout/steps/compute_steps.tsx @@ -53,6 +53,8 @@ export const StandaloneSteps: React.FunctionComponent = ({ setSelectedAPIKeyId, isK8s, cloudSecurityIntegration, + downloadSource, + downloadSourceProxy, }) => { const { yaml, onCreateApiKey, isCreatingApiKey, apiKey, downloadYaml } = useFetchFullPolicy( selectedPolicy, @@ -62,7 +64,11 @@ export const StandaloneSteps: React.FunctionComponent = ({ const agentVersion = useAgentVersion(); const instructionsSteps = useMemo(() => { - const standaloneInstallCommands = StandaloneInstructions(agentVersion || ''); + const standaloneInstallCommands = StandaloneInstructions({ + agentVersion: agentVersion || '', + downloadSource, + downloadSourceProxy, + }); const steps: EuiContainedStepProps[] = !agentPolicy ? [ @@ -107,6 +113,8 @@ export const StandaloneSteps: React.FunctionComponent = ({ return steps; }, [ agentVersion, + downloadSource, + downloadSourceProxy, agentPolicy, selectedPolicy, agentPolicies, diff --git a/x-pack/plugins/fleet/public/components/enrollment_instructions/manual/index.tsx b/x-pack/plugins/fleet/public/components/enrollment_instructions/manual/index.tsx index d5e49bf92bfef..166df5fdcf23c 100644 --- a/x-pack/plugins/fleet/public/components/enrollment_instructions/manual/index.tsx +++ b/x-pack/plugins/fleet/public/components/enrollment_instructions/manual/index.tsx @@ -23,6 +23,40 @@ function getfleetServerHostsEnrollArgs( return `--url=${fleetServerHost || `FLEET_SERVER_HOST`} --enrollment-token=${apiKey}${proxyArgs}`; } +export const getDownloadBaseUrl = (downloadSource?: DownloadSource) => { + return downloadSource?.host.endsWith('/') + ? downloadSource.host.substring(0, downloadSource.host.length - 1) + : 'https://artifacts.elastic.co/downloads'; +}; + +export const getDownloadSourceProxyArgs = (downloadSourceProxy?: FleetProxy) => { + const windows = `${downloadSourceProxy?.url ? `-Proxy "${downloadSourceProxy.url}"` : ''} ${ + downloadSourceProxy?.proxy_headers + ? `-Headers @{${Object.entries(downloadSourceProxy.proxy_headers) + .reduce((acc, [proxyKey, proyVal]) => { + acc.push(`"${proxyKey}"="${proyVal}"`); + return acc; + }, [] as string[]) + .join(';')}` + : '' + }}`.trim(); + const curl = `${downloadSourceProxy?.url ? `--proxy ${downloadSourceProxy.url}` : ''} ${ + downloadSourceProxy?.proxy_headers + ? Object.entries(downloadSourceProxy.proxy_headers) + .reduce((acc, [proxyKey, proyVal]) => { + acc.push(`--proxy-header "${proxyKey}=${proyVal}"`); + return acc; + }, [] as string[]) + .join(' ') + : '' + }`.trim(); + + return { + windows, + curl, + }; +}; + export const ManualInstructions = ({ apiKey, fleetServerHost, @@ -45,41 +79,15 @@ export const ManualInstructions = ({ gcpAccountType?: string; }) => { const enrollArgs = getfleetServerHostsEnrollArgs(apiKey, fleetServerHost, fleetProxy); - const downloadBaseUrl = downloadSource - ? downloadSource.host.endsWith('/') - ? downloadSource.host.substring(0, downloadSource.host.length - 1) - : downloadSource.host - : 'https://artifacts.elastic.co/downloads'; + const downloadBaseUrl = getDownloadBaseUrl(downloadSource); const fleetServerUrl = enrollArgs?.split('--url=')?.pop()?.split('--enrollment')[0]; const enrollmentToken = enrollArgs?.split('--enrollment-token=')[1]; const k8sCommand = 'kubectl apply -f elastic-agent-managed-kubernetes.yml'; - const windowsDownloadSourceProxyArgs = `${ - downloadSourceProxy?.url ? `-Proxy "${downloadSourceProxy.url}"` : '' - } ${ - downloadSourceProxy?.proxy_headers - ? `@{${Object.entries(downloadSourceProxy.proxy_headers) - .reduce((acc, [proxyKey, proyVal]) => { - acc.push(`"${proxyKey}"="${proyVal}"`); - return acc; - }, [] as string[]) - .join(';')}` - : '' - }}`.trim(); - const curlDownloadSourceProxyArgs = `${ - downloadSourceProxy?.url ? `--proxy ${downloadSourceProxy.url}` : '' - } ${ - downloadSourceProxy?.proxy_headers - ? Object.entries(downloadSourceProxy.proxy_headers) - .reduce((acc, [proxyKey, proyVal]) => { - acc.push(`--proxy-header ${proxyKey}=${proyVal}`); - return acc; - }, [] as string[]) - .join(' ') - : '' - }`.trim(); + const { windows: windowsDownloadSourceProxyArgs, curl: curlDownloadSourceProxyArgs } = + getDownloadSourceProxyArgs(downloadSourceProxy); const linuxCommand = `curl -L -O ${downloadBaseUrl}/beats/elastic-agent/elastic-agent-${agentVersion}-linux-x86_64.tar.gz ${curlDownloadSourceProxyArgs} tar xzvf elastic-agent-${agentVersion}-linux-x86_64.tar.gz diff --git a/x-pack/plugins/fleet/public/components/enrollment_instructions/standalone/index.tsx b/x-pack/plugins/fleet/public/components/enrollment_instructions/standalone/index.tsx index 21221fdaba79f..dd8eafec86ec4 100644 --- a/x-pack/plugins/fleet/public/components/enrollment_instructions/standalone/index.tsx +++ b/x-pack/plugins/fleet/public/components/enrollment_instructions/standalone/index.tsx @@ -4,27 +4,42 @@ * 2.0; you may not use this file except in compliance with the Elastic License * 2.0. */ + import type { CommandsByPlatform } from '../../../applications/fleet/components/fleet_server_instructions/utils/install_command_utils'; +import type { DownloadSource, FleetProxy } from '../../../types'; +import { getDownloadBaseUrl, getDownloadSourceProxyArgs } from '../manual'; + +export const StandaloneInstructions = ({ + agentVersion, + downloadSource, + downloadSourceProxy, +}: { + agentVersion: string; + downloadSource?: DownloadSource; + downloadSourceProxy?: FleetProxy; +}): CommandsByPlatform => { + const downloadBaseUrl = getDownloadBaseUrl(downloadSource); + const { windows: windowsDownloadSourceProxyArgs, curl: curlDownloadSourceProxyArgs } = + getDownloadSourceProxyArgs(downloadSourceProxy); -export const StandaloneInstructions = (agentVersion: string): CommandsByPlatform => { - const linuxDebCommand = `curl -L -O https://artifacts.elastic.co/downloads/beats/elastic-agent/elastic-agent-${agentVersion}-amd64.deb + const linuxDebCommand = `curl -L -O ${downloadBaseUrl}/downloads/beats/elastic-agent/elastic-agent-${agentVersion}-amd64.deb ${curlDownloadSourceProxyArgs} sudo dpkg -i elastic-agent-${agentVersion}-amd64.deb \nsudo systemctl enable elastic-agent \nsudo systemctl start elastic-agent`; - const linuxRpmCommand = `curl -L -O https://artifacts.elastic.co/downloads/beats/elastic-agent/elastic-agent-${agentVersion}-x86_64.rpm + const linuxRpmCommand = `curl -L -O ${downloadBaseUrl}/downloads/beats/elastic-agent/elastic-agent-${agentVersion}-x86_64.rpm ${curlDownloadSourceProxyArgs} sudo rpm -vi elastic-agent-${agentVersion}-x86_64.rpm \nsudo systemctl enable elastic-agent \nsudo systemctl start elastic-agent`; - const linuxCommand = `curl -L -O https://artifacts.elastic.co/downloads/beats/elastic-agent/elastic-agent-${agentVersion}-linux-x86_64.tar.gz + const linuxCommand = `curl -L -O ${downloadBaseUrl}/downloads/beats/elastic-agent/elastic-agent-${agentVersion}-linux-x86_64.tar.gz ${curlDownloadSourceProxyArgs} tar xzvf elastic-agent-${agentVersion}-linux-x86_64.tar.gz cd elastic-agent-${agentVersion}-linux-x86_64 sudo ./elastic-agent install`; - const macCommand = `curl -L -O https://artifacts.elastic.co/downloads/beats/elastic-agent/elastic-agent-${agentVersion}-darwin-aarch64.tar.gz + const macCommand = `curl -L -O ${downloadBaseUrl}/downloads/beats/elastic-agent/elastic-agent-${agentVersion}-darwin-aarch64.tar.gz ${curlDownloadSourceProxyArgs} tar xzvf elastic-agent-${agentVersion}-darwin-aarch64.tar.gz cd elastic-agent-${agentVersion}-darwin-aarch64 sudo ./elastic-agent install`; const windowsCommand = `$ProgressPreference = 'SilentlyContinue' -Invoke-WebRequest -Uri https://artifacts.elastic.co/downloads/beats/elastic-agent/elastic-agent-${agentVersion}-windows-x86_64.zip -OutFile elastic-agent-${agentVersion}-windows-x86_64.zip +Invoke-WebRequest -Uri ${downloadBaseUrl}/downloads/beats/elastic-agent/elastic-agent-${agentVersion}-windows-x86_64.zip -OutFile elastic-agent-${agentVersion}-windows-x86_64.zip ${windowsDownloadSourceProxyArgs} Expand-Archive .\elastic-agent-${agentVersion}-windows-x86_64.zip -DestinationPath . cd elastic-agent-${agentVersion}-windows-x86_64 .\\elastic-agent.exe install`; From 3f825bb8e05cdc798e4daf2102356a0f1b4e87b0 Mon Sep 17 00:00:00 2001 From: Jen Huang Date: Tue, 24 Sep 2024 09:42:07 -0700 Subject: [PATCH 04/10] Fix proxy header quoting in install/enroll snippets --- .../public/components/enrollment_instructions/manual/index.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x-pack/plugins/fleet/public/components/enrollment_instructions/manual/index.tsx b/x-pack/plugins/fleet/public/components/enrollment_instructions/manual/index.tsx index 166df5fdcf23c..6d8538739c2cd 100644 --- a/x-pack/plugins/fleet/public/components/enrollment_instructions/manual/index.tsx +++ b/x-pack/plugins/fleet/public/components/enrollment_instructions/manual/index.tsx @@ -14,7 +14,7 @@ function getfleetServerHostsEnrollArgs( ) { const proxyHeadersArgs = fleetProxy?.proxy_headers ? Object.entries(fleetProxy.proxy_headers).reduce((acc, [proxyKey, proyVal]) => { - acc += ` --proxy-header ${proxyKey}=${proyVal}`; + acc += ` --proxy-header "${proxyKey}=${proyVal}"`; return acc; }, '') From b10b303853493c1b9e9aa01d167ec80fc097e4bc Mon Sep 17 00:00:00 2001 From: Jen Huang Date: Tue, 24 Sep 2024 13:16:46 -0700 Subject: [PATCH 05/10] Add ES host proxy args and custom binary source (with proxy) to fleet enrollment snippets --- .../fleet/common/types/rest_spec/settings.ts | 12 ++++- .../steps/install_fleet_server.tsx | 23 ++++++--- .../utils/install_command_utils.ts | 49 ++++++++++++------- .../use_fleet_server_hosts_for_policy.ts | 6 ++- .../settings/enrollment_settings_handler.ts | 22 ++++++++- .../fleet/server/types/rest_spec/settings.ts | 5 ++ 6 files changed, 87 insertions(+), 30 deletions(-) diff --git a/x-pack/plugins/fleet/common/types/rest_spec/settings.ts b/x-pack/plugins/fleet/common/types/rest_spec/settings.ts index 75908ae3a5621..c281b79b4d50c 100644 --- a/x-pack/plugins/fleet/common/types/rest_spec/settings.ts +++ b/x-pack/plugins/fleet/common/types/rest_spec/settings.ts @@ -5,7 +5,14 @@ * 2.0. */ -import type { Settings, AgentPolicy, FleetServerHost, FleetProxy, DownloadSource } from '../models'; +import type { + Settings, + AgentPolicy, + FleetServerHost, + FleetProxy, + DownloadSource, + Output, +} from '../models'; export interface GetSettingsResponse { item: Settings; @@ -35,12 +42,15 @@ export type EnrollmentSettingsFleetServerPolicy = Pick< | 'fleet_server_host_id' | 'download_source_id' | 'space_ids' + | 'data_output_id' >; export interface GetEnrollmentSettingsResponse { fleet_server: { policies: EnrollmentSettingsFleetServerPolicy[]; has_active: boolean; + es_output?: Output; + es_output_proxy?: FleetProxy; host?: FleetServerHost; host_proxy?: FleetProxy; }; diff --git a/x-pack/plugins/fleet/public/applications/fleet/components/fleet_server_instructions/steps/install_fleet_server.tsx b/x-pack/plugins/fleet/public/applications/fleet/components/fleet_server_instructions/steps/install_fleet_server.tsx index 97edbd849e0ae..feeee0f49b577 100644 --- a/x-pack/plugins/fleet/public/applications/fleet/components/fleet_server_instructions/steps/install_fleet_server.tsx +++ b/x-pack/plugins/fleet/public/applications/fleet/components/fleet_server_instructions/steps/install_fleet_server.tsx @@ -13,8 +13,8 @@ import { i18n } from '@kbn/i18n'; import { FormattedMessage } from '@kbn/i18n-react'; import type { PLATFORM_TYPE } from '../../../hooks'; -import { useDefaultDownloadSource } from '../../../hooks'; -import { useStartServices, useDefaultOutput, useKibanaVersion } from '../../../hooks'; +import { useFleetServerHostsForPolicy } from '../../../hooks'; +import { useStartServices, useKibanaVersion } from '../../../hooks'; import { PlatformSelector } from '../..'; @@ -61,23 +61,30 @@ const InstallFleetServerStepContent: React.FunctionComponent<{ }> = ({ serviceToken, fleetServerHost, fleetServerPolicyId, deploymentMode }) => { const { docLinks } = useStartServices(); const kibanaVersion = useKibanaVersion(); - const { output } = useDefaultOutput(); - const { downloadSource } = useDefaultDownloadSource(); - const commandOutput = output?.type === 'elasticsearch' ? output : undefined; + const { esOutput, esOutputProxy, downloadSource, downloadSourceProxy } = + useFleetServerHostsForPolicy( + fleetServerPolicyId + ? { + id: fleetServerPolicyId, + } + : null + ); const installCommands = (['linux', 'mac', 'windows', 'deb', 'rpm'] as PLATFORM_TYPE[]).reduce( (acc, platform) => { acc[platform] = getInstallCommandForPlatform( platform, - commandOutput?.hosts?.[0] ?? '', + esOutput?.hosts?.[0] ?? '', + esOutputProxy, serviceToken ?? '', fleetServerPolicyId, fleetServerHost, deploymentMode === 'production', - commandOutput?.ca_trusted_fingerprint ?? undefined, + esOutput?.ca_trusted_fingerprint ?? undefined, kibanaVersion, - downloadSource + downloadSource, + downloadSourceProxy ); return acc; diff --git a/x-pack/plugins/fleet/public/applications/fleet/components/fleet_server_instructions/utils/install_command_utils.ts b/x-pack/plugins/fleet/public/applications/fleet/components/fleet_server_instructions/utils/install_command_utils.ts index 79809b94470e4..d37587c18d5fd 100644 --- a/x-pack/plugins/fleet/public/applications/fleet/components/fleet_server_instructions/utils/install_command_utils.ts +++ b/x-pack/plugins/fleet/public/applications/fleet/components/fleet_server_instructions/utils/install_command_utils.ts @@ -5,7 +5,11 @@ * 2.0. */ -import type { DownloadSource } from '../../../../../../common/types'; +import type { DownloadSource, FleetProxy } from '../../../../../../common/types'; +import { + getDownloadBaseUrl, + getDownloadSourceProxyArgs, +} from '../../../../../components/enrollment_instructions/manual'; import type { PLATFORM_TYPE } from '../../../hooks'; export type CommandsByPlatform = { @@ -15,27 +19,24 @@ export type CommandsByPlatform = { function getArtifact( platform: PLATFORM_TYPE, kibanaVersion: string, - downloadSource?: DownloadSource + downloadSource?: DownloadSource, + downloadSourceProxy?: FleetProxy ) { - const ARTIFACT_BASE_URL = `${ - downloadSource - ? downloadSource.host.endsWith('/') - ? downloadSource.host.substring(0, downloadSource.host.length - 1) - : downloadSource.host - : 'https://artifacts.elastic.co/downloads' - }/beats/elastic-agent`; + const ARTIFACT_BASE_URL = `${getDownloadBaseUrl(downloadSource)}/beats/elastic-agent`; + const { windows: windowsDownloadSourceProxyArgs, curl: curlDownloadSourceProxyArgs } = + getDownloadSourceProxyArgs(downloadSourceProxy); const artifactMap: Record = { linux: { downloadCommand: [ - `curl -L -O ${ARTIFACT_BASE_URL}/elastic-agent-${kibanaVersion}-linux-x86_64.tar.gz`, + `curl -L -O ${ARTIFACT_BASE_URL}/elastic-agent-${kibanaVersion}-linux-x86_64.tar.gz ${curlDownloadSourceProxyArgs}`, `tar xzvf elastic-agent-${kibanaVersion}-linux-x86_64.tar.gz`, `cd elastic-agent-${kibanaVersion}-linux-x86_64`, ].join(`\n`), }, mac: { downloadCommand: [ - `curl -L -O ${ARTIFACT_BASE_URL}/elastic-agent-${kibanaVersion}-darwin-aarch64.tar.gz`, + `curl -L -O ${ARTIFACT_BASE_URL}/elastic-agent-${kibanaVersion}-darwin-aarch64.tar.gz ${curlDownloadSourceProxyArgs}`, `tar xzvf elastic-agent-${kibanaVersion}-darwin-aarch64.tar.gz`, `cd elastic-agent-${kibanaVersion}-darwin-aarch64`, ].join(`\n`), @@ -43,20 +44,20 @@ function getArtifact( windows: { downloadCommand: [ `$ProgressPreference = 'SilentlyContinue'`, - `Invoke-WebRequest -Uri https://artifacts.elastic.co/downloads/beats/elastic-agent/elastic-agent-${kibanaVersion}-windows-x86_64.zip -OutFile elastic-agent-${kibanaVersion}-windows-x86_64.zip`, + `Invoke-WebRequest -Uri ${ARTIFACT_BASE_URL}/elastic-agent/elastic-agent-${kibanaVersion}-windows-x86_64.zip -OutFile elastic-agent-${kibanaVersion}-windows-x86_64.zip ${windowsDownloadSourceProxyArgs}`, `Expand-Archive .\\elastic-agent-${kibanaVersion}-windows-x86_64.zip`, `cd elastic-agent-${kibanaVersion}-windows-x86_64`, ].join(`\n`), }, deb: { downloadCommand: [ - `curl -L -O ${ARTIFACT_BASE_URL}/elastic-agent-${kibanaVersion}-amd64.deb`, + `curl -L -O ${ARTIFACT_BASE_URL}/elastic-agent-${kibanaVersion}-amd64.deb ${curlDownloadSourceProxyArgs}`, `sudo dpkg -i elastic-agent-${kibanaVersion}-amd64.deb`, ].join(`\n`), }, rpm: { downloadCommand: [ - `curl -L -O ${ARTIFACT_BASE_URL}/elastic-agent-${kibanaVersion}-x86_64.rpm`, + `curl -L -O ${ARTIFACT_BASE_URL}/elastic-agent-${kibanaVersion}-x86_64.rpm ${curlDownloadSourceProxyArgs}`, `sudo rpm -vi elastic-agent-${kibanaVersion}-x86_64.rpm`, ].join(`\n`), }, @@ -70,18 +71,20 @@ function getArtifact( export function getInstallCommandForPlatform( platform: PLATFORM_TYPE, - esHost: string, + esOutput: string, + esOutputProxy: FleetProxy | undefined, serviceToken: string, policyId?: string, fleetServerHost?: string, isProductionDeployment?: boolean, sslCATrustedFingerprint?: string, kibanaVersion?: string, - downloadSource?: DownloadSource + downloadSource?: DownloadSource, + downloadSourceProxy?: FleetProxy ): string { const newLineSeparator = platform === 'windows' ? '`\n' : '\\\n'; - const artifact = getArtifact(platform, kibanaVersion ?? '', downloadSource); + const artifact = getArtifact(platform, kibanaVersion ?? '', downloadSource, downloadSourceProxy); const commandArguments = []; @@ -89,7 +92,7 @@ export function getInstallCommandForPlatform( commandArguments.push(['url', fleetServerHost]); } - commandArguments.push(['fleet-server-es', esHost]); + commandArguments.push(['fleet-server-es', esOutput]); commandArguments.push(['fleet-server-service-token', serviceToken]); if (policyId) { commandArguments.push(['fleet-server-policy', policyId]); @@ -110,7 +113,15 @@ export function getInstallCommandForPlatform( commandArguments.push(['fleet-server-port', '8220']); - const commandArgumentsStr = commandArguments + const enrollmentProxyArgs = []; + if (esOutputProxy) { + enrollmentProxyArgs.push(['proxy-url', esOutputProxy.url]); + Object.entries(esOutputProxy.proxy_headers || []).forEach(([key, value]) => { + enrollmentProxyArgs.push(['proxy-header', `"${key}=${value}"`]); + }); + } + + const commandArgumentsStr = [...commandArguments, ...enrollmentProxyArgs] .reduce((acc, [key, val]) => { if (acc === '' && key === 'url') { return `--${key}=${val}`; diff --git a/x-pack/plugins/fleet/public/hooks/use_fleet_server_hosts_for_policy.ts b/x-pack/plugins/fleet/public/hooks/use_fleet_server_hosts_for_policy.ts index 0dd627603cb3f..12b8523f13028 100644 --- a/x-pack/plugins/fleet/public/hooks/use_fleet_server_hosts_for_policy.ts +++ b/x-pack/plugins/fleet/public/hooks/use_fleet_server_hosts_for_policy.ts @@ -14,7 +14,7 @@ import { useGetEnrollmentSettings } from './use_request'; /** * Return Fleet server hosts urls and proxy for a given agent policy */ -export function useFleetServerHostsForPolicy(agentPolicy?: AgentPolicy | null) { +export function useFleetServerHostsForPolicy(agentPolicy?: Pick | null) { const { isLoading, isInitialRequest, @@ -26,6 +26,8 @@ export function useFleetServerHostsForPolicy(agentPolicy?: AgentPolicy | null) { isLoadingInitialRequest: isLoading && isInitialRequest, fleetServerHost: enrollmentSettings?.fleet_server.host?.host_urls[0] || '', fleetProxy: enrollmentSettings?.fleet_server.host_proxy, + esOutput: enrollmentSettings?.fleet_server.es_output, + esOutputProxy: enrollmentSettings?.fleet_server.es_output_proxy, downloadSource: enrollmentSettings?.download_source, downloadSourceProxy: enrollmentSettings?.download_source_proxy, }), @@ -34,6 +36,8 @@ export function useFleetServerHostsForPolicy(agentPolicy?: AgentPolicy | null) { isInitialRequest, enrollmentSettings?.fleet_server.host, enrollmentSettings?.fleet_server.host_proxy, + enrollmentSettings?.fleet_server.es_output, + enrollmentSettings?.fleet_server.es_output_proxy, enrollmentSettings?.download_source, enrollmentSettings?.download_source_proxy, ] diff --git a/x-pack/plugins/fleet/server/routes/settings/enrollment_settings_handler.ts b/x-pack/plugins/fleet/server/routes/settings/enrollment_settings_handler.ts index 5bf49e9851733..f2afcd01a236a 100644 --- a/x-pack/plugins/fleet/server/routes/settings/enrollment_settings_handler.ts +++ b/x-pack/plugins/fleet/server/routes/settings/enrollment_settings_handler.ts @@ -22,6 +22,7 @@ import { agentPolicyService, appContextService, downloadSourceService } from '.. import { getFleetServerHostsForAgentPolicy } from '../../services/fleet_server_host'; import { getFleetProxy } from '../../services/fleet_proxies'; import { getFleetServerPolicies, hasFleetServersForPolicies } from '../../services/fleet_server'; +import { getDataOutputForAgentPolicy } from '../../services/agent_policies'; export const getEnrollmentSettingsHandler: FleetRequestHandler< undefined, @@ -46,6 +47,7 @@ export const getEnrollmentSettingsHandler: FleetRequestHandler< name: undefined, fleet_server_host_id: undefined, download_source_id: undefined, + data_output_id: undefined, }; // Check if there is any active fleet server enrolled into the fleet server policies policies if (fleetServerPolicies) { @@ -93,7 +95,7 @@ export const getEnrollmentSettingsHandler: FleetRequestHandler< settingsResponse.fleet_server.host = undefined; } - // if a fleet server host was found, get associated fleet server host proxy if any + // If a fleet server host was found, get associated fleet server host proxy if any // ignore errors if the proxy is not found try { if (settingsResponse.fleet_server.host?.proxy_id) { @@ -106,6 +108,23 @@ export const getEnrollmentSettingsHandler: FleetRequestHandler< settingsResponse.fleet_server.host_proxy = undefined; } + // Get associated output and proxy (if any) to use for Fleet Server enrollment + try { + const dataOutput = await getDataOutputForAgentPolicy(soClient, scopedAgentPolicy); + if (dataOutput.type === 'elasticsearch' && dataOutput.hosts?.[0]) { + settingsResponse.fleet_server.es_output = dataOutput; + if (dataOutput.proxy_id) { + settingsResponse.fleet_server.es_output_proxy = await getFleetProxy( + soClient, + dataOutput.proxy_id + ); + } + } + } catch (e) { + settingsResponse.fleet_server.es_output = undefined; + settingsResponse.fleet_server.es_output_proxy = undefined; + } + return response.ok({ body: settingsResponse }); } catch (error) { return defaultFleetErrorHandler({ error, response }); @@ -128,6 +147,7 @@ export const getFleetServerOrAgentPolicies = async ( fleet_server_host_id: policy.fleet_server_host_id, download_source_id: policy.download_source_id, space_ids: policy.space_ids, + data_output_id: policy.data_output_id, }); // If an agent policy is specified, return only that policy diff --git a/x-pack/plugins/fleet/server/types/rest_spec/settings.ts b/x-pack/plugins/fleet/server/types/rest_spec/settings.ts index 81383298dc8dc..0adaa69f1d30a 100644 --- a/x-pack/plugins/fleet/server/types/rest_spec/settings.ts +++ b/x-pack/plugins/fleet/server/types/rest_spec/settings.ts @@ -9,6 +9,8 @@ import { schema } from '@kbn/config-schema'; import { isDiffPathProtocol } from '../../../common/services'; +import { OutputSchema } from '../models'; + import { FleetProxySchema } from './fleet_proxies'; export const GetSettingsRequestSchema = {}; @@ -103,6 +105,7 @@ export const GetEnrollmentSettingsResponseSchema = schema.object({ fleet_server_host_id: schema.maybe(schema.oneOf([schema.literal(null), schema.string()])), download_source_id: schema.maybe(schema.oneOf([schema.literal(null), schema.string()])), space_ids: schema.maybe(schema.arrayOf(schema.string())), + data_output_id: schema.maybe(schema.oneOf([schema.literal(null), schema.string()])), }) ), has_active: schema.boolean(), @@ -118,6 +121,8 @@ export const GetEnrollmentSettingsResponseSchema = schema.object({ }) ), host_proxy: schema.maybe(FleetProxySchema), + es_output: schema.maybe(OutputSchema), + es_output_proxy: schema.maybe(FleetProxySchema), }), download_source: schema.maybe( schema.object({ From e1a3310c57d5bb3c7f3cba93221cb57dc57e1a82 Mon Sep 17 00:00:00 2001 From: Jen Huang Date: Tue, 24 Sep 2024 14:09:51 -0700 Subject: [PATCH 06/10] Add tests for fleet server snippets --- .../utils/install_command_utils.test.ts | 517 ++++++++++++++++++ .../utils/install_command_utils.ts | 21 +- .../enrollment_instructions/manual/index.tsx | 4 +- 3 files changed, 533 insertions(+), 9 deletions(-) diff --git a/x-pack/plugins/fleet/public/applications/fleet/components/fleet_server_instructions/utils/install_command_utils.test.ts b/x-pack/plugins/fleet/public/applications/fleet/components/fleet_server_instructions/utils/install_command_utils.test.ts index 21a2cc53257f7..0f55de9e36c1e 100644 --- a/x-pack/plugins/fleet/public/applications/fleet/components/fleet_server_instructions/utils/install_command_utils.test.ts +++ b/x-pack/plugins/fleet/public/applications/fleet/components/fleet_server_instructions/utils/install_command_utils.test.ts @@ -13,6 +13,7 @@ describe('getInstallCommandForPlatform', () => { const res = getInstallCommandForPlatform( 'linux', 'http://elasticsearch:9200', + undefined, 'service-token-1' ); @@ -31,6 +32,7 @@ describe('getInstallCommandForPlatform', () => { const res = getInstallCommandForPlatform( 'mac', 'http://elasticsearch:9200', + undefined, 'service-token-1' ); @@ -49,6 +51,7 @@ describe('getInstallCommandForPlatform', () => { const res = getInstallCommandForPlatform( 'windows', 'http://elasticsearch:9200', + undefined, 'service-token-1' ); @@ -68,6 +71,7 @@ describe('getInstallCommandForPlatform', () => { const res = getInstallCommandForPlatform( 'rpm', 'http://elasticsearch:9200', + undefined, 'service-token-1' ); @@ -87,6 +91,7 @@ describe('getInstallCommandForPlatform', () => { const res = getInstallCommandForPlatform( 'deb', 'http://elasticsearch:9200', + undefined, 'service-token-1' ); @@ -106,6 +111,7 @@ describe('getInstallCommandForPlatform', () => { const res = getInstallCommandForPlatform( 'linux', 'http://elasticsearch:9200', + undefined, 'service-token-1', undefined, undefined, @@ -131,6 +137,7 @@ describe('getInstallCommandForPlatform', () => { const res = getInstallCommandForPlatform( 'linux', 'http://elasticsearch:9200', + undefined, 'service-token-1', 'policy-1' ); @@ -151,6 +158,7 @@ describe('getInstallCommandForPlatform', () => { const res = getInstallCommandForPlatform( 'mac', 'http://elasticsearch:9200', + undefined, 'service-token-1', 'policy-1' ); @@ -171,6 +179,7 @@ describe('getInstallCommandForPlatform', () => { const res = getInstallCommandForPlatform( 'windows', 'http://elasticsearch:9200', + undefined, 'service-token-1', 'policy-1' ); @@ -192,6 +201,7 @@ describe('getInstallCommandForPlatform', () => { const res = getInstallCommandForPlatform( 'rpm', 'http://elasticsearch:9200', + undefined, 'service-token-1', 'policy-1' ); @@ -213,6 +223,7 @@ describe('getInstallCommandForPlatform', () => { const res = getInstallCommandForPlatform( 'deb', 'http://elasticsearch:9200', + undefined, 'service-token-1', 'policy-1' ); @@ -236,6 +247,7 @@ describe('getInstallCommandForPlatform', () => { const res = getInstallCommandForPlatform( 'linux', 'http://elasticsearch:9200', + undefined, 'service-token-1', 'policy-1', undefined, @@ -268,6 +280,7 @@ describe('getInstallCommandForPlatform', () => { const res = getInstallCommandForPlatform( 'linux', 'http://elasticsearch:9200', + undefined, 'service-token-1', 'policy-1', 'http://fleetserver:8220', @@ -294,6 +307,7 @@ describe('getInstallCommandForPlatform', () => { const res = getInstallCommandForPlatform( 'mac', 'http://elasticsearch:9200', + undefined, 'service-token-1', 'policy-1', 'http://fleetserver:8220', @@ -320,6 +334,7 @@ describe('getInstallCommandForPlatform', () => { const res = getInstallCommandForPlatform( 'windows', 'http://elasticsearch:9200', + undefined, 'service-token-1', 'policy-1', 'http://fleetserver:8220', @@ -347,6 +362,7 @@ describe('getInstallCommandForPlatform', () => { const res = getInstallCommandForPlatform( 'rpm', 'http://elasticsearch:9200', + undefined, 'service-token-1', 'policy-1', 'http://fleetserver:8220', @@ -374,6 +390,7 @@ describe('getInstallCommandForPlatform', () => { const res = getInstallCommandForPlatform( 'deb', 'http://elasticsearch:9200', + undefined, 'service-token-1', 'policy-1', 'http://fleetserver:8220', @@ -397,4 +414,504 @@ describe('getInstallCommandForPlatform', () => { `); }); }); + + describe('with simple proxy settings', () => { + it('should return the correct command if proxies are set for linux', () => { + const res = getInstallCommandForPlatform( + 'linux', + 'http://elasticsearch:9200', + { + id: 'es-proxy', + name: 'es-proxy', + url: 'http://es-proxy:1111', + is_preconfigured: false, + }, + 'service-token-1', + 'policy-1', + 'http://fleetserver:8220', + undefined, + undefined, + undefined, + { + id: 'download-src', + name: 'download-src', + host: 'https://download-src/8.12.0-test/', + is_default: false, + proxy_id: 'download-proxy', + }, + { + id: 'download-src-proxy', + name: 'download-src-proxy', + url: 'http://download-src-proxy:2222', + is_preconfigured: false, + } + ); + + expect(res).toMatchInlineSnapshot(` + "curl -L -O https://download-src/8.12.0-test/beats/elastic-agent/elastic-agent--linux-x86_64.tar.gz --proxy http://download-src-proxy:2222 + tar xzvf elastic-agent--linux-x86_64.tar.gz + cd elastic-agent--linux-x86_64 + sudo ./elastic-agent install \\\\ + --fleet-server-es=http://elasticsearch:9200 \\\\ + --fleet-server-service-token=service-token-1 \\\\ + --fleet-server-policy=policy-1 \\\\ + --fleet-server-port=8220 \\\\ + --proxy-url=http://es-proxy:1111" + `); + }); + + it('should return the correct command if proxies are set for mac', () => { + const res = getInstallCommandForPlatform( + 'mac', + 'http://elasticsearch:9200', + { + id: 'es-proxy', + name: 'es-proxy', + url: 'http://es-proxy:1111', + is_preconfigured: false, + }, + 'service-token-1', + 'policy-1', + 'http://fleetserver:8220', + undefined, + undefined, + undefined, + { + id: 'download-src', + name: 'download-src', + host: 'https://download-src/8.12.0-test/', + is_default: false, + proxy_id: 'download-proxy', + }, + { + id: 'download-src-proxy', + name: 'download-src-proxy', + url: 'http://download-src-proxy:2222', + is_preconfigured: false, + } + ); + + expect(res).toMatchInlineSnapshot(` + "curl -L -O https://download-src/8.12.0-test/beats/elastic-agent/elastic-agent--darwin-aarch64.tar.gz --proxy http://download-src-proxy:2222 + tar xzvf elastic-agent--darwin-aarch64.tar.gz + cd elastic-agent--darwin-aarch64 + sudo ./elastic-agent install \\\\ + --fleet-server-es=http://elasticsearch:9200 \\\\ + --fleet-server-service-token=service-token-1 \\\\ + --fleet-server-policy=policy-1 \\\\ + --fleet-server-port=8220 \\\\ + --proxy-url=http://es-proxy:1111" + `); + }); + + it('should return the correct command if proxies are set for windows', () => { + const res = getInstallCommandForPlatform( + 'windows', + 'http://elasticsearch:9200', + { + id: 'es-proxy', + name: 'es-proxy', + url: 'http://es-proxy:1111', + is_preconfigured: false, + }, + 'service-token-1', + 'policy-1', + 'http://fleetserver:8220', + undefined, + undefined, + undefined, + { + id: 'download-src', + name: 'download-src', + host: 'https://download-src/8.12.0-test/', + is_default: false, + proxy_id: 'download-proxy', + }, + { + id: 'download-src-proxy', + name: 'download-src-proxy', + url: 'http://download-src-proxy:2222', + is_preconfigured: false, + } + ); + + expect(res).toMatchInlineSnapshot(` + "$ProgressPreference = 'SilentlyContinue' + Invoke-WebRequest -Uri https://download-src/8.12.0-test/beats/elastic-agent/elastic-agent--windows-x86_64.zip -OutFile elastic-agent--windows-x86_64.zip -Proxy \\"http://download-src-proxy:2222\\" + Expand-Archive .\\\\elastic-agent--windows-x86_64.zip + cd elastic-agent--windows-x86_64 + .\\\\elastic-agent.exe install \` + --fleet-server-es=http://elasticsearch:9200 \` + --fleet-server-service-token=service-token-1 \` + --fleet-server-policy=policy-1 \` + --fleet-server-port=8220 \` + --proxy-url=http://es-proxy:1111" + `); + }); + + it('should return the correct command if proxies are set for rpm', () => { + const res = getInstallCommandForPlatform( + 'rpm', + 'http://elasticsearch:9200', + { + id: 'es-proxy', + name: 'es-proxy', + url: 'http://es-proxy:1111', + is_preconfigured: false, + }, + 'service-token-1', + 'policy-1', + 'http://fleetserver:8220', + undefined, + undefined, + undefined, + { + id: 'download-src', + name: 'download-src', + host: 'https://download-src/8.12.0-test/', + is_default: false, + proxy_id: 'download-proxy', + }, + { + id: 'download-src-proxy', + name: 'download-src-proxy', + url: 'http://download-src-proxy:2222', + is_preconfigured: false, + } + ); + + expect(res).toMatchInlineSnapshot(` + "curl -L -O https://download-src/8.12.0-test/beats/elastic-agent/elastic-agent--x86_64.rpm --proxy http://download-src-proxy:2222 + sudo rpm -vi elastic-agent--x86_64.rpm + sudo elastic-agent enroll \\\\ + --fleet-server-es=http://elasticsearch:9200 \\\\ + --fleet-server-service-token=service-token-1 \\\\ + --fleet-server-policy=policy-1 \\\\ + --fleet-server-port=8220 \\\\ + --proxy-url=http://es-proxy:1111 + sudo systemctl enable elastic-agent + sudo systemctl start elastic-agent" + `); + }); + + it('should return the correct command if proxies are set for deb', () => { + const res = getInstallCommandForPlatform( + 'deb', + 'http://elasticsearch:9200', + { + id: 'es-proxy', + name: 'es-proxy', + url: 'http://es-proxy:1111', + is_preconfigured: false, + }, + 'service-token-1', + 'policy-1', + 'http://fleetserver:8220', + undefined, + undefined, + undefined, + { + id: 'download-src', + name: 'download-src', + host: 'https://download-src/8.12.0-test/', + is_default: false, + proxy_id: 'download-proxy', + }, + { + id: 'download-src-proxy', + name: 'download-src-proxy', + url: 'http://download-src-proxy:2222', + is_preconfigured: false, + } + ); + + expect(res).toMatchInlineSnapshot(` + "curl -L -O https://download-src/8.12.0-test/beats/elastic-agent/elastic-agent--amd64.deb --proxy http://download-src-proxy:2222 + sudo dpkg -i elastic-agent--amd64.deb + sudo elastic-agent enroll \\\\ + --fleet-server-es=http://elasticsearch:9200 \\\\ + --fleet-server-service-token=service-token-1 \\\\ + --fleet-server-policy=policy-1 \\\\ + --fleet-server-port=8220 \\\\ + --proxy-url=http://es-proxy:1111 + sudo systemctl enable elastic-agent + sudo systemctl start elastic-agent" + `); + }); + }); + + describe('with full proxy settings', () => { + it('should return the correct command if proxies are set for linux', () => { + const res = getInstallCommandForPlatform( + 'linux', + 'http://elasticsearch:9200', + { + id: 'es-proxy', + name: 'es-proxy', + url: 'http://es-proxy:1111', + proxy_headers: { + 'X-Forwarded-For': 'forwarded-value', + 'test-header': 'test-value', + }, + is_preconfigured: false, + }, + 'service-token-1', + 'policy-1', + 'http://fleetserver:8220', + undefined, + undefined, + undefined, + { + id: 'download-src', + name: 'download-src', + host: 'https://download-src/8.12.0-test/', + is_default: false, + proxy_id: 'download-proxy', + }, + { + id: 'download-src-proxy', + name: 'download-src-proxy', + url: 'http://download-src-proxy:2222', + proxy_headers: { + 'Accept-Language': 'en-US,en;q=0.5', + 'second-header': 'second-value', + }, + is_preconfigured: false, + } + ); + + expect(res).toMatchInlineSnapshot(` + "curl -L -O https://download-src/8.12.0-test/beats/elastic-agent/elastic-agent--linux-x86_64.tar.gz --proxy http://download-src-proxy:2222 --proxy-header \\"Accept-Language=en-US,en;q=0.5\\" --proxy-header \\"second-header=second-value\\" + tar xzvf elastic-agent--linux-x86_64.tar.gz + cd elastic-agent--linux-x86_64 + sudo ./elastic-agent install \\\\ + --fleet-server-es=http://elasticsearch:9200 \\\\ + --fleet-server-service-token=service-token-1 \\\\ + --fleet-server-policy=policy-1 \\\\ + --fleet-server-port=8220 \\\\ + --proxy-url=http://es-proxy:1111 \\\\ + --proxy-header=\\"X-Forwarded-For=forwarded-value\\" \\\\ + --proxy-header=\\"test-header=test-value\\"" + `); + }); + + it('should return the correct command if proxies are set for mac', () => { + const res = getInstallCommandForPlatform( + 'mac', + 'http://elasticsearch:9200', + { + id: 'es-proxy', + name: 'es-proxy', + url: 'http://es-proxy:1111', + proxy_headers: { + 'X-Forwarded-For': 'forwarded-value', + 'test-header': 'test-value', + }, + is_preconfigured: false, + }, + 'service-token-1', + 'policy-1', + 'http://fleetserver:8220', + undefined, + undefined, + undefined, + { + id: 'download-src', + name: 'download-src', + host: 'https://download-src/8.12.0-test/', + is_default: false, + proxy_id: 'download-proxy', + }, + { + id: 'download-src-proxy', + name: 'download-src-proxy', + url: 'http://download-src-proxy:2222', + proxy_headers: { + 'Accept-Language': 'en-US,en;q=0.5', + 'second-header': 'second-value', + }, + is_preconfigured: false, + } + ); + + expect(res).toMatchInlineSnapshot(` + "curl -L -O https://download-src/8.12.0-test/beats/elastic-agent/elastic-agent--darwin-aarch64.tar.gz --proxy http://download-src-proxy:2222 --proxy-header \\"Accept-Language=en-US,en;q=0.5\\" --proxy-header \\"second-header=second-value\\" + tar xzvf elastic-agent--darwin-aarch64.tar.gz + cd elastic-agent--darwin-aarch64 + sudo ./elastic-agent install \\\\ + --fleet-server-es=http://elasticsearch:9200 \\\\ + --fleet-server-service-token=service-token-1 \\\\ + --fleet-server-policy=policy-1 \\\\ + --fleet-server-port=8220 \\\\ + --proxy-url=http://es-proxy:1111 \\\\ + --proxy-header=\\"X-Forwarded-For=forwarded-value\\" \\\\ + --proxy-header=\\"test-header=test-value\\"" + `); + }); + + it('should return the correct command if proxies are set for windows', () => { + const res = getInstallCommandForPlatform( + 'windows', + 'http://elasticsearch:9200', + { + id: 'es-proxy', + name: 'es-proxy', + url: 'http://es-proxy:1111', + proxy_headers: { + 'X-Forwarded-For': 'forwarded-value', + 'test-header': 'test-value', + }, + is_preconfigured: false, + }, + 'service-token-1', + 'policy-1', + 'http://fleetserver:8220', + undefined, + undefined, + undefined, + { + id: 'download-src', + name: 'download-src', + host: 'https://download-src/8.12.0-test/', + is_default: false, + proxy_id: 'download-proxy', + }, + { + id: 'download-src-proxy', + name: 'download-src-proxy', + url: 'http://download-src-proxy:2222', + proxy_headers: { + 'Accept-Language': 'en-US,en;q=0.5', + 'second-header': 'second-value', + }, + is_preconfigured: false, + } + ); + + expect(res).toMatchInlineSnapshot(` + "$ProgressPreference = 'SilentlyContinue' + Invoke-WebRequest -Uri https://download-src/8.12.0-test/beats/elastic-agent/elastic-agent--windows-x86_64.zip -OutFile elastic-agent--windows-x86_64.zip -Proxy \\"http://download-src-proxy:2222\\" -Headers @{\\"Accept-Language\\"=\\"en-US,en;q=0.5\\"; \\"second-header\\"=\\"second-value\\"} + Expand-Archive .\\\\elastic-agent--windows-x86_64.zip + cd elastic-agent--windows-x86_64 + .\\\\elastic-agent.exe install \` + --fleet-server-es=http://elasticsearch:9200 \` + --fleet-server-service-token=service-token-1 \` + --fleet-server-policy=policy-1 \` + --fleet-server-port=8220 \` + --proxy-url=http://es-proxy:1111 \` + --proxy-header=\\"X-Forwarded-For=forwarded-value\\" \` + --proxy-header=\\"test-header=test-value\\"" + `); + }); + + it('should return the correct command if proxies are set for rpm', () => { + const res = getInstallCommandForPlatform( + 'rpm', + 'http://elasticsearch:9200', + { + id: 'es-proxy', + name: 'es-proxy', + url: 'http://es-proxy:1111', + proxy_headers: { + 'X-Forwarded-For': 'forwarded-value', + 'test-header': 'test-value', + }, + is_preconfigured: false, + }, + 'service-token-1', + 'policy-1', + 'http://fleetserver:8220', + undefined, + undefined, + undefined, + { + id: 'download-src', + name: 'download-src', + host: 'https://download-src/8.12.0-test/', + is_default: false, + proxy_id: 'download-proxy', + }, + { + id: 'download-src-proxy', + name: 'download-src-proxy', + url: 'http://download-src-proxy:2222', + proxy_headers: { + 'Accept-Language': 'en-US,en;q=0.5', + 'second-header': 'second-value', + }, + is_preconfigured: false, + } + ); + + expect(res).toMatchInlineSnapshot(` + "curl -L -O https://download-src/8.12.0-test/beats/elastic-agent/elastic-agent--x86_64.rpm --proxy http://download-src-proxy:2222 --proxy-header \\"Accept-Language=en-US,en;q=0.5\\" --proxy-header \\"second-header=second-value\\" + sudo rpm -vi elastic-agent--x86_64.rpm + sudo elastic-agent enroll \\\\ + --fleet-server-es=http://elasticsearch:9200 \\\\ + --fleet-server-service-token=service-token-1 \\\\ + --fleet-server-policy=policy-1 \\\\ + --fleet-server-port=8220 \\\\ + --proxy-url=http://es-proxy:1111 \\\\ + --proxy-header=\\"X-Forwarded-For=forwarded-value\\" \\\\ + --proxy-header=\\"test-header=test-value\\" + sudo systemctl enable elastic-agent + sudo systemctl start elastic-agent" + `); + }); + + it('should return the correct command if proxies are set for deb', () => { + const res = getInstallCommandForPlatform( + 'deb', + 'http://elasticsearch:9200', + { + id: 'es-proxy', + name: 'es-proxy', + url: 'http://es-proxy:1111', + proxy_headers: { + 'X-Forwarded-For': 'forwarded-value', + 'test-header': 'test-value', + }, + is_preconfigured: false, + }, + 'service-token-1', + 'policy-1', + 'http://fleetserver:8220', + undefined, + undefined, + undefined, + { + id: 'download-src', + name: 'download-src', + host: 'https://download-src/8.12.0-test/', + is_default: false, + proxy_id: 'download-proxy', + }, + { + id: 'download-src-proxy', + name: 'download-src-proxy', + url: 'http://download-src-proxy:2222', + proxy_headers: { + 'Accept-Language': 'en-US,en;q=0.5', + 'second-header': 'second-value', + }, + is_preconfigured: false, + } + ); + + expect(res).toMatchInlineSnapshot(` + "curl -L -O https://download-src/8.12.0-test/beats/elastic-agent/elastic-agent--amd64.deb --proxy http://download-src-proxy:2222 --proxy-header \\"Accept-Language=en-US,en;q=0.5\\" --proxy-header \\"second-header=second-value\\" + sudo dpkg -i elastic-agent--amd64.deb + sudo elastic-agent enroll \\\\ + --fleet-server-es=http://elasticsearch:9200 \\\\ + --fleet-server-service-token=service-token-1 \\\\ + --fleet-server-policy=policy-1 \\\\ + --fleet-server-port=8220 \\\\ + --proxy-url=http://es-proxy:1111 \\\\ + --proxy-header=\\"X-Forwarded-For=forwarded-value\\" \\\\ + --proxy-header=\\"test-header=test-value\\" + sudo systemctl enable elastic-agent + sudo systemctl start elastic-agent" + `); + }); + }); }); diff --git a/x-pack/plugins/fleet/public/applications/fleet/components/fleet_server_instructions/utils/install_command_utils.ts b/x-pack/plugins/fleet/public/applications/fleet/components/fleet_server_instructions/utils/install_command_utils.ts index d37587c18d5fd..c2e72358051aa 100644 --- a/x-pack/plugins/fleet/public/applications/fleet/components/fleet_server_instructions/utils/install_command_utils.ts +++ b/x-pack/plugins/fleet/public/applications/fleet/components/fleet_server_instructions/utils/install_command_utils.ts @@ -26,17 +26,24 @@ function getArtifact( const { windows: windowsDownloadSourceProxyArgs, curl: curlDownloadSourceProxyArgs } = getDownloadSourceProxyArgs(downloadSourceProxy); + const appendWindowsDownloadSourceProxyArgs = windowsDownloadSourceProxyArgs + ? ` ${windowsDownloadSourceProxyArgs}` + : ''; + const appendCurlDownloadSourceProxyArgs = curlDownloadSourceProxyArgs + ? ` ${curlDownloadSourceProxyArgs}` + : ''; + const artifactMap: Record = { linux: { downloadCommand: [ - `curl -L -O ${ARTIFACT_BASE_URL}/elastic-agent-${kibanaVersion}-linux-x86_64.tar.gz ${curlDownloadSourceProxyArgs}`, + `curl -L -O ${ARTIFACT_BASE_URL}/elastic-agent-${kibanaVersion}-linux-x86_64.tar.gz${appendCurlDownloadSourceProxyArgs}`, `tar xzvf elastic-agent-${kibanaVersion}-linux-x86_64.tar.gz`, `cd elastic-agent-${kibanaVersion}-linux-x86_64`, ].join(`\n`), }, mac: { downloadCommand: [ - `curl -L -O ${ARTIFACT_BASE_URL}/elastic-agent-${kibanaVersion}-darwin-aarch64.tar.gz ${curlDownloadSourceProxyArgs}`, + `curl -L -O ${ARTIFACT_BASE_URL}/elastic-agent-${kibanaVersion}-darwin-aarch64.tar.gz${appendCurlDownloadSourceProxyArgs}`, `tar xzvf elastic-agent-${kibanaVersion}-darwin-aarch64.tar.gz`, `cd elastic-agent-${kibanaVersion}-darwin-aarch64`, ].join(`\n`), @@ -44,20 +51,20 @@ function getArtifact( windows: { downloadCommand: [ `$ProgressPreference = 'SilentlyContinue'`, - `Invoke-WebRequest -Uri ${ARTIFACT_BASE_URL}/elastic-agent/elastic-agent-${kibanaVersion}-windows-x86_64.zip -OutFile elastic-agent-${kibanaVersion}-windows-x86_64.zip ${windowsDownloadSourceProxyArgs}`, + `Invoke-WebRequest -Uri ${ARTIFACT_BASE_URL}/elastic-agent-${kibanaVersion}-windows-x86_64.zip -OutFile elastic-agent-${kibanaVersion}-windows-x86_64.zip${appendWindowsDownloadSourceProxyArgs}`, `Expand-Archive .\\elastic-agent-${kibanaVersion}-windows-x86_64.zip`, `cd elastic-agent-${kibanaVersion}-windows-x86_64`, ].join(`\n`), }, deb: { downloadCommand: [ - `curl -L -O ${ARTIFACT_BASE_URL}/elastic-agent-${kibanaVersion}-amd64.deb ${curlDownloadSourceProxyArgs}`, + `curl -L -O ${ARTIFACT_BASE_URL}/elastic-agent-${kibanaVersion}-amd64.deb${appendCurlDownloadSourceProxyArgs}`, `sudo dpkg -i elastic-agent-${kibanaVersion}-amd64.deb`, ].join(`\n`), }, rpm: { downloadCommand: [ - `curl -L -O ${ARTIFACT_BASE_URL}/elastic-agent-${kibanaVersion}-x86_64.rpm ${curlDownloadSourceProxyArgs}`, + `curl -L -O ${ARTIFACT_BASE_URL}/elastic-agent-${kibanaVersion}-x86_64.rpm${appendCurlDownloadSourceProxyArgs}`, `sudo rpm -vi elastic-agent-${kibanaVersion}-x86_64.rpm`, ].join(`\n`), }, @@ -71,7 +78,7 @@ function getArtifact( export function getInstallCommandForPlatform( platform: PLATFORM_TYPE, - esOutput: string, + esOutputHost: string, esOutputProxy: FleetProxy | undefined, serviceToken: string, policyId?: string, @@ -92,7 +99,7 @@ export function getInstallCommandForPlatform( commandArguments.push(['url', fleetServerHost]); } - commandArguments.push(['fleet-server-es', esOutput]); + commandArguments.push(['fleet-server-es', esOutputHost]); commandArguments.push(['fleet-server-service-token', serviceToken]); if (policyId) { commandArguments.push(['fleet-server-policy', policyId]); diff --git a/x-pack/plugins/fleet/public/components/enrollment_instructions/manual/index.tsx b/x-pack/plugins/fleet/public/components/enrollment_instructions/manual/index.tsx index 6d8538739c2cd..284cbb088f2c9 100644 --- a/x-pack/plugins/fleet/public/components/enrollment_instructions/manual/index.tsx +++ b/x-pack/plugins/fleet/public/components/enrollment_instructions/manual/index.tsx @@ -37,9 +37,9 @@ export const getDownloadSourceProxyArgs = (downloadSourceProxy?: FleetProxy) => acc.push(`"${proxyKey}"="${proyVal}"`); return acc; }, [] as string[]) - .join(';')}` + .join('; ')}}` : '' - }}`.trim(); + }`.trim(); const curl = `${downloadSourceProxy?.url ? `--proxy ${downloadSourceProxy.url}` : ''} ${ downloadSourceProxy?.proxy_headers ? Object.entries(downloadSourceProxy.proxy_headers) From e1087bcd2f03a65e855df7d062fa95d0d30f0deb Mon Sep 17 00:00:00 2001 From: Jen Huang Date: Tue, 24 Sep 2024 16:18:25 -0700 Subject: [PATCH 07/10] Add api integration tests --- .../fleet_settings_enrollment_response.yaml | 10 ++++ ...use_fleet_server_hosts_for_policy.test.tsx | 52 +++++++++++++++++++ .../settings/enrollment_settings_handler.ts | 18 ++++--- .../apis/settings/enrollment.ts | 45 ++++++++++++++-- .../es_archives/fleet/fleet_server/data.json | 5 +- 5 files changed, 115 insertions(+), 15 deletions(-) diff --git a/x-pack/plugins/fleet/common/openapi/components/schemas/fleet_settings_enrollment_response.yaml b/x-pack/plugins/fleet/common/openapi/components/schemas/fleet_settings_enrollment_response.yaml index 5ba8e5df028bd..8de00dae5c9ea 100644 --- a/x-pack/plugins/fleet/common/openapi/components/schemas/fleet_settings_enrollment_response.yaml +++ b/x-pack/plugins/fleet/common/openapi/components/schemas/fleet_settings_enrollment_response.yaml @@ -23,6 +23,12 @@ properties: type: string download_source_id: type: string + space_ids: + type: array + items: + type: string + data_output_id: + type: string required: - id - name @@ -33,6 +39,10 @@ properties: $ref: ./fleet_server_host.yaml host_proxy: $ref: ./proxies.yaml + es_output: + $ref: ./output_create_request_elasticsearch.yaml + es_output_proxy: + $ref: ./proxies.yaml required: - agent_policies - has_active diff --git a/x-pack/plugins/fleet/public/hooks/use_fleet_server_hosts_for_policy.test.tsx b/x-pack/plugins/fleet/public/hooks/use_fleet_server_hosts_for_policy.test.tsx index 9e6c8ab5c62b1..6f9c96d130ebd 100644 --- a/x-pack/plugins/fleet/public/hooks/use_fleet_server_hosts_for_policy.test.tsx +++ b/x-pack/plugins/fleet/public/hooks/use_fleet_server_hosts_for_policy.test.tsx @@ -42,6 +42,23 @@ describe('useFleetServerHostsForPolicy', () => { is_preconfigured: false, }, has_active: true, + es_output: { + id: 'es-output', + name: 'es-output', + is_default: false, + is_default_monitoring: false, + type: 'elasticsearch', + hosts: ['https://elasticsearch:9200'], + }, + es_output_proxy: { + id: 'es-output-proxy', + name: 'es-output-proxy', + url: 'https://es-output-proxy', + proxy_headers: { + 'header-key': 'header-value', + }, + is_preconfigured: false, + }, }, download_source: { id: 'default-source', @@ -49,6 +66,15 @@ describe('useFleetServerHostsForPolicy', () => { host: 'https://defaultsource', is_default: false, }, + download_source_proxy: { + id: 'download-src-proxy', + name: 'download-src-proxy', + url: 'https://download-src-proxy', + proxy_headers: { + 'header-key': 'header-value', + }, + is_preconfigured: false, + }, }, }); }); @@ -64,12 +90,38 @@ describe('useFleetServerHostsForPolicy', () => { url: 'https://defaultproxy', is_preconfigured: false, }, + esOutput: { + id: 'es-output', + name: 'es-output', + is_default: false, + is_default_monitoring: false, + type: 'elasticsearch', + hosts: ['https://elasticsearch:9200'], + }, + esOutputProxy: { + id: 'es-output-proxy', + name: 'es-output-proxy', + url: 'https://es-output-proxy', + proxy_headers: { + 'header-key': 'header-value', + }, + is_preconfigured: false, + }, downloadSource: { id: 'default-source', name: 'default-source', host: 'https://defaultsource', is_default: false, }, + downloadSourceProxy: { + id: 'download-src-proxy', + name: 'download-src-proxy', + url: 'https://download-src-proxy', + proxy_headers: { + 'header-key': 'header-value', + }, + is_preconfigured: false, + }, }); }); }); diff --git a/x-pack/plugins/fleet/server/routes/settings/enrollment_settings_handler.ts b/x-pack/plugins/fleet/server/routes/settings/enrollment_settings_handler.ts index f2afcd01a236a..69bf95207f82c 100644 --- a/x-pack/plugins/fleet/server/routes/settings/enrollment_settings_handler.ts +++ b/x-pack/plugins/fleet/server/routes/settings/enrollment_settings_handler.ts @@ -110,14 +110,16 @@ export const getEnrollmentSettingsHandler: FleetRequestHandler< // Get associated output and proxy (if any) to use for Fleet Server enrollment try { - const dataOutput = await getDataOutputForAgentPolicy(soClient, scopedAgentPolicy); - if (dataOutput.type === 'elasticsearch' && dataOutput.hosts?.[0]) { - settingsResponse.fleet_server.es_output = dataOutput; - if (dataOutput.proxy_id) { - settingsResponse.fleet_server.es_output_proxy = await getFleetProxy( - soClient, - dataOutput.proxy_id - ); + if (settingsResponse.fleet_server.policies.length > 0) { + const dataOutput = await getDataOutputForAgentPolicy(soClient, scopedAgentPolicy); + if (dataOutput.type === 'elasticsearch' && dataOutput.hosts?.[0]) { + settingsResponse.fleet_server.es_output = dataOutput; + if (dataOutput.proxy_id) { + settingsResponse.fleet_server.es_output_proxy = await getFleetProxy( + soClient, + dataOutput.proxy_id + ); + } } } } catch (e) { diff --git a/x-pack/test/fleet_api_integration/apis/settings/enrollment.ts b/x-pack/test/fleet_api_integration/apis/settings/enrollment.ts index fc464ce0e3ded..d69051b6140a9 100644 --- a/x-pack/test/fleet_api_integration/apis/settings/enrollment.ts +++ b/x-pack/test/fleet_api_integration/apis/settings/enrollment.ts @@ -18,11 +18,8 @@ export default function (providerContext: FtrProviderContext) { describe('Enrollment settings - get', function () { skipIfNoDockerRegistry(providerContext); - before(async () => { - await fleetAndAgents.setup(); - }); - it('should respond with empty enrollment settings on empty cluster', async function () { + await fleetAndAgents.setup(); const response = await supertest .get(`/internal/fleet/settings/enrollment`) .set('kbn-xsrf', 'xxxx') @@ -51,6 +48,7 @@ export default function (providerContext: FtrProviderContext) { .set('kbn-xsrf', 'xxxx') .send({ force: true }) .expect(200); + await fleetAndAgents.setup(); }); after(async () => { await esArchiver.unload('x-pack/test/functional/es_archives/fleet/fleet_server'); @@ -92,12 +90,25 @@ export default function (providerContext: FtrProviderContext) { host_proxy: { id: 'my-proxy', name: 'my proxy', + proxy_headers: { + 'Accept-Encoding': 'gzip, deflate, br', + 'Accept-Language': 'en-US,en;q=0.5', + }, url: 'https://my-proxy', certificate: '', certificate_authorities: '', certificate_key: '', is_preconfigured: false, }, + es_output: { + hosts: ['http://localhost:9200'], + id: 'fleet-default-output', + is_default: true, + is_default_monitoring: true, + name: 'default', + preset: 'balanced', + type: 'elasticsearch', + }, }, download_source: { id: 'fleet-default-download-source', @@ -137,12 +148,25 @@ export default function (providerContext: FtrProviderContext) { host_proxy: { id: 'my-proxy', name: 'my proxy', + proxy_headers: { + 'Accept-Encoding': 'gzip, deflate, br', + 'Accept-Language': 'en-US,en;q=0.5', + }, url: 'https://my-proxy', certificate: '', certificate_authorities: '', certificate_key: '', is_preconfigured: false, }, + es_output: { + hosts: ['http://localhost:9200'], + id: 'fleet-default-output', + is_default: true, + is_default_monitoring: true, + name: 'default', + preset: 'balanced', + type: 'elasticsearch', + }, }, download_source: { id: 'fleet-default-download-source', @@ -178,6 +202,19 @@ export default function (providerContext: FtrProviderContext) { host: 'https://localhost:2222', proxy_id: 'my-proxy', }, + download_source_proxy: { + certificate: '', + certificate_authorities: '', + certificate_key: '', + id: 'my-proxy', + is_preconfigured: false, + name: 'my proxy', + proxy_headers: { + 'Accept-Encoding': 'gzip, deflate, br', + 'Accept-Language': 'en-US,en;q=0.5', + }, + url: 'https://my-proxy', + }, }); }); }); diff --git a/x-pack/test/functional/es_archives/fleet/fleet_server/data.json b/x-pack/test/functional/es_archives/fleet/fleet_server/data.json index a3729885c13e9..32e3735981802 100644 --- a/x-pack/test/functional/es_archives/fleet/fleet_server/data.json +++ b/x-pack/test/functional/es_archives/fleet/fleet_server/data.json @@ -435,12 +435,11 @@ "certificate_key": "", "is_preconfigured": false, "name": "my proxy", - "proxy_headers": null, + "proxy_headers": "{\"Accept-Language\":\"en-US,en;q=0.5\",\"Accept-Encoding\":\"gzip, deflate, br\"}", "url": "https://my-proxy" }, "managed": false, - "references": [ - ], + "references": [], "type": "fleet-proxy", "updated_at": "2024-04-22T22:07:16.226Z" } From 140acf531ba4a70330c669ec68fd0189049d1315 Mon Sep 17 00:00:00 2001 From: Jen Huang Date: Wed, 25 Sep 2024 15:04:15 -0700 Subject: [PATCH 08/10] Change `getInstallCommandForPlatform` arguments to named object properties --- .../steps/install_fleet_server.tsx | 16 +- .../utils/install_command_utils.test.ts | 492 ++++++++---------- .../utils/install_command_utils.ts | 38 +- 3 files changed, 252 insertions(+), 294 deletions(-) diff --git a/x-pack/plugins/fleet/public/applications/fleet/components/fleet_server_instructions/steps/install_fleet_server.tsx b/x-pack/plugins/fleet/public/applications/fleet/components/fleet_server_instructions/steps/install_fleet_server.tsx index feeee0f49b577..a7631deb88b80 100644 --- a/x-pack/plugins/fleet/public/applications/fleet/components/fleet_server_instructions/steps/install_fleet_server.tsx +++ b/x-pack/plugins/fleet/public/applications/fleet/components/fleet_server_instructions/steps/install_fleet_server.tsx @@ -73,19 +73,19 @@ const InstallFleetServerStepContent: React.FunctionComponent<{ const installCommands = (['linux', 'mac', 'windows', 'deb', 'rpm'] as PLATFORM_TYPE[]).reduce( (acc, platform) => { - acc[platform] = getInstallCommandForPlatform( + acc[platform] = getInstallCommandForPlatform({ platform, - esOutput?.hosts?.[0] ?? '', + esOutputHost: esOutput?.hosts?.[0] ?? '', esOutputProxy, - serviceToken ?? '', - fleetServerPolicyId, + serviceToken: serviceToken ?? '', + policyId: fleetServerPolicyId, fleetServerHost, - deploymentMode === 'production', - esOutput?.ca_trusted_fingerprint ?? undefined, + isProductionDeployment: deploymentMode === 'production', + sslCATrustedFingerprint: esOutput?.ca_trusted_fingerprint ?? undefined, kibanaVersion, downloadSource, - downloadSourceProxy - ); + downloadSourceProxy, + }); return acc; }, diff --git a/x-pack/plugins/fleet/public/applications/fleet/components/fleet_server_instructions/utils/install_command_utils.test.ts b/x-pack/plugins/fleet/public/applications/fleet/components/fleet_server_instructions/utils/install_command_utils.test.ts index 0f55de9e36c1e..c86811da2fde7 100644 --- a/x-pack/plugins/fleet/public/applications/fleet/components/fleet_server_instructions/utils/install_command_utils.test.ts +++ b/x-pack/plugins/fleet/public/applications/fleet/components/fleet_server_instructions/utils/install_command_utils.test.ts @@ -10,12 +10,11 @@ import { getInstallCommandForPlatform } from './install_command_utils'; describe('getInstallCommandForPlatform', () => { describe('without policy id', () => { it('should return the correct command if the the policyId is not set for linux', () => { - const res = getInstallCommandForPlatform( - 'linux', - 'http://elasticsearch:9200', - undefined, - 'service-token-1' - ); + const res = getInstallCommandForPlatform({ + platform: 'linux', + esOutputHost: 'http://elasticsearch:9200', + serviceToken: 'service-token-1', + }); expect(res).toMatchInlineSnapshot(` "curl -L -O https://artifacts.elastic.co/downloads/beats/elastic-agent/elastic-agent--linux-x86_64.tar.gz @@ -29,12 +28,11 @@ describe('getInstallCommandForPlatform', () => { }); it('should return the correct command if the the policyId is not set for mac', () => { - const res = getInstallCommandForPlatform( - 'mac', - 'http://elasticsearch:9200', - undefined, - 'service-token-1' - ); + const res = getInstallCommandForPlatform({ + platform: 'mac', + esOutputHost: 'http://elasticsearch:9200', + serviceToken: 'service-token-1', + }); expect(res).toMatchInlineSnapshot(` "curl -L -O https://artifacts.elastic.co/downloads/beats/elastic-agent/elastic-agent--darwin-aarch64.tar.gz @@ -48,12 +46,11 @@ describe('getInstallCommandForPlatform', () => { }); it('should return the correct command if the the policyId is not set for windows', () => { - const res = getInstallCommandForPlatform( - 'windows', - 'http://elasticsearch:9200', - undefined, - 'service-token-1' - ); + const res = getInstallCommandForPlatform({ + platform: 'windows', + esOutputHost: 'http://elasticsearch:9200', + serviceToken: 'service-token-1', + }); expect(res).toMatchInlineSnapshot(` "$ProgressPreference = 'SilentlyContinue' @@ -68,12 +65,11 @@ describe('getInstallCommandForPlatform', () => { }); it('should return the correct command if the the policyId is not set for rpm', () => { - const res = getInstallCommandForPlatform( - 'rpm', - 'http://elasticsearch:9200', - undefined, - 'service-token-1' - ); + const res = getInstallCommandForPlatform({ + platform: 'rpm', + esOutputHost: 'http://elasticsearch:9200', + serviceToken: 'service-token-1', + }); expect(res).toMatchInlineSnapshot(` "curl -L -O https://artifacts.elastic.co/downloads/beats/elastic-agent/elastic-agent--x86_64.rpm @@ -88,12 +84,11 @@ describe('getInstallCommandForPlatform', () => { }); it('should return the correct command if the the policyId is not set for deb', () => { - const res = getInstallCommandForPlatform( - 'deb', - 'http://elasticsearch:9200', - undefined, - 'service-token-1' - ); + const res = getInstallCommandForPlatform({ + platform: 'deb', + esOutputHost: 'http://elasticsearch:9200', + serviceToken: 'service-token-1', + }); expect(res).toMatchInlineSnapshot(` "curl -L -O https://artifacts.elastic.co/downloads/beats/elastic-agent/elastic-agent--amd64.deb @@ -108,16 +103,12 @@ describe('getInstallCommandForPlatform', () => { }); it('should return the correct command sslCATrustedFingerprint option is passed', () => { - const res = getInstallCommandForPlatform( - 'linux', - 'http://elasticsearch:9200', - undefined, - 'service-token-1', - undefined, - undefined, - false, - 'fingerprint123456' - ); + const res = getInstallCommandForPlatform({ + platform: 'linux', + esOutputHost: 'http://elasticsearch:9200', + serviceToken: 'service-token-1', + sslCATrustedFingerprint: 'fingerprint123456', + }); expect(res).toMatchInlineSnapshot(` "curl -L -O https://artifacts.elastic.co/downloads/beats/elastic-agent/elastic-agent--linux-x86_64.tar.gz @@ -134,13 +125,12 @@ describe('getInstallCommandForPlatform', () => { describe('with policy id', () => { it('should return the correct command if the the policyId is set for linux', () => { - const res = getInstallCommandForPlatform( - 'linux', - 'http://elasticsearch:9200', - undefined, - 'service-token-1', - 'policy-1' - ); + const res = getInstallCommandForPlatform({ + platform: 'linux', + esOutputHost: 'http://elasticsearch:9200', + serviceToken: 'service-token-1', + policyId: 'policy-1', + }); expect(res).toMatchInlineSnapshot(` "curl -L -O https://artifacts.elastic.co/downloads/beats/elastic-agent/elastic-agent--linux-x86_64.tar.gz @@ -155,13 +145,12 @@ describe('getInstallCommandForPlatform', () => { }); it('should return the correct command if the the policyId is set for mac', () => { - const res = getInstallCommandForPlatform( - 'mac', - 'http://elasticsearch:9200', - undefined, - 'service-token-1', - 'policy-1' - ); + const res = getInstallCommandForPlatform({ + platform: 'mac', + esOutputHost: 'http://elasticsearch:9200', + serviceToken: 'service-token-1', + policyId: 'policy-1', + }); expect(res).toMatchInlineSnapshot(` "curl -L -O https://artifacts.elastic.co/downloads/beats/elastic-agent/elastic-agent--darwin-aarch64.tar.gz @@ -176,13 +165,12 @@ describe('getInstallCommandForPlatform', () => { }); it('should return the correct command if the the policyId is set for windows', () => { - const res = getInstallCommandForPlatform( - 'windows', - 'http://elasticsearch:9200', - undefined, - 'service-token-1', - 'policy-1' - ); + const res = getInstallCommandForPlatform({ + platform: 'windows', + esOutputHost: 'http://elasticsearch:9200', + serviceToken: 'service-token-1', + policyId: 'policy-1', + }); expect(res).toMatchInlineSnapshot(` "$ProgressPreference = 'SilentlyContinue' @@ -198,13 +186,12 @@ describe('getInstallCommandForPlatform', () => { }); it('should return the correct command if the the policyId is set for rpm', () => { - const res = getInstallCommandForPlatform( - 'rpm', - 'http://elasticsearch:9200', - undefined, - 'service-token-1', - 'policy-1' - ); + const res = getInstallCommandForPlatform({ + platform: 'rpm', + esOutputHost: 'http://elasticsearch:9200', + serviceToken: 'service-token-1', + policyId: 'policy-1', + }); expect(res).toMatchInlineSnapshot(` "curl -L -O https://artifacts.elastic.co/downloads/beats/elastic-agent/elastic-agent--x86_64.rpm @@ -220,13 +207,12 @@ describe('getInstallCommandForPlatform', () => { }); it('should return the correct command if the the policyId is set for deb', () => { - const res = getInstallCommandForPlatform( - 'deb', - 'http://elasticsearch:9200', - undefined, - 'service-token-1', - 'policy-1' - ); + const res = getInstallCommandForPlatform({ + platform: 'deb', + esOutputHost: 'http://elasticsearch:9200', + serviceToken: 'service-token-1', + policyId: 'policy-1', + }); expect(res).toMatchInlineSnapshot(` "curl -L -O https://artifacts.elastic.co/downloads/beats/elastic-agent/elastic-agent--amd64.deb @@ -244,23 +230,18 @@ describe('getInstallCommandForPlatform', () => { describe('with policy id and downloadSource', () => { it('should return the correct command if the the policyId is set for linux', () => { - const res = getInstallCommandForPlatform( - 'linux', - 'http://elasticsearch:9200', - undefined, - 'service-token-1', - 'policy-1', - undefined, - undefined, - undefined, - undefined, - { + const res = getInstallCommandForPlatform({ + platform: 'linux', + esOutputHost: 'http://elasticsearch:9200', + serviceToken: 'service-token-1', + policyId: 'policy-1', + downloadSource: { id: 'test', name: 'test', is_default: false, host: 'https://test.fr/8.12.0-test/', - } - ); + }, + }); expect(res).toMatchInlineSnapshot(` "curl -L -O https://test.fr/8.12.0-test/beats/elastic-agent/elastic-agent--linux-x86_64.tar.gz @@ -277,15 +258,14 @@ describe('getInstallCommandForPlatform', () => { describe('with policy id and fleet server host and production deployment', () => { it('should return the correct command if the the policyId is set for linux', () => { - const res = getInstallCommandForPlatform( - 'linux', - 'http://elasticsearch:9200', - undefined, - 'service-token-1', - 'policy-1', - 'http://fleetserver:8220', - true - ); + const res = getInstallCommandForPlatform({ + platform: 'linux', + esOutputHost: 'http://elasticsearch:9200', + serviceToken: 'service-token-1', + policyId: 'policy-1', + fleetServerHost: 'http://fleetserver:8220', + isProductionDeployment: true, + }); expect(res).toMatchInlineSnapshot(` "curl -L -O https://artifacts.elastic.co/downloads/beats/elastic-agent/elastic-agent--linux-x86_64.tar.gz @@ -304,15 +284,14 @@ describe('getInstallCommandForPlatform', () => { }); it('should return the correct command if the the policyId is set for mac', () => { - const res = getInstallCommandForPlatform( - 'mac', - 'http://elasticsearch:9200', - undefined, - 'service-token-1', - 'policy-1', - 'http://fleetserver:8220', - true - ); + const res = getInstallCommandForPlatform({ + platform: 'mac', + esOutputHost: 'http://elasticsearch:9200', + serviceToken: 'service-token-1', + policyId: 'policy-1', + fleetServerHost: 'http://fleetserver:8220', + isProductionDeployment: true, + }); expect(res).toMatchInlineSnapshot(` "curl -L -O https://artifacts.elastic.co/downloads/beats/elastic-agent/elastic-agent--darwin-aarch64.tar.gz @@ -331,15 +310,14 @@ describe('getInstallCommandForPlatform', () => { }); it('should return the correct command if the the policyId is set for windows', () => { - const res = getInstallCommandForPlatform( - 'windows', - 'http://elasticsearch:9200', - undefined, - 'service-token-1', - 'policy-1', - 'http://fleetserver:8220', - true - ); + const res = getInstallCommandForPlatform({ + platform: 'windows', + esOutputHost: 'http://elasticsearch:9200', + serviceToken: 'service-token-1', + policyId: 'policy-1', + fleetServerHost: 'http://fleetserver:8220', + isProductionDeployment: true, + }); expect(res).toMatchInlineSnapshot(` "$ProgressPreference = 'SilentlyContinue' @@ -359,15 +337,14 @@ describe('getInstallCommandForPlatform', () => { }); it('should return the correct command if the the policyId is set for rpm', () => { - const res = getInstallCommandForPlatform( - 'rpm', - 'http://elasticsearch:9200', - undefined, - 'service-token-1', - 'policy-1', - 'http://fleetserver:8220', - true - ); + const res = getInstallCommandForPlatform({ + platform: 'rpm', + esOutputHost: 'http://elasticsearch:9200', + serviceToken: 'service-token-1', + policyId: 'policy-1', + fleetServerHost: 'http://fleetserver:8220', + isProductionDeployment: true, + }); expect(res).toMatchInlineSnapshot(` "curl -L -O https://artifacts.elastic.co/downloads/beats/elastic-agent/elastic-agent--x86_64.rpm @@ -387,15 +364,14 @@ describe('getInstallCommandForPlatform', () => { }); it('should return the correct command if the the policyId is set for deb', () => { - const res = getInstallCommandForPlatform( - 'deb', - 'http://elasticsearch:9200', - undefined, - 'service-token-1', - 'policy-1', - 'http://fleetserver:8220', - true - ); + const res = getInstallCommandForPlatform({ + platform: 'deb', + esOutputHost: 'http://elasticsearch:9200', + serviceToken: 'service-token-1', + policyId: 'policy-1', + fleetServerHost: 'http://fleetserver:8220', + isProductionDeployment: true, + }); expect(res).toMatchInlineSnapshot(` "curl -L -O https://artifacts.elastic.co/downloads/beats/elastic-agent/elastic-agent--amd64.deb @@ -417,35 +393,32 @@ describe('getInstallCommandForPlatform', () => { describe('with simple proxy settings', () => { it('should return the correct command if proxies are set for linux', () => { - const res = getInstallCommandForPlatform( - 'linux', - 'http://elasticsearch:9200', - { + const res = getInstallCommandForPlatform({ + platform: 'linux', + esOutputHost: 'http://elasticsearch:9200', + esOutputProxy: { id: 'es-proxy', name: 'es-proxy', url: 'http://es-proxy:1111', is_preconfigured: false, }, - 'service-token-1', - 'policy-1', - 'http://fleetserver:8220', - undefined, - undefined, - undefined, - { + serviceToken: 'service-token-1', + policyId: 'policy-1', + fleetServerHost: 'http://fleetserver:8220', + downloadSource: { id: 'download-src', name: 'download-src', host: 'https://download-src/8.12.0-test/', is_default: false, proxy_id: 'download-proxy', }, - { + downloadSourceProxy: { id: 'download-src-proxy', name: 'download-src-proxy', url: 'http://download-src-proxy:2222', is_preconfigured: false, - } - ); + }, + }); expect(res).toMatchInlineSnapshot(` "curl -L -O https://download-src/8.12.0-test/beats/elastic-agent/elastic-agent--linux-x86_64.tar.gz --proxy http://download-src-proxy:2222 @@ -461,35 +434,32 @@ describe('getInstallCommandForPlatform', () => { }); it('should return the correct command if proxies are set for mac', () => { - const res = getInstallCommandForPlatform( - 'mac', - 'http://elasticsearch:9200', - { + const res = getInstallCommandForPlatform({ + platform: 'mac', + esOutputHost: 'http://elasticsearch:9200', + esOutputProxy: { id: 'es-proxy', name: 'es-proxy', url: 'http://es-proxy:1111', is_preconfigured: false, }, - 'service-token-1', - 'policy-1', - 'http://fleetserver:8220', - undefined, - undefined, - undefined, - { + serviceToken: 'service-token-1', + policyId: 'policy-1', + fleetServerHost: 'http://fleetserver:8220', + downloadSource: { id: 'download-src', name: 'download-src', host: 'https://download-src/8.12.0-test/', is_default: false, proxy_id: 'download-proxy', }, - { + downloadSourceProxy: { id: 'download-src-proxy', name: 'download-src-proxy', url: 'http://download-src-proxy:2222', is_preconfigured: false, - } - ); + }, + }); expect(res).toMatchInlineSnapshot(` "curl -L -O https://download-src/8.12.0-test/beats/elastic-agent/elastic-agent--darwin-aarch64.tar.gz --proxy http://download-src-proxy:2222 @@ -505,35 +475,32 @@ describe('getInstallCommandForPlatform', () => { }); it('should return the correct command if proxies are set for windows', () => { - const res = getInstallCommandForPlatform( - 'windows', - 'http://elasticsearch:9200', - { + const res = getInstallCommandForPlatform({ + platform: 'windows', + esOutputHost: 'http://elasticsearch:9200', + esOutputProxy: { id: 'es-proxy', name: 'es-proxy', url: 'http://es-proxy:1111', is_preconfigured: false, }, - 'service-token-1', - 'policy-1', - 'http://fleetserver:8220', - undefined, - undefined, - undefined, - { + serviceToken: 'service-token-1', + policyId: 'policy-1', + fleetServerHost: 'http://fleetserver:8220', + downloadSource: { id: 'download-src', name: 'download-src', host: 'https://download-src/8.12.0-test/', is_default: false, proxy_id: 'download-proxy', }, - { + downloadSourceProxy: { id: 'download-src-proxy', name: 'download-src-proxy', url: 'http://download-src-proxy:2222', is_preconfigured: false, - } - ); + }, + }); expect(res).toMatchInlineSnapshot(` "$ProgressPreference = 'SilentlyContinue' @@ -550,35 +517,32 @@ describe('getInstallCommandForPlatform', () => { }); it('should return the correct command if proxies are set for rpm', () => { - const res = getInstallCommandForPlatform( - 'rpm', - 'http://elasticsearch:9200', - { + const res = getInstallCommandForPlatform({ + platform: 'rpm', + esOutputHost: 'http://elasticsearch:9200', + esOutputProxy: { id: 'es-proxy', name: 'es-proxy', url: 'http://es-proxy:1111', is_preconfigured: false, }, - 'service-token-1', - 'policy-1', - 'http://fleetserver:8220', - undefined, - undefined, - undefined, - { + serviceToken: 'service-token-1', + policyId: 'policy-1', + fleetServerHost: 'http://fleetserver:8220', + downloadSource: { id: 'download-src', name: 'download-src', host: 'https://download-src/8.12.0-test/', is_default: false, proxy_id: 'download-proxy', }, - { + downloadSourceProxy: { id: 'download-src-proxy', name: 'download-src-proxy', url: 'http://download-src-proxy:2222', is_preconfigured: false, - } - ); + }, + }); expect(res).toMatchInlineSnapshot(` "curl -L -O https://download-src/8.12.0-test/beats/elastic-agent/elastic-agent--x86_64.rpm --proxy http://download-src-proxy:2222 @@ -595,35 +559,32 @@ describe('getInstallCommandForPlatform', () => { }); it('should return the correct command if proxies are set for deb', () => { - const res = getInstallCommandForPlatform( - 'deb', - 'http://elasticsearch:9200', - { + const res = getInstallCommandForPlatform({ + platform: 'deb', + esOutputHost: 'http://elasticsearch:9200', + esOutputProxy: { id: 'es-proxy', name: 'es-proxy', url: 'http://es-proxy:1111', is_preconfigured: false, }, - 'service-token-1', - 'policy-1', - 'http://fleetserver:8220', - undefined, - undefined, - undefined, - { + serviceToken: 'service-token-1', + policyId: 'policy-1', + fleetServerHost: 'http://fleetserver:8220', + downloadSource: { id: 'download-src', name: 'download-src', host: 'https://download-src/8.12.0-test/', is_default: false, proxy_id: 'download-proxy', }, - { + downloadSourceProxy: { id: 'download-src-proxy', name: 'download-src-proxy', url: 'http://download-src-proxy:2222', is_preconfigured: false, - } - ); + }, + }); expect(res).toMatchInlineSnapshot(` "curl -L -O https://download-src/8.12.0-test/beats/elastic-agent/elastic-agent--amd64.deb --proxy http://download-src-proxy:2222 @@ -642,10 +603,10 @@ describe('getInstallCommandForPlatform', () => { describe('with full proxy settings', () => { it('should return the correct command if proxies are set for linux', () => { - const res = getInstallCommandForPlatform( - 'linux', - 'http://elasticsearch:9200', - { + const res = getInstallCommandForPlatform({ + platform: 'linux', + esOutputHost: 'http://elasticsearch:9200', + esOutputProxy: { id: 'es-proxy', name: 'es-proxy', url: 'http://es-proxy:1111', @@ -655,20 +616,17 @@ describe('getInstallCommandForPlatform', () => { }, is_preconfigured: false, }, - 'service-token-1', - 'policy-1', - 'http://fleetserver:8220', - undefined, - undefined, - undefined, - { + serviceToken: 'service-token-1', + policyId: 'policy-1', + fleetServerHost: 'http://fleetserver:8220', + downloadSource: { id: 'download-src', name: 'download-src', host: 'https://download-src/8.12.0-test/', is_default: false, proxy_id: 'download-proxy', }, - { + downloadSourceProxy: { id: 'download-src-proxy', name: 'download-src-proxy', url: 'http://download-src-proxy:2222', @@ -677,8 +635,8 @@ describe('getInstallCommandForPlatform', () => { 'second-header': 'second-value', }, is_preconfigured: false, - } - ); + }, + }); expect(res).toMatchInlineSnapshot(` "curl -L -O https://download-src/8.12.0-test/beats/elastic-agent/elastic-agent--linux-x86_64.tar.gz --proxy http://download-src-proxy:2222 --proxy-header \\"Accept-Language=en-US,en;q=0.5\\" --proxy-header \\"second-header=second-value\\" @@ -696,10 +654,10 @@ describe('getInstallCommandForPlatform', () => { }); it('should return the correct command if proxies are set for mac', () => { - const res = getInstallCommandForPlatform( - 'mac', - 'http://elasticsearch:9200', - { + const res = getInstallCommandForPlatform({ + platform: 'mac', + esOutputHost: 'http://elasticsearch:9200', + esOutputProxy: { id: 'es-proxy', name: 'es-proxy', url: 'http://es-proxy:1111', @@ -709,20 +667,17 @@ describe('getInstallCommandForPlatform', () => { }, is_preconfigured: false, }, - 'service-token-1', - 'policy-1', - 'http://fleetserver:8220', - undefined, - undefined, - undefined, - { + serviceToken: 'service-token-1', + policyId: 'policy-1', + fleetServerHost: 'http://fleetserver:8220', + downloadSource: { id: 'download-src', name: 'download-src', host: 'https://download-src/8.12.0-test/', is_default: false, proxy_id: 'download-proxy', }, - { + downloadSourceProxy: { id: 'download-src-proxy', name: 'download-src-proxy', url: 'http://download-src-proxy:2222', @@ -731,8 +686,8 @@ describe('getInstallCommandForPlatform', () => { 'second-header': 'second-value', }, is_preconfigured: false, - } - ); + }, + }); expect(res).toMatchInlineSnapshot(` "curl -L -O https://download-src/8.12.0-test/beats/elastic-agent/elastic-agent--darwin-aarch64.tar.gz --proxy http://download-src-proxy:2222 --proxy-header \\"Accept-Language=en-US,en;q=0.5\\" --proxy-header \\"second-header=second-value\\" @@ -750,10 +705,10 @@ describe('getInstallCommandForPlatform', () => { }); it('should return the correct command if proxies are set for windows', () => { - const res = getInstallCommandForPlatform( - 'windows', - 'http://elasticsearch:9200', - { + const res = getInstallCommandForPlatform({ + platform: 'windows', + esOutputHost: 'http://elasticsearch:9200', + esOutputProxy: { id: 'es-proxy', name: 'es-proxy', url: 'http://es-proxy:1111', @@ -763,20 +718,17 @@ describe('getInstallCommandForPlatform', () => { }, is_preconfigured: false, }, - 'service-token-1', - 'policy-1', - 'http://fleetserver:8220', - undefined, - undefined, - undefined, - { + serviceToken: 'service-token-1', + policyId: 'policy-1', + fleetServerHost: 'http://fleetserver:8220', + downloadSource: { id: 'download-src', name: 'download-src', host: 'https://download-src/8.12.0-test/', is_default: false, proxy_id: 'download-proxy', }, - { + downloadSourceProxy: { id: 'download-src-proxy', name: 'download-src-proxy', url: 'http://download-src-proxy:2222', @@ -785,8 +737,8 @@ describe('getInstallCommandForPlatform', () => { 'second-header': 'second-value', }, is_preconfigured: false, - } - ); + }, + }); expect(res).toMatchInlineSnapshot(` "$ProgressPreference = 'SilentlyContinue' @@ -805,10 +757,10 @@ describe('getInstallCommandForPlatform', () => { }); it('should return the correct command if proxies are set for rpm', () => { - const res = getInstallCommandForPlatform( - 'rpm', - 'http://elasticsearch:9200', - { + const res = getInstallCommandForPlatform({ + platform: 'rpm', + esOutputHost: 'http://elasticsearch:9200', + esOutputProxy: { id: 'es-proxy', name: 'es-proxy', url: 'http://es-proxy:1111', @@ -818,20 +770,17 @@ describe('getInstallCommandForPlatform', () => { }, is_preconfigured: false, }, - 'service-token-1', - 'policy-1', - 'http://fleetserver:8220', - undefined, - undefined, - undefined, - { + serviceToken: 'service-token-1', + policyId: 'policy-1', + fleetServerHost: 'http://fleetserver:8220', + downloadSource: { id: 'download-src', name: 'download-src', host: 'https://download-src/8.12.0-test/', is_default: false, proxy_id: 'download-proxy', }, - { + downloadSourceProxy: { id: 'download-src-proxy', name: 'download-src-proxy', url: 'http://download-src-proxy:2222', @@ -840,8 +789,8 @@ describe('getInstallCommandForPlatform', () => { 'second-header': 'second-value', }, is_preconfigured: false, - } - ); + }, + }); expect(res).toMatchInlineSnapshot(` "curl -L -O https://download-src/8.12.0-test/beats/elastic-agent/elastic-agent--x86_64.rpm --proxy http://download-src-proxy:2222 --proxy-header \\"Accept-Language=en-US,en;q=0.5\\" --proxy-header \\"second-header=second-value\\" @@ -860,10 +809,10 @@ describe('getInstallCommandForPlatform', () => { }); it('should return the correct command if proxies are set for deb', () => { - const res = getInstallCommandForPlatform( - 'deb', - 'http://elasticsearch:9200', - { + const res = getInstallCommandForPlatform({ + platform: 'deb', + esOutputHost: 'http://elasticsearch:9200', + esOutputProxy: { id: 'es-proxy', name: 'es-proxy', url: 'http://es-proxy:1111', @@ -873,20 +822,17 @@ describe('getInstallCommandForPlatform', () => { }, is_preconfigured: false, }, - 'service-token-1', - 'policy-1', - 'http://fleetserver:8220', - undefined, - undefined, - undefined, - { + serviceToken: 'service-token-1', + policyId: 'policy-1', + fleetServerHost: 'http://fleetserver:8220', + downloadSource: { id: 'download-src', name: 'download-src', host: 'https://download-src/8.12.0-test/', is_default: false, proxy_id: 'download-proxy', }, - { + downloadSourceProxy: { id: 'download-src-proxy', name: 'download-src-proxy', url: 'http://download-src-proxy:2222', @@ -895,8 +841,8 @@ describe('getInstallCommandForPlatform', () => { 'second-header': 'second-value', }, is_preconfigured: false, - } - ); + }, + }); expect(res).toMatchInlineSnapshot(` "curl -L -O https://download-src/8.12.0-test/beats/elastic-agent/elastic-agent--amd64.deb --proxy http://download-src-proxy:2222 --proxy-header \\"Accept-Language=en-US,en;q=0.5\\" --proxy-header \\"second-header=second-value\\" diff --git a/x-pack/plugins/fleet/public/applications/fleet/components/fleet_server_instructions/utils/install_command_utils.ts b/x-pack/plugins/fleet/public/applications/fleet/components/fleet_server_instructions/utils/install_command_utils.ts index c2e72358051aa..5656e49de4bc9 100644 --- a/x-pack/plugins/fleet/public/applications/fleet/components/fleet_server_instructions/utils/install_command_utils.ts +++ b/x-pack/plugins/fleet/public/applications/fleet/components/fleet_server_instructions/utils/install_command_utils.ts @@ -76,19 +76,31 @@ function getArtifact( return artifactMap[platform]; } -export function getInstallCommandForPlatform( - platform: PLATFORM_TYPE, - esOutputHost: string, - esOutputProxy: FleetProxy | undefined, - serviceToken: string, - policyId?: string, - fleetServerHost?: string, - isProductionDeployment?: boolean, - sslCATrustedFingerprint?: string, - kibanaVersion?: string, - downloadSource?: DownloadSource, - downloadSourceProxy?: FleetProxy -): string { +export function getInstallCommandForPlatform({ + platform, + esOutputHost, + esOutputProxy, + serviceToken, + policyId, + fleetServerHost, + isProductionDeployment, + sslCATrustedFingerprint, + kibanaVersion, + downloadSource, + downloadSourceProxy, +}: { + platform: PLATFORM_TYPE; + esOutputHost: string; + esOutputProxy?: FleetProxy | undefined; + serviceToken: string; + policyId?: string; + fleetServerHost?: string; + isProductionDeployment?: boolean; + sslCATrustedFingerprint?: string; + kibanaVersion?: string; + downloadSource?: DownloadSource; + downloadSourceProxy?: FleetProxy; +}): string { const newLineSeparator = platform === 'windows' ? '`\n' : '\\\n'; const artifact = getArtifact(platform, kibanaVersion ?? '', downloadSource, downloadSourceProxy); From 75551bdd758f35f731dff5528d557237d541b5c4 Mon Sep 17 00:00:00 2001 From: Jen Huang Date: Wed, 25 Sep 2024 15:05:58 -0700 Subject: [PATCH 09/10] Fix types --- .../page_steps/install_agent/install_agent_standalone.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/create_package_policy_page/multi_page_layout/components/page_steps/install_agent/install_agent_standalone.tsx b/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/create_package_policy_page/multi_page_layout/components/page_steps/install_agent/install_agent_standalone.tsx index 0b9a6f6e85830..2802ac1319b15 100644 --- a/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/create_package_policy_page/multi_page_layout/components/page_steps/install_agent/install_agent_standalone.tsx +++ b/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/create_package_policy_page/multi_page_layout/components/page_steps/install_agent/install_agent_standalone.tsx @@ -52,7 +52,7 @@ export const InstallElasticAgentStandalonePageStep: React.FC Date: Tue, 1 Oct 2024 10:35:47 -0700 Subject: [PATCH 10/10] Use constant for default download uri --- .../components/enrollment_instructions/manual/index.tsx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/x-pack/plugins/fleet/public/components/enrollment_instructions/manual/index.tsx b/x-pack/plugins/fleet/public/components/enrollment_instructions/manual/index.tsx index 284cbb088f2c9..654b3a782649c 100644 --- a/x-pack/plugins/fleet/public/components/enrollment_instructions/manual/index.tsx +++ b/x-pack/plugins/fleet/public/components/enrollment_instructions/manual/index.tsx @@ -5,6 +5,7 @@ * 2.0. */ +import { DEFAULT_DOWNLOAD_SOURCE_URI } from '../../../../common/constants'; import type { DownloadSource, FleetProxy } from '../../../types'; function getfleetServerHostsEnrollArgs( @@ -24,9 +25,8 @@ function getfleetServerHostsEnrollArgs( } export const getDownloadBaseUrl = (downloadSource?: DownloadSource) => { - return downloadSource?.host.endsWith('/') - ? downloadSource.host.substring(0, downloadSource.host.length - 1) - : 'https://artifacts.elastic.co/downloads'; + const source = downloadSource?.host || DEFAULT_DOWNLOAD_SOURCE_URI; + return source.endsWith('/') ? source.substring(0, source.length - 1) : source; }; export const getDownloadSourceProxyArgs = (downloadSourceProxy?: FleetProxy) => {