chore(server): support elasticsearch apiKey (#12405)

This commit is contained in:
fengmk2
2025-05-21 08:35:51 +00:00
parent ff15779208
commit abfc994180
12 changed files with 36 additions and 29 deletions

View File

@@ -16,6 +16,7 @@ declare global {
provider: {
type: SearchProviderType;
endpoint: string;
apiKey: string;
username: string;
password: string;
};
@@ -50,6 +51,12 @@ defineModuleConfig('indexer', {
return z.string().url().safeParse(val);
},
},
'provider.apiKey': {
desc: 'Indexer search service api key. Optional for elasticsearch',
link: 'https://www.elastic.co/guide/server/current/api-key.html',
default: '',
env: ['AFFINE_INDEXER_SEARCH_API_KEY', 'string'],
},
'provider.username': {
desc: 'Indexer search service auth username, if not set, basic auth will be disabled. Optional for elasticsearch',
link: 'https://www.elastic.co/guide/en/elasticsearch/reference/current/http-clients.html',

View File

@@ -248,7 +248,9 @@ export class ElasticsearchProvider extends SearchProvider {
const headers = {
'Content-Type': contentType,
} as Record<string, string>;
if (this.config.provider.password) {
if (this.config.provider.apiKey) {
headers.Authorization = `ApiKey ${this.config.provider.apiKey}`;
} else if (this.config.provider.password) {
headers.Authorization = `Basic ${Buffer.from(`${this.config.provider.username}:${this.config.provider.password}`).toString('base64')}`;
}
const response = await fetch(url, {