Skip to content

Commit

Permalink
[Fleet] fix remote output health reporting if remote es output is def…
Browse files Browse the repository at this point in the history
…ault (#178857)

## Summary

Closes #177927

Replaced "default" with real output id in full agent policy. This fixes
the issue that the remote es health reporting was incorrect if the
output was set as default.

More explanation on the bug:
#177927 (comment)

To verify:
- create a remote es output and set as default (both data and
monitoring)
- create an agent policy that uses default output 
- enroll an agent
- expect that the agent sends system and elastic-agent metrics/logs to
remote es
- verify that the remote es health badge shows up on UI

<img width="1283" alt="image"
src="https://github.com/elastic/kibana/assets/90178898/348406d4-69e6-4eda-b396-635771d1edf3">
<img width="695" alt="image"
src="https://github.com/elastic/kibana/assets/90178898/cd03310c-d50d-42ea-8f28-136bf068c52d">



### Checklist

- [x] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios
  • Loading branch information
juliaElastic authored Mar 19, 2024
1 parent 5614bd7 commit 674a750
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 38 deletions.

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 @@ -197,7 +197,7 @@ describe('getFullAgentPolicy', () => {
expect(agentPolicy).toMatchObject({
id: 'agent-policy',
outputs: {
default: {
'test-id': {
type: 'elasticsearch',
hosts: ['http://127.0.0.1:9201'],
},
Expand Down Expand Up @@ -228,7 +228,7 @@ describe('getFullAgentPolicy', () => {
expect(agentPolicy).toMatchObject({
id: 'agent-policy',
outputs: {
default: {
'test-id': {
type: 'elasticsearch',
hosts: ['http://127.0.0.1:9201'],
},
Expand All @@ -244,7 +244,7 @@ describe('getFullAgentPolicy', () => {
},
monitoring: {
namespace: 'default',
use_output: 'default',
use_output: 'test-id',
enabled: true,
logs: true,
metrics: false,
Expand All @@ -264,7 +264,7 @@ describe('getFullAgentPolicy', () => {
expect(agentPolicy).toMatchObject({
id: 'agent-policy',
outputs: {
default: {
'test-id': {
type: 'elasticsearch',
hosts: ['http://127.0.0.1:9201'],
},
Expand All @@ -280,7 +280,7 @@ describe('getFullAgentPolicy', () => {
},
monitoring: {
namespace: 'default',
use_output: 'default',
use_output: 'test-id',
enabled: true,
logs: false,
metrics: true,
Expand Down Expand Up @@ -358,7 +358,7 @@ describe('getFullAgentPolicy', () => {
expect(agentPolicy).toMatchSnapshot();
});

it('should use "default" as the default policy id', async () => {
it('should use output id from default policy id', async () => {
mockAgentPolicy({
id: 'policy',
status: 'active',
Expand All @@ -372,7 +372,7 @@ describe('getFullAgentPolicy', () => {

const agentPolicy = await getFullAgentPolicy(savedObjectsClientMock.create(), 'agent-policy');

expect(agentPolicy?.outputs.default).toBeDefined();
expect(agentPolicy?.outputs['test-id']).toBeDefined();
});

it('should return the sourceURI from the agent policy', async () => {
Expand All @@ -387,7 +387,7 @@ describe('getFullAgentPolicy', () => {
expect(agentPolicy).toMatchObject({
id: 'agent-policy',
outputs: {
default: {
'test-id': {
type: 'elasticsearch',
hosts: ['http://127.0.0.1:9201'],
},
Expand All @@ -403,7 +403,7 @@ describe('getFullAgentPolicy', () => {
},
monitoring: {
namespace: 'default',
use_output: 'default',
use_output: 'test-id',
enabled: true,
logs: false,
metrics: true,
Expand All @@ -427,7 +427,7 @@ describe('getFullAgentPolicy', () => {
expect(agentPolicy).toMatchObject({
id: 'agent-policy',
outputs: {
default: {
'test-id': {
type: 'elasticsearch',
hosts: ['http://127.0.0.1:9201'],
},
Expand All @@ -440,7 +440,7 @@ describe('getFullAgentPolicy', () => {
agent: {
monitoring: {
namespace: 'default',
use_output: 'default',
use_output: 'test-id',
enabled: true,
logs: false,
metrics: true,
Expand Down Expand Up @@ -626,7 +626,7 @@ describe('getFullAgentPolicy', () => {
},
],
type: 'test-logs',
use_output: 'default',
use_output: 'test-id',
},
{
data_stream: {
Expand All @@ -652,11 +652,11 @@ describe('getFullAgentPolicy', () => {
},
],
type: 'test-logs',
use_output: 'default',
use_output: 'test-id',
},
],
output_permissions: {
default: {
'test-id': {
_elastic_agent_checks: {
cluster: ['monitor'],
},
Expand All @@ -679,7 +679,7 @@ describe('getFullAgentPolicy', () => {
},
},
outputs: {
default: {
'test-id': {
hosts: ['http://127.0.0.1:9201'],
preset: 'balanced',
type: 'elasticsearch',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import type {
} from '../../../common/types';
import { agentPolicyService } from '../agent_policy';
import { dataTypes, kafkaCompressionType, outputType } from '../../../common/constants';
import { DEFAULT_OUTPUT } from '../../constants';

import { getPackageInfo } from '../epm/packages';
import { pkgToPkgKey, splitPkgKey } from '../epm/registry';
Expand Down Expand Up @@ -491,13 +490,8 @@ export function transformOutputToFullPolicyOutput(

/**
* Get id used in full agent policy (sent to the agents)
* we use "default" for the default policy to avoid breaking changes
*/
function getOutputIdForAgentPolicy(output: Output) {
if (output.is_default) {
return DEFAULT_OUTPUT.name;
}

return output.id;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,13 @@ export default function ApiTest(ftrProviderContext: FtrProviderContext) {
registry.when('APM package policy', { config: 'basic', archives: [] }, () => {
async function getAgentPolicyPermissions(agentPolicyId: string, packagePolicyId: string) {
const res = await bettertest<{
item: { output_permissions: { default: Record<string, SecurityRoleDescriptor> } };
item: { output_permissions: { [key: string]: Record<string, SecurityRoleDescriptor> } };
}>({
pathname: `/api/fleet/agent_policies/${agentPolicyId}/full`,
method: 'get',
});

return res.body.item.output_permissions.default[packagePolicyId];
return Object.values(res.body.item.output_permissions)[0][packagePolicyId];
}

describe('input only package', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,9 @@ export default function (providerContext: FtrProviderContext) {
.set('kbn-xsrf', 'xxxx');

// Check that the privileges are correct
expect(fullAgentPolicy.output_permissions.default[packagePolicyId].indices).to.eql([
expect(
(Object.values(fullAgentPolicy.output_permissions)[0] as any)[packagePolicyId].indices
).to.eql([
{ names: ['logs-*-*'], privileges: ['auto_configure', 'create_doc'] },
{ names: ['metrics-*-*'], privileges: ['auto_configure', 'create_doc'] },
]);
Expand Down

0 comments on commit 674a750

Please sign in to comment.