Skip to content

Commit

Permalink
send kibana path to fleet (#80540)
Browse files Browse the repository at this point in the history
  • Loading branch information
neptunian authored Oct 14, 2020
1 parent 00a9a46 commit fb9ccc0
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { getFullAgentPolicyKibanaConfig } from './full_agent_policy_kibana_config';

describe('Fleet - getFullAgentPolicyKibanaConfig', () => {
it('should return no path when there is no path', () => {
expect(getFullAgentPolicyKibanaConfig(['http://localhost:5601'])).toEqual({
hosts: ['localhost:5601'],
protocol: 'http',
});
});
it('should return correct config when there is a path', () => {
expect(getFullAgentPolicyKibanaConfig(['http://localhost:5601/ssg'])).toEqual({
hosts: ['localhost:5601'],
protocol: 'http',
path: '/ssg/',
});
});
it('should return correct config when there is a path that ends in a slash', () => {
expect(getFullAgentPolicyKibanaConfig(['http://localhost:5601/ssg/'])).toEqual({
hosts: ['localhost:5601'],
protocol: 'http',
path: '/ssg/',
});
});
it('should return correct config when there are multiple hosts', () => {
expect(
getFullAgentPolicyKibanaConfig(['http://localhost:5601/ssg/', 'http://localhost:3333/ssg/'])
).toEqual({
hosts: ['localhost:5601', 'localhost:3333'],
protocol: 'http',
path: '/ssg/',
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { FullAgentPolicyKibanaConfig } from '../types';

export function getFullAgentPolicyKibanaConfig(kibanaUrls: string[]): FullAgentPolicyKibanaConfig {
// paths and protocol are validated to be the same for all urls, so use the first to get them
const firstUrlParsed = new URL(kibanaUrls[0]);
const config: FullAgentPolicyKibanaConfig = {
// remove the : from http:
protocol: firstUrlParsed.protocol.replace(':', ''),
hosts: kibanaUrls.map((url) => new URL(url).host),
};

// add path if user provided one
if (firstUrlParsed.pathname !== '/') {
// make sure the path ends with /
config.path = firstUrlParsed.pathname.endsWith('/')
? firstUrlParsed.pathname
: `${firstUrlParsed.pathname}/`;
}
return config;
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,7 @@ export interface FullAgentPolicy {
};
};
fleet?: {
kibana: {
hosts: string[];
protocol: string;
};
kibana: FullAgentPolicyKibanaConfig;
};
inputs: FullAgentPolicyInput[];
revision?: number;
Expand All @@ -78,3 +75,9 @@ export interface FullAgentPolicy {
};
};
}

export interface FullAgentPolicyKibanaConfig {
hosts: string[];
protocol: string;
path?: string;
}
12 changes: 3 additions & 9 deletions x-pack/plugins/ingest_manager/server/services/agent_policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { outputService } from './output';
import { agentPolicyUpdateEventHandler } from './agent_policy_update';
import { getSettings } from './settings';
import { normalizeKuery, escapeSearchQueryPhrase } from './saved_object';
import { getFullAgentPolicyKibanaConfig } from '../../common/services/full_agent_policy_kibana_config';

const SAVED_OBJECT_TYPE = AGENT_POLICY_SAVED_OBJECT_TYPE;

Expand Down Expand Up @@ -537,18 +538,11 @@ class AgentPolicyService {
}
if (!settings.kibana_urls || !settings.kibana_urls.length)
throw new Error('kibana_urls is missing');
const hostsWithoutProtocol = settings.kibana_urls.map((url) => {
const parsedURL = new URL(url);
return `${parsedURL.host}${parsedURL.pathname !== '/' ? parsedURL.pathname : ''}`;
});

fullAgentPolicy.fleet = {
kibana: {
protocol: new URL(settings.kibana_urls[0]).protocol.replace(':', ''),
hosts: hostsWithoutProtocol,
},
kibana: getFullAgentPolicyKibanaConfig(settings.kibana_urls),
};
}

return fullAgentPolicy;
}
}
Expand Down

0 comments on commit fb9ccc0

Please sign in to comment.