Skip to content

Commit 5fede9e

Browse files
committed
chore(kubernetes-client): add ability to ignore ssl
1 parent b69df69 commit 5fede9e

9 files changed

Lines changed: 825 additions & 231 deletions

File tree

commitlint.config.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export const scopes = [
3131
"heureka",
3232
"infra",
3333
"juno",
34-
"k8s",
34+
"kubernetes-client",
3535
"message-provider",
3636
"main",
3737
"npm",

packages/k8s-client/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@cloudoperators/juno-k8s-client",
3-
"version": "1.0.7",
3+
"version": "1.1.0",
44
"author": "UI-Team",
55
"description": "JavaScript client for Kubernetes API",
66
"main": "build/index.cjs.js",

packages/k8s-client/src/client.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,33 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
import request from "./request"
6+
import request, { RequestOptions } from "./request"
77
import { buildUrl } from "./urlHelpers"
88
import { Watch, ADDED, MODIFIED, DELETED, ERROR } from "./watch"
99
import handleApiError, { K8sApiError } from "./apiErrorHandler"
1010

1111
interface ClientOptions {
1212
apiEndpoint: string
1313
token: string
14+
ignoreSsl?: boolean // New option to ignore SSL certificate validation
15+
debug?: boolean // Optional debug flag
1416
[key: string]: any // To allow additional properties if needed
1517
}
1618

17-
interface RequestOptions {
18-
params?: Record<string, any>
19-
headers?: Record<string, string>
20-
body?: Object | null
21-
signal?: AbortSignal
22-
mode?: RequestMode
23-
cache?: RequestCache
24-
credentials?: RequestCredentials
25-
}
26-
2719
function createClient(options: ClientOptions) {
28-
const { apiEndpoint } = options
20+
const { apiEndpoint, ignoreSsl = false } = options
2921
let token = options.token
3022

3123
if (!apiEndpoint || !token) {
3224
throw new Error(`Bad options: ${JSON.stringify(options, null, 4)}. Please provide apiEndpoint and token`)
3325
}
3426

27+
// Log warning when SSL verification is disabled
28+
if (ignoreSsl && options.debug === true) {
29+
console.warn(`⚠️ K8s Client: SSL certificate verification disabled for ${apiEndpoint}`)
30+
console.warn(`🔒 This should only be used in development or secure internal networks`)
31+
}
32+
3533
const defaultHeaders = () => ({
3634
Authorization: `Bearer ${token}`,
3735
"Content-Type": "application/json",
@@ -44,10 +42,14 @@ function createClient(options: ClientOptions) {
4442
...options.headers,
4543
}
4644

45+
// Use per-request ignoreSsl if provided, otherwise fall back to client-level setting
46+
const shouldIgnoreSsl = options.ignoreSsl !== undefined ? options.ignoreSsl : ignoreSsl
47+
4748
return {
4849
...defaultOptions,
4950
...options,
5051
headers,
52+
ignoreSsl: shouldIgnoreSsl, // Pass ignoreSsl to request function
5153
}
5254
}
5355

packages/k8s-client/src/request.ts

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Juno contributors
33
* SPDX-License-Identifier: Apache-2.0
44
*/
5-
5+
import https from "https"
66
import { buildUrl } from "./urlHelpers"
77
import * as logger from "./logger"
88
import { K8sApiError } from "./apiErrorHandler"
@@ -11,11 +11,13 @@ import { K8sApiError } from "./apiErrorHandler"
1111
interface RequestOptions {
1212
params?: Record<string, any>
1313
signal?: AbortSignal
14-
headers?: HeadersInit | null
14+
headers?: Record<string, any>
1515
body?: Object | null
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
1921
[key: string]: any
2022
}
2123

@@ -35,36 +37,62 @@ const checkStatus = (response: Response): Response => {
3537
*
3638
* @param {string} method Http method.
3739
* @param {string} url The URL to send the request to.
38-
* @param {RequestOptions} options params, headers, and other options supported by fetch.
40+
* @param {RequestOptions} options params, headers, ignoreSsl, and other options supported by fetch.
3941
* @return {Promise<Response>} The response promise.
4042
*/
4143
function request(method: string, url: string, options: RequestOptions = {}): Promise<Response> {
4244
// add params to url
4345
if (options.params) url = buildUrl(url, options.params)
4446

45-
// add allowed options to fetch
46-
const requestFields = ["signal", "headers", "body", "mode", "cache", "credentials"] as const
47+
// Handle SSL ignore option
48+
const { ignoreSsl, ...restOptions } = options
49+
50+
// Create HTTPS agent if SSL should be ignored for HTTPS URLs
51+
let agent: https.Agent | undefined
52+
if (ignoreSsl && url.startsWith("https:")) {
53+
agent = new https.Agent({
54+
rejectUnauthorized: false,
55+
})
56+
57+
// Log warning when SSL verification is disabled
58+
if (process.env.NODE_ENV !== "test" && options.debug === true) {
59+
// Avoid spam in tests
60+
logger.debug(`⚠️ SSL verification disabled for request to: ${url}`)
61+
}
62+
}
4763

48-
const fetchOptions: RequestInit = requestFields.reduce(
64+
// add allowed options to fetch (excluding ignoreSsl as it's handled separately)
65+
const requestFields = ["signal", "headers", "body", "mode", "cache", "credentials"] as const
66+
const fetchOptions: RequestInit & { agent?: https.Agent } = requestFields.reduce(
4967
(map, key) => {
50-
if (options[key]) {
51-
return { ...map, [key]: options[key] }
68+
if (restOptions[key]) {
69+
return { ...map, [key]: restOptions[key] }
5270
}
53-
5471
return map
5572
},
56-
{ credentials: "same-origin", method }
73+
{ credentials: "same-origin", method } as RequestInit & { agent?: https.Agent }
5774
)
5875

76+
// Add agent if SSL should be ignored (Node.js environment only)
77+
if (agent) {
78+
fetchOptions.agent = agent
79+
}
80+
5981
// stringify body if it's an object
6082
if (fetchOptions.body && typeof fetchOptions.body !== "string") {
6183
fetchOptions.body = JSON.stringify(fetchOptions.body)
6284
}
6385

64-
logger.debug("fetch >", url, fetchOptions)
86+
if (options.debug === true) {
87+
logger.debug("fetch >", url, {
88+
...fetchOptions,
89+
agent: agent ? "HTTPS Agent (SSL ignored)" : fetchOptions.agent,
90+
})
91+
}
6592

6693
// make the call
6794
return fetch(url, fetchOptions).then(checkStatus)
6895
}
6996

7097
export default request
98+
export type { RequestOptions }

packages/k8s-client/src/watch.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ interface WatchOptions {
4040
mode?: RequestMode
4141
cache?: RequestCache
4242
credentials?: RequestCredentials
43+
ignoreSsl?: boolean // Allow per-request SSL override
44+
debug?: boolean // Optional debug flag
4345
}
4446

4547
class Watch {

0 commit comments

Comments
 (0)