@@ -2,6 +2,28 @@ import { config } from './config'
22import { warn , error , info } from './log'
33import https from 'https'
44
5+ // Create a custom fetch function that handles TLS configuration
6+ async function customFetch ( url : string , options : RequestInit = { } ) : Promise < Response > {
7+ if ( config . GRAFANA_INSECURE_TLS && url . startsWith ( 'https://' ) ) {
8+ // For HTTPS URLs when TLS verification is disabled, use a custom agent
9+ const agent = new https . Agent ( {
10+ rejectUnauthorized : false
11+ } )
12+
13+ // Create options with the custom agent
14+ const fetchOptions = {
15+ ...options ,
16+ agent : agent
17+ }
18+
19+ info ( '[grafana] Using insecure TLS agent for HTTPS request' , { url : url . substring ( 0 , 50 ) + '...' } )
20+ return fetch ( url , fetchOptions )
21+ }
22+
23+ // For HTTP URLs or when TLS verification is enabled, use regular fetch
24+ return fetch ( url , options )
25+ }
26+
527// Types describing Grafana REST API shapes we touch in this helper.
628interface GrafanaOrgUserLookupItem {
729 id ?: number
@@ -27,20 +49,11 @@ interface GrafanaGlobalCache {
2749
2850declare const global : typeof globalThis & GrafanaGlobalCache
2951
30- // Get fetch options with optional TLS configuration
52+ // Get fetch options - TLS configuration is handled in customFetch function
3153function getFetchOptions ( headers : Record < string , string > , method : string , body ?: string ) : RequestInit {
3254 const options : RequestInit = { method, headers }
3355 if ( body ) options . body = body
3456
35- // If TLS verification is disabled, create an HTTPS agent that ignores certificate errors
36- if ( config . GRAFANA_INSECURE_TLS ) {
37- const agent = new https . Agent ( {
38- rejectUnauthorized : false
39- } )
40- // @ts -expect-error - Node.js specific fetch option
41- options . agent = agent
42- }
43-
4457 return options
4558}
4659
@@ -82,7 +95,7 @@ export async function addUserToTeamByEmail(teamId: number, email: string, userna
8295 const headers : Record < string , string > = { Authorization : `Bearer ${ token } ` , 'Content-Type' : 'application/json' }
8396
8497 info ( '[grafana] org user lookup' , { email, url : lookupUrl } )
85- const lookupRes = await fetch ( lookupUrl , getFetchOptions ( headers , 'GET' ) )
98+ const lookupRes = await customFetch ( lookupUrl , getFetchOptions ( headers , 'GET' ) )
8699 const lookupText = await lookupRes . text ( ) . catch ( ( ) => '<no body>' )
87100 info ( '[grafana] org lookup response' , { email, url : lookupUrl , status : lookupRes . status , body : lookupText } )
88101
@@ -130,7 +143,7 @@ export async function addUserToTeamByEmail(teamId: number, email: string, userna
130143 info ( '[grafana] user not found in org lookup; retrying lookup' , { email, attempt, backoff } )
131144 await new Promise ( ( res ) => setTimeout ( res , backoff ) )
132145 backoff *= 2
133- const retryRes = await fetch ( lookupUrl , getFetchOptions ( headers , 'GET' ) )
146+ const retryRes = await customFetch ( lookupUrl , getFetchOptions ( headers , 'GET' ) )
134147 const retryText = await retryRes . text ( ) . catch ( ( ) => '<no body>' )
135148 info ( '[grafana] org lookup retry response' , { email, url : lookupUrl , attempt, status : retryRes . status , body : retryText } )
136149 if ( ! retryRes . ok ) {
@@ -156,7 +169,7 @@ export async function addUserToTeamByEmail(teamId: number, email: string, userna
156169 const teamUrl = grafanaBase ? `${ grafanaBase } /api/teams/${ teamId } /members` : `/api/teams/${ teamId } /members`
157170 // Check current team members to avoid duplicate adds (idempotent)
158171 try {
159- const membersRes = await fetch ( teamUrl , getFetchOptions ( headers , 'GET' ) )
172+ const membersRes = await customFetch ( teamUrl , getFetchOptions ( headers , 'GET' ) )
160173 const membersText = await membersRes . text ( ) . catch ( ( ) => '<no body>' )
161174 if ( membersRes . ok ) {
162175 try {
@@ -176,7 +189,7 @@ export async function addUserToTeamByEmail(teamId: number, email: string, userna
176189 }
177190
178191 info ( '[grafana] add user to team via POST' , { teamId, userId, email, url : teamUrl } )
179- const addRes = await fetch ( teamUrl , getFetchOptions ( headers , 'POST' , JSON . stringify ( { userId, role } ) ) )
192+ const addRes = await customFetch ( teamUrl , getFetchOptions ( headers , 'POST' , JSON . stringify ( { userId, role } ) ) )
180193 const addText = await addRes . text ( ) . catch ( ( ) => '<no body>' )
181194 info ( '[grafana] add response' , { teamId, userId, status : addRes . status , body : addText } )
182195 if ( ! addRes . ok ) {
0 commit comments