Sub topic 12
Monitoring and Auditing Activity
This chapter assumes you have already built the integration user, its permission set, and its OAuth connection. You should know basic SOQL and be able to open Setup as an administrator in a sandbox or Developer Edition org. The goal here is to watch the user after it goes live, so that over-privilege, stolen credentials, and silent failures show up as evidence instead of surprises.
Why monitoring matters for a least-privilege user
Least privilege limits what the integration user can do. Monitoring tells you what it actually does. The two work together:
- Restrictions can drift. Someone adds a permission set during an incident and forgets to remove it.
- Credentials can leak. A private key copied into a repository still lets an attacker log in as the integration user.
- Integrations can break. A denied field or an expired certificate often appears first as a login failure or an API error.
An integration user is predictable. It logs in from a small set of IP addresses, uses one connected app, and calls the same few objects on a schedule. That predictability makes anomalies easy to spot, which is the main reason to give the integration its own user.
What Salesforce records
Several sources answer different questions. Pick the source that matches the question.
| Question | Source | Where to find it |
|---|---|---|
| When did the user log in, and from where? | Login History | `LoginHistory` object, Setup > Login History |
| Did anyone change its permissions or settings? | Setup Audit Trail | `SetupAuditTrail` object, Setup > View Setup Audit Trail |
| Which API calls and queries did it run? | Event Monitoring (Event Log Files) | `EventLogFile` object, requires Event Monitoring |
| Which OAuth tokens are active? | OAuth connected apps usage | Setup > Connected Apps OAuth Usage |
| Which records did it change? | Field history, `LastModifiedById`, `CreatedById` | Record fields and history tracking |
Login History and Setup Audit Trail are available in every org. Event Log Files with API-level detail need the Event Monitoring add-on (Shield or the standalone license), so check what your org has before you plan around them.
Reviewing logins
Login History is the first place to look. The query below returns the 20 most recent logins for the integration user.
SELECT LoginTime, SourceIp, LoginType, Status, Application
FROM LoginHistory
WHERE UserId = '005XXXXXXXXXXXXXXX'
ORDER BY LoginTime DESC
LIMIT 20Read each column with a specific expectation:
SourceIpshould match the addresses of your integration host or middleware. Any other address is worth investigating, even if the login succeeded.LoginTypefor a JWT or OAuth flow shows an OAuth type, not "Application". A plain password login from this user means someone is using it interactively.Statusshould be "Success" for nearly all rows. A run of failures followed by a success can mean someone guessed their way in, or that your own integration retried against an expired credential.Applicationshould name your connected app or external client app. An unfamiliar application name means a different client obtained a token for this user.
If you set Login IP Ranges on the profile, logins from other addresses are refused and show up as failures. Treat a burst of those failures as a signal that either the integration host moved or something else is trying to use the account.
Summarizing instead of scanning
For a user that logs in often, scanning rows by eye does not scale. Aggregate instead. This query counts logins by IP address and status for the last seven days.
SELECT SourceIp, Status, COUNT(Id)
FROM LoginHistory
WHERE UserId = '005XXXXXXXXXXXXXXX'
AND LoginTime = LAST_N_DAYS:7
GROUP BY SourceIp, StatusYou expect a short list. A new SourceIp row is the finding.
Reviewing configuration changes
Login History shows use of the account. Setup Audit Trail shows changes to it. Query it for recent edits to the integration user, its permission sets, and its connected app.
SELECT CreatedDate, CreatedBy.Name, Action, Section, Display
FROM SetupAuditTrail
WHERE CreatedDate = LAST_N_DAYS:30
AND (Display LIKE '%Integration%' OR Section = 'Manage Users')
ORDER BY CreatedDate DESC
LIMIT 50Look for these actions:
- A permission set assigned to the integration user that you did not plan.
- A change to an object or field permission in the integration permission set, especially anything that adds Delete, View All, or Modify All.
- Changes to the profile's Login IP Ranges or login hours.
- A password reset, freeze, or unfreeze of the user.
- Edits to the connected app, such as new OAuth scopes or a new certificate.
Setup Audit Trail keeps 180 days of history in the UI and the SetupAuditTrail object. If your compliance rules require longer retention, export the data on a schedule.
Reviewing what the user did
Login History says the user connected. It does not say what the user did. To see that, you have three options, from cheapest to most complete.
Record ownership and modification
Every record carries CreatedById and LastModifiedById. Because the integration user is dedicated, these fields tell you which records the integration touched.
SELECT Id, Name, LastModifiedDate
FROM Account
WHERE LastModifiedById = '005XXXXXXXXXXXXXXX'
AND LastModifiedDate = LAST_N_DAYS:1For a read-only integration, this query should return nothing. If it returns rows, the user has Edit permission somewhere it should not, and you have found over-privilege by its effect. Go back to the permission checks from the verification chapter and compare the permission set against what you intended.
Event Log Files
With Event Monitoring, EventLogFile holds daily CSV logs of API and report activity. The ApiEvent and RestApi event types list the calls made, the user, the client IP, and the objects queried.
SELECT Id, EventType, LogDate, LogFileLength
FROM EventLogFile
WHERE EventType = 'RestApi'
AND LogDate = LAST_N_DAYS:7The log content is downloaded from the LogFile field of each row through the REST API. Filter the downloaded rows by USER_ID to isolate the integration user. Compare the URIs and query text with the list of objects you wrote during requirements analysis. A call to an object outside that list means either the permission set is too broad or the integration code has changed.
Field audit and history tracking
If the integration writes to fields that matter, enable field history tracking for those fields. History records show the old value, new value, and the user who made the change. This applies only to the fields you select, and each object has limits on how many fields can be tracked, so use it for the few fields where a wrong write would hurt.
Checking active tokens and sessions
A JWT bearer flow creates an access token each time the integration logs in. Tokens are short-lived, but refresh tokens from other OAuth flows can last until revoked. Review them in two places:
- Setup > Connected Apps OAuth Usage lists apps with active tokens, the number of users, and the use count. A connected app you do not recognize, or an unexpected user count on your integration app, is a finding.
- In the user's detail page, the OAuth Connected Apps section lists tokens for that user, and you can revoke them individually.
Revoking a token is a fast containment step. It ends access without changing the user's configuration, and a legitimate integration that uses JWT simply logs in again.
Turning checks into alerts
Manual review only works if someone does it. Convert the most useful checks into automated alerts.
- Run the login summary query on a schedule, for example from a scheduled job or an external script that authenticates with a separate read-only monitoring user, and notify a team channel when a new
SourceIpappears. - With Event Monitoring, Transaction Security policies can block or notify on conditions such as a very large query result or an API call from an unexpected IP. Use these only if your license includes them.
- Create a report on
LoginHistoryfiltered to the integration user and subscribe the security owner to it.
Keep the monitoring user separate from the integration user. A monitor that shares the integration's credentials would be silenced by the same compromise it is meant to detect.
A short review routine
A routine you can repeat is more useful than a perfect design you never run. A weekly pass for a busy integration, or monthly for a quiet one, covers the essentials:
- Run the login summary query and confirm every
SourceIpandApplicationis known. - Query
PermissionSetAssignmentfor the user and confirm that only the expected permission sets appear. - Search Setup Audit Trail for changes to the user, its permission sets, and the connected app.
- Check that
LastModifiedByIdshows no writes outside what the integration is allowed to do. - Record the result and who reviewed it, even when nothing was found.
Common errors
- Watching only failures. A successful login from an unknown IP is more dangerous than a failed one. Review successes too.
- Assuming Login History covers API calls. It records logins, not what happened afterward. Without Event Monitoring, rely on
LastModifiedByIdand history tracking. - Reusing the integration user for monitoring. Compromise of one credential then blinds the check.
- Ignoring data retention. Login History in the UI is limited to six months, and Setup Audit Trail to 180 days. Export what you need to keep.
- Treating a clean report as proof. A quiet log means the checks found nothing. Confirm the checks work by making a deliberate test change in a sandbox and seeing it appear.
Exercise
Your integration user is meant to be read-only on Account. In Login History you see successful logins from the expected IP, yet a query on Account for LastModifiedById equal to the integration user returns 14 records modified yesterday. Which record source revealed the problem, what does it mean, and what two steps do you take next?
Show answer
The record fields (LastModifiedById and LastModifiedDate) revealed it. Login History cannot show writes, so the clean login history hid the problem. The result means the integration user has Edit permission on Account, either through the permission set, another assigned permission set, or its profile. Next, (1) run the PermissionSetAssignment and ObjectPermissions queries to find which permission set grants Edit, then remove it. (2) Check Setup Audit Trail for who added the permission and when, and find out whether the integration code or someone else made the writes.
Exercise
A new SourceIp appears in the weekly login summary for the integration user, with Status "Success" and the correct connected app name. Give two possible explanations and one immediate containment step that does not break the integration if the activity is legitimate.
Show answer
Possible explanations: the integration host or middleware moved to a new address, or someone obtained the private key and is logging in from another machine. Containment: revoke the user's active OAuth tokens from the user's detail page or Connected Apps OAuth Usage. A legitimate JWT integration obtains a new token on its next run. If the IP is not explained, also rotate the certificate and key and check that Login IP Ranges are set.