Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fleet] Disable custom source_uri option on upgrade APIs by default #123464

Merged
merged 4 commits into from
Jan 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -96,6 +96,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