Skip to content

Commit

Permalink
[Observability Onboarding] Fix auto-detect flow fails with tar error (e…
Browse files Browse the repository at this point in the history
…lastic#194002)

Resolves [elastic#193284](elastic#193284)

- Fixes an issue where auto-detect would fail on GNU tar.
- Also changes the behaviour of the integration installation step to
only throw an error when all integrations fail to install.
  • Loading branch information
thomheymann authored Sep 27, 2024
1 parent 6fd1913 commit 7d395ad
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -289,16 +289,16 @@ apply_elastic_agent_config() {
local decoded_ingest_api_key=$(echo "$ingest_api_key_encoded" | base64 -d)

# Verify that the downloaded archive contains the expected `elastic-agent.yml` file
tar --list --file "$elastic_agent_tmp_config_path" --include 'elastic-agent.yml' >/dev/null &&
tar --list --file "$elastic_agent_tmp_config_path" | grep "$(basename "$elastic_agent_config_path")" >/dev/null &&
# Remove existing config file including `inputs.d` directory
rm -rf "$elastic_agent_config_path" "$(dirname "$elastic_agent_config_path")/inputs.d" &&
# Extract new config files from downloaded archive
tar --extract --file "$elastic_agent_tmp_config_path" --include 'elastic-agent.yml' --include 'inputs.d/*.yml' --directory "$(dirname "$elastic_agent_config_path")" &&
tar --extract --file "$elastic_agent_tmp_config_path" --directory "$(dirname "$elastic_agent_config_path")" &&
# Replace placeholder with the Ingest API key
sed -i '' "s/\${API_KEY}/$decoded_ingest_api_key/" "$elastic_agent_config_path"
sed -i='' "s/\${API_KEY}/$decoded_ingest_api_key/" "$elastic_agent_config_path"
if [ "$?" -eq 0 ]; then
printf "\e[1;32m✓\e[0m %s\n" "Config written to:"
tar --list --file "$elastic_agent_tmp_config_path" --include 'elastic-agent.yml' --include 'inputs.d/*.yml' | while read -r file; do
tar --list --file "$elastic_agent_tmp_config_path" | grep '\.yml$' | while read -r file; do
echo " - $(dirname "$elastic_agent_config_path")/$file"
done

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,9 +263,12 @@ const createFlowRoute = createObservabilityOnboardingServerRoute({
*
* The request format is TSV (tab-separated values) to simplify parsing in bash.
*
* The response format is either a YAML file or a tarball containing the Elastic Agent
* The response format is either a YAML file or a tar archive containing the Elastic Agent
* configuration, depending on the `Accept` header.
*
* Errors during installation are ignore unless all integrations fail to install. When that happens
* a 500 Internal Server Error is returned with the first error message.
*
* Example request:
*
* ```text
Expand Down Expand Up @@ -335,10 +338,21 @@ const integrationsInstallRoute = createObservabilityOnboardingServerRoute({

let installedIntegrations: InstalledIntegration[] = [];
try {
installedIntegrations = await ensureInstalledIntegrations(
const settledResults = await ensureInstalledIntegrations(
integrationsToInstall,
packageClient
);
installedIntegrations = settledResults.reduce<InstalledIntegration[]>((acc, result) => {
if (result.status === 'fulfilled') {
acc.push(result.value);
}
return acc;
}, []);
// Errors during installation are ignore unless all integrations fail to install. When that happens
// a 500 Internal Server Error is returned with the first error message.
if (!installedIntegrations.length) {
throw (settledResults[0] as PromiseRejectedResult).reason;
}
} catch (error) {
if (error instanceof FleetUnauthorizedError) {
return response.forbidden({
Expand Down Expand Up @@ -401,8 +415,8 @@ export type IntegrationToInstall = RegistryIntegrationToInstall | CustomIntegrat
async function ensureInstalledIntegrations(
integrationsToInstall: IntegrationToInstall[],
packageClient: PackageClient
): Promise<InstalledIntegration[]> {
return Promise.all(
): Promise<Array<PromiseSettledResult<InstalledIntegration>>> {
return Promise.allSettled(
integrationsToInstall.map(async (integration) => {
const { pkgName, installSource } = integration;

Expand Down

0 comments on commit 7d395ad

Please sign in to comment.