1111// See the License for the specific language governing permissions and
1212// limitations under the License.
1313
14+ import { getCSRFToken } from '@openshift-console/dynamic-plugin-sdk/lib/utils/fetch/console-fetch-utils' ;
1415import {
16+ BuildDatasourceProxyUrlFunc ,
17+ DatasourceApi ,
1518 DatasourceResource ,
1619 DatasourceSelector ,
1720 GlobalDatasourceResource ,
18- BuildDatasourceProxyUrlFunc ,
19- DatasourceApi ,
2021} from '@perses-dev/core' ;
2122import LRUCache from 'lru-cache' ;
2223
@@ -126,7 +127,7 @@ export class CachedDatasourceAPI implements DatasourceApi {
126127 ) : Promise < DatasourceResource | undefined > {
127128 const { resource, keyExist } = this . cache . getDatasource ( project , selector ) ;
128129 if ( resource ) {
129- return Promise . resolve ( resource ) ;
130+ return Promise . resolve ( addCsrfToken ( resource ) ) ;
130131 }
131132 if ( keyExist ) {
132133 // in case the keyExist, then it means we already did the query,
@@ -145,14 +146,14 @@ export class CachedDatasourceAPI implements DatasourceApi {
145146 } else {
146147 this . cache . setDatasource ( result ) ;
147148 }
148- return result ;
149+ return addCsrfToken ( result ) ;
149150 } ) ;
150151 }
151152
152153 getGlobalDatasource ( selector : DatasourceSelector ) : Promise < GlobalDatasourceResource | undefined > {
153154 const { resource, keyExist } = this . cache . getGlobalDatasource ( selector ) ;
154155 if ( resource ) {
155- return Promise . resolve ( resource ) ;
156+ return Promise . resolve ( addCsrfToken ( resource ) ) ;
156157 }
157158 if ( keyExist ) {
158159 return Promise . resolve ( undefined ) ;
@@ -163,7 +164,7 @@ export class CachedDatasourceAPI implements DatasourceApi {
163164 } else {
164165 this . cache . setGlobalDatasource ( result ) ;
165166 }
166- return result ;
167+ return addCsrfToken ( result ) ;
167168 } ) ;
168169 }
169170
@@ -183,3 +184,35 @@ export class CachedDatasourceAPI implements DatasourceApi {
183184 } ) ;
184185 }
185186}
187+
188+ // Perses panels use @perses -dev/core fetch internally, this is a workaround to add the
189+ // CSRF token needed in the OpenShift console.
190+ // TODO: Remove once Perses supports overriding the internal fetch function.
191+ const addCsrfToken = < T extends DatasourceResource | GlobalDatasourceResource | undefined > (
192+ datasource : T ,
193+ ) : T => {
194+ if ( ! datasource ) {
195+ return datasource ;
196+ }
197+
198+ const pluginSpec = datasource . spec . plugin . spec as Record < string , unknown > ;
199+ const proxySpec = ( pluginSpec . proxy as Record < string , unknown > ) ?. spec as
200+ | Record < string , unknown >
201+ | undefined ;
202+ const existingHeaders = ( proxySpec ?. headers as Record < string , string > ) ?? { } ;
203+
204+ datasource . spec . plugin . spec = {
205+ ...pluginSpec ,
206+ proxy : {
207+ spec : {
208+ ...proxySpec ,
209+ headers : {
210+ ...existingHeaders ,
211+ 'X-CSRFToken' : getCSRFToken ( ) ,
212+ 'Sec-Fetch-Site' : 'same-origin' ,
213+ } ,
214+ } ,
215+ } ,
216+ } ;
217+ return datasource ;
218+ } ;
0 commit comments