Skip to content

Commit db0497a

Browse files
[9.0] [ML] Fix OpenAI connector does not use the action proxy configuration for all subactions (#219617) (#224124)
# Backport This will backport the following commits from `main` to `9.0`: - [[ML] Fix OpenAI connector does not use the action proxy configuration for all subactions (#219617)](#219617) <!--- Backport version: 10.0.0 --> ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sorenlouv/backport) <!--BACKPORT [{"author":{"name":"Quynh Nguyen (Quinn)","email":"43350163+qn895@users.noreply.github.com"},"sourceCommit":{"committedDate":"2025-06-05T17:34:27Z","message":"[ML] Fix OpenAI connector does not use the action proxy configuration for all subactions (#219617)\n\n## Summary\n\nThis PR fixes #214057 by adding\nthe httpsAgent/httpAgent to the OpenAI client.\n\n### Checklist\n\nCheck the PR satisfies following conditions. \n\nReviewers should verify this PR satisfies this list as well.\n\n- [ ] Any text added follows [EUI's writing\nguidelines](https://elastic.github.io/eui/#/guidelines/writing), uses\nsentence case text and includes [i18n\nsupport](https://github.com/elastic/kibana/blob/main/src/platform/packages/shared/kbn-i18n/README.md)\n- [ ]\n[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)\nwas added for features that require explanation or tutorials\n- [x] [Unit or functional\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\nwere updated or added to match the most common scenarios\n- [ ] If a plugin configuration key changed, check if it needs to be\nallowlisted in the cloud and added to the [docker\nlist](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker)\n- [ ] This was checked for breaking HTTP API changes, and any breaking\nchanges have been approved by the breaking-change committee. The\n`release_note:breaking` label should be applied in these situations.\n- [ ] [Flaky Test\nRunner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1) was\nused on any tests changed\n- [ ] The PR description includes the appropriate Release Notes section,\nand the correct `release_note:*` label is applied per the\n[guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)\n\n### Identify risks\n\nDoes this PR introduce any risks? For example, consider risks like hard\nto test bugs, performance regression, potential of data loss.\n\nDescribe the risk, its severity, and mitigation for each identified\nrisk. Invite stakeholders and evaluate how to proceed before merging.\n\n- [ ] [See some risk\nexamples](https://github.com/elastic/kibana/blob/main/RISK_MATRIX.mdx)\n- [ ] ...\n\n---------\n\nCo-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>","sha":"2564d6de38855a32be94ececd062cf1820dc52bb","branchLabelMapping":{"^v9.1.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["bug","release_note:fix",":ml","backport:version","Team:AI Infra","v9.1.0","v8.19.0","v9.0.3","v8.18.3"],"title":"[ML] Fix OpenAI connector does not use the action proxy configuration for all subactions","number":219617,"url":"https://github.com/elastic/kibana/pull/219617","mergeCommit":{"message":"[ML] Fix OpenAI connector does not use the action proxy configuration for all subactions (#219617)\n\n## Summary\n\nThis PR fixes #214057 by adding\nthe httpsAgent/httpAgent to the OpenAI client.\n\n### Checklist\n\nCheck the PR satisfies following conditions. \n\nReviewers should verify this PR satisfies this list as well.\n\n- [ ] Any text added follows [EUI's writing\nguidelines](https://elastic.github.io/eui/#/guidelines/writing), uses\nsentence case text and includes [i18n\nsupport](https://github.com/elastic/kibana/blob/main/src/platform/packages/shared/kbn-i18n/README.md)\n- [ ]\n[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)\nwas added for features that require explanation or tutorials\n- [x] [Unit or functional\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\nwere updated or added to match the most common scenarios\n- [ ] If a plugin configuration key changed, check if it needs to be\nallowlisted in the cloud and added to the [docker\nlist](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker)\n- [ ] This was checked for breaking HTTP API changes, and any breaking\nchanges have been approved by the breaking-change committee. The\n`release_note:breaking` label should be applied in these situations.\n- [ ] [Flaky Test\nRunner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1) was\nused on any tests changed\n- [ ] The PR description includes the appropriate Release Notes section,\nand the correct `release_note:*` label is applied per the\n[guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)\n\n### Identify risks\n\nDoes this PR introduce any risks? For example, consider risks like hard\nto test bugs, performance regression, potential of data loss.\n\nDescribe the risk, its severity, and mitigation for each identified\nrisk. Invite stakeholders and evaluate how to proceed before merging.\n\n- [ ] [See some risk\nexamples](https://github.com/elastic/kibana/blob/main/RISK_MATRIX.mdx)\n- [ ] ...\n\n---------\n\nCo-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>","sha":"2564d6de38855a32be94ececd062cf1820dc52bb"}},"sourceBranch":"main","suggestedTargetBranches":["9.0","8.18"],"targetPullRequestStates":[{"branch":"main","label":"v9.1.0","branchLabelMappingKey":"^v9.1.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/219617","number":219617,"mergeCommit":{"message":"[ML] Fix OpenAI connector does not use the action proxy configuration for all subactions (#219617)\n\n## Summary\n\nThis PR fixes #214057 by adding\nthe httpsAgent/httpAgent to the OpenAI client.\n\n### Checklist\n\nCheck the PR satisfies following conditions. \n\nReviewers should verify this PR satisfies this list as well.\n\n- [ ] Any text added follows [EUI's writing\nguidelines](https://elastic.github.io/eui/#/guidelines/writing), uses\nsentence case text and includes [i18n\nsupport](https://github.com/elastic/kibana/blob/main/src/platform/packages/shared/kbn-i18n/README.md)\n- [ ]\n[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)\nwas added for features that require explanation or tutorials\n- [x] [Unit or functional\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\nwere updated or added to match the most common scenarios\n- [ ] If a plugin configuration key changed, check if it needs to be\nallowlisted in the cloud and added to the [docker\nlist](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker)\n- [ ] This was checked for breaking HTTP API changes, and any breaking\nchanges have been approved by the breaking-change committee. The\n`release_note:breaking` label should be applied in these situations.\n- [ ] [Flaky Test\nRunner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1) was\nused on any tests changed\n- [ ] The PR description includes the appropriate Release Notes section,\nand the correct `release_note:*` label is applied per the\n[guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)\n\n### Identify risks\n\nDoes this PR introduce any risks? For example, consider risks like hard\nto test bugs, performance regression, potential of data loss.\n\nDescribe the risk, its severity, and mitigation for each identified\nrisk. Invite stakeholders and evaluate how to proceed before merging.\n\n- [ ] [See some risk\nexamples](https://github.com/elastic/kibana/blob/main/RISK_MATRIX.mdx)\n- [ ] ...\n\n---------\n\nCo-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>","sha":"2564d6de38855a32be94ececd062cf1820dc52bb"}},{"branch":"8.19","label":"v8.19.0","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"url":"https://github.com/elastic/kibana/pull/222869","number":222869,"state":"MERGED","mergeCommit":{"sha":"b48ff971cada9153ab220a78d012204a53681efe","message":"[8.19] [ML] Fix OpenAI connector does not use the action proxy configuration for all subactions (#219617) (#222869)\n\n# Backport\n\nThis will backport the following commits from `main` to `8.19`:\n- [[ML] Fix OpenAI connector does not use the action proxy configuration\nfor all subactions\n(#219617)](https://github.com/elastic/kibana/pull/219617)\n\n\n\n### Questions ?\nPlease refer to the [Backport tool\ndocumentation](https://github.com/sorenlouv/backport)\n\n\n\nCo-authored-by: Quynh Nguyen (Quinn) <43350163+qn895@users.noreply.github.com>\nCo-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>"}},{"branch":"9.0","label":"v9.0.3","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"8.18","label":"v8.18.3","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"}]}] BACKPORT--> Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
1 parent d322792 commit db0497a

4 files changed

Lines changed: 155 additions & 2 deletions

File tree

‎x-pack/platform/plugins/shared/actions/server/lib/get_custom_agents.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ import { ActionsConfigurationUtilities } from '../actions_config';
1414
import { getNodeSSLOptions, getSSLSettingsFromConfig } from './get_node_ssl_options';
1515
import { SSLSettings } from '../types';
1616

17+
/**
18+
* Create http and https proxy agents with custom proxy /hosts/SSL settings from configurationUtilities
19+
*/
1720
interface GetCustomAgentsResponse {
1821
httpAgent: HttpAgent | undefined;
1922
httpsAgent: HttpsAgent | undefined;

‎x-pack/platform/plugins/shared/actions/server/sub_action_framework/sub_action_connector.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export abstract class SubActionConnector<Config, Secrets> {
4343
[k: string]: ((params: unknown) => unknown) | unknown;
4444
private axiosInstance: AxiosInstance;
4545
private subActions: Map<string, SubAction> = new Map();
46-
private configurationUtilities: ActionsConfigurationUtilities;
46+
protected configurationUtilities: ActionsConfigurationUtilities;
4747
protected readonly kibanaRequest?: KibanaRequest;
4848
protected logger: Logger;
4949
protected esClient: ElasticsearchClient;
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
8+
import { DEFAULT_TIMEOUT_MS, OPENAI_CONNECTOR_ID } from '../../../common/openai/constants';
9+
import { actionsMock } from '@kbn/actions-plugin/server/mocks';
10+
import { DEFAULT_OPENAI_MODEL } from '../../../common/openai/constants';
11+
import { actionsConfigMock } from '@kbn/actions-plugin/server/actions_config.mock';
12+
import { OpenAIConnector } from './openai';
13+
import { OpenAiProviderType } from '../../../common/openai/constants';
14+
import { loggingSystemMock } from '@kbn/core/server/mocks';
15+
import { ConnectorUsageCollector } from '@kbn/actions-plugin/server/types';
16+
import { RunActionResponseSchema } from '../../../common/openai/schema';
17+
18+
const logger = loggingSystemMock.createLogger();
19+
20+
// Mock an instance of the OpenAI class
21+
// with overridden flag for purpose of jest test
22+
jest.mock('openai', () => {
23+
const UnmodifiedOpenAIClient = jest.requireActual('openai').default;
24+
25+
return {
26+
__esModule: true,
27+
default: jest.fn().mockImplementation((config) => {
28+
return new UnmodifiedOpenAIClient({
29+
...config,
30+
dangerouslyAllowBrowser: true,
31+
});
32+
}),
33+
};
34+
});
35+
describe('OpenAI with proxy config', () => {
36+
let mockProxiedRequest: jest.Mock;
37+
let connectorUsageCollector: ConnectorUsageCollector;
38+
const mockDefaults = {
39+
timeout: DEFAULT_TIMEOUT_MS,
40+
url: 'https://api.openai.com/v1/chat/completions',
41+
method: 'post',
42+
responseSchema: RunActionResponseSchema,
43+
};
44+
45+
const mockResponse = {
46+
headers: {},
47+
data: {},
48+
};
49+
50+
const configurationUtilities = actionsConfigMock.create();
51+
const PROXY_HOST = 'proxy.custom.elastic.co';
52+
const PROXY_URL = `http://${PROXY_HOST}`;
53+
54+
configurationUtilities.getProxySettings.mockReturnValue({
55+
proxyUrl: PROXY_URL,
56+
proxySSLSettings: {
57+
verificationMode: 'none',
58+
},
59+
proxyBypassHosts: undefined,
60+
proxyOnlyHosts: undefined,
61+
});
62+
63+
const connector = new OpenAIConnector({
64+
configurationUtilities,
65+
connector: { id: '1', type: OPENAI_CONNECTOR_ID },
66+
config: {
67+
apiUrl: 'https://api.openai.com/v1/chat/completions',
68+
apiProvider: OpenAiProviderType.OpenAi,
69+
defaultModel: DEFAULT_OPENAI_MODEL,
70+
organizationId: 'org-id',
71+
projectId: 'proj-id',
72+
headers: {
73+
'X-My-Custom-Header': 'foo',
74+
Authorization: 'override',
75+
},
76+
},
77+
secrets: { apiKey: '123' },
78+
logger,
79+
services: actionsMock.createServices(),
80+
});
81+
82+
const sampleOpenAiBody = {
83+
messages: [
84+
{
85+
role: 'user',
86+
content: 'Hello world',
87+
},
88+
],
89+
};
90+
91+
beforeEach(() => {
92+
connectorUsageCollector = new ConnectorUsageCollector({
93+
logger,
94+
connectorId: 'test-connector-id',
95+
});
96+
mockProxiedRequest = jest.fn().mockResolvedValue(mockResponse);
97+
// @ts-ignore
98+
connector.request = mockProxiedRequest;
99+
jest.clearAllMocks();
100+
});
101+
102+
it('verifies that the OpenAI client is initialized with the custom proxy HTTP agent', () => {
103+
// @ts-ignore .openAI is private
104+
const openAIClient = connector.openAI;
105+
106+
// Verify the client was initialized with the custom agent configuration
107+
expect(openAIClient).toBeDefined();
108+
expect(openAIClient.httpAgent).toBeDefined();
109+
expect(openAIClient.httpAgent.proxy).toBeDefined();
110+
expect(openAIClient.httpAgent.proxy.host).toBe(PROXY_HOST);
111+
expect(openAIClient.httpAgent.proxy.port).toBe(80);
112+
});
113+
114+
it('verifies that requests use the configured HTTP agent', async () => {
115+
// Make a test request
116+
const response = await connector.runApi(
117+
{ body: JSON.stringify(sampleOpenAiBody) },
118+
connectorUsageCollector
119+
);
120+
expect(mockProxiedRequest).toBeCalledTimes(1);
121+
expect(mockProxiedRequest).toHaveBeenCalledWith(
122+
{
123+
...mockDefaults,
124+
signal: undefined,
125+
data: JSON.stringify({
126+
...sampleOpenAiBody,
127+
stream: false,
128+
model: DEFAULT_OPENAI_MODEL,
129+
}),
130+
headers: {
131+
Authorization: 'Bearer 123',
132+
'X-My-Custom-Header': 'foo',
133+
'content-type': 'application/json',
134+
'OpenAI-Organization': 'org-id',
135+
'OpenAI-Project': 'proj-id',
136+
},
137+
},
138+
connectorUsageCollector
139+
);
140+
expect(response).toEqual(mockResponse.data);
141+
});
142+
});

‎x-pack/platform/plugins/shared/stack_connectors/server/connector_types/openai/openai.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
} from 'openai/resources/chat/completions';
1818
import { Stream } from 'openai/streaming';
1919
import { ConnectorUsageCollector } from '@kbn/actions-plugin/server/types';
20+
import { getCustomAgents } from '@kbn/actions-plugin/server/lib/get_custom_agents';
2021
import { removeEndpointFromUrl } from './lib/openai_utils';
2122
import {
2223
RunActionParamsSchema,
@@ -63,7 +64,6 @@ export class OpenAIConnector extends SubActionConnector<Config, Secrets> {
6364

6465
constructor(params: ServiceParams<Config, Secrets>) {
6566
super(params);
66-
6767
this.url = this.config.apiUrl;
6868
this.provider = this.config.apiProvider;
6969
this.key = this.secrets.apiKey;
@@ -75,6 +75,12 @@ export class OpenAIConnector extends SubActionConnector<Config, Secrets> {
7575
...('projectId' in this.config ? { 'OpenAI-Project': this.config.projectId } : {}),
7676
};
7777

78+
const { httpAgent, httpsAgent } = getCustomAgents(
79+
this.configurationUtilities,
80+
this.logger,
81+
this.url
82+
);
83+
7884
this.openAI =
7985
this.config.apiProvider === OpenAiProviderType.AzureAi
8086
? new OpenAI({
@@ -85,13 +91,15 @@ export class OpenAIConnector extends SubActionConnector<Config, Secrets> {
8591
...this.headers,
8692
'api-key': this.secrets.apiKey,
8793
},
94+
httpAgent: httpsAgent ?? httpAgent,
8895
})
8996
: new OpenAI({
9097
baseURL: removeEndpointFromUrl(this.config.apiUrl),
9198
apiKey: this.secrets.apiKey,
9299
defaultHeaders: {
93100
...this.headers,
94101
},
102+
httpAgent: httpsAgent ?? httpAgent,
95103
});
96104

97105
this.registerSubActions();

0 commit comments

Comments
 (0)