auditLogList.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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. biasLabel: retentionPrioritiesLabels[entry.data.name],
  179. }
  180. )}
  181. </Note>
  182. );
  183. }
  184. if (entry.event === 'sampling_priority.disabled') {
  185. return (
  186. <Note>
  187. {tct(
  188. 'Disabled retention priority "[biasLabel]" in project [samplingInProjectSettingsLink]',
  189. {
  190. samplingInProjectSettingsLink: (
  191. <Link to={`/settings/${orgSlug}/projects/${project.slug}/performance/`}>
  192. {entry.data.slug}
  193. </Link>
  194. ),
  195. biasLabel: retentionPrioritiesLabels[entry.data.name],
  196. }
  197. )}
  198. </Note>
  199. );
  200. }
  201. if (entry.event === 'project.ownership-rule.edit') {
  202. return (
  203. <Note>
  204. {tct('Modified ownership rules in project [projectSettingsLink]', {
  205. projectSettingsLink: (
  206. <Link to={`/settings/${orgSlug}/projects/${project.slug}/`}>
  207. {entry.data.slug}
  208. </Link>
  209. ),
  210. })}
  211. </Note>
  212. );
  213. }
  214. return <Note>{entry.note}</Note>;
  215. }
  216. type Props = {
  217. entries: AuditLog[] | null;
  218. eventType: string | undefined;
  219. eventTypes: string[] | null;
  220. isLoading: boolean;
  221. onCursor: CursorHandler | undefined;
  222. onEventSelect: (value: string) => void;
  223. pageLinks: string | null;
  224. };
  225. function AuditLogList({
  226. isLoading,
  227. pageLinks,
  228. entries,
  229. eventType,
  230. eventTypes,
  231. onCursor,
  232. onEventSelect,
  233. }: Props) {
  234. const is24Hours = shouldUse24Hours();
  235. const organization = useOrganization();
  236. const hasEntries = entries && entries.length > 0;
  237. const ipv4Length = 15;
  238. const action = (
  239. <EventSelector
  240. clearable
  241. isDisabled={isLoading}
  242. name="eventFilter"
  243. value={eventType}
  244. placeholder={t('Select Action: ')}
  245. options={getEventOptions(eventTypes)}
  246. onChange={options => {
  247. onEventSelect(options?.value);
  248. }}
  249. />
  250. );
  251. return (
  252. <div>
  253. <SettingsPageHeader title={t('Audit Log')} action={action} />
  254. <PanelTable
  255. headers={[t('Member'), t('Action'), t('IP'), t('Time')]}
  256. isEmpty={!hasEntries && entries?.length === 0}
  257. emptyMessage={t('No audit entries available')}
  258. isLoading={isLoading}
  259. >
  260. {(entries ?? []).map(entry => {
  261. if (!entry) {
  262. return null;
  263. }
  264. return (
  265. <Fragment key={entry.id}>
  266. <UserInfo>
  267. <div>{getAvatarDisplay(entry.actor)}</div>
  268. <NameContainer>
  269. {addUsernameDisplay(entry.actor)}
  270. <AuditNote entry={entry} orgSlug={organization.slug} />
  271. </NameContainer>
  272. </UserInfo>
  273. <FlexCenter>
  274. <MonoDetail>{getTypeDisplay(entry.event)}</MonoDetail>
  275. </FlexCenter>
  276. <FlexCenter>
  277. {entry.ipAddress && (
  278. <IpAddressOverflow>
  279. <Tooltip
  280. title={entry.ipAddress}
  281. disabled={entry.ipAddress.length <= ipv4Length}
  282. >
  283. <MonoDetail>{entry.ipAddress}</MonoDetail>
  284. </Tooltip>
  285. </IpAddressOverflow>
  286. )}
  287. </FlexCenter>
  288. <TimestampInfo>
  289. <DateTime dateOnly date={entry.dateCreated} />
  290. <DateTime
  291. timeOnly
  292. format={is24Hours ? 'HH:mm zz' : 'LT zz'}
  293. date={entry.dateCreated}
  294. />
  295. </TimestampInfo>
  296. </Fragment>
  297. );
  298. })}
  299. </PanelTable>
  300. {pageLinks && <Pagination pageLinks={pageLinks} onCursor={onCursor} />}
  301. </div>
  302. );
  303. }
  304. const SentryAvatar = styled(ActivityAvatar)`
  305. margin-right: ${space(1)};
  306. `;
  307. const Name = styled('strong')`
  308. font-size: ${p => p.theme.fontSizeMedium};
  309. `;
  310. const StaffTag = styled(Tag)`
  311. padding: ${space(1)};
  312. `;
  313. const EventSelector = styled(SelectControl)`
  314. width: 250px;
  315. `;
  316. const UserInfo = styled('div')`
  317. display: flex;
  318. align-items: center;
  319. line-height: 1.2;
  320. font-size: ${p => p.theme.fontSizeSmall};
  321. min-width: 250px;
  322. `;
  323. const NameContainer = styled('div')`
  324. display: flex;
  325. flex-direction: column;
  326. justify-content: center;
  327. `;
  328. const Note = styled('div')`
  329. font-size: ${p => p.theme.fontSizeSmall};
  330. word-break: break-word;
  331. margin-top: ${space(0.5)};
  332. `;
  333. const FlexCenter = styled('div')`
  334. display: flex;
  335. align-items: center;
  336. `;
  337. const IpAddressOverflow = styled('div')`
  338. ${p => p.theme.overflowEllipsis};
  339. min-width: 90px;
  340. `;
  341. const MonoDetail = styled('code')`
  342. font-size: ${p => p.theme.fontSizeMedium};
  343. white-space: no-wrap;
  344. `;
  345. const TimestampInfo = styled('div')`
  346. display: grid;
  347. grid-template-rows: auto auto;
  348. gap: ${space(1)};
  349. font-size: ${p => p.theme.fontSizeMedium};
  350. `;
  351. export default AuditLogList;