Skip to content
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
77 changes: 10 additions & 67 deletions x-pack/plugins/fleet/public/applications/integrations/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@

import React, { memo, useEffect, useState } from 'react';
import type { AppMountParameters } from 'kibana/public';
import { EuiCode, EuiEmptyPrompt, EuiErrorBoundary, EuiPanel, EuiPortal } from '@elastic/eui';
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 { i18n } from '@kbn/i18n';
import styled from 'styled-components';
import useObservable from 'react-use/lib/useObservable';

import {
Expand Down Expand Up @@ -49,29 +47,23 @@ const ErrorLayout = ({ children }: { children: JSX.Element }) => (
</EuiErrorBoundary>
);

const Panel = styled(EuiPanel)`
max-width: 500px;
margin-right: auto;
margin-left: auto;
`;

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

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

useEffect(() => {
(async () => {
setPermissionsError(undefined);
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();
Expand All @@ -83,69 +75,20 @@ export const WithPermissionsAndSetup: React.FC = memo(({ children }) => {
}
setIsInitialized(true);
} else {
setPermissionsError(permissionsResponse.data?.error || 'REQUEST_ERROR');
setIsInitialized(true);
}
} catch (err) {
setPermissionsError('REQUEST_ERROR');
} 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 || permissionsError) {
if (isPermissionsLoading) {
return (
<ErrorLayout>
{isPermissionsLoading ? (
<Loading />
) : permissionsError === 'REQUEST_ERROR' ? (
<Error
title={
<FormattedMessage
id="xpack.fleet.permissionsRequestErrorMessageTitle"
defaultMessage="Unable to check permissions"
/>
}
error={i18n.translate('xpack.fleet.permissionsRequestErrorMessageDescription', {
defaultMessage: 'There was a problem checking Fleet permissions',
})}
/>
) : (
<Panel>
<EuiEmptyPrompt
iconType="securityApp"
title={
<h2>
{permissionsError === 'MISSING_SUPERUSER_ROLE' ? (
<FormattedMessage
id="xpack.fleet.permissionDeniedErrorTitle"
defaultMessage="Permission denied"
/>
) : (
<FormattedMessage
id="xpack.fleet.securityRequiredErrorTitle"
defaultMessage="Security is not enabled"
/>
)}
</h2>
}
body={
<p>
{permissionsError === 'MISSING_SUPERUSER_ROLE' ? (
<FormattedMessage
id="xpack.fleet.integrationsPermissionDeniedErrorMessage"
defaultMessage="You are not authorized to access Integrations. Integrations requires {roleName} privileges."
values={{ roleName: <EuiCode>superuser</EuiCode> }}
/>
) : (
<FormattedMessage
id="xpack.fleet.integrationsSecurityRequiredErrorMessage"
defaultMessage="You must enable security in Kibana and Elasticsearch to use Integrations."
/>
)}
</p>
}
/>
</Panel>
)}
<Loading />
</ErrorLayout>
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import React, { useEffect, useState } from 'react';
import { Redirect } from 'react-router-dom';
import { FormattedMessage } from '@kbn/i18n/react';
import { EuiFlexGroup, EuiFlexItem, EuiTitle, EuiSpacer } from '@elastic/eui';
import { groupBy } from 'lodash';

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

Expand Down Expand Up @@ -67,8 +68,26 @@ export const AssetsPage = ({ packageInfo }: AssetsPanelProps) => {
id,
type,
}));
const { savedObjects } = await savedObjectsClient.bulkGet(objectsToGet);
setAssetsSavedObjects(savedObjects as AssetSavedObject[]);

// We don't have an API to know which SO types a user has access to, so instead we make a request for each
// SO type and ignore the 403 errors
const objectsByType = await Promise.all(
Object.entries(groupBy(objectsToGet, 'type')).map(([type, objects]) =>
savedObjectsClient
.bulkGet(objects)
// Ignore privilege errors
.catch((e: any) => {
if (e?.body?.statusCode === 403) {
return { savedObjects: [] };
} else {
throw e;
}
})
.then(({ savedObjects }) => savedObjects as AssetSavedObject[])
)
);

setAssetsSavedObjects(objectsByType.flat());
} catch (e) {
setFetchError(e);
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { act, cleanup } from '@testing-library/react';

import { INTEGRATIONS_ROUTING_PATHS, pagePathGetters } from '../../../../constants';
import type {
CheckPermissionsResponse,
GetAgentPoliciesResponse,
GetFleetStatusResponse,
GetInfoResponse,
Expand All @@ -23,6 +24,7 @@ import type {
} from '../../../../../../../common/types/models';
import {
agentPolicyRouteService,
appRoutesService,
epmRouteService,
fleetSetupRouteService,
packagePolicyRouteService,
Expand Down Expand Up @@ -260,6 +262,7 @@ interface EpmPackageDetailsResponseProvidersMock {
fleetSetup: jest.MockedFunction<() => GetFleetStatusResponse>;
packagePolicyList: jest.MockedFunction<() => GetPackagePoliciesResponse>;
agentPolicyList: jest.MockedFunction<() => GetAgentPoliciesResponse>;
appCheckPermissions: jest.MockedFunction<() => CheckPermissionsResponse>;
}

const mockApiCalls = (
Expand Down Expand Up @@ -740,6 +743,10 @@ On Windows, the module was tested with Nginx installed from the Chocolatey repos
},
};

const appCheckPermissionsResponse: CheckPermissionsResponse = {
success: true,
};

const mockedApiInterface: MockedApi<EpmPackageDetailsResponseProvidersMock> = {
waitForApi() {
return new Promise((resolve) => {
Expand All @@ -757,6 +764,7 @@ On Windows, the module was tested with Nginx installed from the Chocolatey repos
fleetSetup: jest.fn().mockReturnValue(agentsSetupResponse),
packagePolicyList: jest.fn().mockReturnValue(packagePoliciesResponse),
agentPolicyList: jest.fn().mockReturnValue(agentPoliciesResponse),
appCheckPermissions: jest.fn().mockReturnValue(appCheckPermissionsResponse),
},
};

Expand Down Expand Up @@ -792,6 +800,11 @@ On Windows, the module was tested with Nginx installed from the Chocolatey repos
return mockedApiInterface.responseProvider.epmGetStats();
}

if (path === appRoutesService.getCheckPermissionsPath()) {
markApiCallAsHandled();
return mockedApiInterface.responseProvider.appCheckPermissions();
}

const err = new Error(`API [GET ${path}] is not MOCKED!`);
// eslint-disable-next-line no-console
console.error(err);
Expand Down
Loading