auditLogList.tsx 10 KB

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