Skip to content

Commit e9ef31f

Browse files
Merge pull request #63 from thoughtspot/ts-org-inactivity-cleanup
Add script for removing inactive users from org
2 parents 04d265d + 453a30d commit e9ef31f

6 files changed

Lines changed: 1039 additions & 0 deletions

File tree

‎rest-api/Readme.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@
99
- [React](./typescript-sdk/react-rest-api/)
1010
4. [Trusted auth server typescript](./trusted-auth-server-typescript/)
1111
5. [Trusted auth server python](./trusted-auth-server-python/)
12+
6. [Org inactive users cleanup](./ts-org-inactive-users-cleanup/)
1213

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Copy this file to .env and fill in your values:
2+
# cp .env.example .env
3+
4+
TS_CLUSTER_URL=https://your-cluster.thoughtspot.cloud
5+
6+
# Auth — pick ONE of these two methods
7+
# The account needs "Can administer Org" rights on TS_ORG_ID below.
8+
9+
# Method 1: Trusted Auth secret_key. Can itself be org-scoped on some
10+
# deployments. If you hit "service secret code is not valid", get the key
11+
# from TS_ORG_ID's own Security Settings, or use Method 2 instead.
12+
TS_SECRET_KEY=
13+
TS_ADMIN_USERNAME=
14+
15+
# Method 2: username + password (leave TS_SECRET_KEY blank to use this)
16+
TS_USERNAME=
17+
TS_PASSWORD=
18+
19+
#The org to process
20+
TS_ORG_ID=
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Generated by ts-org-inactive-users-cleanup.py at runtime — never commit these
2+
ts_org_cleanup_audit.csv
3+
ts_org_cleanup.log
4+
*.csv
5+
*.log
6+
7+
# Your real credentials — never commit this (see .env.example for the template)
8+
.env
9+
10+
# Python
11+
__pycache__/
12+
*.pyc
13+
.venv/
14+
15+
# Misc
16+
.DS_Store
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
<!-- search-meta
2+
tags: [REST-API, user-management, multi-org, org-admin, Python, automation]
3+
apis: [users/{user_identifier}/update, org_identifiers, auth/token/full, orgs/search, users/search, searchdata, REST-API-v2]
4+
questions:
5+
- How do I remove inactive users from a specific ThoughtSpot org without deleting them from the cluster?
6+
- How do I automate org membership cleanup based on user inactivity?
7+
- How do I remove a user from one org while keeping them in their other orgs?
8+
- How do I determine per-org user activity in ThoughtSpot using the BI Server usage data?
9+
-->
10+
11+
# ts-org-inactive-users-cleanup.py
12+
13+
Removes users from the specified ThoughtSpot org after N days of inactivity in
14+
that org. Users stay on the cluster and in their other orgs — only
15+
membership in that one org is removed. Run once per org.
16+
17+
## Requirements
18+
19+
- Python 3.9+, `pip3 install -r requirements.txt`
20+
- `TS: BI Server` usage-stats reporting enabled on your cluster (what the
21+
inactivity check is based on).
22+
- An account with **"Can administer Org"** rights on the org you're
23+
processing, via one of:
24+
- **Trusted Auth `secret_key`** — can be org-scoped on some deployments
25+
— if you'll run this against several orgs, you may need a different
26+
key per org.
27+
- **Username + password** — simpler across several orgs, since one
28+
account's password works for every org it administers.
29+
30+
## Install
31+
32+
```bash
33+
python3 -m venv .venv
34+
source .venv/bin/activate
35+
pip3 install -r requirements.txt
36+
```
37+
38+
Every run below follows the same three steps, in this order:
39+
**list orgs → dry run → live run**. Always review the dry-run CSV before
40+
going live. Pick one of the two approaches below for supplying
41+
credentials/target org — don't mix them for the same run.
42+
43+
## Approach A: `.env` file (recommended)
44+
45+
Keeps `secret_key`/password out of your shell history and out of `ps`
46+
output, which passing them as CLI flags would not.
47+
48+
```bash
49+
cp .env.example .env
50+
```
51+
52+
Open `.env` and fill in `TS_CLUSTER_URL`, **one** of the two auth methods
53+
above, and `TS_ORG_ID` (find it with step 1 below if you don't have it
54+
yet). `.env` is gitignored — never commit your real one.
55+
56+
```bash
57+
# 1. Find your org ID (skip if you already know it)
58+
python3 ts-org-inactive-users-cleanup.py --list-orgs
59+
60+
# 2. Dry run — no changes made, writes ts_org_cleanup_audit_<org_id>.csv
61+
python3 ts-org-inactive-users-cleanup.py --dry-run
62+
63+
# 3. Live run — same command, minus --dry-run. No confirmation prompt
64+
# (deliberate, so this can run unattended on a schedule/cron).
65+
python3 ts-org-inactive-users-cleanup.py
66+
```
67+
68+
## Approach B: CLI flags (no `.env` file)
69+
70+
Every value normally read from `.env` has a matching flag instead. Useful
71+
for a one-off run, or when credentials come from another secrets store
72+
that injects them as arguments.
73+
74+
```bash
75+
# 1. Find your org ID (skip if you already know it)
76+
python3 ts-org-inactive-users-cleanup.py \
77+
--cluster-url https://your-cluster.thoughtspot.cloud \
78+
--username <admin_user> --password '<admin_password>' \
79+
--list-orgs
80+
81+
# 2. Dry run — no changes made, writes ts_org_cleanup_audit_111.csv
82+
python3 ts-org-inactive-users-cleanup.py \
83+
--cluster-url https://your-cluster.thoughtspot.cloud \
84+
--username <admin_user> --password '<admin_password>' \
85+
--org-id 111 --dry-run
86+
87+
# 3. Live run — same command, minus --dry-run.
88+
python3 ts-org-inactive-users-cleanup.py \
89+
--cluster-url https://your-cluster.thoughtspot.cloud \
90+
--username <admin_user> --password '<admin_password>' \
91+
--org-id 111
92+
```
93+
94+
(Swap `--username`/`--password` for `--secret-key '<key>' --admin-username <user>`
95+
if you're using Trusted Auth instead of a password.)
96+
97+
Mixing is allowed but resolves by precedence — see **Optional flags**
98+
below: a real exported env var or an explicit CLI flag always takes
99+
priority over `.env`.
100+
101+
## Multiple orgs?
102+
103+
Run the script again with a different `--org-id` (this overrides whatever
104+
`TS_ORG_ID` is set in `.env`) — each run is independent, and its audit
105+
CSV/log file is named after that org's ID
106+
(`ts_org_cleanup_audit_<org_id>.csv`), so runs never overwrite each other.
107+
108+
## Optional flags
109+
110+
| Flag | `.env` equivalent | What it does |
111+
|---|---|---|
112+
| `--cluster-url <url>` | `TS_CLUSTER_URL` | ThoughtSpot cluster base URL |
113+
| `--secret-key '<key>'` | `TS_SECRET_KEY` | Trusted Auth key (Method 1) |
114+
| `--admin-username <user>` | `TS_ADMIN_USERNAME` | Username to mint tokens as (used with `--secret-key`) |
115+
| `--username <user>` | `TS_USERNAME` | Admin username for password auth (Method 2) |
116+
| `--password '<password>'` | `TS_PASSWORD` | Admin password for password auth (Method 2) |
117+
| `--org-id 111` | `TS_ORG_ID` | The org to process |
118+
| `--inactivity-days 30` | — | Inactivity threshold (default: 10 days) |
119+
| `--list-orgs` | — | List every org's id + name, then exit |
120+
121+
## Notes
122+
123+
- A live run (no `--dry-run`) removes users immediately with **no
124+
confirmation prompt** — this is intentional, so the script can be
125+
scheduled (cron/etc.) without a `yes` prompt blocking unattended runs.
126+
Always dry-run first.
127+
- Never removes `tsadmin`, `system`, or `thoughtspot` accounts.
128+
- The startup banner states exactly which auth method and source (CLI
129+
flag, a real env var, or `.env` file) is active — check it if a run
130+
doesn't behave as expected. A stale `TS_SECRET_KEY` in your shell or
131+
left in `.env` from an earlier setup silently overrides CLI flags
132+
otherwise.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
requests

0 commit comments

Comments
 (0)