auditLogList.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. import {Fragment} from 'react';
  2. import styled from '@emotion/styled';
  3. import {ActivityAvatar} from 'sentry/components/activity/item/avatar';
  4. import UserAvatar from 'sentry/components/avatar/userAvatar';
  5. import Tag from 'sentry/components/badge/tag';
  6. import {DateTime} from 'sentry/components/dateTime';
  7. import SelectControl from 'sentry/components/forms/controls/selectControl';
  8. import Link from 'sentry/components/links/link';
  9. import type {CursorHandler} from 'sentry/components/pagination';
  10. import Pagination from 'sentry/components/pagination';
  11. import {PanelTable} from 'sentry/components/panels/panelTable';
  12. import {Tooltip} from 'sentry/components/tooltip';
  13. import {t, tct} from 'sentry/locale';
  14. import {space} from 'sentry/styles/space';
  15. import type {AuditLog, Organization} from 'sentry/types/organization';
  16. import type {User} from 'sentry/types/user';
  17. import {shouldUse24Hours} from 'sentry/utils/dates';
  18. import useOrganization from 'sentry/utils/useOrganization';
  19. import useProjects from 'sentry/utils/useProjects';
  20. import SettingsPageHeader from 'sentry/views/settings/components/settingsPageHeader';
  21. import {
  22. projectDetectorSettingsId,
  23. retentionPrioritiesLabels,
  24. } from 'sentry/views/settings/projectPerformance/projectPerformance';
  25. const avatarStyle = {
  26. width: 36,
  27. height: 36,
  28. marginRight: 8,
  29. };
  30. const getAvatarDisplay = (logEntryUser: User | undefined) => {
  31. // Display Sentry's avatar for system or superuser-initiated events
  32. if (
  33. logEntryUser?.isSuperuser ||
  34. (logEntryUser?.name === 'Sentry' && logEntryUser?.email === undefined)
  35. ) {
  36. return <SentryAvatar type="system" size={36} />;
  37. }
  38. // Display user's avatar for non-superusers-initiated events
  39. if (logEntryUser !== undefined) {
  40. return <UserAvatar style={avatarStyle} user={logEntryUser} />;
  41. }
  42. return null;
  43. };
  44. const addUsernameDisplay = (logEntryUser: User | undefined) => {
  45. if (logEntryUser?.isSuperuser) {
  46. return (
  47. <Name data-test-id="actor-name">
  48. {logEntryUser.name}
  49. <StaffTag>{t('Sentry Staff')}</StaffTag>
  50. </Name>
  51. );
  52. }
  53. if (logEntryUser !== undefined) {
  54. return <Name data-test-id="actor-name">{logEntryUser.name}</Name>;
  55. }
  56. return null;
  57. };
  58. const getTypeDisplay = (event: string) => {
  59. if (event.startsWith('rule.')) {
  60. return event.replace('rule.', 'issue-alert.');
  61. }
  62. if (event.startsWith('alertrule.')) {
  63. return event.replace('alertrule.', 'metric-alert.');
  64. }
  65. return event;
  66. };
  67. const getEventOptions = (eventTypes: string[] | null) =>
  68. eventTypes
  69. ?.map(type => {
  70. // Having both rule.x and alertrule.x may be confusing, so we'll replace their labels to be more descriptive.
  71. // We need to maintain the values here so we still fetch the correct audit log events from the backend should we want
  72. // to filter.
  73. // See https://github.com/getsentry/sentry/issues/46997
  74. if (type.startsWith('rule.')) {
  75. return {
  76. label: type.replace('rule.', 'issue-alert.'),
  77. value: type,
  78. };
  79. }
  80. if (type.startsWith('alertrule.')) {
  81. return {
  82. label: type.replace('alertrule.', 'metric-alert.'),
  83. value: type,
  84. };
  85. }
  86. return {
  87. label: type,
  88. value: type,
  89. };
  90. })
  91. .sort((a, b) => a.label.localeCompare(b.label));
  92. function AuditNote({
  93. entry,
  94. orgSlug,
  95. }: {
  96. entry: NonNullable<AuditLog>;
  97. orgSlug: Organization['slug'];
  98. }) {
  99. const {projects} = useProjects();
  100. const project = projects.find(p => p.id === String(entry.data.id));
  101. if (entry.event.startsWith('rule.')) {
  102. return <Note>{entry.note.replace('rule', 'issue alert rule')}</Note>;
  103. }
  104. if (!project) {
  105. return <Note>{entry.note}</Note>;
  106. }
  107. if (entry.event === 'project.create') {
  108. return (
  109. <Note>
  110. {tct('Created project [projectSettingsLink]', {
  111. projectSettingsLink: (
  112. <Link to={`/settings/${orgSlug}/projects/${project.slug}/`}>
  113. {entry.data.slug}
  114. </Link>
  115. ),
  116. })}
  117. </Note>
  118. );
  119. }
  120. if (entry.event === 'project.edit') {
  121. if (entry.data.old_slug && entry.data.new_slug) {
  122. return (
  123. <Note>
  124. {tct('Renamed project slug from [old-slug] to [new-slug]', {
  125. 'old-slug': entry.data.old_slug,
  126. 'new-slug': (
  127. <Link to={`/settings/${orgSlug}/projects/${entry.data.new_slug}/`}>
  128. {entry.data.new_slug}
  129. </Link>
  130. ),
  131. })}
  132. </Note>
  133. );
  134. }
  135. return (
  136. <Note>
  137. {tct('Edited project [projectSettingsLink] [note]', {
  138. projectSettingsLink: (
  139. <Link to={`/settings/${orgSlug}/projects/${project.slug}/`}>
  140. {entry.data.slug}
  141. </Link>
  142. ),
  143. note: entry.note.replace('edited project settings ', ''),
  144. })}
  145. </Note>
  146. );
  147. }
  148. if (entry.event === 'project.change-performance-issue-detection') {
  149. return (
  150. <Note>
  151. {tct('Edited project [projectSettingsLink] [note]', {
  152. projectSettingsLink: (
  153. <Link
  154. to={`/settings/${orgSlug}/projects/${project.slug}/performance/#${projectDetectorSettingsId}`}
  155. >
  156. {entry.data.slug} performance issue detector settings
  157. </Link>
  158. ),
  159. note: entry.note.replace(
  160. 'edited project performance issue detector settings ',
  161. ''
  162. ),
  163. })}
  164. </Note>
  165. );
  166. }
  167. if (entry.event === 'sampling_priority.enabled') {
  168. return (
  169. <Note>
  170. {tct(
  171. 'Enabled retention priority "[biasLabel]" in project [samplingInProjectSettingsLink]',
  172. {
  173. samplingInProjectSettingsLink: (
  174. <Link to={`/settings/${orgSlug}/projects/${project.slug}/performance/`}>
  175. {entry.data.slug}
  176. </Link>
  177. ),
  178. // @ts-expect-error TS(7053): Element implicitly has an 'any' type because expre... Remove this comment to see the full error message
  179. biasLabel: retentionPrioritiesLabels[entry.data.name],
  180. }
  181. )}
  182. </Note>
  183. );
  184. }
  185. if (entry.event === 'sampling_priority.disabled') {
  186. return (
  187. <Note>
  188. {tct(
  189. 'Disabled retention priority "[biasLabel]" in project [samplingInProjectSettingsLink]',
  190. {
  191. samplingInProjectSettingsLink: (
  192. <Link to={`/settings/${orgSlug}/projects/${project.slug}/performance/`}>
  193. {entry.data.slug}
  194. </Link>
  195. ),
  196. // @ts-expect-error TS(7053): Element implicitly has an 'any' type because expre... Remove this comment to see the full error message
  197. biasLabel: retentionPrioritiesLabels[entry.data.name],
  198. }
  199. )}
  200. </Note>
  201. );
  202. }
  203. if (entry.event === 'project.ownership-rule.edit') {
  204. return (
  205. <Note>
  206. {tct('Modified ownership rules in project [projectSettingsLink]', {
  207. projectSettingsLink: (
  208. <Link to={`/settings/${orgSlug}/projects/${project.slug}/`}>
  209. {entry.data.slug}
  210. </Link>
  211. ),
  212. })}
  213. </Note>
  214. );
  215. }
  216. return <Note>{entry.note}</Note>;
  217. }
  218. type Props = {
  219. entries: AuditLog[] | null;
  220. eventType: string | undefined;
  221. eventTypes: string[] | null;
  222. isLoading: boolean;
  223. onCursor: CursorHandler | undefined;
  224. onEventSelect: (value: string) => void;
  225. pageLinks: string | null;
  226. };
  227. function AuditLogList({
  228. isLoading,
  229. pageLinks,
  230. entries,
  231. eventType,
  232. eventTypes,
  233. onCursor,
  234. onEventSelect,
  235. }: Props) {
  236. const is24Hours = shouldUse24Hours();
  237. const organization = useOrganization();
  238. const hasEntries = entries && entries.length > 0;
  239. const ipv4Length = 15;
  240. const action = (
  241. <EventSelector
  242. clearable
  243. isDisabled={isLoading}
  244. name="eventFilter"
  245. value={eventType}
  246. placeholder={t('Select Action: ')}
  247. options={getEventOptions(eventTypes)}
  248. onChange={(options: any) => {
  249. onEventSelect(options?.value);
  250. }}
  251. />
  252. );
  253. return (
  254. <div>
  255. <SettingsPageHeader title={t('Audit Log')} action={action} />
  256. <PanelTable
  257. headers={[t('Member'), t('Action'), t('IP'), t('Time')]}
  258. isEmpty={!hasEntries && entries?.length === 0}
  259. emptyMessage={t('No audit entries available')}
  260. isLoading={isLoading}
  261. >
  262. {(entries ?? []).map(entry => {
  263. if (!entry) {
  264. return null;
  265. }
  266. return (
  267. <Fragment key={entry.id}>
  268. <UserInfo>
  269. <div>{getAvatarDisplay(entry.actor)}</div>
  270. <NameContainer>
  271. {addUsernameDisplay(entry.actor)}
  272. <AuditNote entry={entry} orgSlug={organization.slug} />
  273. </NameContainer>
  274. </UserInfo>
  275. <FlexCenter>
  276. <MonoDetail>{getTypeDisplay(entry.event)}</MonoDetail>
  277. </FlexCenter>
  278. <FlexCenter>
  279. {entry.ipAddress && (
  280. <IpAddressOverflow>
  281. <Tooltip
  282. title={entry.ipAddress}
  283. disabled={entry.ipAddress.length <= ipv4Length}
  284. >
  285. <MonoDetail>{entry.ipAddress}</MonoDetail>
  286. </Tooltip>
  287. </IpAddressOverflow>
  288. )}
  289. </FlexCenter>
  290. <TimestampInfo>
  291. <DateTime dateOnly date={entry.dateCreated} />
  292. <DateTime
  293. timeOnly
  294. format={is24Hours ? 'HH:mm zz' : 'LT zz'}
  295. date={entry.dateCreated}
  296. />
  297. </TimestampInfo>
  298. </Fragment>
  299. );
  300. })}
  301. </PanelTable>
  302. {pageLinks && <Pagination pageLinks={pageLinks} onCursor={onCursor} />}
  303. </div>
  304. );
  305. }
  306. const SentryAvatar = styled(ActivityAvatar)`
  307. margin-right: ${space(1)};
  308. `;
  309. const Name = styled('strong')`
  310. font-size: ${p => p.theme.fontSizeMedium};
  311. `;
  312. const StaffTag = styled(Tag)`
  313. padding: ${space(1)};
  314. `;
  315. const EventSelector = styled(SelectControl)`
  316. width: 250px;
  317. `;
  318. const UserInfo = styled('div')`
  319. display: flex;
  320. align-items: center;
  321. line-height: 1.2;
  322. font-size: ${p => p.theme.fontSizeSmall};
  323. min-width: 250px;
  324. `;
  325. const NameContainer = styled('div')`
  326. display: flex;
  327. flex-direction: column;
  328. justify-content: center;
  329. `;
  330. const Note = styled('div')`
  331. font-size: ${p => p.theme.fontSizeSmall};
  332. word-break: break-word;
  333. margin-top: ${space(0.5)};
  334. `;
  335. const FlexCenter = styled('div')`
  336. display: flex;
  337. align-items: center;
  338. `;
  339. const IpAddressOverflow = styled('div')`
  340. ${p => p.theme.overflowEllipsis};
  341. min-width: 90px;
  342. `;
  343. const MonoDetail = styled('code')`
  344. font-size: ${p => p.theme.fontSizeMedium};
  345. white-space: no-wrap;
  346. `;
  347. const TimestampInfo = styled('div')`
  348. display: grid;
  349. grid-template-rows: auto auto;
  350. gap: ${space(1)};
  351. font-size: ${p => p.theme.fontSizeMedium};
  352. `;
  353. export default AuditLogList;