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 @@ -203,10 +203,32 @@ export default class Client extends API {
203
203
if ( ( opts . cloud != null || opts . serverMode === 'serverless' ) && opts [ kChild ] === undefined ) {
204
204
if ( opts . cloud != null ) {
205
205
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
+
206
217
// the cloud id is `cluster-name:base64encodedurl`
207
218
// the url is a string divided by two '$', the first is the cloud url
208
219
// 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
+ }
210
232
211
233
opts . node = `https://${ cloudUrls [ 1 ] } .${ cloudUrls [ 0 ] } `
212
234
}
Original file line number Diff line number Diff line change @@ -287,9 +287,25 @@ test('Elastic Cloud config', t => {
287
287
t . equal ( connection ?. url . hostname , 'abcd.localhost' )
288
288
t . equal ( connection ?. url . protocol , 'https:' )
289
289
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
+
290
304
t . end ( )
291
305
} )
292
306
307
+
308
+
293
309
test ( 'Override default Elastic Cloud options' , t => {
294
310
const client = new Client ( {
295
311
cloud : {
You can’t perform that action at this time.
0 commit comments