Debian Codegraph @

lib/jnpr/junos/cfg/user_ssh_key.py

raw
1 # 3rd-party modules
2 from lxml.builder import E
3
4 # local module
5 from jnpr.junos.cfg import Resource
6 from jnpr.junos import jxml as JXML
7
8
9 class UserSSHKey(Resource):
10 """
11 [edit system login user <name> authentication <key-type> <key-value> ]
12
13 Resource name: tuple(<key-type>, <key-value>)
14 <key-type> : ['ssh-dsa', 'ssh-rsa']
15 <key-value> : SSH public key string (usually something very long)
16
17 Resource manager utilities:
18 load_key - allows you to load an ssh-key from a file or str
19 """
20
21 # there are no properties, since the name <key-value> constitutes the
22 # actual ssk key data, yo!
23
24 PROPERTIES = []
25
26 # -----------------------------------------------------------------------
27 # XML readers
28 # -----------------------------------------------------------------------
29
30 def _xml_at_top(self):
31 key_t, key_v = self._name
32 return E.system(
33 E.login(
34 E.user(E.name(self.P.name), E.authentication(E(key_t, E.name(key_v))))
35 )
36 )
37
38 def _xml_at_res(self, xml):
39 return xml.find(".//authentication/%s" % self._name[0])
40
41 def _xml_to_py(self, has_xml, has_py):
42 Resource._r_has_xml_status(has_xml, has_py)
43
44 # -----------------------------------------------------------------------
45 # UTILITY FUNCTIONS
46 # -----------------------------------------------------------------------
47
48 def load_key(self, path=None, key_value=None):
49 """
50 Adds a new ssh-key to the user authentication. You can
51 provide either the path to the ssh-key file, or the contents
52 of they key (useful for loading the same key on many devices)
53
54 :path: (optional)
55 path to public ssh-key file on the local server,
56
57 :key_value: (optional)
58 the contents of the ssh public key
59 """
60
61 if not self.is_mgr:
62 raise RuntimeError("must be a resource-manager!")
63
64 if path is None and key_value is None:
65 raise RuntimeError("You must provide either path or key_value")
66
67 if path is not None:
68 # snarf the file into key_value, yo!
69 with open(path, "r") as f:
70 key_value = f.read().strip()
71
72 # extract some data from the key value, this will either
73 # be 'ssh-rsa' or 'ssh-dss'. we need to decode this to set
74 # the type correctly in the RPC.
75
76 vt = key_value[0:7]
77 key_map = {"ssh-rsa": "ssh-rsa", "ssh-dss": "ssh-dsa"}
78 key_type = key_map.get(vt)
79 if key_type is None:
80 raise RuntimeError("Unknown ssh public key file type: %s" % vt)
81
82 # at this point we are going to add a new key, so really what we are
83 # doing is accessing a new instance of this class and
84 # doing a write, but just a touch since there are no properties, yo!
85
86 new_key = self[(key_type, key_value)]
87 return new_key.write(touch=True)
88
89 # -----------------------------------------------------------------------
90 # Manager List, Catalog
91 # -----------------------------------------------------------------------
92
93 def _r_list(self):
94 # the key list comes from the parent object.
95 self._rlist = self.P["$sshkeys"]
96
97 def _r_catalog(self):
98 # no catalog but the keys
99 self._rcatalog = dict((k, None) for k in self.list)