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(console): fix impersonation tag in audit log #6463

Merged
merged 1 commit into from
Aug 19, 2024
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
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { type Log } from '@logto/schemas';
import classNames from 'classnames';
import { Link } from 'react-router-dom';

Expand All @@ -8,14 +9,16 @@ import Tag from '@/ds-components/Tag';
import useTenantPathname from '@/hooks/use-tenant-pathname';

import styles from './index.module.scss';
import { isImpersonationLog } from './utils';

type Props = {
readonly eventKey: string;
readonly isSuccess: boolean;
readonly payload: Log['payload'];
readonly to?: string;
};

function EventName({ eventKey, isSuccess, to }: Props) {
function EventName({ eventKey, payload, isSuccess, to }: Props) {
const title = logEventTitle[eventKey] ?? eventKey;
const { getTo } = useTenantPathname();

Expand All @@ -36,7 +39,10 @@ function EventName({ eventKey, isSuccess, to }: Props) {
</Link>
)}
{!to && <div className={styles.title}>{title}</div>}
{eventKey === 'ExchangeTokenBy.TokenExchange' && <Tag status="alert">Impersonation</Tag>}
{isImpersonationLog({
key: eventKey,
payload,
}) && <Tag status="alert">Impersonation</Tag>}
</div>
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { type Log } from '@logto/schemas';

/**
* Check if the log (token exchange) is an impersonation log.
*
* @param log - The log object.
* @returns Whether the log is an impersonation log.
*/
export const isImpersonationLog = (
log: Pick<Log, 'key'> & { payload: Pick<Log['payload'], 'params'> }
) => {
if (log.key !== 'ExchangeTokenBy.TokenExchange') {
return false;
}

if (log.payload.params?.subject_token_type === 'urn:ietf:params:oauth:token-type:access_token') {
return true;
}

return false;
};
8 changes: 6 additions & 2 deletions packages/console/src/components/AuditLogTable/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
...conditional(!applicationId && { applicationId: '' }),
});

// TODO: LOG-7135, revisit this fallback logic and see whether this should be done outside of this component.

Check warning on line 47 in packages/console/src/components/AuditLogTable/index.tsx

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/console/src/components/AuditLogTable/index.tsx#L47

[no-warning-comments] Unexpected 'todo' comment: 'TODO: LOG-7135, revisit this fallback...'.
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
const searchApplicationId = applicationId || applicationIdFromSearch;
const { data: specifiedApplication } = useSWR<ApplicationResponse>(
Expand All @@ -70,8 +70,12 @@
title: t('logs.event'),
dataIndex: 'event',
colSpan: isUserColumnVisible ? 5 : 6,
render: ({ key, payload: { result } }) => (
<EventName eventKey={key} isSuccess={result === LogResult.Success} />
render: ({ key, payload }) => (
<EventName
eventKey={key}
isSuccess={payload.result === LogResult.Success}
payload={payload}
/>
),
};

Expand Down
5 changes: 2 additions & 3 deletions packages/console/src/pages/AuditLogDetails/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { useLocation, useParams } from 'react-router-dom';
import useSWR from 'swr';

import ApplicationName from '@/components/ApplicationName';
import { isImpersonationLog } from '@/components/AuditLogTable/components/EventName/utils';
import DetailsPage from '@/components/DetailsPage';
import PageMeta from '@/components/PageMeta';
import UserName from '@/components/UserName';
Expand Down Expand Up @@ -87,9 +88,7 @@ function AuditLogDetails() {
<div className={styles.content}>
<div className={styles.eventName}>
{logEventTitle[data.key]}
{data.key === 'ExchangeTokenBy.TokenExchange' && (
<Tag status="alert">Impersonation</Tag>
)}
{isImpersonationLog(data) && <Tag status="alert">Impersonation</Tag>}
</div>
<div className={styles.basicInfo}>
<div className={styles.infoItem}>
Expand Down
1 change: 1 addition & 0 deletions packages/schemas/src/foundations/jsonb-types/logs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export const logContextPayloadGuard = z
userId: z.string().optional(),
applicationId: z.string().optional(),
sessionId: z.string().optional(),
params: z.record(z.string(), z.unknown()).optional(),
})
.catchall(z.unknown());

Expand Down
Loading