Skip to content

Commit

Permalink
Removing the rest of authenticated middleware. Primarily cluster load…
Browse files Browse the repository at this point in the history
…ing.
  • Loading branch information
codyrancher committed Jul 3, 2024
1 parent 078ad87 commit 02749d0
Show file tree
Hide file tree
Showing 11 changed files with 44 additions and 43 deletions.
1 change: 0 additions & 1 deletion shell/components/templates/blank.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import Inactivity from '@shell/components/Inactivity';
export default {
components: { Inactivity },
middleware: ['authenticated'],
mixins: [Brand],
};
</script>
Expand Down
4 changes: 0 additions & 4 deletions shell/components/templates/default.vue
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,6 @@ export default {
},
// Note - These will run on route change
middleware: [
'authenticated'
],
computed: {
...mapState(['managementReady', 'clusterReady']),
...mapGetters(['clusterId', 'currentProduct', 'rootProduct', 'isRancherInHarvester', 'showTopLevelMenu']),
Expand Down
2 changes: 0 additions & 2 deletions shell/components/templates/home.vue
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ export default {
mixins: [Brand, BrowserTabVisibility],
middleware: ['authenticated'],
data() {
return {
// Assume home pages have routes where the name is the key to use for string lookup
Expand Down
2 changes: 0 additions & 2 deletions shell/components/templates/plain.vue
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ export default {
Inactivity
},
middleware: ['authenticated'],
mixins: [Brand, BrowserTabVisibility],
data() {
Expand Down
6 changes: 0 additions & 6 deletions shell/config/middleware.js

This file was deleted.

1 change: 1 addition & 0 deletions shell/config/router/navigation-guards/authentication.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export async function authenticate(to, from, next, { store }) {
}
}

// GC should be notified of route change before any find/get request is made that might be used for that page
store.dispatch('gcStartIntervals');
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@ import { get } from '@shell/utils/object';
import { AFTER_LOGIN_ROUTE, WORKSPACE } from '@shell/store/prefs';
import { NAME as FLEET_NAME } from '@shell/config/product/fleet.js';
import { validateResource, setProduct } from '@shell/utils/auth';
import { getClusterFromRoute, getProductFromRoute, getPackageFromRoute } from '@shell/utils/router';
import { getClusterFromRoute, getProductFromRoute, getPackageFromRoute, routeRequiresAuthentication } from '@shell/utils/router';

export default async function({
route, store, redirect, from, $plugin, next
}) {
if ( store.getters['auth/enabled'] !== false && !store.getters['auth/loggedIn'] ) {
return;
export function install(router, context) {
router.beforeEach((to, from, next) => loadClusters(to, from, next, context));
}

export async function loadClusters(to, from, next, { store }) {
if (!routeRequiresAuthentication(to)) {
return next();
}

const route = to;

try {
let clusterId = get(route, 'params.cluster');

Expand All @@ -27,8 +31,12 @@ export default async function({
const oldPkg = getPackageFromRoute(from);
const oldProduct = getProductFromRoute(from);

// TODO: Replace all references to store.$plugin.
// Unfortunately the initialization code has circular dependencies between creating
// the router and creating the store that will need to be untangled before this can be tackled.

// Leave an old pkg where we weren't before?
const oldPkgPlugin = oldPkg ? Object.values($plugin.getPlugins()).find((p) => p.name === oldPkg) : null;
const oldPkgPlugin = oldPkg ? Object.values(store.$plugin.getPlugins()).find((p) => p.name === oldPkg) : null;

if (oldPkg && oldPkg !== pkg ) {
// Execute anything optional the plugin wants to. For example resetting it's store to remove data
Expand All @@ -46,7 +54,7 @@ export default async function({
];

// Entering a new package where we weren't before?
const newPkgPlugin = pkg ? Object.values($plugin.getPlugins()).find((p) => p.name === pkg) : null;
const newPkgPlugin = pkg ? Object.values(store.$plugin.getPlugins()).find((p) => p.name === pkg) : null;

// Note - We can't block on oldPkg !== newPkg because on a fresh load the `from` route equals the `to` route
if (pkg && (oldPkg !== pkg || from.fullPath === route.fullPath)) {
Expand Down Expand Up @@ -88,7 +96,7 @@ export default async function({
})
]);

validateResource(store, route, redirect);
validateResource(store, route);

if (!clusterId) {
clusterId = store.getters['defaultClusterId']; // This needs the cluster list, so no parallel
Expand All @@ -110,16 +118,17 @@ export default async function({
});
}
}
next();
} catch (e) {
if ( e.name === ClusterNotFoundError.name ) {
return redirect(302, '/home');
return next('/home');
} if ( e.name === RedirectToError.name ) {
return redirect(302, e.url);
return next(e.url);
} else {
// Sets error 500 if lost connection to API
store.commit('setError', { error: e, locationError: new Error(store.getters['i18n/t']('nav.failWhale.authMiddleware')) });

return redirect(302, '/fail-whale');
return next('/fail-whale');
}
}
}
8 changes: 5 additions & 3 deletions shell/config/router/navigation-guards/history.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
export function install(router, context) {
router.afterEach((to, from) => loadHistory(to, from, context));
router.beforeEach((to, from, next) => loadHistory(to, from, next, context));
}

export async function loadHistory(to, from, { store }) {
// GC should be notified of route change before any find/get request is made that might be used for that page
export async function loadHistory(to, from, next) {
// Clear state used to record if back button was used for navigation
// TODO: Investigate if this can be removed. This is only used on the templates/error.vue page and seems hacky.
setTimeout(() => {
window._popStateDetected = false;
}, 1);

next();
}
3 changes: 2 additions & 1 deletion shell/config/router/navigation-guards/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { install as installAuthentication } from '@shell/config/router/navigatio
import { install as installI18N } from '@shell/config/router/navigation-guards/i18n';
import { install as installProducts } from '@shell/config/router/navigation-guards/products';
import { install as installHistory } from '@shell/config/router/navigation-guards/history';
import { install as installClusters } from '@shell/config/router/navigation-guards/clusters';

/**
* Install our router navigation guards. i.e. router.beforeEach(), router.afterEach()
Expand All @@ -12,7 +13,7 @@ export function installNavigationGuards(router, context) {
// NOTE: the order of the installation matters.
// Be intentional when adding, removing or modifying the guards that are installed.

const navigationGuardInstallers = [installLoadInitialSettings, installAttemptFirstLogin, installAuthentication, installProducts, installHistory, installI18N];
const navigationGuardInstallers = [installLoadInitialSettings, installAttemptFirstLogin, installAuthentication, installProducts, installHistory, installClusters, installI18N];

navigationGuardInstallers.forEach((installer) => installer(router, context));
}
5 changes: 5 additions & 0 deletions shell/config/router/navigation-guards/products.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import { setProduct } from '@shell/utils/auth';
import { applyProducts } from '@shell/store/type-map';
import { routeRequiresAuthentication } from '@shell/utils/router';

export function install(router, context) {
router.beforeEach((to, from, next) => loadProducts(to, from, next, context));
}

export async function loadProducts(to, from, next, { store }) {
if (!routeRequiresAuthentication(to)) {
return next();
}

// GC should be notified of route change before any find/get request is made that might be used for that page
store.dispatch('gcRouteChanged', to);

Expand Down
22 changes: 10 additions & 12 deletions shell/initialize/entry-helpers.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import Vue from 'vue';
import { updatePageTitle } from '@shell/utils/title';
import { getVendor } from '@shell/config/private-label';
import middleware from '@shell/config/middleware.js';
import { withQuery } from 'ufo';
import dynamicPluginLoader from '@shell/pkg/dynamic-plugin-loader';

Expand Down Expand Up @@ -123,30 +122,29 @@ export const middlewareSeries = (promises, appContext) => {
*/
function callMiddleware(Components, context) {
let midd = [];
let unknownMiddleware = false;

Components.forEach((Component) => {
if (Component.options.middleware) {
midd = midd.concat(Component.options.middleware);
}
});

midd = midd.filter((middleware) => {
const isMiddlwareFunction = typeof middleware === 'function';

if (!isMiddlwareFunction) {
console.warn('All middleware is deprecated. For a short time inline middleware specified as a function will continue to work.', `We noticed '${ middleware }' middleware is still being used.`); // eslint-disable-line no-console
}

return isMiddlwareFunction;
});

midd = midd.map((name) => {
if (typeof name === 'function') {
return name;
}
if (typeof middleware[name] !== 'function') {
unknownMiddleware = true;
errorRedirect(this, new Error(`500: Unknown middleware ${ name }`));
}

return middleware[name];
});

if (unknownMiddleware) {
return;
}

return middlewareSeries(midd, context);
}

Expand Down

0 comments on commit 02749d0

Please sign in to comment.