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

Always show security screen and shows error page when trying to access forbidden data-source #1964

Merged
Show file tree
Hide file tree
Changes from 8 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
32 changes: 32 additions & 0 deletions public/apps/access-error-component.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright OpenSearch Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

import { EuiPageContent } from '@elastic/eui';
import React from 'react';

interface AccessErrorComponentProps {
dataSourceLabel?: string;
message?: string;
}

export const AccessErrorComponent: React.FC<AccessErrorComponentProps> = (props) => {
DarshitChanpura marked this conversation as resolved.
Show resolved Hide resolved
const { dataSourceLabel, message = 'You do not have permissions to view this data' } = props;
return (

Check warning on line 26 in public/apps/access-error-component.tsx

View check run for this annotation

Codecov / codecov/patch

public/apps/access-error-component.tsx#L26

Added line #L26 was not covered by tests
<EuiPageContent>
{message}
{dataSourceLabel ? ` for ${props.dataSourceLabel}.` : '.'}
</EuiPageContent>
);
};
43 changes: 18 additions & 25 deletions public/apps/configuration/app-router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -163,30 +163,6 @@

const [dataSource, setDataSource] = useState<DataSourceOption>(dataSourceFromUrl);

function getTenancyRoutes() {
if (multitenancyEnabled) {
return (
<>
<Route
path={ROUTE_MAP.tenants.href}
render={() => {
setGlobalBreadcrumbs(ResourceType.tenants);
return <TenantList tabID={'Manage'} {...props} />;
}}
/>
<Route
path={ROUTE_MAP.tenantsConfigureTab.href}
render={() => {
setGlobalBreadcrumbs(ResourceType.tenants);
return <TenantList tabID={'Configure'} {...props} />;
}}
/>
</>
);
}
return null;
}

return (
<DataSourceContext.Provider value={{ dataSource, setDataSource }}>
<Router basename={props.params.appBasePath}>
Expand Down Expand Up @@ -300,7 +276,24 @@
return <GetStarted {...props} />;
}}
/>
{getTenancyRoutes()}
{multitenancyEnabled && (
DarshitChanpura marked this conversation as resolved.
Show resolved Hide resolved
<Route
path={ROUTE_MAP.tenants.href}
render={() => {
setGlobalBreadcrumbs(ResourceType.tenants);
return <TenantList tabID={'Manage'} {...props} />;

Check warning on line 284 in public/apps/configuration/app-router.tsx

View check run for this annotation

Codecov / codecov/patch

public/apps/configuration/app-router.tsx#L282-L284

Added lines #L282 - L284 were not covered by tests
}}
/>
)}
{multitenancyEnabled && (
<Route
path={ROUTE_MAP.tenantsConfigureTab.href}
render={() => {
setGlobalBreadcrumbs(ResourceType.tenants);
return <TenantList tabID={'Configure'} {...props} />;

Check warning on line 293 in public/apps/configuration/app-router.tsx

View check run for this annotation

Codecov / codecov/patch

public/apps/configuration/app-router.tsx#L291-L293

Added lines #L291 - L293 were not covered by tests
}}
/>
)}
<Redirect exact from="/" to={LANDING_PAGE_URL} />
</Switch>
</EuiPageBody>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export function AuditLoggingEditSettings(props: AuditLoggingEditSettingProps) {
};

fetchConfig();
}, [props.coreStart.http, dataSource.id]);
}, [props.coreStart.http, dataSource]);

const renderSaveAndCancel = () => {
return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
} from '@elastic/eui';
import React, { useContext } from 'react';
import { FormattedMessage } from '@osd/i18n/react';
import { DataSourceOption } from 'src/plugins/data_source_management/public';
import { AppDependencies } from '../../../types';
import { ResourceType } from '../../../../../common';
import { getAuditLogging, updateAuditLogging } from '../../utils/audit-logging-utils';
Expand All @@ -45,6 +46,7 @@
import { DocLinks } from '../../constants';
import { DataSourceContext } from '../../app-router';
import { SecurityPluginTopNavMenu } from '../../top-nav-menu';
import { AccessErrorComponent } from '../../../access-error-component';

interface AuditLoggingProps extends AppDependencies {
fromType: string;
Expand Down Expand Up @@ -93,6 +95,18 @@
);
}

function renderAccessErrorPanel(dataSource: DataSourceOption) {
return (

Check warning on line 99 in public/apps/configuration/panels/audit-logging/audit-logging.tsx

View check run for this annotation

Codecov / codecov/patch

public/apps/configuration/panels/audit-logging/audit-logging.tsx#L98-L99

Added lines #L98 - L99 were not covered by tests
<EuiPanel>
<EuiTitle>
<h3>Audit logging</h3>
</EuiTitle>
<EuiHorizontalRule margin="m" />
<AccessErrorComponent dataSourceLabel={dataSource && dataSource.label} />
</EuiPanel>
);
}

export function renderGeneralSettings(config: AuditLoggingSettings) {
return (
<>
Expand Down Expand Up @@ -137,6 +151,7 @@
export function AuditLogging(props: AuditLoggingProps) {
const [configuration, setConfiguration] = React.useState<AuditLoggingSettings>({});
const { dataSource, setDataSource } = useContext(DataSourceContext)!;
const [errorFlag, setErrorFlag] = React.useState(false);

const onSwitchChange = async () => {
try {
Expand All @@ -156,20 +171,24 @@
try {
const auditLogging = await getAuditLogging(props.coreStart.http, dataSource.id);
setConfiguration(auditLogging);
setErrorFlag(false);
} catch (e) {
// TODO: switch to better error handling.
console.log(e);
setErrorFlag(true);
}
};

fetchData();
}, [props.coreStart.http, props.fromType, dataSource.id]);
}, [props.coreStart.http, props.fromType, dataSource]);

const statusPanel = renderStatusPanel(onSwitchChange, configuration.enabled || false);

let content;

