Skip to content

Commit 8643841

Browse files
committed
fix(elasticsearch): resolve a Cloud ID to the real Elasticsearch host
1 parent 62d0800 commit 8643841

16 files changed

Lines changed: 261 additions & 548 deletions

‎apps/sim/tools/elasticsearch/bulk.ts‎

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,49 +2,9 @@ import type {
22
ElasticsearchBulkParams,
33
ElasticsearchBulkResponse,
44
} from '@/tools/elasticsearch/types'
5+
import { buildAuthHeaders, buildBaseUrl } from '@/tools/elasticsearch/utils'
56
import type { ToolConfig } from '@/tools/types'
67

7-
function buildBaseUrl(params: ElasticsearchBulkParams): string {
8-
if (params.deploymentType === 'cloud' && params.cloudId) {
9-
const parts = params.cloudId.split(':')
10-
if (parts.length >= 2) {
11-
try {
12-
const decoded = Buffer.from(parts[1], 'base64').toString('utf-8')
13-
const [esHost] = decoded.split('$')
14-
if (esHost) {
15-
return `https://${parts[0]}.${esHost}`
16-
}
17-
} catch {
18-
// Fallback
19-
}
20-
}
21-
throw new Error('Invalid Cloud ID format')
22-
}
23-
24-
if (!params.host) {
25-
throw new Error('Host is required for self-hosted deployments')
26-
}
27-
28-
return params.host.replace(/\/$/, '')
29-
}
30-
31-
function buildAuthHeaders(params: ElasticsearchBulkParams): Record<string, string> {
32-
const headers: Record<string, string> = {
33-
'Content-Type': 'application/x-ndjson',
34-
}
35-
36-
if (params.authMethod === 'api_key' && params.apiKey) {
37-
headers.Authorization = `ApiKey ${params.apiKey}`
38-
} else if (params.authMethod === 'basic_auth' && params.username && params.password) {
39-
const credentials = Buffer.from(`${params.username}:${params.password}`).toString('base64')
40-
headers.Authorization = `Basic ${credentials}`
41-
} else {
42-
throw new Error('Invalid authentication configuration')
43-
}
44-
45-
return headers
46-
}
47-
488
export const bulkTool: ToolConfig<ElasticsearchBulkParams, ElasticsearchBulkResponse> = {
499
id: 'elasticsearch_bulk',
5010
name: 'Elasticsearch Bulk Operations',
@@ -127,7 +87,7 @@ export const bulkTool: ToolConfig<ElasticsearchBulkParams, ElasticsearchBulkResp
12787
return url
12888
},
12989
method: 'POST',
130-
headers: (params) => buildAuthHeaders(params),
90+
headers: (params) => buildAuthHeaders(params, 'application/x-ndjson'),
13191
body: (params) => {
13292
// The body should be NDJSON format - we pass it as raw string
13393
// Ensure it ends with a newline

‎apps/sim/tools/elasticsearch/cluster_health.ts‎

Lines changed: 1 addition & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,49 +2,9 @@ import type {
22
ElasticsearchClusterHealthParams,
33
ElasticsearchClusterHealthResponse,
44
} from '@/tools/elasticsearch/types'
5+
import { buildAuthHeaders, buildBaseUrl } from '@/tools/elasticsearch/utils'
56
import type { ToolConfig } from '@/tools/types'
67

7-
function buildBaseUrl(params: ElasticsearchClusterHealthParams): string {
8-
if (params.deploymentType === 'cloud' && params.cloudId) {
9-
const parts = params.cloudId.split(':')
10-
if (parts.length >= 2) {
11-
try {
12-
const decoded = Buffer.from(parts[1], 'base64').toString('utf-8')
13-
const [esHost] = decoded.split('$')
14-
if (esHost) {
15-
return `https://${parts[0]}.${esHost}`
16-
}
17-
} catch {
18-
// Fallback
19-
}
20-
}
21-
throw new Error('Invalid Cloud ID format')
22-
}
23-
24-
if (!params.host) {
25-
throw new Error('Host is required for self-hosted deployments')
26-
}
27-
28-
return params.host.replace(/\/$/, '')
29-
}
30-
31-
function buildAuthHeaders(params: ElasticsearchClusterHealthParams): Record<string, string> {
32-
const headers: Record<string, string> = {
33-
'Content-Type': 'application/json',
34-
}
35-
36-
if (params.authMethod === 'api_key' && params.apiKey) {
37-
headers.Authorization = `ApiKey ${params.apiKey}`
38-
} else if (params.authMethod === 'basic_auth' && params.username && params.password) {
39-
const credentials = Buffer.from(`${params.username}:${params.password}`).toString('base64')
40-
headers.Authorization = `Basic ${credentials}`
41-
} else {
42-
throw new Error('Invalid authentication configuration')
43-
}
44-
45-
return headers
46-
}
47-
488
export const clusterHealthTool: ToolConfig<
499
ElasticsearchClusterHealthParams,
5010
ElasticsearchClusterHealthResponse

‎apps/sim/tools/elasticsearch/cluster_stats.ts‎

Lines changed: 1 addition & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,49 +2,9 @@ import type {
22
ElasticsearchClusterStatsParams,
33
ElasticsearchClusterStatsResponse,
44
} from '@/tools/elasticsearch/types'
5+
import { buildAuthHeaders, buildBaseUrl } from '@/tools/elasticsearch/utils'
56
import type { ToolConfig } from '@/tools/types'
67

7-
function buildBaseUrl(params: ElasticsearchClusterStatsParams): string {
8-
if (params.deploymentType === 'cloud' && params.cloudId) {
9-
const parts = params.cloudId.split(':')
10-
if (parts.length >= 2) {
11-
try {
12-
const decoded = Buffer.from(parts[1], 'base64').toString('utf-8')
13-
const [esHost] = decoded.split('$')
14-
if (esHost) {
15-
return `https://${parts[0]}.${esHost}`
16-
}
17-
} catch {
18-
// Fallback
19-
}
20-
}
21-
throw new Error('Invalid Cloud ID format')
22-
}
23-
24-
if (!params.host) {
25-
throw new Error('Host is required for self-hosted deployments')
26-
}
27-
28-
return params.host.replace(/\/$/, '')
29-
}
30-
31-
function buildAuthHeaders(params: ElasticsearchClusterStatsParams): Record<string, string> {
32-
const headers: Record<string, string> = {
33-
'Content-Type': 'application/json',
34-
}
35-
36-
if (params.authMethod === 'api_key' && params.apiKey) {
37-
headers.Authorization = `ApiKey ${params.apiKey}`
38-
} else if (params.authMethod === 'basic_auth' && params.username && params.password) {
39-
const credentials = Buffer.from(`${params.username}:${params.password}`).toString('base64')
40-
headers.Authorization = `Basic ${credentials}`
41-
} else {
42-
throw new Error('Invalid authentication configuration')
43-
}
44-
45-
return headers
46-
}
47-
488
export const clusterStatsTool: ToolConfig<
499
ElasticsearchClusterStatsParams,
5010
ElasticsearchClusterStatsResponse

‎apps/sim/tools/elasticsearch/count.ts‎

Lines changed: 1 addition & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,49 +2,9 @@ import type {
22
ElasticsearchCountParams,
33
ElasticsearchCountResponse,
44
} from '@/tools/elasticsearch/types'
5+
import { buildAuthHeaders, buildBaseUrl } from '@/tools/elasticsearch/utils'
56
import type { ToolConfig } from '@/tools/types'
67

7-
function buildBaseUrl(params: ElasticsearchCountParams): string {
8-
if (params.deploymentType === 'cloud' && params.cloudId) {
9-
const parts = params.cloudId.split(':')
10-
if (parts.length >= 2) {
11-
try {
12-
const decoded = Buffer.from(parts[1], 'base64').toString('utf-8')
13-
const [esHost] = decoded.split('$')
14-
if (esHost) {
15-
return `https://${parts[0]}.${esHost}`
16-
}
17-
} catch {
18-
// Fallback
19-
}
20-
}
21-
throw new Error('Invalid Cloud ID format')
22-
}
23-
24-
if (!params.host) {
25-
throw new Error('Host is required for self-hosted deployments')
26-
}
27-
28-
return params.host.replace(/\/$/, '')
29-
}
30-
31-
function buildAuthHeaders(params: ElasticsearchCountParams): Record<string, string> {
32-
const headers: Record<string, string> = {
33-
'Content-Type': 'application/json',
34-
}
35-
36-
if (params.authMethod === 'api_key' && params.apiKey) {
37-
headers.Authorization = `ApiKey ${params.apiKey}`
38-
} else if (params.authMethod === 'basic_auth' && params.username && params.password) {
39-
const credentials = Buffer.from(`${params.username}:${params.password}`).toString('base64')
40-
headers.Authorization = `Basic ${credentials}`
41-
} else {
42-
throw new Error('Invalid authentication configuration')
43-
}
44-
45-
return headers
46-
}
47-
488
export const countTool: ToolConfig<ElasticsearchCountParams, ElasticsearchCountResponse> = {
499
id: 'elasticsearch_count',
5010
name: 'Elasticsearch Count',

‎apps/sim/tools/elasticsearch/create_index.ts‎

Lines changed: 1 addition & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,49 +2,9 @@ import type {
22
ElasticsearchCreateIndexParams,
33
ElasticsearchIndexResponse,
44
} from '@/tools/elasticsearch/types'
5+
import { buildAuthHeaders, buildBaseUrl } from '@/tools/elasticsearch/utils'
56
import type { ToolConfig } from '@/tools/types'
67

7-
function buildBaseUrl(params: ElasticsearchCreateIndexParams): string {
8-
if (params.deploymentType === 'cloud' && params.cloudId) {
9-
const parts = params.cloudId.split(':')
10-
if (parts.length >= 2) {
11-
try {
12-
const decoded = Buffer.from(parts[1], 'base64').toString('utf-8')
13-
const [esHost] = decoded.split('$')
14-
if (esHost) {
15-
return `https://${parts[0]}.${esHost}`
16-
}
17-
} catch {
18-
// Fallback
19-
}
20-
}
21-
throw new Error('Invalid Cloud ID format')
22-
}
23-
24-
if (!params.host) {
25-
throw new Error('Host is required for self-hosted deployments')
26-
}
27-
28-
return params.host.replace(/\/$/, '')
29-
}
30-
31-
function buildAuthHeaders(params: ElasticsearchCreateIndexParams): Record<string, string> {
32-
const headers: Record<string, string> = {
33-
'Content-Type': 'application/json',
34-
}
35-
36-
if (params.authMethod === 'api_key' && params.apiKey) {
37-
headers.Authorization = `ApiKey ${params.apiKey}`
38-
} else if (params.authMethod === 'basic_auth' && params.username && params.password) {
39-
const credentials = Buffer.from(`${params.username}:${params.password}`).toString('base64')
40-
headers.Authorization = `Basic ${credentials}`
41-
} else {
42-
throw new Error('Invalid authentication configuration')
43-
}
44-
45-
return headers
46-
}
47-
488
export const createIndexTool: ToolConfig<
499
ElasticsearchCreateIndexParams,
5010
ElasticsearchIndexResponse

‎apps/sim/tools/elasticsearch/delete_document.ts‎

Lines changed: 1 addition & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,49 +2,9 @@ import type {
22
ElasticsearchDeleteDocumentParams,
33
ElasticsearchDocumentResponse,
44
} from '@/tools/elasticsearch/types'
5+
import { buildAuthHeaders, buildBaseUrl } from '@/tools/elasticsearch/utils'
56
import type { ToolConfig } from '@/tools/types'
67

7-
function buildBaseUrl(params: ElasticsearchDeleteDocumentParams): string {
8-
if (params.deploymentType === 'cloud' && params.cloudId) {
9-
const parts = params.cloudId.split(':')
10-
if (parts.length >= 2) {
11-
try {
12-
const decoded = Buffer.from(parts[1], 'base64').toString('utf-8')
13-
const [esHost] = decoded.split('$')
14-
if (esHost) {
15-
return `https://${parts[0]}.${esHost}`
16-
}
17-
} catch {
18-
// Fallback
19-
}
20-
}
21-
throw new Error('Invalid Cloud ID format')
22-
}
23-
24-
if (!params.host) {
25-
throw new Error('Host is required for self-hosted deployments')
26-
}
27-
28-
return params.host.replace(/\/$/, '')
29-
}
30-
31-
function buildAuthHeaders(params: ElasticsearchDeleteDocumentParams): Record<string, string> {
32-
const headers: Record<string, string> = {
33-
'Content-Type': 'application/json',
34-
}
35-
36-
if (params.authMethod === 'api_key' && params.apiKey) {
37-
headers.Authorization = `ApiKey ${params.apiKey}`
38-
} else if (params.authMethod === 'basic_auth' && params.username && params.password) {
39-
const credentials = Buffer.from(`${params.username}:${params.password}`).toString('base64')
40-
headers.Authorization = `Basic ${credentials}`
41-
} else {
42-
throw new Error('Invalid authentication configuration')
43-
}
44-
45-
return headers
46-
}
47-
488
export const deleteDocumentTool: ToolConfig<
499
ElasticsearchDeleteDocumentParams,
5010
ElasticsearchDocumentResponse

‎apps/sim/tools/elasticsearch/delete_index.ts‎

Lines changed: 1 addition & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,49 +2,9 @@ import type {
22
ElasticsearchDeleteIndexParams,
33
ElasticsearchIndexResponse,
44
} from '@/tools/elasticsearch/types'
5+
import { buildAuthHeaders, buildBaseUrl } from '@/tools/elasticsearch/utils'
56
import type { ToolConfig } from '@/tools/types'
67

7-
function buildBaseUrl(params: ElasticsearchDeleteIndexParams): string {
8-
if (params.deploymentType === 'cloud' && params.cloudId) {
9-
const parts = params.cloudId.split(':')
10-
if (parts.length >= 2) {
11-
try {
12-
const decoded = Buffer.from(parts[1], 'base64').toString('utf-8')
13-
const [esHost] = decoded.split('$')
14-
if (esHost) {
15-
return `https://${parts[0]}.${esHost}`
16-
}
17-
} catch {
18-
// Fallback
19-
}
20-
}
21-
throw new Error('Invalid Cloud ID format')
22-
}
23-
24-
if (!params.host) {
25-
throw new Error('Host is required for self-hosted deployments')
26-
}
27-
28-
return params.host.replace(/\/$/, '')
29-
}
30-
31-
function buildAuthHeaders(params: ElasticsearchDeleteIndexParams): Record<string, string> {
32-
const headers: Record<string, string> = {
33-
'Content-Type': 'application/json',
34-
}
35-
36-
if (params.authMethod === 'api_key' && params.apiKey) {
37-
headers.Authorization = `ApiKey ${params.apiKey}`
38-
} else if (params.authMethod === 'basic_auth' && params.username && params.password) {
39-
const credentials = Buffer.from(`${params.username}:${params.password}`).toString('base64')
40-
headers.Authorization = `Basic ${credentials}`
41-
} else {
42-
throw new Error('Invalid authentication configuration')
43-
}
44-
45-
return headers
46-
}
47-
488
export const deleteIndexTool: ToolConfig<
499
ElasticsearchDeleteIndexParams,
5010
ElasticsearchIndexResponse

0 commit comments

Comments
 (0)