Skip to content

Commit 1005405

Browse files
committed
[Ingest Manager] Update open API spec and add condition to agent upgrade endpoint (#79579)
* add condition to check for upgradeable agent to endpoint * update the openAPI spec
1 parent e754fb7 commit 1005405

3 files changed

Lines changed: 288 additions & 3 deletions

File tree

x-pack/plugins/ingest_manager/common/openapi/spec_oas3.json

Lines changed: 246 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3520,6 +3520,172 @@
35203520
}
35213521
}
35223522
},
3523+
"/fleet/agents/{agentId}/upgrade": {
3524+
"parameters": [
3525+
{
3526+
"schema": {
3527+
"type": "string"
3528+
},
3529+
"name": "agentId",
3530+
"in": "path",
3531+
"required": true
3532+
}
3533+
],
3534+
"post": {
3535+
"summary": "Fleet - Agent - Upgrade",
3536+
"tags": [],
3537+
"responses": {
3538+
"200": {
3539+
"description": "OK",
3540+
"content": {
3541+
"application/json": {
3542+
"schema": {
3543+
"$ref": "#/components/schemas/UpgradeAgent"
3544+
},
3545+
"examples": {
3546+
"success": {
3547+
"value": {}
3548+
}
3549+
}
3550+
}
3551+
}
3552+
},
3553+
"400": {
3554+
"description": "BAD REQUEST",
3555+
"content": {
3556+
"application/json": {
3557+
"schema": {
3558+
"$ref": "#/components/schemas/UpgradeAgent"
3559+
},
3560+
"examples": {
3561+
"bad request not upgradeable": {
3562+
"value": {
3563+
"statusCode": 400,
3564+
"error": "Bad Request",
3565+
"message": "agent d133b07d-5c2b-42f0-8e6b-bbae53bdce88 is not upgradeable"
3566+
}
3567+
},
3568+
"bad request kibana version": {
3569+
"value": {
3570+
"statusCode": 400,
3571+
"error": "Bad Request",
3572+
"message": "cannot upgrade agent to 8.0.0 because it is different than the installed kibana version 7.9.10"
3573+
}
3574+
},
3575+
"bad request agent unenrolling": {
3576+
"value": {
3577+
"statusCode": 400,
3578+
"error": "Bad Request",
3579+
"message": "cannot upgrade an unenrolling or unenrolled agent"
3580+
}
3581+
}
3582+
}
3583+
}
3584+
}
3585+
}
3586+
},
3587+
"operationId": "post-fleet-agents-upgrade",
3588+
"parameters": [
3589+
{
3590+
"$ref": "#/components/parameters/xsrfHeader"
3591+
}
3592+
],
3593+
"requestBody": {
3594+
"required": true,
3595+
"content": {
3596+
"application/json": {
3597+
"schema": {
3598+
"$ref": "#/components/schemas/UpgradeAgent"
3599+
},
3600+
"examples":{
3601+
"version":{
3602+
"value": {
3603+
"version": "8.0.0"
3604+
}
3605+
},
3606+
"version and source_uri":{
3607+
"value": {
3608+
"version": "8.0.0",
3609+
"source_uri": "http://localhost:8000"
3610+
}
3611+
}
3612+
}
3613+
}
3614+
}
3615+
}
3616+
}
3617+
},
3618+
"/fleet/agents/bulk_upgrade": {
3619+
"post": {
3620+
"summary": "Fleet - Agent - Bulk Upgrade",
3621+
"tags": [],
3622+
"responses": {
3623+
"200": {
3624+
"description": "OK",
3625+
"content": {
3626+
"application/json": {
3627+
"schema": {
3628+
"$ref": "#/components/schemas/BulkUpgradeAgents"
3629+
},
3630+
"examples": {
3631+
"success": {
3632+
"value": {}
3633+
}
3634+
}
3635+
}
3636+
}
3637+
},
3638+
"400": {
3639+
"description": "BAD REQUEST",
3640+
"content": {
3641+
"application/json": {
3642+
"schema": {
3643+
"$ref": "#/components/schemas/UpgradeAgent"
3644+
},
3645+
"examples": {
3646+
"bad request kibana version": {
3647+
"value": {
3648+
"statusCode": 400,
3649+
"error": "Bad Request",
3650+
"message": "cannot upgrade agent to 8.0.0 because it is different than the installed kibana version 7.9.10"
3651+
}
3652+
}
3653+
}
3654+
}
3655+
}
3656+
}
3657+
},
3658+
"operationId": "post-fleet-agents-bulk-upgrade",
3659+
"parameters": [
3660+
{
3661+
"$ref": "#/components/parameters/xsrfHeader"
3662+
}
3663+
],
3664+
"requestBody": {
3665+
"required": true,
3666+
"content": {
3667+
"application/json": {
3668+
"schema": {
3669+
"$ref": "#/components/schemas/BulkUpgradeAgents"
3670+
},
3671+
"examples":{
3672+
"version":{
3673+
"value": {
3674+
"version": "8.0.0"
3675+
}
3676+
},
3677+
"version and source_uri":{
3678+
"value": {
3679+
"version": "8.0.0",
3680+
"source_uri": "http://localhost:8000"
3681+
}
3682+
}
3683+
}
3684+
}
3685+
}
3686+
}
3687+
}
3688+
},
35233689
"/fleet/agent-status": {
35243690
"get": {
35253691
"summary": "Fleet - Agent - Status for policy",
@@ -4226,8 +4392,88 @@
42264392
"type": "string",
42274393
"title": "EnrollmentApiKey",
42284394
"format": "byte"
4395+
},
4396+
"UpgradeAgent":{
4397+
"title": "UpgradeAgent",
4398+
"oneOf": [
4399+
{
4400+
"type": "object",
4401+
"properties": {
4402+
"version": {
4403+
"type": "string"
4404+
}
4405+
},
4406+
"required": ["version"]
4407+
},
4408+
{
4409+
"type": "object",
4410+
"properties": {
4411+
"version": {
4412+
"type": "string"
4413+
},
4414+
"source_uri": {
4415+
"type": "string"
4416+
}
4417+
},
4418+
"required": ["version"]
4419+
}
4420+
]
4421+
},
4422+
"BulkUpgradeAgents":{
4423+
"title": "BulkUpgradeAgents",
4424+
"oneOf": [
4425+
{
4426+
"type": "object",
4427+
"properties": {
4428+
"version": {
4429+
"type": "string"
4430+
},
4431+
"agents":{
4432+
"type": "array",
4433+
"items":{
4434+
"type": "string"
4435+
}
4436+
}
4437+
},
4438+
"required": ["version", "agents"]
4439+
},
4440+
{
4441+
"type": "object",
4442+
"properties": {
4443+
"version": {
4444+
"type": "string"
4445+
},
4446+
"source_uri": {
4447+
"type": "string"
4448+
},
4449+
"agents":{
4450+
"type": "array",
4451+
"items":{
4452+
"type": "string"
4453+
}
4454+
}
4455+
},
4456+
"required": ["version", "agents"]
4457+
},
4458+
{
4459+
"type": "object",
4460+
"properties": {
4461+
"version": {
4462+
"type": "string"
4463+
},
4464+
"source_uri": {
4465+
"type": "string"
4466+
},
4467+
"agents":{
4468+
"type": "string"
4469+
}
4470+
},
4471+
"required": ["version", "agents"]
4472+
}
4473+
]
42294474
}
42304475
},
4476+
42314477
"parameters": {
42324478
"pageSizeParam": {
42334479
"name": "perPage",

x-pack/plugins/ingest_manager/server/routes/agent/upgrade_handler.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import * as AgentService from '../../services/agents';
1616
import { appContextService } from '../../services';
1717
import { defaultIngestErrorHandler } from '../../errors';
1818
import { AGENT_SAVED_OBJECT_TYPE } from '../../constants';
19+
import { savedObjectToAgent } from '../../services/agents/saved_objects';
20+
import { isAgentUpgradeable } from '../../../common/services';
1921

2022
export const postAgentUpgradeHandler: RequestHandler<
2123
TypeOf<typeof PostAgentUpgradeRequestSchema.params>,
@@ -35,15 +37,25 @@ export const postAgentUpgradeHandler: RequestHandler<
3537
},
3638
});
3739
}
38-
const agent = await soClient.get<AgentSOAttributes>(
40+
const agentSO = await soClient.get<AgentSOAttributes>(
3941
AGENT_SAVED_OBJECT_TYPE,
4042
request.params.agentId
4143
);
42-
if (agent.attributes.unenrollment_started_at || agent.attributes.unenrolled_at) {
44+
if (agentSO.attributes.unenrollment_started_at || agentSO.attributes.unenrolled_at) {
4345
return response.customError({
4446
statusCode: 400,
4547
body: {
46-
message: `cannot upgrade an unenrolling or unenrolled agent`,
48+
message: 'cannot upgrade an unenrolling or unenrolled agent',
49+
},
50+
});
51+
}
52+
53+
const agent = savedObjectToAgent(agentSO);
54+
if (!isAgentUpgradeable(agent, kibanaVersion)) {
55+
return response.customError({
56+
statusCode: 400,
57+
body: {
58+
message: `agent ${request.params.agentId} is not upgradeable`,
4759
},
4860
});
4961
}

x-pack/test/ingest_manager_api_integration/apis/fleet/agents/upgrade.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ export default function (providerContext: FtrProviderContext) {
2929

3030
it('should respond 200 to upgrade agent and update the agent SO', async () => {
3131
const kibanaVersion = await kibanaServer.version.get();
32+
await kibanaServer.savedObjects.update({
33+
id: 'agent1',
34+
type: AGENT_SAVED_OBJECT_TYPE,
35+
attributes: {
36+
local_metadata: { elastic: { agent: { upgradeable: true, version: '0.0.0' } } },
37+
},
38+
});
3239
await supertest
3340
.post(`/api/ingest_manager/fleet/agents/agent1/upgrade`)
3441
.set('kbn-xsrf', 'xxx')
@@ -45,6 +52,13 @@ export default function (providerContext: FtrProviderContext) {
4552
});
4653
it('should respond 200 to upgrade agent and update the agent SO without source_uri', async () => {
4754
const kibanaVersion = await kibanaServer.version.get();
55+
await kibanaServer.savedObjects.update({
56+
id: 'agent1',
57+
type: AGENT_SAVED_OBJECT_TYPE,
58+
attributes: {
59+
local_metadata: { elastic: { agent: { upgradeable: true, version: '0.0.0' } } },
60+
},
61+
});
4862
await supertest
4963
.post(`/api/ingest_manager/fleet/agents/agent1/upgrade`)
5064
.set('kbn-xsrf', 'xxx')
@@ -102,8 +116,21 @@ export default function (providerContext: FtrProviderContext) {
102116
.expect(400);
103117
});
104118

119+
it('should respond 400 if trying to upgrade an agent that is not upgradeable', async () => {
120+
const kibanaVersion = await kibanaServer.version.get();
121+
const res = await supertest
122+
.post(`/api/ingest_manager/fleet/agents/agent1/upgrade`)
123+
.set('kbn-xsrf', 'xxx')
124+
.send({
125+
version: kibanaVersion,
126+
})
127+
.expect(400);
128+
expect(res.body.message).to.equal('agent agent1 is not upgradeable');
129+
});
130+
105131
it('should respond 200 to bulk upgrade agents and update the agent SOs', async () => {
106132
const kibanaVersion = await kibanaServer.version.get();
133+
107134
await supertest
108135
.post(`/api/ingest_manager/fleet/agents/bulk_upgrade`)
109136
.set('kbn-xsrf', 'xxx')

0 commit comments

Comments
 (0)