Skip to content

Commit

Permalink
[Fleet] Disable custom source_uri option on upgrade APIs by default (e…
Browse files Browse the repository at this point in the history
…lastic#123464) (elastic#123600)

(cherry picked from commit 79ff195)

Co-authored-by: Josh Dover <1813008+joshdover@users.noreply.github.com>
  • Loading branch information
kibanamachine and joshdover committed Jan 24, 2022
1 parent 727cf5c commit dea718b
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 1 deletion.
1 change: 1 addition & 0 deletions x-pack/plugins/fleet/common/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export interface FleetConfigType {
agentIdVerificationEnabled?: boolean;
developer?: {
disableRegistryVersionCheck?: boolean;
allowAgentUpgradeSourceUri?: boolean;
};
}

Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/fleet/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ export const config: PluginConfigDescriptor = {
agentIdVerificationEnabled: schema.boolean({ defaultValue: true }),
developer: schema.object({
disableRegistryVersionCheck: schema.boolean({ defaultValue: false }),
allowAgentUpgradeSourceUri: schema.boolean({ defaultValue: false }),
}),
}),
};
Expand Down
10 changes: 10 additions & 0 deletions x-pack/plugins/fleet/server/routes/agent/upgrade_handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export const postAgentUpgradeHandler: RequestHandler<
const kibanaVersion = appContextService.getKibanaVersion();
try {
checkVersionIsSame(version, kibanaVersion);
checkSourceUriAllowed(sourceUri);
} catch (err) {
return response.customError({
statusCode: 400,
Expand Down Expand Up @@ -82,6 +83,7 @@ export const postBulkAgentsUpgradeHandler: RequestHandler<
const kibanaVersion = appContextService.getKibanaVersion();
try {
checkVersionIsSame(version, kibanaVersion);
checkSourceUriAllowed(sourceUri);
} catch (err) {
return response.customError({
statusCode: 400,
Expand Down Expand Up @@ -127,3 +129,11 @@ export const checkVersionIsSame = (version: string, kibanaVersion: string) => {
`cannot upgrade agent to ${versionToUpgradeNumber} because it is different than the installed kibana version ${kibanaVersionNumber}`
);
};

const checkSourceUriAllowed = (sourceUri?: string) => {
if (sourceUri && !appContextService.getConfig()?.developer?.allowAgentUpgradeSourceUri) {
throw new Error(
`source_uri is not allowed or recommended in production. Set xpack.fleet.developer.allowAgentUpgradeSourceUri in kibana.yml to enable.`
);
}
};
52 changes: 51 additions & 1 deletion x-pack/test/fleet_api_integration/apis/agents/upgrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ export default function (providerContext: FtrProviderContext) {
.set('kbn-xsrf', 'xxx')
.send({
version: kibanaVersion,
source_uri: 'http://path/to/download',
})
.expect(200);

Expand Down Expand Up @@ -160,9 +159,23 @@ export default function (providerContext: FtrProviderContext) {
.set('kbn-xsrf', 'xxx')
.send({
version: higherVersion,
})
.expect(400);
});
it('should respond 400 if trying to upgrade with source_uri set', async () => {
const kibanaVersion = await kibanaServer.version.get();
const res = await supertest
.post(`/api/fleet/agents/agent1/upgrade`)
.set('kbn-xsrf', 'xxx')
.send({
version: kibanaVersion,
source_uri: 'http://path/to/download',
})
.expect(400);

expect(res.body.message).to.eql(
`source_uri is not allowed or recommended in production. Set xpack.fleet.developer.allowAgentUpgradeSourceUri in kibana.yml to enable.`
);
});
it('should respond 400 if trying to upgrade an agent that is unenrolling', async () => {
const kibanaVersion = await kibanaServer.version.get();
Expand Down Expand Up @@ -545,6 +558,43 @@ export default function (providerContext: FtrProviderContext) {
.expect(400);
});

it('should respond 400 if trying to bulk upgrade to a version that does not match installed kibana version', async () => {
const kibanaVersion = await kibanaServer.version.get();
await es.update({
id: 'agent1',
refresh: 'wait_for',
index: AGENTS_INDEX,
body: {
doc: {
local_metadata: { elastic: { agent: { upgradeable: true, version: '0.0.0' } } },
},
},
});
await es.update({
id: 'agent2',
refresh: 'wait_for',
index: AGENTS_INDEX,
body: {
doc: {
local_metadata: { elastic: { agent: { upgradeable: true, version: '0.0.0' } } },
},
},
});
const res = await supertest
.post(`/api/fleet/agents/bulk_upgrade`)
.set('kbn-xsrf', 'xxx')
.send({
agents: ['agent1', 'agent2'],
version: kibanaVersion,
source_uri: 'http://path/to/download',
force: true,
})
.expect(400);
expect(res.body.message).to.eql(
`source_uri is not allowed or recommended in production. Set xpack.fleet.developer.allowAgentUpgradeSourceUri in kibana.yml to enable.`
);
});

it('enrolled in a hosted agent policy bulk upgrade should respond with 200 and object of results. Should not update the hosted agent SOs', async () => {
// move agent2 to policy2 to keep it regular
await supertest.put(`/api/fleet/agents/agent2/reassign`).set('kbn-xsrf', 'xxx').send({
Expand Down

0 comments on commit dea718b

Please sign in to comment.