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"
66import { buildUrl } from "./urlHelpers"
77import * as logger from "./logger"
88import { K8sApiError } from "./apiErrorHandler"
@@ -11,11 +11,13 @@ import { K8sApiError } from "./apiErrorHandler"
1111interface 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 */
4143function 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
7097export default request
98+ export type { RequestOptions }
0 commit comments