comparison sat_frontends/jp/cmd_encryption.py @ 3040:fee60f17ebac

jp: jp asyncio port: /!\ this commit is huge. Jp is temporarily not working with `dbus` bridge /!\ This patch implements the port of jp to asyncio, so it is now correctly using the bridge asynchronously, and it can be used with bridges like `pb`. This also simplify the code, notably for things which were previously implemented with many callbacks (like pagination with RSM). During the process, some behaviours have been modified/fixed, in jp and backends, check diff for details.
author Goffi <goffi@goffi.org>
date Wed, 25 Sep 2019 08:56:41 +0200
parents ab2696e34d29
children 7574f795bd1e
comparison
equal deleted inserted replaced
3039:a1bc34f90fa5 3040:fee60f17ebac
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. 18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 19
20 from sat_frontends.jp import base 20 from sat_frontends.jp import base
21 from sat_frontends.jp.constants import Const as C 21 from sat_frontends.jp.constants import Const as C
22 from sat.core.i18n import _ 22 from sat.core.i18n import _
23 from functools import partial
24 from sat.tools.common import data_format 23 from sat.tools.common import data_format
25 from sat_frontends.jp import xmlui_manager 24 from sat_frontends.jp import xmlui_manager
26 25
27 __commands__ = ["Encryption"] 26 __commands__ = ["Encryption"]
28 27
35 host, "algorithms", 34 host, "algorithms",
36 use_output=C.OUTPUT_LIST_DICT, 35 use_output=C.OUTPUT_LIST_DICT,
37 extra_outputs=extra_outputs, 36 extra_outputs=extra_outputs,
38 use_profile=False, 37 use_profile=False,
39 help=_("show available encryption algorithms")) 38 help=_("show available encryption algorithms"))
40 self.need_loop = True
41 39
42 def add_parser_options(self): 40 def add_parser_options(self):
43 pass 41 pass
44
45 def encryptionPluginsGetCb(self, plugins):
46 self.output(plugins)
47 self.host.quit()
48 42
49 def default_output(self, plugins): 43 def default_output(self, plugins):
50 if not plugins: 44 if not plugins:
51 self.disp(_("No encryption plugin registered!")) 45 self.disp(_("No encryption plugin registered!"))
52 self.host.quit(C.EXIT_NOT_FOUND)
53 else: 46 else:
54 self.disp(_("Following encryption algorithms are available: {algos}").format( 47 self.disp(_("Following encryption algorithms are available: {algos}").format(
55 algos=', '.join([p['name'] for p in plugins]))) 48 algos=', '.join([p['name'] for p in plugins])))
49
50 async def start(self):
51 try:
52 plugins = await self.host.bridge.encryptionPluginsGet()
53 except Exception as e:
54 self.disp(f"can't retrieve plugins: {e}", error=True)
55 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
56 else:
57 await self.output(plugins)
56 self.host.quit() 58 self.host.quit()
57
58 def start(self):
59 self.host.bridge.encryptionPluginsGet(
60 callback=self.encryptionPluginsGetCb,
61 errback=partial(
62 self.errback,
63 msg=_("can't retrieve plugins: {}"),
64 exit_code=C.EXIT_BRIDGE_ERRBACK,
65 ),
66 )
67 59
68 60
69 class EncryptionGet(base.CommandBase): 61 class EncryptionGet(base.CommandBase):
70 62
71 def __init__(self, host): 63 def __init__(self, host):
72 super(EncryptionGet, self).__init__( 64 super(EncryptionGet, self).__init__(
73 host, "get", 65 host, "get",
74 use_output=C.OUTPUT_DICT, 66 use_output=C.OUTPUT_DICT,
75 help=_("get encryption session data")) 67 help=_("get encryption session data"))
76 self.need_loop = True
77 68
78 def add_parser_options(self): 69 def add_parser_options(self):
79 self.parser.add_argument( 70 self.parser.add_argument(
80 "jid", 71 "jid",
81 help=_("jid of the entity to check") 72 help=_("jid of the entity to check")
82 ) 73 )
83 74
84 def messageEncryptionGetCb(self, serialised): 75 async def start(self):
76 jids = await self.host.check_jids([self.args.jid])
77 jid = jids[0]
78 try:
79 serialised = await self.host.bridge.messageEncryptionGet(jid, self.profile)
80 except Exception as e:
81 self.disp(f"can't get session: {e}", error=True)
82 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
83
85 session_data = data_format.deserialise(serialised) 84 session_data = data_format.deserialise(serialised)
86 if session_data is None: 85 if session_data is None:
87 self.disp( 86 self.disp(
88 "No encryption session found, the messages are sent in plain text.") 87 "No encryption session found, the messages are sent in plain text.")
89 self.host.quit(C.EXIT_NOT_FOUND) 88 self.host.quit(C.EXIT_NOT_FOUND)
90 self.output(session_data) 89 await self.output(session_data)
91 self.host.quit() 90 self.host.quit()
92
93 def start(self):
94 jids = self.host.check_jids([self.args.jid])
95 jid = jids[0]
96 self.host.bridge.messageEncryptionGet(
97 jid, self.profile,
98 callback=self.messageEncryptionGetCb,
99 errback=partial(
100 self.errback,
101 msg=_("can't get session: {}"),
102 exit_code=C.EXIT_BRIDGE_ERRBACK,
103 ),
104 )
105 91
106 92
107 class EncryptionStart(base.CommandBase): 93 class EncryptionStart(base.CommandBase):
108 94
109 def __init__(self, host): 95 def __init__(self, host):
110 super(EncryptionStart, self).__init__( 96 super(EncryptionStart, self).__init__(
111 host, "start", 97 host, "start",
112 help=_("start encrypted session with an entity")) 98 help=_("start encrypted session with an entity"))
113 self.need_loop = True
114 99
115 def add_parser_options(self): 100 def add_parser_options(self):
116 self.parser.add_argument( 101 self.parser.add_argument(
117 "--encrypt-noreplace", 102 "--encrypt-noreplace",
118 action="store_true", 103 action="store_true",
126 self.parser.add_argument( 111 self.parser.add_argument(
127 "jid", 112 "jid",
128 help=_("jid of the entity to stop encrypted session with") 113 help=_("jid of the entity to stop encrypted session with")
129 ) 114 )
130 115
131 def encryptionNamespaceGetCb(self, namespace): 116 async def start(self):
132 jids = self.host.check_jids([self.args.jid])
133 jid = jids[0]
134 self.host.bridge.messageEncryptionStart(
135 jid, namespace, not self.args.encrypt_noreplace,
136 self.profile,
137 callback=self.host.quit,
138 errback=partial(self.errback,
139 msg=_("Can't start encryption session: {}"),
140 exit_code=C.EXIT_BRIDGE_ERRBACK,
141 ))
142
143 def start(self):
144 if self.args.name is not None: 117 if self.args.name is not None:
145 self.host.bridge.encryptionNamespaceGet(self.args.name, 118 try:
146 callback=self.encryptionNamespaceGetCb, 119 namespace = await self.host.bridge.encryptionNamespaceGet(self.args.name)
147 errback=partial(self.errback, 120 except Exception as e:
148 msg=_("Can't get encryption namespace: {}"), 121 self.disp(f"can't get encryption namespace: {e}", error=True)
149 exit_code=C.EXIT_BRIDGE_ERRBACK, 122 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
150 ))
151 elif self.args.namespace is not None: 123 elif self.args.namespace is not None:
152 self.encryptionNamespaceGetCb(self.args.namespace) 124 namespace = self.args.namespace
153 else: 125 else:
154 self.encryptionNamespaceGetCb("") 126 namespace = ""
127
128 jids = await self.host.check_jids([self.args.jid])
129 jid = jids[0]
130
131 try:
132 await self.host.bridge.messageEncryptionStart(
133 jid, namespace, not self.args.encrypt_noreplace,
134 self.profile)
135 except Exception as e:
136 self.disp(f"can't get encryption namespace: {e}", error=True)
137 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
138
139 self.host.quit()
155 140
156 141
157 class EncryptionStop(base.CommandBase): 142 class EncryptionStop(base.CommandBase):
158 143
159 def __init__(self, host): 144 def __init__(self, host):
160 super(EncryptionStop, self).__init__( 145 super(EncryptionStop, self).__init__(
161 host, "stop", 146 host, "stop",
162 help=_("stop encrypted session with an entity")) 147 help=_("stop encrypted session with an entity"))
163 self.need_loop = True
164 148
165 def add_parser_options(self): 149 def add_parser_options(self):
166 self.parser.add_argument( 150 self.parser.add_argument(
167 "jid", 151 "jid",
168 help=_("jid of the entity to stop encrypted session with") 152 help=_("jid of the entity to stop encrypted session with")
169 ) 153 )
170 154
171 def start(self): 155 async def start(self):
172 jids = self.host.check_jids([self.args.jid]) 156 jids = await self.host.check_jids([self.args.jid])
173 jid = jids[0] 157 jid = jids[0]
174 self.host.bridge.messageEncryptionStop( 158 try:
175 jid, self.profile, 159 await self.host.bridge.messageEncryptionStop(jid, self.profile)
176 callback=self.host.quit, 160 except Exception as e:
177 errback=partial( 161 self.disp(f"can't end encrypted session: {e}", error=True)
178 self.errback, 162 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
179 msg=_("can't end encrypted session: {}"), 163
180 exit_code=C.EXIT_BRIDGE_ERRBACK, 164 self.host.quit()
181 ),
182 )
183 165
184 166
185 class TrustUI(base.CommandBase): 167 class TrustUI(base.CommandBase):
186 168
187 def __init__(self, host): 169 def __init__(self, host):
188 super(TrustUI, self).__init__( 170 super(TrustUI, self).__init__(
189 host, "ui", 171 host, "ui",
190 help=_("get UI to manage trust")) 172 help=_("get UI to manage trust"))
191 self.need_loop = True
192 173
193 def add_parser_options(self): 174 def add_parser_options(self):
194 self.parser.add_argument( 175 self.parser.add_argument(
195 "jid", 176 "jid",
196 help=_("jid of the entity to stop encrypted session with") 177 help=_("jid of the entity to stop encrypted session with")
200 "-n", "--name", help=_("algorithm name (DEFAULT: current algorithm)")) 181 "-n", "--name", help=_("algorithm name (DEFAULT: current algorithm)"))
201 algorithm.add_argument( 182 algorithm.add_argument(
202 "-N", "--namespace", 183 "-N", "--namespace",
203 help=_("algorithm namespace (DEFAULT: current algorithm)")) 184 help=_("algorithm namespace (DEFAULT: current algorithm)"))
204 185
205 def encryptionTrustUIGetCb(self, xmlui_raw): 186 async def start(self):
187 if self.args.name is not None:
188 try:
189 namespace = await self.host.bridge.encryptionNamespaceGet(self.args.name)
190 except Exception as e:
191 self.disp(f"can't get encryption namespace: {e}", error=True)
192 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
193 elif self.args.namespace is not None:
194 namespace = self.args.namespace
195 else:
196 namespace = ""
197
198 jids = await self.host.check_jids([self.args.jid])
199 jid = jids[0]
200
201 try:
202 xmlui_raw = await self.host.bridge.encryptionTrustUIGet(
203 jid, namespace, self.profile)
204 except Exception as e:
205 self.disp(f"can't get encryption session trust UI: {e}", error=True)
206 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
207
206 xmlui = xmlui_manager.create(self.host, xmlui_raw) 208 xmlui = xmlui_manager.create(self.host, xmlui_raw)
207 xmlui.show() 209 await xmlui.show()
208 xmlui.submitForm() 210 if xmlui.type != C.XMLUI_DIALOG:
209 211 await xmlui.submitForm()
210 def encryptionNamespaceGetCb(self, namespace): 212 self.host.quit()
211 jids = self.host.check_jids([self.args.jid])
212 jid = jids[0]
213 self.host.bridge.encryptionTrustUIGet(
214 jid, namespace, self.profile,
215 callback=self.encryptionTrustUIGetCb,
216 errback=partial(
217 self.errback,
218 msg=_("can't end encrypted session: {}"),
219 exit_code=C.EXIT_BRIDGE_ERRBACK,
220 ),
221 )
222
223 def start(self):
224 if self.args.name is not None:
225 self.host.bridge.encryptionNamespaceGet(self.args.name,
226 callback=self.encryptionNamespaceGetCb,
227 errback=partial(self.errback,
228 msg=_("Can't get encryption namespace: {}"),
229 exit_code=C.EXIT_BRIDGE_ERRBACK,
230 ))
231 elif self.args.namespace is not None:
232 self.encryptionNamespaceGetCb(self.args.namespace)
233 else:
234 self.encryptionNamespaceGetCb("")
235
236 213
237 class EncryptionTrust(base.CommandBase): 214 class EncryptionTrust(base.CommandBase):
238 subcommands = (TrustUI,) 215 subcommands = (TrustUI,)
239 216
240 def __init__(self, host): 217 def __init__(self, host):