Skip to content

Commit

Permalink
[Fleet] Use elastic_agent package to build monitoring permissions for…
Browse files Browse the repository at this point in the history
… agent (#112730) (#113813)

Co-authored-by: Nicolas Chaulet <nicolas.chaulet@elastic.co>
  • Loading branch information
kibanamachine and nchaulet authored Oct 4, 2021
1 parent 58e534e commit 2768124
Show file tree
Hide file tree
Showing 8 changed files with 485 additions and 99 deletions.
14 changes: 14 additions & 0 deletions x-pack/plugins/fleet/common/constants/agent_policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,17 @@ export const agentPolicyStatuses = {
Active: 'active',
Inactive: 'inactive',
} as const;

export const AGENT_POLICY_DEFAULT_MONITORING_DATASETS = [
'elastic_agent',
'elastic_agent.elastic_agent',
'elastic_agent.apm_server',
'elastic_agent.filebeat',
'elastic_agent.fleet_server',
'elastic_agent.metricbeat',
'elastic_agent.osquerybeat',
'elastic_agent.packetbeat',
'elastic_agent.endpoint_security',
'elastic_agent.auditbeat',
'elastic_agent.heartbeat',
];
1 change: 1 addition & 0 deletions x-pack/plugins/fleet/server/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export {
DEFAULT_OUTPUT,
DEFAULT_PACKAGES,
PACKAGE_POLICY_DEFAULT_INDEX_PRIVILEGES,
AGENT_POLICY_DEFAULT_MONITORING_DATASETS,
// Fleet Server index
FLEET_SERVER_SERVERS_INDEX,
ENROLLMENT_API_KEYS_INDEX,
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ import { agentPolicyService } from '../agent_policy';
import { agentPolicyUpdateEventHandler } from '../agent_policy_update';

import { getFullAgentPolicy } from './full_agent_policy';
import { getMonitoringPermissions } from './monitoring_permissions';

const mockedGetElasticAgentMonitoringPermissions = getMonitoringPermissions as jest.Mock<
ReturnType<typeof getMonitoringPermissions>
>;
const mockedAgentPolicyService = agentPolicyService as jest.Mocked<typeof agentPolicyService>;

function mockAgentPolicy(data: Partial<AgentPolicy>) {
Expand Down Expand Up @@ -87,6 +91,8 @@ jest.mock('../agent_policy_update');
jest.mock('../agents');
jest.mock('../package_policy');

jest.mock('./monitoring_permissions');

function getAgentPolicyUpdateMock() {
return agentPolicyUpdateEventHandler as unknown as jest.Mock<
typeof agentPolicyUpdateEventHandler
Expand All @@ -97,6 +103,29 @@ describe('getFullAgentPolicy', () => {
beforeEach(() => {
getAgentPolicyUpdateMock().mockClear();
mockedAgentPolicyService.get.mockReset();
mockedGetElasticAgentMonitoringPermissions.mockReset();
mockedGetElasticAgentMonitoringPermissions.mockImplementation(
async (soClient, { logs, metrics }, namespace) => {
const names: string[] = [];
if (logs) {
names.push(`logs-${namespace}`);
}
if (metrics) {
names.push(`metrics-${namespace}`);
}

return {
_elastic_agent_monitoring: {
indices: [
{
names,
privileges: [],
},
],
},
};
}
);
});

it('should return a policy without monitoring if monitoring is not enabled', async () => {
Expand Down Expand Up @@ -200,6 +229,24 @@ describe('getFullAgentPolicy', () => {
});
});

it('should get the permissions for monitoring', async () => {
mockAgentPolicy({
namespace: 'testnamespace',
revision: 1,
monitoring_enabled: ['metrics'],
});
await getFullAgentPolicy(savedObjectsClientMock.create(), 'agent-policy');

expect(mockedGetElasticAgentMonitoringPermissions).toHaveBeenCalledWith(
expect.anything(),
{
logs: false,
metrics: true,
},
'testnamespace'
);
});

it('should support a different monitoring output', async () => {
mockAgentPolicy({
namespace: 'default',
Expand Down
Loading

0 comments on commit 2768124

Please sign in to comment.