diff --git a/pkg/rancher-desktop/backend/k3sHelper.ts b/pkg/rancher-desktop/backend/k3sHelper.ts index 9ec4e08188c..a28d563c148 100644 --- a/pkg/rancher-desktop/backend/k3sHelper.ts +++ b/pkg/rancher-desktop/backend/k3sHelper.ts @@ -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(',') }, ] }), diff --git a/pkg/rancher-desktop/preload/dashboard.ts b/pkg/rancher-desktop/preload/dashboard.ts index 1b57a1c070e..b7c5707fad5 100644 --- a/pkg/rancher-desktop/preload/dashboard.ts +++ b/pkg/rancher-desktop/preload/dashboard.ts @@ -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", @@ -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(expr: string) { - return document.evaluate( - expr, - document, - null, - XPathResult.FIRST_ORDERED_NODE_TYPE - ).singleNodeValue as T; - } - $x('//*[@id="username"]/descendant-or-self:input').value = 'admin'; - $x('//*[@id="password"]/descendant-or-self:input').value = 'password'; - $x('//*[@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); + }); } diff --git a/pkg/rancher-desktop/window/dashboard.ts b/pkg/rancher-desktop/window/dashboard.ts index 42031545378..4c54559e0f3 100644 --- a/pkg/rancher-desktop/window/dashboard.ts +++ b/pkg/rancher-desktop/window/dashboard.ts @@ -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((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, {