Improve OpenSearchURL credential storing user and password in separate fields from the URL (#2549)

OpenSearch - Improve OpenSearchURL credential storing user and password in separate fields from the URL
This commit is contained in:
Daniel D'Abate 2024-06-04 03:31:36 +02:00 committed by GitHub
parent 76abd20e85
commit a799ac8087
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 36 additions and 9 deletions

View File

@ -10,12 +10,26 @@ class OpenSearchUrl implements INodeCredential {
constructor() { constructor() {
this.label = 'OpenSearch' this.label = 'OpenSearch'
this.name = 'openSearchUrl' this.name = 'openSearchUrl'
this.version = 1.0 this.version = 2.0
this.inputs = [ this.inputs = [
{ {
label: 'OpenSearch Url', label: 'OpenSearch Url',
name: 'openSearchUrl', name: 'openSearchUrl',
type: 'string' type: 'string'
},
{
label: 'User',
name: 'user',
type: 'string',
placeholder: '<OPENSEARCH_USERNAME>',
optional: true
},
{
label: 'Password',
name: 'password',
type: 'password',
placeholder: '<OPENSEARCH_PASSWORD>',
optional: true
} }
] ]
} }

View File

@ -23,7 +23,7 @@ class OpenSearch_VectorStores implements INode {
constructor() { constructor() {
this.label = 'OpenSearch' this.label = 'OpenSearch'
this.name = 'openSearch' this.name = 'openSearch'
this.version = 2.0 this.version = 3.0
this.type = 'OpenSearch' this.type = 'OpenSearch'
this.icon = 'opensearch.svg' this.icon = 'opensearch.svg'
this.category = 'Vector Stores' this.category = 'Vector Stores'
@ -87,6 +87,10 @@ class OpenSearch_VectorStores implements INode {
const credentialData = await getCredentialData(nodeData.credential ?? '', options) const credentialData = await getCredentialData(nodeData.credential ?? '', options)
const opensearchURL = getCredentialParam('openSearchUrl', credentialData, nodeData) const opensearchURL = getCredentialParam('openSearchUrl', credentialData, nodeData)
const user = getCredentialParam('user', credentialData, nodeData)
const password = getCredentialParam('password', credentialData, nodeData)
const client = getOpenSearchClient(opensearchURL, user, password)
const flattenDocs = docs && docs.length ? flatten(docs) : [] const flattenDocs = docs && docs.length ? flatten(docs) : []
const finalDocs = [] const finalDocs = []
@ -96,10 +100,6 @@ class OpenSearch_VectorStores implements INode {
} }
} }
const client = new Client({
nodes: [opensearchURL]
})
try { try {
await OpenSearchVectorStore.fromDocuments(finalDocs, embeddings, { await OpenSearchVectorStore.fromDocuments(finalDocs, embeddings, {
client, client,
@ -121,10 +121,10 @@ class OpenSearch_VectorStores implements INode {
const credentialData = await getCredentialData(nodeData.credential ?? '', options) const credentialData = await getCredentialData(nodeData.credential ?? '', options)
const opensearchURL = getCredentialParam('openSearchUrl', credentialData, nodeData) const opensearchURL = getCredentialParam('openSearchUrl', credentialData, nodeData)
const user = getCredentialParam('user', credentialData, nodeData)
const password = getCredentialParam('password', credentialData, nodeData)
const client = new Client({ const client = getOpenSearchClient(opensearchURL, user, password)
nodes: [opensearchURL]
})
const vectorStore = new OpenSearchVectorStore(embeddings, { const vectorStore = new OpenSearchVectorStore(embeddings, {
client, client,
@ -142,4 +142,17 @@ class OpenSearch_VectorStores implements INode {
} }
} }
const getOpenSearchClient = (url: string, user?: string, password?: string): Client => {
if (user && password) {
const urlObj = new URL(url)
urlObj.username = user
urlObj.password = password
url = urlObj.toString()
}
return new Client({
nodes: [url]
})
}
module.exports = { nodeClass: OpenSearch_VectorStores } module.exports = { nodeClass: OpenSearch_VectorStores }