Skip to content

Commit b2d5898

Browse files
committed
fix: re enable get CSRF token excluding console internal package to avoid resolution errors
Signed-off-by: Gabriel Bernal <gbernal@redhat.com>
1 parent 11c9227 commit b2d5898

2 files changed

Lines changed: 44 additions & 6 deletions

File tree

web/src/components/dashboards/perses/perses/datasource-cache-api.ts

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@
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';
1415
import {
16+
BuildDatasourceProxyUrlFunc,
17+
DatasourceApi,
1518
DatasourceResource,
1619
DatasourceSelector,
1720
GlobalDatasourceResource,
18-
BuildDatasourceProxyUrlFunc,
19-
DatasourceApi,
2021
} from '@perses-dev/core';
2122
import 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+
};

web/webpack.config.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ const config: Configuration = {
2222
},
2323
resolve: {
2424
extensions: ['.ts', '.tsx', '.js', '.jsx'],
25+
alias: {
26+
// @console/internal modules are provided by the OpenShift Console at runtime.
27+
// Stub them out during development builds to prevent webpack resolution errors.
28+
'@console/internal': false,
29+
},
2530
},
2631
module: {
2732
rules: [

0 commit comments

Comments
 (0)