Advertisement
osmarks

keyctl.py

Dec 15th, 2019
536
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.17 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. import requests
  3. import argparse
  4. import sys
  5.  
  6. parser = argparse.ArgumentParser(description="Manage keys in SPUDNET")
  7. parser.add_argument("--spudnet_url", "-s", required=True, help="URL of the SPUDNET instance to access")
  8. parser.add_argument("--key", "-K", required=True)
  9. subparsers = parser.add_subparsers(required=True)
  10. subparsers.dest = "command"
  11. subparsers.add_parser("info", help="Get information about this key.")
  12. subparsers.add_parser("dependent_keys", help="List keys issued by this key.")
  13. subparsers.add_parser("disable_key", help="Disable this key.")
  14. issue_key = subparsers.add_parser("issue_key", help="Issue a new key.")
  15. issue_key.add_argument("--channel", "-c", action="append", help="Allow new key access to this channel. Can be repeated. If none specified, defaults to issuing key's channels.")
  16. issue_key.add_argument("--bearer", "-b", help="Specifies bearer of new key")
  17. issue_key.add_argument("--use", "-u", help="Specifies intended use of new key")
  18. issue_key.add_argument("--permission_level", "-p", help="Specifies permission level of new key. Not currently used. Must be <= issuing key's permission level", type=int)
  19.  
  20. args = parser.parse_args()
  21.  
  22. def query(subpath, data):
  23.     for key, value in list(data.items()):
  24.         if value == None: del data[key]
  25.     result = requests.post(args.spudnet_url + "/hki/" + subpath, json=data)
  26.     if not result.ok:
  27.         print(result.text)
  28.         sys.exit(1)
  29.     else:
  30.         return result.json()
  31.  
  32. if args.command == "info":
  33.     print(query("key-info", { "key": args.key }))
  34. elif args.command == "dependent_keys":
  35.     print(query("dependent-keys", { "key": args.key }))
  36. elif args.command == "issue_key":
  37.     print(query("issue-key", {
  38.         "key": args.key,
  39.         "bearer": args.bearer,
  40.         "use": args.use,
  41.         "permission_level": args.permission_level,
  42.         "allowed_channels": args.channel
  43.     }))
  44. elif args.command == "disable_key":
  45.     yn = input("Are you sure? All keys issued by this key will also be disabled. This action is irreversible. (y/n) ")
  46.     if yn == "y":
  47.         print(query("disable-key", { "key": args.key }))
  48.     else:
  49.         print("Action cancelled.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement