|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +""" |
| 3 | +Created on Tue Feb 7 13:05:08 2023 |
| 4 | +
|
| 5 | +@author: Cryptolens AB |
| 6 | +""" |
| 7 | + |
| 8 | +import datetime |
| 9 | +import argparse |
| 10 | +import time |
| 11 | +from licensing.models import * |
| 12 | +from licensing.methods import Key, Helpers, Message, Product, Customer, Data, AI |
| 13 | + |
| 14 | + |
| 15 | +def identify_inactive_keys(token, productId, window=30, block=False): |
| 16 | + |
| 17 | + token = token |
| 18 | + product_id=productId |
| 19 | + |
| 20 | + all_keys = [] |
| 21 | + |
| 22 | + time_window = 60*60*24*window # 1 month back in time |
| 23 | + time_now = int(time.time()) |
| 24 | + |
| 25 | + filtered_keys = [] |
| 26 | + |
| 27 | + not_processed_keys = [] |
| 28 | + |
| 29 | + ## only non blocked keys should be listed. |
| 30 | + |
| 31 | + iteration = 1 |
| 32 | + while True: |
| 33 | + temp = Product.get_keys(token, product_id, iteration)[0] |
| 34 | + print("Processed page {0}".format(str(iteration))) |
| 35 | + if len(temp) > 0: |
| 36 | + all_keys.extend(temp) |
| 37 | + iteration+=1 |
| 38 | + else: |
| 39 | + break |
| 40 | + |
| 41 | + for key in all_keys: |
| 42 | + |
| 43 | + req = [] |
| 44 | + try: |
| 45 | + req = AI.get_web_api_log(token, product_id, key["key"], limit=1, order_by="Id descending")[0] |
| 46 | + except: |
| 47 | + not_processed_keys.append(key["key"]) |
| 48 | + continue |
| 49 | + |
| 50 | + if len(req) > 0: |
| 51 | + req = req[0] |
| 52 | + else: |
| 53 | + print("Skipped key {0}".format(key["key"])) |
| 54 | + continue |
| 55 | + |
| 56 | + if req["time"] + time_window > time_now: |
| 57 | + filtered_keys.append(req["key"]) |
| 58 | + print("Detected key {0}".format(req["key"])) |
| 59 | + else: |
| 60 | + print("Skipped key {0}".format(req["key"])) |
| 61 | + |
| 62 | + if block: |
| 63 | + |
| 64 | + for key in filtered_keys: |
| 65 | + try: |
| 66 | + res = Key.block_key(token, product_id, key) |
| 67 | + if res[0] ==True: |
| 68 | + print("Key {0} was blocked successfully.".format(key)) |
| 69 | + else: |
| 70 | + print("Key {0} was not blocked.".format(key)) |
| 71 | + except: |
| 72 | + print("Key {0} was not blocked.".format(key)) |
| 73 | + |
| 74 | + |
| 75 | +if __name__ == "__main__": |
| 76 | + |
| 77 | + parser = argparse.ArgumentParser(description='This script will help you to identify which keys are not active and, if desired, block them.') |
| 78 | + |
| 79 | + parser.add_argument('-t', '--token', help="The access token with GetWebAPILog, GetKeys and BlockKey permissions.", required=True) |
| 80 | + parser.add_argument('-p','--product', help="The ProductId of the product", required=True) |
| 81 | + parser.add_argument('-b','--block', help="If set to true, all inactive keys will be blocked.",action="store_true") |
| 82 | + parser.add_argument("-w", '--window', help="How many days should the key have been inactive to mark it as inactive and eventually block it. Default 30 days.", default=30) |
| 83 | + |
| 84 | + args = parser.parse_args() |
| 85 | + |
| 86 | + print(identify_inactive_keys(args.token, args.product,args.window, args.block)) |
| 87 | + |
| 88 | + |
| 89 | + |
| 90 | + |
0 commit comments