Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove Fleet setup call from Integrations app #117383

Merged
merged 1 commit into from
Nov 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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