Skip to content

Commit 6429c43

Browse files
authored
Reflect removal of accounts in bootstrap.yaml to database (#1146)
Signed-off-by: Matthias Büchse <matthias.buechse@alasca.cloud>
1 parent 7e87e6b commit 6429c43

2 files changed

Lines changed: 13 additions & 1 deletion

File tree

compliance-monitor/monitor.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
db_get_keys, db_insert_report, db_get_recent_results2, db_patch_approval2, db_get_report,
4343
db_ensure_schema, db_get_apikeys, db_update_apikey, db_filter_apikeys, db_clear_delegates,
4444
db_find_subjects, db_insert_result2, db_get_relevant_results2, db_add_delegate, db_get_group,
45+
db_filter_accounts,
4546
)
4647

4748

@@ -225,17 +226,20 @@ def import_bootstrap(bootstrap_path, conn):
225226
if not accounts and not subjects:
226227
return
227228
with conn.cursor() as cur:
229+
accountids = []
228230
for account in accounts:
229231
roles = sum(ROLES[r] for r in account.get('roles', ()))
230232
acc_record = {'subject': account['subject'], 'roles': roles, 'group': account.get('group')}
231233
accountid = db_update_account(cur, acc_record)
234+
accountids.append(accountid)
232235
db_clear_delegates(cur, accountid)
233236
for delegate in account.get('delegates', ()):
234237
db_add_delegate(cur, accountid, delegate)
235238
keyids = set(db_update_apikey(cur, accountid, h) for h in account.get("api_keys", ()))
236239
db_filter_apikeys(cur, accountid, lambda keyid, *_: keyid in keyids)
237240
keyids = set(db_update_publickey(cur, accountid, key) for key in account.get("keys", ()))
238241
db_filter_publickeys(cur, accountid, lambda keyid, *_: keyid in keyids)
242+
db_filter_accounts(cur, lambda accountid, *_: accountid in accountids)
239243
conn.commit()
240244

241245

compliance-monitor/sql.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ def db_upgrade_schema(conn: connection, cur: cursor):
194194
# that way just in case we want to use another database at some point
195195
while True:
196196
current = db_get_schema_version(cur)
197-
if current == SCHEMA_VERSIONS[-1]:
197+
if current >= SCHEMA_VERSIONS[-1]: # bail if version is too new (but hope it's compatible)
198198
break
199199
if current is None:
200200
# this is an empty db, but it also used to be the case with v1
@@ -252,6 +252,14 @@ def db_update_account(cur: cursor, record: dict):
252252
return accountid
253253

254254

255+
def db_filter_accounts(cur: cursor, predicate: callable):
256+
cur.execute('SELECT accountid FROM account;')
257+
removeids = [row[0] for row in cur.fetchall() if not predicate(*row)]
258+
while removeids:
259+
cur.execute('DELETE FROM account WHERE accountid IN %s', (tuple(removeids[:10]), ))
260+
del removeids[:10]
261+
262+
255263
def db_clear_delegates(cur: cursor, accountid):
256264
cur.execute('''DELETE FROM delegation WHERE accountid = %s;''', (accountid, ))
257265

0 commit comments

Comments
 (0)