auditLogList.tsx 10 KB

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