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

fix(Auth): do not show access error when redirecting to auth #1803

Merged
merged 1 commit into from
Jan 9, 2025
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
15 changes: 6 additions & 9 deletions src/components/Errors/PageError/PageError.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react';

import {isAccessError, isRedirectToAuth} from '../../../utils/response';
import type {EmptyStateProps} from '../../EmptyState';
import {EmptyState} from '../../EmptyState';
import {Illustration} from '../../Illustration';
Expand All @@ -15,6 +16,11 @@ interface PageErrorProps extends Omit<EmptyStateProps, 'image' | 'title' | 'desc
}

export function PageError({title, description, error, children, ...restProps}: PageErrorProps) {
if (isRedirectToAuth(error)) {
// Do not show an error, because we redirect to auth anyway.
return null;
}

if (isAccessError(error)) {
return <AccessDenied title={title} description={description} {...restProps} />;
}
Expand All @@ -32,12 +38,3 @@ export function PageError({title, description, error, children, ...restProps}: P

return <React.Fragment>{children}</React.Fragment>;
}

export function isAccessError(error: unknown) {
return Boolean(
error &&
typeof error === 'object' &&
'status' in error &&
(error.status === 403 || error.status === 401),
);
}
2 changes: 1 addition & 1 deletion src/containers/Operations/Operations.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import React from 'react';

import {AccessDenied} from '../../components/Errors/403';
import {isAccessError} from '../../components/Errors/PageError/PageError';
import {ResponseError} from '../../components/Errors/ResponseError';
import {ResizeableDataTable} from '../../components/ResizeableDataTable/ResizeableDataTable';
import {TableWithControlsLayout} from '../../components/TableWithControlsLayout/TableWithControlsLayout';
import {operationsApi} from '../../store/reducers/operations';
import {useAutoRefreshInterval} from '../../utils/hooks';
import {isAccessError} from '../../utils/response';

import {OperationsControls} from './OperationsControls';
import {getColumns} from './columns';
Expand Down
3 changes: 2 additions & 1 deletion src/containers/Tenant/Tenant.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import React from 'react';
import {Helmet} from 'react-helmet-async';
import {StringParam, useQueryParams} from 'use-query-params';

import {PageError, isAccessError} from '../../components/Errors/PageError/PageError';
import {PageError} from '../../components/Errors/PageError/PageError';
import {LoaderWrapper} from '../../components/LoaderWrapper/LoaderWrapper';
import SplitPane from '../../components/SplitPane';
import {setHeaderBreadcrumbs} from '../../store/reducers/header/header';
Expand All @@ -12,6 +12,7 @@ import type {AdditionalNodesProps, AdditionalTenantsProps} from '../../types/add
import {cn} from '../../utils/cn';
import {DEFAULT_IS_TENANT_SUMMARY_COLLAPSED, DEFAULT_SIZE_TENANT_KEY} from '../../utils/constants';
import {useAutoRefreshInterval, useTypedDispatch} from '../../utils/hooks';
import {isAccessError} from '../../utils/response';

import ObjectGeneral from './ObjectGeneral/ObjectGeneral';
import {ObjectSummary} from './ObjectSummary/ObjectSummary';
Expand Down
3 changes: 2 additions & 1 deletion src/services/api/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import axiosRetry from 'axios-retry';

import {backend as BACKEND} from '../../store';
import {DEV_ENABLE_TRACING_FOR_ALL_REQUESTS} from '../../utils/constants';
import {isRedirectToAuth} from '../../utils/response';
import {settingsManager} from '../settings';

export type AxiosOptions = {
Expand Down Expand Up @@ -63,7 +64,7 @@ export class BaseYdbAPI extends AxiosWrapper {
// OIDC proxy returns 401 response with authUrl in it
// authUrl - external auth service link, after successful auth additional cookies will be appended
// that will allow access to clusters where OIDC proxy is a balancer
if (response && response.status === 401 && response.data?.authUrl) {
if (isRedirectToAuth(response)) {
window.location.assign(response.data.authUrl);
}

Expand Down
22 changes: 22 additions & 0 deletions src/utils/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,25 @@ export function isAxiosError(error: unknown): error is AxiosErrorObject {
error && typeof error === 'object' && 'name' in error && error.name === 'AxiosError',
);
}

export function isAccessError(error: unknown): error is {status: number} {
return Boolean(
error &&
typeof error === 'object' &&
'status' in error &&
(error.status === 403 || error.status === 401),
);
}

export function isRedirectToAuth(error: unknown): error is {status: 401; data: {authUrl: string}} {
return Boolean(
isAccessError(error) &&
error.status === 401 &&
'data' in error &&
error.data &&
typeof error.data === 'object' &&
'authUrl' in error.data &&
error.data.authUrl &&
typeof error.data.authUrl === 'string',
);
}
Loading