Debian Codegraph @

lib/jnpr/junos/cfg/user.py

raw
1 # 3rd-party modules
2 from lxml.builder import E
3
4 # module packages
5 from jnpr.junos.cfg import Resource
6 from jnpr.junos import jxml as JXML
7 from jnpr.junos.cfg.user_ssh_key import UserSSHKey
8
9
10 class User(Resource):
11 """
12 [edit system login user <name>]
13
14 Resource name: str
15 <name> is the user login name
16
17 Manages resources:
18 sshkey, UserSSHKey
19 """
20
21 PROPERTIES = [
22 "uid",
23 "fullname", # the full-name field
24 "userclass", # user class
25 "password", # write-only clear-text password, will get crypt'd
26 "$password", # read-only crypt'd password
27 "$sshkeys", # read-only names of ssh-keys
28 ]
29
30 MANAGES = {"sshkey": UserSSHKey}
31
32 # -----------------------------------------------------------------------
33 # XML readers
34 # -----------------------------------------------------------------------
35
36 def _xml_at_top(self):
37 return E.system(E.login(E.user(E.name(self._name))))
38
39 def _xml_at_res(self, xml):
40 return xml.find(".//user")
41
42 def _xml_to_py(self, has_xml, has_py):
43 Resource._r_has_xml_status(has_xml, has_py)
44
45 has_py["userclass"] = has_xml.findtext("class")
46
47 Resource.copyifexists(has_xml, "full-name", has_py, "fullname")
48
49 Resource.copyifexists(has_xml, "uid", has_py)
50 if "uid" in has_py:
51 has_py["uid"] = int(has_py["uid"])
52
53 auth = has_xml.find("authentication")
54 if auth is not None:
55 # plain-text password
56 Resource.copyifexists(auth, "encrypted-password", has_py, "$password")
57
58 # ssh-keys
59 sshkeys = auth.xpath("ssh-rsa | ssh-dsa")
60 if sshkeys is not None:
61 has_py["$sshkeys"] = [
62 (sshkey.tag, sshkey.findtext("name").strip()) for sshkey in sshkeys
63 ]
64
65 # -----------------------------------------------------------------------
66 # XML property writers
67 # -----------------------------------------------------------------------
68
69 def _xml_change_fullname(self, xml):
70 xml.append(E("full-name", self["fullname"]))
71 return True
72
73 def _xml_change_userclass(self, xml):
74 xml.append(E("class", self["userclass"]))
75 return True
76
77 def _xml_change_password(self, xml):
78 xml.append(E.authentication(E("plain-text-password-value", self["password"])))
79 return True
80
81 def _xml_change_uid(self, xml):
82 xml.append(E.uid(str(self["uid"])))
83 return True
84
85 # -----------------------------------------------------------------------
86 # Manager List, Catalog
87 # -----------------------------------------------------------------------
88
89 def _r_list(self):
90 get = E.system(E.login(E.user(JXML.NAMES_ONLY)))
91 got = self.R.get_config(get)
92 self._rlist = [name.text for name in got.xpath(".//user/name")]