Skip to content

Commit 700182c

Browse files
committed
STOMP bots: add support for authentication by username + password
Each of the *STOMP collector* and *STOMP output* bots obtained the following new configuration parameters: * `auth_by_ssl_client_certificate` (a Boolean flag; it is `True` by default -- to keep backward compatibility); * `username` and `password` -- to be used as STOMP authentication credentials (login and passcode), but *only* if the aforementioned parameter `auth_by_ssl_client_certificate` is `False`. If `auth_by_ssl_client_certificate` is `False`, then the (supported also previously...) `ssl_client_certificate` and `ssl_client_certificate_key` parameters are ignored (i.e., not only left unused, but also there are *no checks* whether the files they refer to actually exist).
1 parent e7c1235 commit 700182c

File tree

3 files changed

+43
-10
lines changed

3 files changed

+43
-10
lines changed

intelmq/bots/collectors/stomp/collector.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ class StompCollectorBot(CollectorBot, StompMixin):
7777
exchange: str = ''
7878
port: int = 61614
7979
server: str = "n6stream.cert.pl"
80+
auth_by_ssl_client_certificate: bool = True
81+
username: str = 'guest' # ignored if `auth_by_ssl_client_certificate` is true
82+
password: str = 'guest' # ignored if `auth_by_ssl_client_certificate` is true
8083
ssl_ca_certificate: str = 'ca.pem' # TODO pathlib.Path
8184
ssl_client_certificate: str = 'client.pem' # TODO pathlib.Path
8285
ssl_client_certificate_key: str = 'client.key' # TODO pathlib.Path

intelmq/bots/outputs/stomp/output.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ class StompOutputBot(OutputBot, StompMixin):
2626
port: int = 61614
2727
server: str = "127.0.0.1" # TODO: could be ip address
2828
single_key: bool = False
29+
auth_by_ssl_client_certificate: bool = True
30+
username: str = 'guest' # ignored if `auth_by_ssl_client_certificate` is true
31+
password: str = 'guest' # ignored if `auth_by_ssl_client_certificate` is true
2932
ssl_ca_certificate: str = 'ca.pem' # TODO: could be pathlib.Path
3033
ssl_client_certificate: str = 'client.pem' # TODO: pathlib.Path
3134
ssl_client_certificate_key: str = 'client.key' # TODO: patlib.Path

intelmq/lib/mixins/stomp.py

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ class StompMixin:
3131
port: int
3232
heartbeat: int
3333

34+
auth_by_ssl_client_certificate: bool
35+
36+
username: str # to be ignored if `auth_by_ssl_client_certificate` is true
37+
password: str # to be ignored if `auth_by_ssl_client_certificate` is true
38+
3439
ssl_ca_certificate: str # TODO: could be pathlib.Path
3540
ssl_client_certificate: str # TODO: could be pathlib.Path
3641
ssl_client_certificate_key: str # TODO: could be patlib.Path
@@ -102,13 +107,29 @@ def __verify_dependency(cls) -> None:
102107
def __verify_parameters(cls,
103108
get_param: Callable[[str], Any],
104109
on_error: Callable[[str], None]) -> None:
105-
for param_name in [
106-
'ssl_ca_certificate',
107-
'ssl_client_certificate',
108-
'ssl_client_certificate_key',
109-
]:
110+
file_param_names = ['ssl_ca_certificate']
111+
if cls.__should_cert_auth_params_be_verified(get_param, on_error):
112+
file_param_names.extend([
113+
'ssl_client_certificate',
114+
'ssl_client_certificate_key',
115+
])
116+
for param_name in file_param_names:
110117
cls.__verify_file_param(param_name, get_param, on_error)
111118

119+
@classmethod
120+
def __should_cert_auth_params_be_verified(cls,
121+
get_param: Callable[[str], Any],
122+
on_error: Callable[[str], None]) -> bool:
123+
flag = get_param('auth_by_ssl_client_certificate')
124+
if not isinstance(flag, bool):
125+
# Let us better be strict here -- explicitly rejecting any
126+
# non-`bool` values as potentially misleading (e.g., consider
127+
# a string like "false", which would be interpreted as True).
128+
on_error(f"Parameter 'auth_by_ssl_client_certificate' "
129+
f"is not set to a bool value (got: {flag!r}).")
130+
flag = False
131+
return flag
132+
112133
@classmethod
113134
def __verify_file_param(cls,
114135
param_name: str,
@@ -136,10 +157,16 @@ def __get_ssl_and_connect_kwargs(self) -> Tuple[dict, dict]:
136157
# Note: the `ca_certs` argument to `set_ssl()` must always be
137158
# provided, otherwise the `stomp.py`'s machinery would *not*
138159
# perform any certificate verification!
139-
ssl_kwargs = dict(
140-
ca_certs=self.ssl_ca_certificate,
141-
cert_file=self.ssl_client_certificate,
142-
key_file=self.ssl_client_certificate_key,
143-
)
160+
ssl_kwargs = dict(ca_certs=self.ssl_ca_certificate)
144161
connect_kwargs = dict(wait=True)
162+
if self.auth_by_ssl_client_certificate:
163+
ssl_kwargs.update(
164+
cert_file=self.ssl_client_certificate,
165+
key_file=self.ssl_client_certificate_key,
166+
)
167+
else:
168+
connect_kwargs.update(
169+
username=self.username,
170+
passcode=self.password,
171+
)
145172
return ssl_kwargs, connect_kwargs

0 commit comments

Comments
 (0)