if (!configuration.enabled) {
if (errorFlag) {
content = renderAccessErrorPanel(dataSource);

Check warning on line 190 in public/apps/configuration/panels/audit-logging/audit-logging.tsx

View check run for this annotation

Codecov / codecov/patch

public/apps/configuration/panels/audit-logging/audit-logging.tsx#L190

Added line #L190 was not covered by tests
} else if (!configuration.enabled) {
content = statusPanel;
} else {
content = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@ describe('Audit logs', () => {
};

beforeEach(() => {
jest.spyOn(React, 'useState').mockImplementation((initialValue) => [initialValue, setState]);
// jest.spyOn(React, 'useState').mockImplementation((initialValue) => [initialValue, setState]);
jest.spyOn(React, 'useState').mockRestore();
jest
.spyOn(React, 'useState')
.mockImplementationOnce(() => [[], setState])
.mockImplementationOnce(() => [false, jest.fn()]);
jest.spyOn(React, 'useEffect').mockImplementationOnce((f) => f());
});

Expand Down Expand Up @@ -140,7 +145,11 @@ describe('Audit logs', () => {

it('render when AuditLoggingSettings.enabled is true', () => {
const auditLoggingSettings = { enabled: true };
jest.spyOn(React, 'useState').mockImplementation(() => [auditLoggingSettings, setState]);
jest.spyOn(React, 'useState').mockRestore();
jest
.spyOn(React, 'useState')
.mockImplementationOnce(() => [auditLoggingSettings, setState])
.mockImplementationOnce(() => [false, jest.fn()]);
const component = shallow(
<AuditLogging coreStart={mockCoreStart as any} navigation={{} as any} />
);
Expand All @@ -149,7 +158,11 @@ describe('Audit logs', () => {

it('Click Configure button of general setting section', () => {
const auditLoggingSettings = { enabled: true };
jest.spyOn(React, 'useState').mockImplementation(() => [auditLoggingSettings, setState]);
jest.spyOn(React, 'useState').mockRestore();
jest
.spyOn(React, 'useState')
.mockImplementationOnce(() => [auditLoggingSettings, setState])
.mockImplementationOnce(() => [false, jest.fn()]);
const component = shallow(
<AuditLogging coreStart={mockCoreStart as any} navigation={{} as any} />
);
Expand All @@ -161,7 +174,11 @@ describe('Audit logs', () => {

it('Click Configure button of Compliance settings section', () => {
const auditLoggingSettings = { enabled: true };
jest.spyOn(React, 'useState').mockImplementation(() => [auditLoggingSettings, setState]);
jest.spyOn(React, 'useState').mockRestore();
jest
.spyOn(React, 'useState')
.mockImplementationOnce(() => [auditLoggingSettings, setState])
.mockImplementationOnce(() => [false, jest.fn()]);
const component = shallow(
<AuditLogging coreStart={mockCoreStart as any} navigation={{} as any} />
);
Expand Down
33 changes: 25 additions & 8 deletions public/apps/configuration/panels/auth-view/auth-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@ import { getSecurityConfig } from '../../utils/auth-view-utils';
import { InstructionView } from './instruction-view';
import { DataSourceContext } from '../../app-router';
import { SecurityPluginTopNavMenu } from '../../top-nav-menu';
import { AccessErrorComponent } from '../../../access-error-component';

export function AuthView(props: AppDependencies) {
const [authentication, setAuthentication] = React.useState([]);
const [authorization, setAuthorization] = React.useState([]);
const [loading, setLoading] = useState(false);
const { dataSource, setDataSource } = useContext(DataSourceContext)!;
const [errorFlag, setErrorFlag] = React.useState(false);

React.useEffect(() => {
const fetchData = async () => {
Expand All @@ -40,15 +42,17 @@ export function AuthView(props: AppDependencies) {

setAuthentication(config.authc);
setAuthorization(config.authz);
setErrorFlag(false);
} catch (e) {
console.log(e);
setErrorFlag(true);
DarshitChanpura marked this conversation as resolved.
Show resolved Hide resolved
} finally {
setLoading(false);
}
};

fetchData();
}, [props.coreStart.http, dataSource.id]);
}, [props.coreStart.http, dataSource]);

if (isEmpty(authentication)) {
return (
Expand All @@ -59,7 +63,14 @@ export function AuthView(props: AppDependencies) {
setDataSource={setDataSource}
selectedDataSource={dataSource}
/>
<InstructionView config={props.config} />
<EuiTitle size="l">
<h1>Authentication and authorization</h1>
</EuiTitle>
{errorFlag ? (
<AccessErrorComponent dataSourceLabel={dataSource && dataSource.label} />
) : (
<InstructionView config={props.config} />
)}
</>
);
}
Expand All @@ -76,18 +87,24 @@ export function AuthView(props: AppDependencies) {
<EuiTitle size="l">
<h1>Authentication and authorization</h1>
</EuiTitle>
{props.config.ui.backend_configurable && (
{!errorFlag && props.config.ui.backend_configurable && (
<ExternalLinkButton
href={DocLinks.BackendConfigurationDoc}
text="Manage via config.yml"
/>
)}
</EuiPageHeader>
{/* @ts-ignore */}
<AuthenticationSequencePanel authc={authentication} loading={loading} />
<EuiSpacer size="m" />
{/* @ts-ignore */}
<AuthorizationPanel authz={authorization} loading={loading} config={props.config} />
{errorFlag ? (
<AccessErrorComponent dataSourceLabel={dataSource && dataSource.label} />
) : (
<>
{/* @ts-ignore */}
DarshitChanpura marked this conversation as resolved.
Show resolved Hide resolved
<AuthenticationSequencePanel authc={authentication} loading={loading} />
<EuiSpacer size="m" />
{/* @ts-ignore */}
<AuthorizationPanel authz={authorization} loading={loading} config={props.config} />
</>
)}
</>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,14 @@ describe('Auth view', () => {
const setState = jest.fn();

beforeEach(() => {
jest.spyOn(React, 'useState').mockImplementation((initialValue) => [initialValue, setState]);
// jest.spyOn(React, 'useState').mockImplementation((initialValue) => [initialValue, setState]);
jest.spyOn(React, 'useState').mockRestore();
jest
.spyOn(React, 'useState')
.mockImplementationOnce(() => [[], setState])
.mockImplementationOnce(() => [[], setState])
.mockImplementationOnce(() => [false, jest.fn()])
.mockImplementationOnce(() => [false, jest.fn()]);
jest.spyOn(React, 'useEffect').mockImplementationOnce((f) => f());
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export function InternalUserEdit(props: InternalUserEditDeps) {

fetchData();
}
}, [addToast, props.action, props.coreStart.http, props.sourceUserName, dataSource.id]);
}, [addToast, props.action, props.coreStart.http, props.sourceUserName, dataSource]);

const updateUserHandler = async () => {
try {
Expand Down
Loading
Loading