File tree 2 files changed +39
-1
lines changed
2 files changed +39
-1
lines changed Original file line number Diff line number Diff line change @@ -201,10 +201,32 @@ export default class Client extends API {
201
201
// @ts -expect-error kChild symbol is for internal use only
202
202
if ( ( opts . cloud != null ) && opts [ kChild ] === undefined ) {
203
203
const { id } = opts . cloud
204
+ if ( typeof id !== 'string' ) {
205
+ throw new errors . ConfigurationError ( 'Cloud ID must be a string.' )
206
+ }
207
+
208
+ const parts = id . split ( ':' )
209
+ if ( parts . length !== 2 || parts [ 1 ] === '' ) {
210
+ throw new errors . ConfigurationError (
211
+ 'Cloud ID must be in the format "name:base64string".'
212
+ )
213
+ }
214
+
204
215
// the cloud id is `cluster-name:base64encodedurl`
205
216
// the url is a string divided by two '$', the first is the cloud url
206
217
// the second the elasticsearch instance, the third the kibana instance
207
- const cloudUrls = Buffer . from ( id . split ( ':' ) [ 1 ] , 'base64' ) . toString ( ) . split ( '$' )
218
+
219
+ let cloudUrls
220
+ try {
221
+ cloudUrls = Buffer . from ( parts [ 1 ] , 'base64' ) . toString ( ) . split ( '$' )
222
+ } catch ( err ) {
223
+ throw new errors . ConfigurationError ( 'Cloud ID base64 decoding failed.' )
224
+ }
225
+ if ( cloudUrls . length < 2 || cloudUrls [ 0 ] === '' || cloudUrls [ 1 ] === '' ) {
226
+ throw new errors . ConfigurationError (
227
+ 'Cloud ID base64 must contain at least two "$" separated parts: "<cloudUrl>$<esId>[$<kibanaId>]".'
228
+ )
229
+ }
208
230
209
231
opts . node = `https://${ cloudUrls [ 1 ] } .${ cloudUrls [ 0 ] } `
210
232
Original file line number Diff line number Diff line change @@ -300,9 +300,25 @@ test('Elastic Cloud config', t => {
300
300
t . equal ( connection ?. url . hostname , 'abcd.localhost' )
301
301
t . equal ( connection ?. url . protocol , 'https:' )
302
302
303
+ t . test ( 'Invalid Cloud ID will throw ConfigurationError' , t => {
304
+ t . throws ( ( ) => new Client ( {
305
+ cloud : {
306
+ id : 'invalidCloudIdThatIsNotBase64'
307
+ } ,
308
+ auth : {
309
+ username : 'elastic' ,
310
+ password : 'changeme'
311
+ }
312
+
313
+ } ) , errors . ConfigurationError )
314
+ t . end ( )
315
+ } )
316
+
303
317
t . end ( )
304
318
} )
305
319
320
+
321
+
306
322
test ( 'Override default Elastic Cloud options' , t => {
307
323
const client = new Client ( {
308
324
cloud : {
You can’t perform that action at this time.
0 commit comments