forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Fleet] Ensure policies are not out of sync (elastic#175065)
- Loading branch information
Showing
7 changed files
with
241 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
163 changes: 163 additions & 0 deletions
163
x-pack/plugins/fleet/server/services/setup/fleet_server_policies_enrollment_keys.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
); | ||
}); | ||
}); |
62 changes: 62 additions & 0 deletions
62
x-pack/plugins/fleet/server/services/setup/fleet_server_policies_enrollment_keys.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters