Skip to content

Commit 97e4c77

Browse files
committed
Dashboard: Fix auto-login; hide extensions
We are (at least for now) disabling dashboard extensions to reduce the amount of stuff we have to worry about. We may end up re-enabling them before this is merged (after cleanup).
1 parent b29cdf4 commit 97e4c77

File tree

3 files changed

+57
-34
lines changed

3 files changed

+57
-34
lines changed

pkg/rancher-desktop/backend/k3sHelper.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1263,13 +1263,13 @@ export default class K3sHelper extends events.EventEmitter {
12631263
extraEnv: [
12641264
{ name: 'CATTLE_FEATURES',
12651265
value: [
1266-
'auth=false',
1267-
'multi-cluster-management=false',
1266+
'continuous-delivery=false',
12681267
'fleet=false',
12691268
'harvester=false',
1270-
'continuous-delivery=false',
1269+
'multi-cluster-management=false',
12711270
'rke1-ui=false',
12721271
'rke2=false',
1272+
'uiextension=false',
12731273
].join(',') },
12741274
]
12751275
}),

pkg/rancher-desktop/preload/dashboard.ts

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,16 @@ export default function initDashboard(): void {
44
if (!document.location.href.startsWith('https://localhost/dashboard/')) {
55
return;
66
}
7-
console.log('Will init dashboard!');
8-
async function onNavigate(event: Event) {
9-
console.log(`${ event.type }! -> ${ location.href }`);
10-
7+
// Navigation API is only available in Chrome-derived browsers like Electron.
8+
// https://developer.mozilla.org/en-US/docs/Web/API/Navigation
9+
(window as any).navigation.addEventListener('navigate', async function onNavigate() {
1110
const resp = await fetch('https://localhost/v3/users?me=true');
11+
let loginSuccessful = false;
1212

13-
console.log(resp);
1413
if (resp.status === 401) {
15-
// Need to login
1614
const token = await ipcRenderer.invoke('dashboard/get-csrf-token') ?? '';
17-
await fetch("https://localhost/v3-public/localProviders/local?action=login", {
15+
const loginURL = 'https://localhost/v3-public/localProviders/local?action=login';
16+
const resp = await fetch(loginURL, {
1817
headers: {
1918
'Accept': "application/json",
2019
'Content-Type': "application/json",
@@ -29,25 +28,30 @@ export default function initDashboard(): void {
2928
method: "POST",
3029
credentials: "include"
3130
});
31+
loginSuccessful = resp.ok;
3232
}
3333

34-
if (location.pathname === '/dashboard/auth/login') {
35-
console.log('Logging in!');
36-
/** Helper to evalute a singel XPath expression */
37-
function $x<T extends Element>(expr: string) {
38-
return document.evaluate(
39-
expr,
40-
document,
41-
null,
42-
XPathResult.FIRST_ORDERED_NODE_TYPE
43-
).singleNodeValue as T;
44-
}
45-
$x<HTMLInputElement>('//*[@id="username"]/descendant-or-self:input').value = 'admin';
46-
$x<HTMLInputElement>('//*[@id="password"]/descendant-or-self:input').value = 'password';
47-
$x<HTMLButtonElement>('//*[@id=submit]').click();
34+
switch (location.pathname) {
35+
case '/dashboard/auth/login':
36+
// If we logged in, return to the page before the login form.
37+
if (loginSuccessful) {
38+
history.back();
39+
}
40+
return;
41+
case '/dashboard/home':
42+
// Whenever we go to home, replace with cluster explorer.
43+
location.pathname = '/dashboard/c/local/explorer';
44+
return;
4845
}
49-
}
50-
window.addEventListener('hashchange', onNavigate);
51-
window.addEventListener('pageshow', onNavigate);
52-
window.addEventListener('popstate', onNavigate);
46+
});
47+
window.addEventListener('load', function() {
48+
const stylesheet = new CSSStyleSheet();
49+
// Hide the extensions navgation button.
50+
stylesheet.insertRule(`
51+
.side-menu div:has(> a.option[href="/dashboard/c/local/uiplugins"]) {
52+
display: none;
53+
}
54+
`);
55+
document.adoptedStyleSheets.push(stylesheet);
56+
});
5357
}

pkg/rancher-desktop/window/dashboard.ts

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,31 @@ const ipcMain = getIpcMainProxy(console);
1212
ipcMain.removeHandler('dashboard/get-csrf-token');
1313
ipcMain.handle('dashboard/get-csrf-token', async (event) => {
1414
const webContents = event.sender;
15-
const cookies = await webContents.session.cookies.get({
16-
url: webContents.getURL(),
17-
name: 'CSRF',
18-
});
19-
return cookies?.[0].value ?? null;
20-
})
15+
const url = new URL(webContents.getURL());
16+
const cookies = webContents.session.cookies;
17+
18+
while (true) {
19+
const existingCookies = await cookies.get({domain: url.hostname, name: 'CSRF'});
20+
if (existingCookies.length > 0) {
21+
console.log(`Got existing cookie: ${ existingCookies[0].value }`);
22+
return existingCookies[0].value;
23+
}
24+
25+
// Cookie does not exist yet; wait for a cookie with the correct name to be
26+
// created, then try again (to match the hostname).
27+
console.log('Waiting for cookie to show up');
28+
await new Promise<void>((resolve) => {
29+
function onCookieChange(_event: any, cookie: Electron.Cookie, _cause: any, removed: boolean) {
30+
console.log(`Cookie change: ${ cookie.name } (${ removed })`);
31+
if (!removed && cookie.name === 'CSRF') {
32+
cookies.removeListener('changed', onCookieChange);
33+
resolve();
34+
}
35+
}
36+
cookies.addListener('changed', onCookieChange);
37+
});
38+
}
39+
});
2140

2241
export function openDashboard() {
2342
const window = createWindow('dashboard', dashboardURL, {

0 commit comments

Comments
 (0)