chore: use proper proxy configuration

Signed-off-by: Vladyslav Zhukovskyi <vzhukovs@redhat.com>
This commit is contained in:
Vladyslav Zhukovskyi 2024-04-29 12:58:18 +03:00
parent 106f54fc4a
commit 6fa3cbf5b0
2 changed files with 44 additions and 21 deletions

View file

@ -113,6 +113,7 @@ const proxy: Proxy = {
onDidStateChange: vi.fn(),
onDidUpdateProxy: onDidUpdateProxyEmitter.event,
isEnabled: isEnabledProxyMock,
proxy: vi.fn(),
} as unknown as Proxy;
const configurationRegistry: ConfigurationRegistry = {
@ -181,18 +182,28 @@ test('check getHttpOptions with Proxy', async () => {
getAllCertificatesMock.mockReturnValue(['1', '2', '3']);
isEnabledProxyMock.mockReturnValue(true);
extensionsCatalog = new ExtensionsCatalog(certificates, proxy, configurationRegistry);
onDidUpdateProxyEmitter.fire({
httpProxy: 'http://proxy:8080',
httpsProxy: 'http://proxy:8080',
const proxy: Proxy = {
onDidStateChange: vi.fn(),
onDidUpdateProxy: vi.fn(),
isEnabled: isEnabledProxyMock,
proxy: vi.fn(),
} as unknown as Proxy;
vi.spyOn(proxy, 'proxy', 'get').mockReturnValue({
httpProxy: 'http://localhost',
httpsProxy: 'http://localhost',
noProxy: 'localhost',
});
extensionsCatalog = new ExtensionsCatalog(certificates, proxy, configurationRegistry);
const options = extensionsCatalog.getHttpOptions();
expect(options).toBeDefined();
// expect the two agents being defined
expect(options.agent?.http).toBeDefined();
expect(options.agent?.https).toBeDefined();
// @ts-expect-error proxy property exists on http object
expect(options.agent?.http?.proxy.href).toBe('http://localhost/');
// @ts-expect-error proxy property exists on https object
expect(options.agent?.https?.proxy.href).toBe('http://localhost/');
// certificates should be 1, 2, 3
expect(options.https?.certificateAuthority).toBeDefined();
@ -292,3 +303,29 @@ test('should fetch alternate link', async () => {
// no error
expect(console.error).not.toBeCalled();
});
test('Should use proxy object if proxySettings is undefined', () => {
isEnabledProxyMock.mockReturnValue(true);
const proxy: Proxy = {
onDidStateChange: vi.fn(),
onDidUpdateProxy: vi.fn(),
isEnabled: isEnabledProxyMock,
proxy: vi.fn(),
} as unknown as Proxy;
vi.spyOn(proxy, 'proxy', 'get').mockReturnValue({
httpProxy: 'http://localhost',
httpsProxy: 'https://localhost',
noProxy: 'localhost',
});
extensionsCatalog = new ExtensionsCatalog(certificates, proxy, configurationRegistry);
const options = extensionsCatalog.getHttpOptions();
expect(options).toBeDefined();
// expect the two agents being defined
expect(options.agent?.http).toBeDefined();
expect(options.agent?.https).toBeDefined();
// @ts-expect-error proxy property exists on http object
expect(options.agent?.http?.proxy.href).toBe('http://localhost/');
// @ts-expect-error proxy property exists on https object
expect(options.agent?.https?.proxy.href).toBe('https://localhost/');
});

View file

@ -16,7 +16,6 @@
* SPDX-License-Identifier: Apache-2.0
***********************************************************************/
import type * as podmanDesktopAPI from '@podman-desktop/api';
import type { HttpsOptions, OptionsOfTextResponseBody } from 'got';
import got from 'got';
import { HttpProxyAgent, HttpsProxyAgent } from 'hpagent';
@ -37,9 +36,6 @@ import { ExtensionsCatalogSettings } from './extensions-catalog-settings.js';
export class ExtensionsCatalog {
public static readonly DEFAULT_EXTENSIONS_URL = 'https://registry.podman-desktop.io/api/extensions.json';
private proxySettings: podmanDesktopAPI.ProxySettings | undefined;
private proxyEnabled: boolean;
private lastFetchTime = 0;
private cachedCatalog: InternalCatalogJSON | undefined;
static readonly CACHE_TIMEOUT = 1000 * 60 * 60 * 4; // 4 hours
@ -48,17 +44,7 @@ export class ExtensionsCatalog {
private certificates: Certificates,
private proxy: Proxy,
private configurationRegistry: ConfigurationRegistry,
) {
this.proxy.onDidUpdateProxy(settings => {
this.proxySettings = settings;
});
this.proxy.onDidStateChange(state => {
this.proxyEnabled = state;
});
this.proxyEnabled = this.proxy.isEnabled();
}
) {}
init(): void {
// register a configuration
@ -190,9 +176,9 @@ export class ExtensionsCatalog {
options.https.certificateAuthority = this.certificates.getAllCertificates();
}
if (this.proxyEnabled) {
if (this.proxy.isEnabled()) {
// use proxy when performing got request
const proxy = this.proxySettings;
const proxy = this.proxy.proxy;
const httpProxyUrl = proxy?.httpProxy;
const httpsProxyUrl = proxy?.httpsProxy;