Skip to content

Commit

Permalink
Remove Fleet setup call from Integrations app (elastic#117383)
Browse files Browse the repository at this point in the history
  • Loading branch information
joshdover authored and kibanamachine committed Nov 3, 2021
1 parent 9d4f5d9 commit cd58d19
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 92 deletions.
92 changes: 4 additions & 88 deletions x-pack/plugins/fleet/public/applications/integrations/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,14 @@
* 2.0.
*/

import React, { memo, useEffect, useState } from 'react';
import React, { memo } from 'react';
import type { AppMountParameters } from 'kibana/public';
import { EuiErrorBoundary, EuiPortal } from '@elastic/eui';
import type { History } from 'history';
import { Router, Redirect, Route, Switch } from 'react-router-dom';
import { FormattedMessage } from '@kbn/i18n/react';
import useObservable from 'react-use/lib/useObservable';

import {
ConfigContext,
FleetStatusProvider,
KibanaVersionContext,
sendGetPermissionsCheck,
sendSetup,
} from '../../hooks';
import { ConfigContext, FleetStatusProvider, KibanaVersionContext } from '../../hooks';

import type { FleetConfigType, FleetStartServices } from '../../plugin';

Expand All @@ -32,91 +25,14 @@ import { EuiThemeProvider } from '../../../../../../src/plugins/kibana_react/com
import { AgentPolicyContextProvider, useUrlModal } from './hooks';
import { INTEGRATIONS_ROUTING_PATHS, pagePathGetters } from './constants';

import { Error, Loading, SettingFlyout } from './components';
import { SettingFlyout } from './components';

import type { UIExtensionsStorage } from './types';

import { EPMApp } from './sections/epm';
import { DefaultLayout } from './layouts';
import { PackageInstallProvider } from './hooks';
import { useBreadcrumbs, UIExtensionsContext } from './hooks';
import { PackageInstallProvider, UIExtensionsContext } from './hooks';
import { IntegrationsHeader } from './components/header';

const ErrorLayout = ({ children }: { children: JSX.Element }) => (
<EuiErrorBoundary>
<DefaultLayout>{children}</DefaultLayout>
</EuiErrorBoundary>
);

export const WithPermissionsAndSetup: React.FC = memo(({ children }) => {
useBreadcrumbs('integrations');

const [isPermissionsLoading, setIsPermissionsLoading] = useState<boolean>(false);
const [isInitialized, setIsInitialized] = useState(false);
const [initializationError, setInitializationError] = useState<Error | null>(null);

useEffect(() => {
(async () => {
setIsInitialized(false);
setInitializationError(null);
try {
// Attempt Fleet Setup if user has permissions, otherwise skip
setIsPermissionsLoading(true);
const permissionsResponse = await sendGetPermissionsCheck();
setIsPermissionsLoading(false);

if (permissionsResponse.data?.success) {
try {
const setupResponse = await sendSetup();
if (setupResponse.error) {
setInitializationError(setupResponse.error);
}
} catch (err) {
setInitializationError(err);
}
setIsInitialized(true);
} else {
setIsInitialized(true);
}
} catch {
// If there's an error checking permissions, default to proceeding without running setup
// User will only have access to EPM endpoints if they actually have permission
setIsInitialized(true);
}
})();
}, []);

if (isPermissionsLoading) {
return (
<ErrorLayout>
<Loading />
</ErrorLayout>
);
}

if (!isInitialized || initializationError) {
return (
<ErrorLayout>
{initializationError ? (
<Error
title={
<FormattedMessage
id="xpack.fleet.initializationErrorMessageTitle"
defaultMessage="Unable to initialize Fleet"
/>
}
error={initializationError}
/>
) : (
<Loading />
)}
</ErrorLayout>
);
}

return <>{children}</>;
});

/**
* Fleet Application context all the way down to the Router, but with no permissions or setup checks
* and no routes defined
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import type { FleetConfigType, FleetStartServices } from '../../plugin';
import { licenseService } from '../../hooks';
import type { UIExtensionsStorage } from '../../types';

import { AppRoutes, IntegrationsAppContext, WithPermissionsAndSetup } from './app';
import { AppRoutes, IntegrationsAppContext } from './app';

export interface ProtectedRouteProps extends RouteProps {
isAllowed?: boolean;
Expand Down Expand Up @@ -58,9 +58,7 @@ const IntegrationsApp = ({
extensions={extensions}
setHeaderActionMenu={setHeaderActionMenu}
>
<WithPermissionsAndSetup>
<AppRoutes />
</WithPermissionsAndSetup>
<AppRoutes />
</IntegrationsAppContext>
);
};
Expand Down

0 comments on commit cd58d19

Please sign in to comment.