Skip to content

Commit 4a059fd

Browse files
JoshMockSiddhu545Siddhu545
authored
Bug #2694 (#2731) (#2744)
Co-authored-by: Siddhu545 <[email protected]> Co-authored-by: Josh Mock <[email protected]> Co-authored-by: Siddharth Khengare <[email protected]>
1 parent c5dd4e9 commit 4a059fd

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

src/client.ts

+23-1
Original file line numberDiff line numberDiff line change
@@ -201,10 +201,32 @@ export default class Client extends API {
201201
// @ts-expect-error kChild symbol is for internal use only
202202
if ((opts.cloud != null) && opts[kChild] === undefined) {
203203
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+
204215
// the cloud id is `cluster-name:base64encodedurl`
205216
// the url is a string divided by two '$', the first is the cloud url
206217
// 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+
}
208230

209231
opts.node = `https://${cloudUrls[1]}.${cloudUrls[0]}`
210232

test/unit/client.test.ts

+16
Original file line numberDiff line numberDiff line change
@@ -300,9 +300,25 @@ test('Elastic Cloud config', t => {
300300
t.equal(connection?.url.hostname, 'abcd.localhost')
301301
t.equal(connection?.url.protocol, 'https:')
302302

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+
303317
t.end()
304318
})
305319

320+
321+
306322
test('Override default Elastic Cloud options', t => {
307323
const client = new Client({
308324
cloud: {

0 commit comments

Comments
 (0)