auditLogList.tsx 9.4 KB

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