Skip to content

Commit 7b4c799

Browse files
github-actions[bot]Siddhu545JoshMockSiddhu545
authored
Bug #2694 (#2731) (#2742)
Co-authored-by: Siddhu545 <[email protected]> Co-authored-by: Josh Mock <[email protected]> Co-authored-by: Siddharth Khengare <[email protected]>
1 parent 7feb422 commit 7b4c799

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
@@ -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
}

test/unit/client.test.ts

+16
Original file line numberDiff line numberDiff 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+
293309
test('Override default Elastic Cloud options', t => {
294310
const client = new Client({
295311
cloud: {

0 commit comments

Comments
 (0)