Skip to content

Commit eac44a9

Browse files
authored
fix(kubernetes): fix HTTPS module bundling issues in production (#1141)
fix(kubernetes): remove unsued variables
1 parent 0074802 commit eac44a9

3 files changed

Lines changed: 48 additions & 35 deletions

File tree

packages/k8s-client/src/request.ts

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
* SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Juno contributors
33
* SPDX-License-Identifier: Apache-2.0
44
*/
5-
import https from "https"
65
import { buildUrl } from "./urlHelpers"
76
import * as logger from "./logger"
87
import { K8sApiError } from "./apiErrorHandler"
8+
import https from "https"
99

1010
// Define the shape of the options parameter
1111
interface RequestOptions {
@@ -16,8 +16,8 @@ interface RequestOptions {
1616
mode?: RequestMode
1717
cache?: RequestCache
1818
credentials?: RequestCredentials
19-
ignoreSsl?: boolean // New option to ignore SSL certificate validation
20-
debug?: boolean // Optional debug flag
19+
ignoreSsl?: boolean
20+
debug?: boolean
2121
[key: string]: any
2222
}
2323

@@ -27,11 +27,31 @@ const checkStatus = (response: Response): Response => {
2727
return response
2828
} else {
2929
const error = new Error(response.statusText || `${response.status}`)
30-
;(error as K8sApiError).response = response // Type assertion to attach the response to the error
30+
;(error as K8sApiError).response = response
3131
throw error
3232
}
3333
}
3434

35+
// Helper function to create HTTPS agent only in Node.js
36+
function createHttpsAgent(ignoreSsl: boolean, url: string): https.Agent | undefined {
37+
if (!ignoreSsl || !url.startsWith("https:") || typeof window !== "undefined") {
38+
return undefined
39+
}
40+
41+
try {
42+
if (!https?.Agent) {
43+
return undefined
44+
}
45+
46+
return new https.Agent({
47+
rejectUnauthorized: false,
48+
})
49+
} catch (_error) {
50+
// https module not available (browser environment or bundle)
51+
return undefined
52+
}
53+
}
54+
3555
/**
3656
* Creates a request.
3757
*
@@ -41,57 +61,50 @@ const checkStatus = (response: Response): Response => {
4161
* @return {Promise<Response>} The response promise.
4262
*/
4363
function request(method: string, url: string, options: RequestOptions = {}): Promise<Response> {
44-
// add params to url
64+
// Add params to url
4565
if (options.params) url = buildUrl(url, options.params)
4666

4767
// Handle SSL ignore option
48-
const { ignoreSsl, ...restOptions } = options
68+
const { ignoreSsl, debug, ...restOptions } = options
4969

50-
// Create HTTPS agent if SSL should be ignored for HTTPS URLs
51-
let agent: https.Agent | undefined
70+
// Create HTTPS agent if needed (Node.js only)
71+
const agent = createHttpsAgent(ignoreSsl || false, url)
5272

53-
if (ignoreSsl && url.startsWith("https:") && typeof window === "undefined") {
54-
agent = new https.Agent({
55-
rejectUnauthorized: false,
56-
})
57-
58-
// Log warning when SSL verification is disabled
59-
if (process.env.NODE_ENV !== "test" && options.debug === true) {
60-
// Avoid spam in tests
61-
logger.debug(`⚠️ SSL verification disabled for request to: ${url}`)
62-
}
73+
// Log warning when SSL verification is disabled
74+
if (agent && debug === true && process.env.NODE_ENV !== "test") {
75+
logger.debug(`⚠️ SSL verification disabled for request to: ${url}`)
6376
}
6477

65-
// add allowed options to fetch (excluding ignoreSsl as it's handled separately)
78+
// Add allowed options to fetch (excluding ignoreSsl as it's handled separately)
6679
const requestFields = ["signal", "headers", "body", "mode", "cache", "credentials"] as const
67-
const fetchOptions: RequestInit & { agent?: https.Agent } = requestFields.reduce(
80+
const fetchOptions: RequestInit & { agent?: typeof agent } = requestFields.reduce(
6881
(map, key) => {
6982
if (restOptions[key]) {
7083
return { ...map, [key]: restOptions[key] }
7184
}
7285
return map
7386
},
74-
{ credentials: "same-origin", method } as RequestInit & { agent?: https.Agent }
87+
{ credentials: "same-origin", method } as RequestInit & { agent?: typeof agent }
7588
)
7689

77-
// Add agent if SSL should be ignored (Node.js environment only)
90+
// Add agent if available
7891
if (agent) {
7992
fetchOptions.agent = agent
8093
}
8194

82-
// stringify body if it's an object
95+
// Stringify body if it's an object
8396
if (fetchOptions.body && typeof fetchOptions.body !== "string") {
8497
fetchOptions.body = JSON.stringify(fetchOptions.body)
8598
}
8699

87-
if (options.debug === true) {
100+
if (debug === true) {
88101
logger.debug("fetch >", url, {
89102
...fetchOptions,
90103
agent: agent ? "HTTPS Agent (SSL ignored)" : fetchOptions.agent,
91104
})
92105
}
93106

94-
// make the call
107+
// Make the call
95108
return fetch(url, fetchOptions).then(checkStatus)
96109
}
97110

packages/k8s-client/test/request.test.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,8 @@ import https from "https"
99

1010
// Mock https module with proper return value
1111
vi.mock("https", () => ({
12-
default: {
13-
Agent: vi.fn(),
14-
},
1512
Agent: vi.fn(),
13+
default: { Agent: vi.fn() },
1614
}))
1715

1816
const testUrl = "https://apiEndpoint.com"
@@ -26,13 +24,6 @@ describe("request", () => {
2624
mockFetch = vi.fn()
2725
mockFetch.mockResolvedValue({ status: 200 } as Response)
2826
global.fetch = mockFetch
29-
30-
// Mock https.Agent for SSL tests
31-
const mockAgent = {} as https.Agent
32-
Object.defineProperty(https, "Agent", {
33-
value: vi.fn().mockImplementation(() => mockAgent),
34-
writable: true,
35-
})
3627
})
3728

3829
afterEach(() => {

packages/k8s-client/vite.config.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@ export default {
1414
fileName: (format) => `index.${format}.js`, // Output file names
1515
},
1616
outDir: "build",
17+
rollupOptions: {
18+
// Treat these as externals - they won't be bundled
19+
external: ["https"],
20+
output: {
21+
globals: {
22+
https: "https",
23+
},
24+
},
25+
},
1726
},
1827
plugins: [
1928
dts({

0 commit comments

Comments
 (0)