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] Use elastic_agent package to build monitoring permissions for elastic agent #112730

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