reguire gnupg1 on f>=31; sense the system to use gpg1 when installed
[nodemanager.git] / bootauth.py
1 #!/usr/bin/python3
2 #
3 # Obtaining a node session key. Usually, the Boot
4 # Manager obtains it, then writes it to /etc/planetlab/session.
5 #
6 # Mark Huang <mlhuang@cs.princeton.edu>
7 # Copyright (C) 2006 The Trustees of Princeton University
8 #
9
10 import os, sys
11 import getopt
12
13 from config import Config
14 from plcapi import PLCAPI
15
16 def main():
17     # Defaults
18     config = None
19     node_id = None
20     key = None
21
22     # Help
23     def usage():
24         print("Usage: %s [OPTION]..." % sys.argv[0])
25         print("Options:")
26         print("     -f, --config=FILE       PLC configuration file (default: /etc/planetlab/plc_config)")
27         print("     -n, --node-id=FILE      Node ID (or file)")
28         print("     -k, --key=FILE          Node key (or file)")
29         print("     --help                  This message")
30         sys.exit(1)
31
32     # Get options
33     try:
34         (opts, argv) = getopt.getopt(sys.argv[1:], "f:n:k:h",
35                                      ["config=", "cfg=", "file=",
36                                       "node=", "nodeid=", "node-id", "node_id",
37                                       "key=",
38                                       "help"])
39     except getopt.GetoptError as err:
40         print("Error: " + err.msg)
41         usage()
42
43     for (opt, optval) in opts:
44         if opt == "-f" or opt == "--config" or opt == "--cfg" or opt == "--file":
45             config = Config(optval)
46         elif opt == "-n" or opt == "--node" or opt == "--nodeid" or opt == "--node-id" or opt == "--node_id":
47             if os.path.exists(optval):
48                 with open(optval) as optfile:
49                     node_id = optfile.read().strip()
50             else:
51                 node_id = int(optval)
52         elif opt == "-k" or opt == "--key":
53             if os.path.exists(optval):
54                 with open(optval) as optfile:
55                     key = optfile.read().strip()
56             else:
57                 key = optval
58         else:
59             usage()
60
61     if config is None:
62         config = Config()
63
64     if node_id is None or \
65        key is None:
66         usage()
67
68     # Authenticate as the Boot Manager would and get a session key
69     plc = PLCAPI(config.plc_api_uri, config.cacert, (node_id, key))
70     session = plc.BootGetNodeDetails()['session']
71
72     plc = PLCAPI(config.plc_api_uri, config.cacert, session)
73     assert session == plc.GetSession()
74
75     print(session)
76
77 if __name__ == '__main__':
78     main()