Skip to content

Commit

Permalink
[Fleet] add force flag to upgrade agents api (#82178)
Browse files Browse the repository at this point in the history
* add force flag to upgrade agents api

* use 1.0.0 as version that wont match to see if test passes
  • Loading branch information
neptunian authored Nov 2, 2020
1 parent 9d4adb2 commit f2bf8c9
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const postAgentUpgradeHandler: RequestHandler<
TypeOf<typeof PostAgentUpgradeRequestSchema.body>
> = async (context, request, response) => {
const soClient = context.core.savedObjects.client;
const { version, source_uri: sourceUri } = request.body;
const { version, source_uri: sourceUri, force } = request.body;
const kibanaVersion = appContextService.getKibanaVersion();
try {
checkVersionIsSame(version, kibanaVersion);
Expand All @@ -53,7 +53,7 @@ export const postAgentUpgradeHandler: RequestHandler<
}

const agent = savedObjectToAgent(agentSO);
if (!isAgentUpgradeable(agent, kibanaVersion)) {
if (!force && !isAgentUpgradeable(agent, kibanaVersion)) {
return response.customError({
statusCode: 400,
body: {
Expand Down Expand Up @@ -83,7 +83,7 @@ export const postBulkAgentsUpgradeHandler: RequestHandler<
TypeOf<typeof PostBulkAgentUpgradeRequestSchema.body>
> = async (context, request, response) => {
const soClient = context.core.savedObjects.client;
const { version, source_uri: sourceUri, agents } = request.body;
const { version, source_uri: sourceUri, agents, force } = request.body;
const kibanaVersion = appContextService.getKibanaVersion();
try {
checkVersionIsSame(version, kibanaVersion);
Expand All @@ -102,12 +102,14 @@ export const postBulkAgentsUpgradeHandler: RequestHandler<
agentIds: agents,
sourceUri,
version,
force,
});
} else {
await AgentService.sendUpgradeAgentsActions(soClient, {
kuery: agents,
sourceUri,
version,
force,
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,13 @@ export async function sendUpgradeAgentsActions(
agentIds: string[];
sourceUri: string | undefined;
version: string;
force?: boolean;
}
| {
kuery: string;
sourceUri: string | undefined;
version: string;
force?: boolean;
}
) {
const kibanaVersion = appContextService.getKibanaVersion();
Expand All @@ -82,7 +84,9 @@ export async function sendUpgradeAgentsActions(
showInactive: false,
})
).agents;
const agentsToUpdate = agents.filter((agent) => isAgentUpgradeable(agent, kibanaVersion));
const agentsToUpdate = options.force
? agents
: agents.filter((agent) => isAgentUpgradeable(agent, kibanaVersion));
const now = new Date().toISOString();
const data = {
version: options.version,
Expand Down
2 changes: 2 additions & 0 deletions x-pack/plugins/ingest_manager/server/types/rest_spec/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ export const PostAgentUpgradeRequestSchema = {
body: schema.object({
source_uri: schema.maybe(schema.string()),
version: schema.string(),
force: schema.maybe(schema.boolean()),
}),
};

Expand All @@ -196,6 +197,7 @@ export const PostBulkAgentUpgradeRequestSchema = {
agents: schema.oneOf([schema.arrayOf(schema.string()), schema.string()]),
source_uri: schema.maybe(schema.string()),
version: schema.string(),
force: schema.maybe(schema.boolean()),
}),
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,25 @@ export default function (providerContext: FtrProviderContext) {
})
.expect(400);
});
it('should respond 200 if upgrading agent with version the same as snapshot version and force flag is passed', async () => {
const kibanaVersion = await kibanaServer.version.get();
const kibanaVersionSnapshot = makeSnapshotVersion(kibanaVersion);
await kibanaServer.savedObjects.update({
id: 'agent1',
type: AGENT_SAVED_OBJECT_TYPE,
attributes: {
local_metadata: { elastic: { agent: { upgradeable: true, version: kibanaVersion } } },
},
});
await supertest
.post(`/api/fleet/agents/agent1/upgrade`)
.set('kbn-xsrf', 'xxx')
.send({
version: kibanaVersionSnapshot,
force: true,
})
.expect(200);
});
it('should respond 200 if upgrading agent with version less than kibana snapshot version', async () => {
const kibanaVersion = await kibanaServer.version.get();
const kibanaVersionSnapshot = makeSnapshotVersion(kibanaVersion);
Expand Down Expand Up @@ -340,5 +359,72 @@ export default function (providerContext: FtrProviderContext) {
expect(typeof agent2data.body.item.upgrade_started_at).to.be('undefined');
expect(typeof agent3data.body.item.upgrade_started_at).to.be('undefined');
});
it('should upgrade a non upgradeable agent during bulk_upgrade with force flag', async () => {
const kibanaVersion = await kibanaServer.version.get();
await kibanaServer.savedObjects.update({
id: 'agent1',
type: AGENT_SAVED_OBJECT_TYPE,
attributes: {
local_metadata: { elastic: { agent: { upgradeable: true, version: '0.0.0' } } },
},
});
await kibanaServer.savedObjects.update({
id: 'agent2',
type: AGENT_SAVED_OBJECT_TYPE,
attributes: {
local_metadata: {
elastic: { agent: { upgradeable: true, version: semver.inc(kibanaVersion, 'patch') } },
},
},
});
await kibanaServer.savedObjects.update({
id: 'agent3',
type: AGENT_SAVED_OBJECT_TYPE,
attributes: {
local_metadata: { elastic: { agent: { upgradeable: false, version: '0.0.0' } } },
},
});
await supertest
.post(`/api/fleet/agents/bulk_upgrade`)
.set('kbn-xsrf', 'xxx')
.send({
agents: ['agent1', 'agent2', 'agent3'],
version: kibanaVersion,
force: true,
});
const [agent1data, agent2data, agent3data] = await Promise.all([
supertest.get(`/api/fleet/agents/agent1`).set('kbn-xsrf', 'xxx'),
supertest.get(`/api/fleet/agents/agent2`).set('kbn-xsrf', 'xxx'),
supertest.get(`/api/fleet/agents/agent3`).set('kbn-xsrf', 'xxx'),
]);
expect(typeof agent1data.body.item.upgrade_started_at).to.be('string');
expect(typeof agent2data.body.item.upgrade_started_at).to.be('string');
expect(typeof agent3data.body.item.upgrade_started_at).to.be('string');
});
it('should respond 400 if trying to bulk upgrade to a version that does not match installed kibana version', async () => {
await kibanaServer.savedObjects.update({
id: 'agent1',
type: AGENT_SAVED_OBJECT_TYPE,
attributes: {
local_metadata: { elastic: { agent: { upgradeable: true, version: '0.0.0' } } },
},
});
await kibanaServer.savedObjects.update({
id: 'agent2',
type: AGENT_SAVED_OBJECT_TYPE,
attributes: {
local_metadata: { elastic: { agent: { upgradeable: true, version: '0.0.0' } } },
},
});
await supertest
.post(`/api/fleet/agents/bulk_upgrade`)
.set('kbn-xsrf', 'xxx')
.send({
agents: ['agent1', 'agent2'],
version: '1.0.0',
force: true,
})
.expect(400);
});
});
}

0 comments on commit f2bf8c9

Please sign in to comment.