|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The ASF licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | + |
| 18 | +"""Periodic cleanup of Personal Access Tokens for banned or deleted accounts.""" |
| 19 | + |
| 20 | +import asyncio |
| 21 | +from typing import Final |
| 22 | + |
| 23 | +import sqlalchemy |
| 24 | +import sqlmodel |
| 25 | + |
| 26 | +import atr.db as db |
| 27 | +import atr.ldap as ldap |
| 28 | +import atr.log as log |
| 29 | +import atr.models.sql as sql |
| 30 | +import atr.storage as storage |
| 31 | + |
| 32 | +# ~1 hour, deliberately offset from the admin poll interval (3631s) |
| 33 | +# to avoid simultaneous LDAP request spikes |
| 34 | +POLL_INTERVAL_SECONDS: Final[int] = 3617 |
| 35 | + |
| 36 | + |
| 37 | +async def cleanup_loop() -> None: |
| 38 | + """Periodically revoke PATs belonging to banned or deleted LDAP accounts.""" |
| 39 | + while True: |
| 40 | + await asyncio.sleep(POLL_INTERVAL_SECONDS) |
| 41 | + try: |
| 42 | + await revoke_pats_for_banned_users() |
| 43 | + except Exception as e: |
| 44 | + log.warning(f"PAT banned-user cleanup failed: {e}") |
| 45 | + |
| 46 | + |
| 47 | +async def revoke_pats_for_banned_users() -> int: |
| 48 | + """Check all PAT-holding users against LDAP and revoke tokens for banned/deleted accounts. |
| 49 | +
|
| 50 | + Returns the total number of tokens revoked. |
| 51 | + """ |
| 52 | + # Step 1: Get distinct UIDs that have PATs |
| 53 | + async with db.session() as data: |
| 54 | + stmt = sqlmodel.select(sql.PersonalAccessToken.asfuid).distinct() |
| 55 | + rows = await data.execute_query(stmt) |
| 56 | + uids_with_pats = [row[0] for row in rows] |
| 57 | + |
| 58 | + if not uids_with_pats: |
| 59 | + return 0 |
| 60 | + |
| 61 | + # Step 2: Check each against LDAP, revoke if banned/deleted |
| 62 | + revoked_total = 0 |
| 63 | + for uid in uids_with_pats: |
| 64 | + try: |
| 65 | + account = await ldap.account_lookup(uid) |
| 66 | + if (account is not None) and (not ldap.is_banned(account)): |
| 67 | + continue |
| 68 | + |
| 69 | + # Account is gone or banned — delete all their tokens |
| 70 | + async with db.session() as data: |
| 71 | + delete_stmt = sqlalchemy.delete(sql.PersonalAccessToken).where(sql.PersonalAccessToken.asfuid == uid) |
| 72 | + result = await data.execute_query(delete_stmt) |
| 73 | + await data.commit() |
| 74 | + count: int = getattr(result, "rowcount", 0) |
| 75 | + |
| 76 | + if count > 0: |
| 77 | + storage.audit( |
| 78 | + target_asf_uid=uid, |
| 79 | + tokens_revoked=count, |
| 80 | + reason="account_banned_or_deleted", |
| 81 | + source="pat_cleanup_loop", |
| 82 | + ) |
| 83 | + log.info(f"Auto-revoked {count} PAT(s) for banned/deleted user {uid}") |
| 84 | + revoked_total += count |
| 85 | + |
| 86 | + except Exception as e: |
| 87 | + log.warning(f"PAT cleanup: failed to check/revoke for {uid}: {e}") |
| 88 | + |
| 89 | + if revoked_total > 0: |
| 90 | + log.info(f"PAT cleanup cycle complete: revoked {revoked_total} total token(s)") |
| 91 | + |
| 92 | + return revoked_total |
0 commit comments