Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import { RedactionOptions } from '@elastic/transport/lib/Transport'
import BaseConnection, { prepareHeaders, ConnectionOptions } from '@elastic/transport/lib/connection/BaseConnection'
import SniffingTransport from './sniffingTransport'
import Helpers from './helpers'
import API from './api'
import API from '../src/api/index'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should not be changed, and is the reason some tests are failing.

Suggested change
import API from '../src/api/index'
import API from './api'

import packageJson from '../package.json'
import transportPackageJson from '@elastic/transport/package.json'

Expand Down Expand Up @@ -203,10 +203,32 @@ export default class Client extends API {
if ((opts.cloud != null || opts.serverMode === 'serverless') && opts[kChild] === undefined) {
if (opts.cloud != null) {
const { id } = opts.cloud
if (typeof id !== 'string') {
throw new errors.ConfigurationError('Cloud ID must be a string.')
}

const parts = id.split(':')
if (parts.length !== 2 || parts[1] === '') {
throw new errors.ConfigurationError(
'Cloud ID must be in the format "name:base64string".'
)
}

// the cloud id is `cluster-name:base64encodedurl`
// the url is a string divided by two '$', the first is the cloud url
// the second the elasticsearch instance, the third the kibana instance
const cloudUrls = Buffer.from(id.split(':')[1], 'base64').toString().split('$')

let cloudUrls
try {
cloudUrls = Buffer.from(parts[1], 'base64').toString().split('$')
} catch (err) {
throw new errors.ConfigurationError('Cloud ID base64 decoding failed.')
}
if (cloudUrls.length < 2 || cloudUrls[0] === '' || cloudUrls[1] === '') {
throw new errors.ConfigurationError(
'Cloud ID base64 must contain at least two "$" separated parts: "<cloudUrl>$<esId>[$<kibanaId>]".'
)
}

opts.node = `https://${cloudUrls[1]}.${cloudUrls[0]}`
}
Expand Down
16 changes: 16 additions & 0 deletions test/unit/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,9 +287,25 @@ test('Elastic Cloud config', t => {
t.equal(connection?.url.hostname, 'abcd.localhost')
t.equal(connection?.url.protocol, 'https:')

t.test('Invalid Cloud ID will throw ConfigurationError', t => {
t.throws(() => new Client({
cloud : {
id : 'invalidCloudIdThatIsNotBase64'
},
auth : {
username: 'elastic',
password: 'changeme'
}

}), errors.ConfigurationError)
t.end()
})

t.end()
})



test('Override default Elastic Cloud options', t => {
const client = new Client({
cloud: {
Expand Down
Loading