Skip to content

Commit

Permalink
Consider all errors non-fatal during dry runs (#117594)
Browse files Browse the repository at this point in the history
Since we've enabled auto upgrades for some packages in 7.16, we need to
revert a previous change that only considered policy validation errors
non-fatal. Any error encountered during the dry-run/upgrade process
should be considered non-fatal so that users can still access the Fleet
UI if setup fails.

Fixes #116985
  • Loading branch information
kpollich authored Nov 5, 2021
1 parent 55d40a2 commit b7076e8
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 20 deletions.
6 changes: 6 additions & 0 deletions x-pack/plugins/fleet/common/types/rest_spec/package_policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ export type DeletePackagePoliciesResponse = Array<{

export interface UpgradePackagePolicyBaseResponse {
name?: string;

// Support generic errors
statusCode?: number;
body?: {
message: string;
};
}

export interface UpgradePackagePolicyDryRunResponseItem extends UpgradePackagePolicyBaseResponse {
Expand Down
21 changes: 21 additions & 0 deletions x-pack/plugins/fleet/server/routes/package_policy/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ export const deletePackagePolicyHandler: RequestHandler<
}
};

// TODO: Separate the upgrade and dry-run processes into separate endpoints, and address
// duplicate logic in error handling as part of https://github.com/elastic/kibana/issues/63123
export const upgradePackagePolicyHandler: RequestHandler<
unknown,
unknown,
Expand All @@ -212,6 +214,16 @@ export const upgradePackagePolicyHandler: RequestHandler<
);
body.push(result);
}

const firstFatalError = body.find((item) => item.statusCode && item.statusCode !== 200);

if (firstFatalError) {
return response.customError({
statusCode: firstFatalError.statusCode!,
body: { message: firstFatalError.body!.message },
});
}

return response.ok({
body,
});
Expand All @@ -222,6 +234,15 @@ export const upgradePackagePolicyHandler: RequestHandler<
request.body.packagePolicyIds,
{ user }
);

const firstFatalError = body.find((item) => item.statusCode && item.statusCode !== 200);

if (firstFatalError) {
return response.customError({
statusCode: firstFatalError.statusCode!,
body: { message: firstFatalError.body!.message },
});
}
return response.ok({
body,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ export const upgradeManagedPackagePolicies = async (
);

if (dryRunResults.hasErrors) {
const errors = dryRunResults.diff?.[1].errors;
const errors = dryRunResults.diff
? dryRunResults.diff?.[1].errors
: dryRunResults.body?.message;

appContextService
.getLogger()
.error(
Expand Down
11 changes: 0 additions & 11 deletions x-pack/plugins/fleet/server/services/package_policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -620,13 +620,6 @@ class PackagePolicyService {
success: true,
});
} catch (error) {
// We only want to specifically handle validation errors for the new package policy. If a more severe or
// general error is thrown elsewhere during the upgrade process, we want to surface that directly in
// order to preserve any status code mappings, etc that might be included w/ the particular error type
if (!(error instanceof PackagePolicyValidationError)) {
throw error;
}

result.push({
id,
success: false,
Expand Down Expand Up @@ -704,10 +697,6 @@ class PackagePolicyService {
hasErrors,
};
} catch (error) {
if (!(error instanceof PackagePolicyValidationError)) {
throw error;
}

return {
hasErrors: true,
...ingestErrorToResponseOptions(error),
Expand Down
12 changes: 4 additions & 8 deletions x-pack/test/fleet_api_integration/apis/package_policy/upgrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -570,16 +570,14 @@ export default function (providerContext: FtrProviderContext) {

describe('upgrade', function () {
it('fails to upgrade package policy', async function () {
const { body }: { body: UpgradePackagePolicyResponse } = await supertest
await supertest
.post(`/api/fleet/package_policies/upgrade`)
.set('kbn-xsrf', 'xxxx')
.send({
packagePolicyIds: [packagePolicyId],
dryRun: false,
})
.expect(200);

expect(body[0].success).to.be(false);
.expect(400);
});
});
});
Expand Down Expand Up @@ -672,16 +670,14 @@ export default function (providerContext: FtrProviderContext) {

describe('upgrade', function () {
it('fails to upgrade package policy', async function () {
const { body }: { body: UpgradePackagePolicyResponse } = await supertest
await supertest
.post(`/api/fleet/package_policies/upgrade`)
.set('kbn-xsrf', 'xxxx')
.send({
packagePolicyIds: [packagePolicyId],
dryRun: false,
})
.expect(200);

expect(body[0].success).to.be(false);
.expect(400);
});
});
});
Expand Down

0 comments on commit b7076e8

Please sign in to comment.