File tree Expand file tree Collapse file tree 2 files changed +39
-1
lines changed
Expand file tree Collapse file tree 2 files changed +39
-1
lines changed Original file line number Diff line number Diff line change @@ -203,10 +203,32 @@ export default class Client extends API {
203203 if ( ( opts . cloud != null || opts . serverMode === 'serverless' ) && opts [ kChild ] === undefined ) {
204204 if ( opts . cloud != null ) {
205205 const { id } = opts . cloud
206+ if ( typeof id !== 'string' ) {
207+ throw new errors . ConfigurationError ( 'Cloud ID must be a string.' )
208+ }
209+
210+ const parts = id . split ( ':' )
211+ if ( parts . length !== 2 || parts [ 1 ] === '' ) {
212+ throw new errors . ConfigurationError (
213+ 'Cloud ID must be in the format "name:base64string".'
214+ )
215+ }
216+
206217 // the cloud id is `cluster-name:base64encodedurl`
207218 // the url is a string divided by two '$', the first is the cloud url
208219 // the second the elasticsearch instance, the third the kibana instance
209- const cloudUrls = Buffer . from ( id . split ( ':' ) [ 1 ] , 'base64' ) . toString ( ) . split ( '$' )
220+
221+ let cloudUrls
222+ try {
223+ cloudUrls = Buffer . from ( parts [ 1 ] , 'base64' ) . toString ( ) . split ( '$' )
224+ } catch ( err ) {
225+ throw new errors . ConfigurationError ( 'Cloud ID base64 decoding failed.' )
226+ }
227+ if ( cloudUrls . length < 2 || cloudUrls [ 0 ] === '' || cloudUrls [ 1 ] === '' ) {
228+ throw new errors . ConfigurationError (
229+ 'Cloud ID base64 must contain at least two "$" separated parts: "<cloudUrl>$<esId>[$<kibanaId>]".'
230+ )
231+ }
210232
211233 opts . node = `https://${ cloudUrls [ 1 ] } .${ cloudUrls [ 0 ] } `
212234 }
Original file line number Diff line number Diff line change @@ -287,9 +287,25 @@ test('Elastic Cloud config', t => {
287287 t . equal ( connection ?. url . hostname , 'abcd.localhost' )
288288 t . equal ( connection ?. url . protocol , 'https:' )
289289
290+ t . test ( 'Invalid Cloud ID will throw ConfigurationError' , t => {
291+ t . throws ( ( ) => new Client ( {
292+ cloud : {
293+ id : 'invalidCloudIdThatIsNotBase64'
294+ } ,
295+ auth : {
296+ username : 'elastic' ,
297+ password : 'changeme'
298+ }
299+
300+ } ) , errors . ConfigurationError )
301+ t . end ( )
302+ } )
303+
290304 t . end ( )
291305} )
292306
307+
308+
293309test ( 'Override default Elastic Cloud options' , t => {
294310 const client = new Client ( {
295311 cloud : {
You can’t perform that action at this time.
0 commit comments