Skip to content

Commit

Permalink
Dashboard: Fix auto-login; hide extensions
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
mook-as committed Aug 19, 2024
1 parent 90b1bb8 commit 50a9163
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 34 deletions.
6 changes: 3 additions & 3 deletions pkg/rancher-desktop/backend/k3sHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1263,13 +1263,13 @@ export default class K3sHelper extends events.EventEmitter {
extraEnv: [
{ name: 'CATTLE_FEATURES',
value: [
'auth=false',
'multi-cluster-management=false',
'continuous-delivery=false',
'fleet=false',
'harvester=false',
'continuous-delivery=false',
'multi-cluster-management=false',
'rke1-ui=false',
'rke2=false',
'uiextension=false',
].join(',') },
]
}),
Expand Down
54 changes: 29 additions & 25 deletions pkg/rancher-desktop/preload/dashboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@ export default function initDashboard(): void {
if (!document.location.href.startsWith('https://localhost/dashboard/')) {
return;
}
console.log('Will init dashboard!');
async function onNavigate(event: Event) {
console.log(`${ event.type }! -> ${ location.href }`);

// Navigation API is only available in Chrome-derived browsers like Electron.
// https://developer.mozilla.org/en-US/docs/Web/API/Navigation
(window as any).navigation.addEventListener('navigate', async function onNavigate() {
const resp = await fetch('https://localhost/v3/users?me=true');
let loginSuccessful = false;

console.log(resp);
if (resp.status === 401) {
// Need to login
const token = await ipcRenderer.invoke('dashboard/get-csrf-token') ?? '';
await fetch("https://localhost/v3-public/localProviders/local?action=login", {
const loginURL = 'https://localhost/v3-public/localProviders/local?action=login';
const resp = await fetch(loginURL, {
headers: {
'Accept': "application/json",
'Content-Type': "application/json",
Expand All @@ -29,25 +28,30 @@ export default function initDashboard(): void {
method: "POST",
credentials: "include"
});
loginSuccessful = resp.ok;
}

if (location.pathname === '/dashboard/auth/login') {
console.log('Logging in!');
/** Helper to evalute a singel XPath expression */
function $x<T extends Element>(expr: string) {
return document.evaluate(
expr,
document,
null,
XPathResult.FIRST_ORDERED_NODE_TYPE
).singleNodeValue as T;
}
$x<HTMLInputElement>('//*[@id="username"]/descendant-or-self:input').value = 'admin';
$x<HTMLInputElement>('//*[@id="password"]/descendant-or-self:input').value = 'password';
$x<HTMLButtonElement>('//*[@id=submit]').click();
switch (location.pathname) {
case '/dashboard/auth/login':
// If we logged in, return to the page before the login form.
if (loginSuccessful) {
history.back();
}
return;
case '/dashboard/home':
// Whenever we go to home, replace with cluster explorer.
location.pathname = '/dashboard/c/local/explorer';
return;
}
}
window.addEventListener('hashchange', onNavigate);
window.addEventListener('pageshow', onNavigate);
window.addEventListener('popstate', onNavigate);
});
window.addEventListener('load', function() {
const stylesheet = new CSSStyleSheet();
// Hide the extensions navigation button.
stylesheet.insertRule(`
.side-menu div:has(> a.option[href="/dashboard/c/local/uiplugins"]) {
display: none;
}
`);
document.adoptedStyleSheets.push(stylesheet);
});
}
31 changes: 25 additions & 6 deletions pkg/rancher-desktop/window/dashboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,31 @@ const ipcMain = getIpcMainProxy(console);
ipcMain.removeHandler('dashboard/get-csrf-token');
ipcMain.handle('dashboard/get-csrf-token', async (event) => {
const webContents = event.sender;
const cookies = await webContents.session.cookies.get({
url: webContents.getURL(),
name: 'CSRF',
});
return cookies?.[0].value ?? null;
})
const url = new URL(webContents.getURL());
const cookies = webContents.session.cookies;

while (true) {
const existingCookies = await cookies.get({domain: url.hostname, name: 'CSRF'});
if (existingCookies.length > 0) {
console.log(`Got existing cookie: ${ existingCookies[0].value }`);
return existingCookies[0].value;
}

// Cookie does not exist yet; wait for a cookie with the correct name to be
// created, then try again (to match the hostname).
console.log('Waiting for cookie to show up');
await new Promise<void>((resolve) => {
function onCookieChange(_event: any, cookie: Electron.Cookie, _cause: any, removed: boolean) {
console.log(`Cookie change: ${ cookie.name } (${ removed })`);
if (!removed && cookie.name === 'CSRF') {
cookies.removeListener('changed', onCookieChange);
resolve();
}
}
cookies.addListener('changed', onCookieChange);
});
}
});

export function openDashboard() {
const window = createWindow('dashboard', dashboardURL, {
Expand Down

0 comments on commit 50a9163

Please sign in to comment.