Get active and inactive users
Identify users who have logged into Harness over a specified time period.
You can identify which users have logged into your Harness account over a specific time period. This is useful for:
Compliance and auditing: Track user access for security and regulatory requirements.
License management: Identify active users to optimize license usage.
User lifecycle management: Find inactive users who may need to be offboarded.
This guide provides a Python script that uses the Harness Audit API to fetch user login activity and categorize users as active, inactive, or deleted.
How it works
The script queries the Harness Audit API for LOGIN events within a specified date range and compares this data against all users in your account. It then generates three output files:
active_users.ndjson: Users who logged in during the specified time period.
inactive_users.ndjson: Users who exist in the account but did not log in during the specified time period.
deleted_users.ndjson: Users who logged in during the specified time period but no longer exist in the account.
The output files use NDJSON format (newline-delimited JSON), where each line is a valid JSON object representing a user record.
Prerequisites
Before using the script, ensure you have:
Python 3.x installed on your system.
Python requests library: Install using
pip install requests.A token with appropriate permissions. For more information, go to Manage API keys.
Permission to view audit logs in your Harness account.
Your Harness account ID. You can find this in any Harness URL (e.g.,
https://app.harness.io/ng/account/<ACCOUNT_ID>/...).
Usage
Basic usage
Run the script with your environment URL and authentication credentials:
By default, the script analyzes the last 30 days of login activity.
Specify custom date range
To analyze a specific time period, use the --start and --end parameters:
Use environment variables
You can set authentication credentials as environment variables:
Supported environment variables:
HARNESS_API_KEY: Your Harness API keyHARNESS_BEARER: Your Bearer token
Script parameters
--env
Yes
Harness environment URL in the format <domain>.harness.io/ng/account/<account_id>/
None
app.harness.io/ng/account/abc123/
--apikey
No*
Harness API key for authentication
HARNESS_API_KEY env var
pat.abc123.xyz...
--bearer
No*
Bearer token for authentication
HARNESS_BEARER env var
eyJhbGc...
--start
No
Start date and time in YYYY-MM-DD HH:MM format
30 days ago
2025-01-01 00:00
--end
No
End date and time in YYYY-MM-DD HH:MM format
Current time
2025-01-31 23:59
* One of --apikey or --bearer is required (or their corresponding environment variables).
Understanding the output
The script generates three NDJSON files in the current directory:
active_users.ndjson
Contains audit log entries for users who logged in during the specified time period. Each line includes:
inactive_users.ndjson
Contains user records for users who exist in the account but did not log in during the specified time period. Each line includes:
deleted_users.ndjson
Contains audit log entries for users who logged in during the specified time period but no longer exist in the account (deleted users).
Analyzing the output
You can process the NDJSON files using command-line tools or Python:
Count users using wc
Extract emails using jq
Process with Python
Complete script
Save the following script as get_inactive_users.py:
Troubleshooting
Authentication errors
Error: 401 Unauthorized
Solution: Verify that your API key or Bearer token is valid and has the necessary permissions to access audit logs. For more information, go to Manage API keys.
Invalid date format
Error: Invalid date format
Solution: Ensure dates are in the format YYYY-MM-DD HH:MM. For example: 2025-01-01 00:00.
Rate limiting
Error: 429 Too Many Requests
Solution: The script has exceeded the Harness API rate limits. Wait a few minutes and try again. For more information about rate limits, go to Rate limits.
Permission errors
Error: 403 Forbidden
Solution: Your API key or Bearer token does not have permission to view audit logs or user information. Ensure you have the necessary permissions to access these resources.
Large datasets
Note: For accounts with many users or extensive audit history, the script may take several minutes to complete. The script processes data in pages and displays progress as it runs.
Related documentation
Last updated
Was this helpful?