-
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] Ensure policies are not out of sync #175065
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
/* | ||
* 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 { elasticsearchServiceMock, savedObjectsClientMock } from '@kbn/core/server/mocks'; | ||
|
||
import { appContextService } from '../app_context'; | ||
import { agentPolicyService } from '../agent_policy'; | ||
import { ensureDefaultEnrollmentAPIKeyForAgentPolicy } from '../api_keys'; | ||
|
||
import { ensureAgentPoliciesFleetServerKeysAndPolicies } from './fleet_server_policies_enrollment_keys'; | ||
|
||
jest.mock('../app_context'); | ||
jest.mock('../agent_policy'); | ||
jest.mock('../api_keys'); | ||
|
||
const mockedEnsureDefaultEnrollmentAPIKeyForAgentPolicy = jest.mocked( | ||
ensureDefaultEnrollmentAPIKeyForAgentPolicy | ||
); | ||
|
||
const mockedAgentPolicyService = jest.mocked(agentPolicyService); | ||
|
||
describe('ensureAgentPoliciesFleetServerKeysAndPolicies', () => { | ||
beforeEach(() => { | ||
jest.mocked(appContextService).getSecurity.mockReturnValue({ | ||
authc: { apiKeys: { areAPIKeysEnabled: async () => true } }, | ||
} as any); | ||
|
||
mockedEnsureDefaultEnrollmentAPIKeyForAgentPolicy.mockReset(); | ||
mockedAgentPolicyService.getLatestFleetPolicy.mockReset(); | ||
mockedAgentPolicyService.deployPolicies.mockImplementation(async () => {}); | ||
mockedAgentPolicyService.list.mockResolvedValue({ | ||
items: [ | ||
{ | ||
id: 'policy1', | ||
revision: 1, | ||
}, | ||
{ | ||
id: 'policy2', | ||
revision: 2, | ||
}, | ||
], | ||
} as any); | ||
}); | ||
|
||
it('should do nothing with policies already deployed', async () => { | ||
const logger = loggingSystemMock.createLogger(); | ||
const esClient = elasticsearchServiceMock.createInternalClient(); | ||
const soClient = savedObjectsClientMock.create(); | ||
mockedAgentPolicyService.getLatestFleetPolicy.mockImplementation(async (_, agentPolicyId) => { | ||
if (agentPolicyId === 'policy1') { | ||
return { | ||
revision_idx: 1, | ||
} as any; | ||
} | ||
|
||
if (agentPolicyId === 'policy2') { | ||
return { | ||
revision_idx: 2, | ||
} as any; | ||
} | ||
|
||
return null; | ||
}); | ||
|
||
await ensureAgentPoliciesFleetServerKeysAndPolicies({ | ||
logger, | ||
esClient, | ||
soClient, | ||
}); | ||
|
||
expect(mockedEnsureDefaultEnrollmentAPIKeyForAgentPolicy).toBeCalledTimes(2); | ||
expect(mockedAgentPolicyService.deployPolicies).not.toBeCalled(); | ||
}); | ||
|
||
it('should do deploy policies out of sync', async () => { | ||
const logger = loggingSystemMock.createLogger(); | ||
const esClient = elasticsearchServiceMock.createInternalClient(); | ||
const soClient = savedObjectsClientMock.create(); | ||
mockedAgentPolicyService.getLatestFleetPolicy.mockImplementation(async (_, agentPolicyId) => { | ||
if (agentPolicyId === 'policy1') { | ||
return { | ||
revision_idx: 1, | ||
} as any; | ||
} | ||
|
||
if (agentPolicyId === 'policy2') { | ||
return { | ||
revision_idx: 1, | ||
} as any; | ||
} | ||
|
||
return null; | ||
}); | ||
|
||
await ensureAgentPoliciesFleetServerKeysAndPolicies({ | ||
logger, | ||
esClient, | ||
soClient, | ||
}); | ||
|
||
expect(mockedEnsureDefaultEnrollmentAPIKeyForAgentPolicy).toBeCalledTimes(2); | ||
expect(mockedAgentPolicyService.deployPolicies).toBeCalledWith(expect.anything(), ['policy2']); | ||
}); | ||
|
||
it('should do deploy policies never deployed', async () => { | ||
const logger = loggingSystemMock.createLogger(); | ||
const esClient = elasticsearchServiceMock.createInternalClient(); | ||
const soClient = savedObjectsClientMock.create(); | ||
mockedAgentPolicyService.getLatestFleetPolicy.mockImplementation(async (_, agentPolicyId) => { | ||
if (agentPolicyId === 'policy1') { | ||
return { | ||
revision_idx: 1, | ||
} as any; | ||
} | ||
|
||
return null; | ||
}); | ||
|
||
await ensureAgentPoliciesFleetServerKeysAndPolicies({ | ||
logger, | ||
esClient, | ||
soClient, | ||
}); | ||
|
||
expect(mockedEnsureDefaultEnrollmentAPIKeyForAgentPolicy).toBeCalledTimes(2); | ||
expect(mockedAgentPolicyService.deployPolicies).toBeCalledWith(expect.anything(), ['policy2']); | ||
}); | ||
|
||
it('handle errors when deploying policies', async () => { | ||
const logger = loggingSystemMock.createLogger(); | ||
const esClient = elasticsearchServiceMock.createInternalClient(); | ||
const soClient = savedObjectsClientMock.create(); | ||
mockedAgentPolicyService.getLatestFleetPolicy.mockImplementation(async (_, agentPolicyId) => { | ||
if (agentPolicyId === 'policy1') { | ||
return { | ||
revision_idx: 1, | ||
} as any; | ||
} | ||
|
||
return null; | ||
}); | ||
mockedAgentPolicyService.deployPolicies.mockRejectedValue(new Error('test rejection')); | ||
|
||
await ensureAgentPoliciesFleetServerKeysAndPolicies({ | ||
logger, | ||
esClient, | ||
soClient, | ||
}); | ||
|
||
expect(mockedEnsureDefaultEnrollmentAPIKeyForAgentPolicy).toBeCalledTimes(2); | ||
expect(mockedAgentPolicyService.deployPolicies).toBeCalledWith(expect.anything(), ['policy2']); | ||
|
||
expect(logger.warn).toBeCalledWith( | ||
'Error deploying policies: test rejection', | ||
expect.anything() | ||
); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* 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 { ElasticsearchClient, SavedObjectsClientContract, Logger } from '@kbn/core/server'; | ||
import pMap from 'p-map'; | ||
|
||
import { agentPolicyService } from '../agent_policy'; | ||
import { ensureDefaultEnrollmentAPIKeyForAgentPolicy } from '../api_keys'; | ||
import { SO_SEARCH_LIMIT } from '../../constants'; | ||
import { appContextService } from '../app_context'; | ||
|
||
export async function ensureAgentPoliciesFleetServerKeysAndPolicies({ | ||
logger, | ||
soClient, | ||
esClient, | ||
}: { | ||
logger: Logger; | ||
soClient: SavedObjectsClientContract; | ||
esClient: ElasticsearchClient; | ||
}) { | ||
const security = appContextService.getSecurity(); | ||
if (!security) { | ||
return; | ||
} | ||
|
||
if (!(await security.authc.apiKeys.areAPIKeysEnabled())) { | ||
return; | ||
} | ||
|
||
const { items: agentPolicies } = await agentPolicyService.list(soClient, { | ||
perPage: SO_SEARCH_LIMIT, | ||
}); | ||
|
||
const outdatedAgentPolicyIds: string[] = []; | ||
|
||
await pMap( | ||
agentPolicies, | ||
async (agentPolicy) => { | ||
const [latestFleetPolicy] = await Promise.all([ | ||
agentPolicyService.getLatestFleetPolicy(esClient, agentPolicy.id), | ||
ensureDefaultEnrollmentAPIKeyForAgentPolicy(soClient, esClient, agentPolicy.id), | ||
]); | ||
|
||
if ((latestFleetPolicy?.revision_idx ?? -1) < agentPolicy.revision) { | ||
outdatedAgentPolicyIds.push(agentPolicy.id); | ||
} | ||
}, | ||
{ | ||
concurrency: 20, | ||
} | ||
); | ||
|
||
if (outdatedAgentPolicyIds.length) { | ||
await agentPolicyService.deployPolicies(soClient, outdatedAgentPolicyIds).catch((error) => { | ||
logger.warn(`Error deploying policies: ${error.message}`, { error }); | ||
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. I think there is no need to crash the setup if this fail, it's why I am swallowing the error here 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. nit: could we add a unit test for the error scenario? I usually combine async-await with try-catch, I suppose it should work with catch too. |
||
}); | ||
} | ||
} |
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.
👍