File tree Expand file tree Collapse file tree
x-pack/plugins/ingest_manager Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ /*
2+ * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+ * or more contributor license agreements. Licensed under the Elastic License;
4+ * you may not use this file except in compliance with the Elastic License.
5+ */
6+
7+ import { getFullAgentPolicyKibanaConfig } from './full_agent_policy_kibana_config' ;
8+
9+ describe ( 'Fleet - getFullAgentPolicyKibanaConfig' , ( ) => {
10+ it ( 'should return no path when there is no path' , ( ) => {
11+ expect ( getFullAgentPolicyKibanaConfig ( [ 'http://localhost:5601' ] ) ) . toEqual ( {
12+ hosts : [ 'localhost:5601' ] ,
13+ protocol : 'http' ,
14+ } ) ;
15+ } ) ;
16+ it ( 'should return correct config when there is a path' , ( ) => {
17+ expect ( getFullAgentPolicyKibanaConfig ( [ 'http://localhost:5601/ssg' ] ) ) . toEqual ( {
18+ hosts : [ 'localhost:5601' ] ,
19+ protocol : 'http' ,
20+ path : '/ssg/' ,
21+ } ) ;
22+ } ) ;
23+ it ( 'should return correct config when there is a path that ends in a slash' , ( ) => {
24+ expect ( getFullAgentPolicyKibanaConfig ( [ 'http://localhost:5601/ssg/' ] ) ) . toEqual ( {
25+ hosts : [ 'localhost:5601' ] ,
26+ protocol : 'http' ,
27+ path : '/ssg/' ,
28+ } ) ;
29+ } ) ;
30+ it ( 'should return correct config when there are multiple hosts' , ( ) => {
31+ expect (
32+ getFullAgentPolicyKibanaConfig ( [ 'http://localhost:5601/ssg/' , 'http://localhost:3333/ssg/' ] )
33+ ) . toEqual ( {
34+ hosts : [ 'localhost:5601' , 'localhost:3333' ] ,
35+ protocol : 'http' ,
36+ path : '/ssg/' ,
37+ } ) ;
38+ } ) ;
39+ } ) ;
Original file line number Diff line number Diff line change 1+ /*
2+ * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+ * or more contributor license agreements. Licensed under the Elastic License;
4+ * you may not use this file except in compliance with the Elastic License.
5+ */
6+
7+ import { FullAgentPolicyKibanaConfig } from '../types' ;
8+
9+ export function getFullAgentPolicyKibanaConfig ( kibanaUrls : string [ ] ) : FullAgentPolicyKibanaConfig {
10+ // paths and protocol are validated to be the same for all urls, so use the first to get them
11+ const firstUrlParsed = new URL ( kibanaUrls [ 0 ] ) ;
12+ const config : FullAgentPolicyKibanaConfig = {
13+ // remove the : from http:
14+ protocol : firstUrlParsed . protocol . replace ( ':' , '' ) ,
15+ hosts : kibanaUrls . map ( ( url ) => new URL ( url ) . host ) ,
16+ } ;
17+
18+ // add path if user provided one
19+ if ( firstUrlParsed . pathname !== '/' ) {
20+ // make sure the path ends with /
21+ config . path = firstUrlParsed . pathname . endsWith ( '/' )
22+ ? firstUrlParsed . pathname
23+ : `${ firstUrlParsed . pathname } /` ;
24+ }
25+ return config ;
26+ }
Original file line number Diff line number Diff line change @@ -62,10 +62,7 @@ export interface FullAgentPolicy {
6262 } ;
6363 } ;
6464 fleet ?: {
65- kibana : {
66- hosts : string [ ] ;
67- protocol : string ;
68- } ;
65+ kibana : FullAgentPolicyKibanaConfig ;
6966 } ;
7067 inputs : FullAgentPolicyInput [ ] ;
7168 revision ?: number ;
@@ -78,3 +75,9 @@ export interface FullAgentPolicy {
7875 } ;
7976 } ;
8077}
78+
79+ export interface FullAgentPolicyKibanaConfig {
80+ hosts : string [ ] ;
81+ protocol : string ;
82+ path ?: string ;
83+ }
Original file line number Diff line number Diff line change @@ -33,6 +33,7 @@ import { outputService } from './output';
3333import { agentPolicyUpdateEventHandler } from './agent_policy_update' ;
3434import { getSettings } from './settings' ;
3535import { normalizeKuery , escapeSearchQueryPhrase } from './saved_object' ;
36+ import { getFullAgentPolicyKibanaConfig } from '../../common/services/full_agent_policy_kibana_config' ;
3637
3738const SAVED_OBJECT_TYPE = AGENT_POLICY_SAVED_OBJECT_TYPE ;
3839
@@ -537,18 +538,11 @@ class AgentPolicyService {
537538 }
538539 if ( ! settings . kibana_urls || ! settings . kibana_urls . length )
539540 throw new Error ( 'kibana_urls is missing' ) ;
540- const hostsWithoutProtocol = settings . kibana_urls . map ( ( url ) => {
541- const parsedURL = new URL ( url ) ;
542- return `${ parsedURL . host } ${ parsedURL . pathname !== '/' ? parsedURL . pathname : '' } ` ;
543- } ) ;
541+
544542 fullAgentPolicy . fleet = {
545- kibana : {
546- protocol : new URL ( settings . kibana_urls [ 0 ] ) . protocol . replace ( ':' , '' ) ,
547- hosts : hostsWithoutProtocol ,
548- } ,
543+ kibana : getFullAgentPolicyKibanaConfig ( settings . kibana_urls ) ,
549544 } ;
550545 }
551-
552546 return fullAgentPolicy ;
553547 }
554548}
You can’t perform that action at this time.
0 commit comments