-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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] flag package policy SO to trigger agent policy bump #200536
Changes from 5 commits
c46cff2
0985863
7cd7cf5
33e2a37
65eee0f
600590a
58c89a9
409bbf7
32dc285
92a2a62
6704eb9
3409529
2878cbd
eeb2c54
9daf046
2681c7c
26e46d4
4ce6b39
d8c9711
e031878
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { loggingSystemMock } from '@kbn/core/server/mocks'; | ||
|
||
import { agentPolicyService } from '../agent_policy'; | ||
|
||
import { packagePolicyService } from '../package_policy'; | ||
import type { PackagePolicy } from '../../types'; | ||
|
||
import { _updatePackagePoliciesThatNeedBump } from './bump_agent_policies_task'; | ||
|
||
jest.mock('../app_context'); | ||
jest.mock('../agent_policy'); | ||
jest.mock('../package_policy'); | ||
|
||
const mockedAgentPolicyService = jest.mocked(agentPolicyService); | ||
const mockedPackagePolicyService = jest.mocked(packagePolicyService); | ||
|
||
describe('_updatePackagePoliciesThatNeedBump', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
mockedPackagePolicyService.list.mockResolvedValueOnce({ | ||
total: 1, | ||
items: [ | ||
{ | ||
id: 'packagePolicy1', | ||
bump_agent_policy_revision: true, | ||
} as PackagePolicy, | ||
], | ||
page: 1, | ||
perPage: 100, | ||
}); | ||
mockedPackagePolicyService.list.mockResolvedValueOnce({ | ||
total: 0, | ||
items: [], | ||
page: 1, | ||
perPage: 100, | ||
}); | ||
}); | ||
|
||
it('should update package policy if bump agent policy revision needed', async () => { | ||
const logger = loggingSystemMock.createLogger(); | ||
|
||
await _updatePackagePoliciesThatNeedBump(logger); | ||
|
||
expect(mockedPackagePolicyService.bulkUpdate).toHaveBeenCalledWith(undefined, undefined, [ | ||
{ bump_agent_policy_revision: false, id: 'packagePolicy1' }, | ||
]); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
import type { Logger } from '@kbn/core/server'; | ||
import type { | ||
ConcreteTaskInstance, | ||
TaskManagerSetupContract, | ||
TaskManagerStartContract, | ||
} from '@kbn/task-manager-plugin/server'; | ||
import { v4 as uuidv4 } from 'uuid'; | ||
|
||
import { appContextService, packagePolicyService } from '..'; | ||
import { runWithCache } from '../epm/packages/cache'; | ||
|
||
const TASK_TYPE = 'fleet:bump_agent_policies'; | ||
const BATCH_SIZE = 100; | ||
export function registerBumpAgentPoliciesTask(taskManagerSetup: TaskManagerSetupContract) { | ||
taskManagerSetup.registerTaskDefinitions({ | ||
[TASK_TYPE]: { | ||
title: 'Fleet Bump policies', | ||
timeout: '5m', | ||
maxAttempts: 3, | ||
createTaskRunner: ({ taskInstance }: { taskInstance: ConcreteTaskInstance }) => { | ||
let cancelled = false; | ||
return { | ||
async run() { | ||
if (cancelled) { | ||
throw new Error('Task has been cancelled'); | ||
} | ||
|
||
await runWithCache(async () => { | ||
await _updatePackagePoliciesThatNeedBump(appContextService.getLogger()); | ||
|
||
// TODO agent policies | ||
}); | ||
}, | ||
async cancel() { | ||
cancelled = true; | ||
}, | ||
}; | ||
}, | ||
}, | ||
}); | ||
} | ||
|
||
async function getPackagePoliciesToBump() { | ||
return await packagePolicyService.list(appContextService.getInternalUserSOClient(), { | ||
kuery: 'ingest-package-policies.bump_agent_policy_revision:true', | ||
perPage: BATCH_SIZE, | ||
}); | ||
} | ||
|
||
export async function _updatePackagePoliciesThatNeedBump(logger: Logger) { | ||
// TODO spaces? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we probably need to use the SO for the correct space here |
||
let packagePoliciesToBump = await getPackagePoliciesToBump(); | ||
|
||
logger.info( | ||
`Found ${packagePoliciesToBump.total} package policies that need agent policy revision bump` | ||
); | ||
|
||
while (packagePoliciesToBump.total > 0) { | ||
const start = Date.now(); | ||
// resetting the flag will trigger a revision bump | ||
await packagePolicyService.bulkUpdate( | ||
appContextService.getInternalUserSOClient(), | ||
appContextService.getInternalUserESClient(), | ||
packagePoliciesToBump.items.map((item) => ({ | ||
...item, | ||
bump_agent_policy_revision: false, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The flag has to be set to false, otherwise an update will happen on every Fleet setup. |
||
})) | ||
); | ||
const updatedCount = packagePoliciesToBump.items.length; | ||
|
||
packagePoliciesToBump = await getPackagePoliciesToBump(); | ||
logger.debug( | ||
`Updated ${updatedCount} package policies in ${Date.now() - start}ms, ${ | ||
packagePoliciesToBump.total | ||
} remaining` | ||
); | ||
} | ||
} | ||
|
||
export async function scheduleBumpAgentPoliciesTask(taskManagerStart: TaskManagerStartContract) { | ||
await taskManagerStart.ensureScheduled({ | ||
id: `${TASK_TYPE}:${uuidv4()}`, | ||
scope: ['fleet'], | ||
params: {}, | ||
taskType: TASK_TYPE, | ||
runAt: new Date(Date.now() + 3 * 1000), | ||
state: {}, | ||
}); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In order to query package policies from all spaces, we need to query with soClient for each space. For this, we need to query all spaces first.
I think similarly the deploy policies task doesn't work correctly, because the logic only queries agent policies from the default space: https://github.com/elastic/kibana/blob/main/x-pack/plugins/fleet/server/services/setup/fleet_server_policies_enrollment_keys.ts#L35
I'll query from all spaces like this:
kibana/x-pack/plugins/fleet/server/services/agent_policy.ts
Lines 920 to 927 in b3f27a9