Sub topic 12
Monitoring and Auditing Activity
This chapter assumes you have already built the integration user, its permission set, and its OAuth connection. It covers how to watch that user after go-live. A least-privilege user can still be abused if its key leaks, so you need to notice when its behavior changes.
What to watch
An integration user behaves predictably. It logs in from a few known addresses, through one connected app, at regular intervals. Anything outside that pattern is a signal. The main sources in Salesforce are:
| Source | What it tells you |
|---|---|
| `LoginHistory` | Who logged in, when, from which IP, and through which application |
| Setup Audit Trail | Changes to the user, its permission sets, and its connected app |
| Event Monitoring (add-on) | API calls, queries, and report exports at a detailed level |
| API usage limits | Unexpected spikes in request volume |
LoginHistory and Setup Audit Trail are included in all orgs. Event Monitoring needs an extra license, so plan for it only if you have it.
Review logins
Start with the logins. This query 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 20Check each column against what you expect:
SourceIpshould match your integration server or its NAT gateway. A new address needs an explanation.Applicationshould name your connected app. A different application means the user is being used some other way.Statusshould beSuccessalmost always. Repeated failures can mean a rotated key, an expired certificate, or someone guessing.LoginTypeshould be the OAuth type you set up, not a direct UI login.
Review configuration changes
Logins show use. The Setup Audit Trail shows tampering. Open Setup, then View Setup Audit Trail, and look for changes to the integration user, its permission sets, and its connected app. Permission creep often happens through a small edit, such as an admin adding "Modify All Data" to fix an error.
You can also query the trail. The SetupAuditTrail object is available through the API:
SELECT CreatedDate, CreatedBy.Name, Action, Section, Display
FROM SetupAuditTrail
WHERE Section IN ('Manage Users', 'Permission Sets')
ORDER BY CreatedDate DESC
LIMIT 50The trail keeps 180 days of history, and you can download it as a CSV. Compare any permission set changes against the version in your source control. If the org differs from the repository, find out who changed it and why.
Re-check effective permissions
Run the permission queries from the verification chapter again on a schedule. The assignment query lists everything granted to the user:
SELECT PermissionSet.Name, PermissionSet.Label, PermissionSet.IsOwnedByProfile
FROM PermissionSetAssignment
WHERE AssigneeId = '005XXXXXXXXXXXXXXX'Any permission set that is not in your design is a finding. The same applies to flags such as Delete, View All, or Modify All on the object permissions.
A simple review loop
Run the loop weekly at first, then monthly once the pattern is stable. Automate the queries where you can, and send the results to the people who own the integration.
Common mistakes
- Reviewing only when something breaks. A leaked key often causes no visible failure.
- Alerting on every failed login. Tune the alert to a threshold, or it will be ignored.
- Sharing the integration user's credentials with a person "just to debug". This makes the logs meaningless, because you can no longer tell the integration from the human.
- Forgetting that a change in
SourceIpis normal after an infrastructure move. Update your expected list when that happens.
Exercise
The integration user's LoginHistory shows successful logins from an IP address you do not recognize, through the correct connected app. The login IP range restriction was set on the profile. List two places you would look next.
Show answer
First, the Setup Audit Trail, to see whether someone changed the profile's login IP ranges or the connected app's IP relaxation setting. Second, the permission and assignment queries, to see whether the user has gained extra access. Also check whether the integration's hosting changed, since a new NAT address is the most common harmless cause. If it is not, rotate the key or certificate and revoke the user's active sessions.