perun.proxy.utils 2.2.2__tar.gz → 2.4.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (34) hide show
  1. {perun.proxy.utils-2.2.2 → perun.proxy.utils-2.4.0}/PKG-INFO +26 -3
  2. {perun.proxy.utils-2.2.2 → perun.proxy.utils-2.4.0}/README.md +21 -0
  3. {perun.proxy.utils-2.2.2 → perun.proxy.utils-2.4.0}/perun/proxy/utils/nagios/check_ldap_syncrepl.py +1 -1
  4. {perun.proxy.utils-2.2.2 → perun.proxy.utils-2.4.0}/perun/proxy/utils/nagios/check_mongodb.py +1 -1
  5. {perun.proxy.utils-2.2.2 → perun.proxy.utils-2.4.0}/perun/proxy/utils/nagios/check_nginx.py +1 -1
  6. {perun.proxy.utils-2.2.2 → perun.proxy.utils-2.4.0}/perun/proxy/utils/run_probes.py +34 -2
  7. perun.proxy.utils-2.4.0/perun/proxy/utils/sync_usable_token_types.py +137 -0
  8. {perun.proxy.utils-2.2.2 → perun.proxy.utils-2.4.0}/perun.proxy.utils.egg-info/PKG-INFO +26 -3
  9. {perun.proxy.utils-2.2.2 → perun.proxy.utils-2.4.0}/perun.proxy.utils.egg-info/SOURCES.txt +1 -0
  10. {perun.proxy.utils-2.2.2 → perun.proxy.utils-2.4.0}/perun.proxy.utils.egg-info/entry_points.txt +1 -0
  11. {perun.proxy.utils-2.2.2 → perun.proxy.utils-2.4.0}/perun.proxy.utils.egg-info/requires.txt +2 -0
  12. {perun.proxy.utils-2.2.2 → perun.proxy.utils-2.4.0}/setup.cfg +1 -1
  13. {perun.proxy.utils-2.2.2 → perun.proxy.utils-2.4.0}/setup.py +5 -2
  14. {perun.proxy.utils-2.2.2 → perun.proxy.utils-2.4.0}/LICENSE +0 -0
  15. {perun.proxy.utils-2.2.2 → perun.proxy.utils-2.4.0}/perun/proxy/utils/__init__.py +0 -0
  16. {perun.proxy.utils-2.2.2 → perun.proxy.utils-2.4.0}/perun/proxy/utils/metadata_expiration.py +0 -0
  17. {perun.proxy.utils-2.2.2 → perun.proxy.utils-2.4.0}/perun/proxy/utils/nagios/__init__.py +0 -0
  18. {perun.proxy.utils-2.2.2 → perun.proxy.utils-2.4.0}/perun/proxy/utils/nagios/check_custom_command.py +0 -0
  19. {perun.proxy.utils-2.2.2 → perun.proxy.utils-2.4.0}/perun/proxy/utils/nagios/check_dockers.py +0 -0
  20. {perun.proxy.utils-2.2.2 → perun.proxy.utils-2.4.0}/perun/proxy/utils/nagios/check_exabgp_propagation.py +0 -0
  21. {perun.proxy.utils-2.2.2 → perun.proxy.utils-2.4.0}/perun/proxy/utils/nagios/check_ldap.py +0 -0
  22. {perun.proxy.utils-2.2.2 → perun.proxy.utils-2.4.0}/perun/proxy/utils/nagios/check_pgsql.py +0 -0
  23. {perun.proxy.utils-2.2.2 → perun.proxy.utils-2.4.0}/perun/proxy/utils/nagios/check_php_syntax.py +0 -0
  24. {perun.proxy.utils-2.2.2 → perun.proxy.utils-2.4.0}/perun/proxy/utils/nagios/check_privacyidea.py +0 -0
  25. {perun.proxy.utils-2.2.2 → perun.proxy.utils-2.4.0}/perun/proxy/utils/nagios/check_rpc_status.py +0 -0
  26. {perun.proxy.utils-2.2.2 → perun.proxy.utils-2.4.0}/perun/proxy/utils/nagios/check_saml.py +0 -0
  27. {perun.proxy.utils-2.2.2 → perun.proxy.utils-2.4.0}/perun/proxy/utils/nagios/check_user_logins.py +0 -0
  28. {perun.proxy.utils-2.2.2 → perun.proxy.utils-2.4.0}/perun/proxy/utils/nagios/webserver_availability.py +0 -0
  29. {perun.proxy.utils-2.2.2 → perun.proxy.utils-2.4.0}/perun/proxy/utils/print_docker_versions.py +0 -0
  30. {perun.proxy.utils-2.2.2 → perun.proxy.utils-2.4.0}/perun/proxy/utils/run_version_script.py +0 -0
  31. {perun.proxy.utils-2.2.2 → perun.proxy.utils-2.4.0}/perun/proxy/utils/separate_oidc_logs.py +0 -0
  32. {perun.proxy.utils-2.2.2 → perun.proxy.utils-2.4.0}/perun/proxy/utils/separate_ssp_logs.py +0 -0
  33. {perun.proxy.utils-2.2.2 → perun.proxy.utils-2.4.0}/perun.proxy.utils.egg-info/dependency_links.txt +0 -0
  34. {perun.proxy.utils-2.2.2 → perun.proxy.utils-2.4.0}/perun.proxy.utils.egg-info/top_level.txt +0 -0
@@ -1,8 +1,8 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: perun.proxy.utils
3
- Version: 2.2.2
4
- Summary: Module with utilities and monitoring probes
5
- Home-page: https://gitlab.ics.muni.cz/perun-proxy-aai/proxyidp-scripts.git
3
+ Version: 2.4.0
4
+ Summary: Utilities and monitoring probes for Perun ProxyIdP
5
+ Home-page: https://gitlab.ics.muni.cz/perun/perun-proxyidp/perun-proxy-utils.git
6
6
  Requires-Python: >=3.9
7
7
  Description-Content-Type: text/markdown
8
8
  License-File: LICENSE
@@ -15,6 +15,8 @@ Requires-Dist: requests~=2.31
15
15
  Requires-Dist: PyYAML<7.0,>=5.4
16
16
  Requires-Dist: check_nginx_status~=1.0
17
17
  Requires-Dist: pyotp~=2.9
18
+ Requires-Dist: perun.connector~=3.8
19
+ Requires-Dist: privacyidea~=3.9
18
20
  Provides-Extra: ldap
19
21
  Requires-Dist: ldap3~=2.9.1; extra == "ldap"
20
22
  Requires-Dist: check_syncrepl_extended~=2020.13; extra == "ldap"
@@ -96,6 +98,27 @@ For usage instructions, run:
96
98
  run_version_script --help
97
99
  ```
98
100
 
101
+ ### sync_usable_token_types.py
102
+
103
+ Collects information about the usable token types of each privacyIDEA user and sends it
104
+ to Perun. Each user with usable tokens in privacyIDEA is assigned a list of their types, for example: `['backupcode', 'totp']`. A token is considered usable when it is **active** and it is not **locked** or
105
+ **revoked** and its rollout state allows logging in.
106
+
107
+ For more usage instructions, run:
108
+
109
+ ```sh
110
+ sync_usable_token_types --help
111
+ ```
112
+
113
+ Example:
114
+
115
+ ```sh
116
+ python3 sync_usable_token_types.py
117
+ --mfa-active-tokens-attr-name "attr_name"
118
+ --perun-user-id-regex "\d+"
119
+ --perun-connector-config-path "/path/to/file"
120
+ ```
121
+
99
122
  ## Nagios probes
100
123
 
101
124
  All nagios scripts are located under `nagios` directory.
@@ -73,6 +73,27 @@ For usage instructions, run:
73
73
  run_version_script --help
74
74
  ```
75
75
 
76
+ ### sync_usable_token_types.py
77
+
78
+ Collects information about the usable token types of each privacyIDEA user and sends it
79
+ to Perun. Each user with usable tokens in privacyIDEA is assigned a list of their types, for example: `['backupcode', 'totp']`. A token is considered usable when it is **active** and it is not **locked** or
80
+ **revoked** and its rollout state allows logging in.
81
+
82
+ For more usage instructions, run:
83
+
84
+ ```sh
85
+ sync_usable_token_types --help
86
+ ```
87
+
88
+ Example:
89
+
90
+ ```sh
91
+ python3 sync_usable_token_types.py
92
+ --mfa-active-tokens-attr-name "attr_name"
93
+ --perun-user-id-regex "\d+"
94
+ --perun-connector-config-path "/path/to/file"
95
+ ```
96
+
76
97
  ## Nagios probes
77
98
 
78
99
  All nagios scripts are located under `nagios` directory.
@@ -3,7 +3,7 @@
3
3
  from check_syncrepl_extended.check_syncrepl_extended import main
4
4
 
5
5
  # for program arguments check
6
- # https://gitlab.ics.muni.cz/perun-proxy-aai/python/check_syncrepl_extended
6
+ # https://gitlab.ics.muni.cz/perun/deployment/proxyidp/check_syncrepl_extended
7
7
 
8
8
  if __name__ == "__main__":
9
9
  main()
@@ -627,7 +627,7 @@ def mongo_connect(
627
627
  port,
628
628
  read_preference=pymongo.ReadPreference.SECONDARY,
629
629
  replicaSet=replica,
630
- **con_args
630
+ **con_args,
631
631
  )
632
632
 
633
633
  try:
@@ -4,7 +4,7 @@ import sys
4
4
  from check_nginx_status.check_nginx_status import main
5
5
 
6
6
  # for program arguments check
7
- # https://gitlab.ics.muni.cz/perun-proxy-aai/python/check_nginx_status/
7
+ # https://gitlab.ics.muni.cz/perun/deployment/proxyidp/check_nginx_status
8
8
 
9
9
  if __name__ == "__main__":
10
10
  sys.exit(main())
@@ -19,16 +19,48 @@ def open_file(filepath):
19
19
  sys.exit(2)
20
20
 
21
21
 
22
+ def get_metrics_and_new_output(output):
23
+ """
24
+ Parses metrics from output, metrics must be in one of (or combination of)
25
+ the following formats:
26
+ 1) |metric1=val;val;;;|metric2=val (delimiter |)
27
+ 2) |metric1=val;metric2=val2; (delimiter ;)
28
+ 3) |metric1=val metric2=val2 (delimiter ' ')
29
+
30
+ Values must be int or float
31
+ """
32
+ metrics_pattern = r"(\s\|\s|\|)(\w+=[\d.;]+(;|\s|$))+"
33
+
34
+ match = re.search(metrics_pattern, output)
35
+ if match:
36
+ output = re.sub(metrics_pattern, " ", output)
37
+ metrics = re.sub(r"\s", "", match.group())
38
+ metrics = re.sub(r"^\|", "", metrics)
39
+ metrics = re.sub(r"(\d;?)([a-zA-Z])", r"\1|\2", metrics)
40
+ return metrics.strip(), output.strip()
41
+ return None, output
42
+
43
+
22
44
  def run_probe(probe_name, command):
45
+ """
46
+ Runs nagios monitoring probe and prints output in following format:
47
+ return_code metrics output
48
+
49
+ metrics output format:
50
+ metric1=val;|metric2=val2|metric3=val3;val3;;;|metric4=val4
51
+ """
23
52
  result = subprocess.run(
24
53
  command, text=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT
25
54
  )
26
55
  output = re.sub("[ \t\n]+", " ", result.stdout)
27
56
  search = re.search(r" - .*", output)
28
57
  if search:
29
- print(f"{result.returncode} {probe_name}{search.group()}")
58
+ output = re.sub(r"^ - ", "", search.group())
59
+ metrics, new_output = get_metrics_and_new_output(output)
60
+ if metrics:
61
+ print(f"{result.returncode} {probe_name} {metrics} {new_output}")
30
62
  else:
31
- print(f"{result.returncode} {probe_name} - {output}")
63
+ print(f"{result.returncode} {probe_name} {output}")
32
64
  return result.returncode
33
65
 
34
66
 
@@ -0,0 +1,137 @@
1
+ import argparse
2
+ import os
3
+ import re
4
+ from typing import List, Tuple, Optional, Pattern
5
+
6
+ import yaml
7
+ from perun.connector import AdaptersManager
8
+ from privacyidea.models import db, TokenOwner, Token
9
+ from sqlalchemy import tuple_
10
+
11
+ # supplied as default values for arguments
12
+ PERUN_USER_ID_REGEX = r"perunUserId=(?P<perun_user_id>\d+),"
13
+ MFA_ACTIVE_TOKENS_ATTR_NAME = "urn:perun:user:attribute-def:def:mfaTokenTypes:mu"
14
+ PERUN_CONNECTOR_CONFIG_PATH = "/etc/perun-connector.yaml"
15
+
16
+
17
+ class ROLLOUTSTATE(object):
18
+ CLIENTWAIT = "clientwait"
19
+ PENDING = "pending"
20
+ VERIFYPENDING = "verify"
21
+ ENROLLED = "enrolled"
22
+ BROKEN = "broken"
23
+ FAILED = "failed"
24
+ DENIED = "denied"
25
+
26
+
27
+ USABLE_ROLLOUT_STATES = {
28
+ "webauthn": [""],
29
+ "backupcode": [""],
30
+ "totp": ["", ROLLOUTSTATE.ENROLLED],
31
+ }
32
+
33
+
34
+ def load_attrs_manager_config(config_filepath):
35
+ if os.path.exists(config_filepath):
36
+ with open(config_filepath, "r") as f:
37
+ config = yaml.safe_load(f)
38
+ return config
39
+ else:
40
+ raise FileNotFoundError(
41
+ f"Attempted to load attributes manager config from '{config_filepath}' "
42
+ f"but the file was not found."
43
+ )
44
+
45
+
46
+ def get_adapters_manager(config_path: str) -> AdaptersManager:
47
+ cfg = load_attrs_manager_config(config_path)
48
+ if not cfg:
49
+ raise ValueError("Was not able to load the attributes manager config.")
50
+ adapters_manager = AdaptersManager(
51
+ cfg["attributes_manager_config"], cfg["attributes_map"]
52
+ )
53
+ return adapters_manager
54
+
55
+
56
+ def get_args():
57
+ """
58
+ Supports the command-line arguments listed below.
59
+ """
60
+ parser = argparse.ArgumentParser(description="SAML authentication check")
61
+ parser._optionals.title = "Options"
62
+ parser.add_argument(
63
+ "--mfa-active-tokens-attr-name",
64
+ "-a",
65
+ default=MFA_ACTIVE_TOKENS_ATTR_NAME,
66
+ help="name of Perun attribute containing user's active MFA tokens",
67
+ )
68
+ parser.add_argument(
69
+ "--perun-user-id-regex",
70
+ "-r",
71
+ default=PERUN_USER_ID_REGEX,
72
+ help="regex for parsing Perun user ID from privacyIDEA user ID",
73
+ )
74
+ parser.add_argument(
75
+ "--perun-connector-config-path",
76
+ "-c",
77
+ default=PERUN_CONNECTOR_CONFIG_PATH,
78
+ help="path to config for Perun Connector",
79
+ )
80
+ args = parser.parse_args()
81
+ return args
82
+
83
+
84
+ def get_user_token_types() -> List[Tuple[str, str]]:
85
+ usable_rollout_states_tuples = [
86
+ (token_type, usable_state)
87
+ for token_type, usable_states_lst in USABLE_ROLLOUT_STATES.items()
88
+ for usable_state in usable_states_lst
89
+ ]
90
+ user_token_types = (
91
+ db.session.query(TokenOwner.user_id, Token.tokentype)
92
+ .join(Token, Token.id == TokenOwner.token_id)
93
+ .filter(Token.active.is_(True))
94
+ .filter(Token.locked.is_(False))
95
+ .filter(Token.revoked.is_(False))
96
+ .filter(
97
+ tuple_(Token.tokentype, Token.rollout_state).in_(
98
+ usable_rollout_states_tuples
99
+ )
100
+ )
101
+ .distinct(TokenOwner.user_id, Token.tokentype)
102
+ .order_by(TokenOwner.user_id)
103
+ .all()
104
+ )
105
+ return user_token_types
106
+
107
+
108
+ def parse_perun_user_id(
109
+ perun_user_id_regex: Pattern[str], privacyidea_user_id: str
110
+ ) -> Optional[str]:
111
+ match = re.search(perun_user_id_regex, privacyidea_user_id)
112
+ if match:
113
+ return match.group("perun_user_id")
114
+ return None
115
+
116
+
117
+ def main(args):
118
+ perun_user_id_regex = re.compile(args.perun_user_id_regex)
119
+ mfa_active_tokens_attr_name = args.mfa_active_tokens_attr_name
120
+ adapters_manager = get_adapters_manager(args.perun_connector_config_path)
121
+ user_token_types = get_user_token_types()
122
+ current_user_id = ""
123
+ current_user_token_types = []
124
+ for privacyidea_user_id, token_type in user_token_types:
125
+ perun_user_id = parse_perun_user_id(perun_user_id_regex, privacyidea_user_id)
126
+ if perun_user_id and perun_user_id != current_user_id:
127
+ if current_user_id:
128
+ current_user_token_types.sort()
129
+ attr_to_set = {mfa_active_tokens_attr_name: current_user_token_types}
130
+ adapters_manager.set_user_attributes(int(perun_user_id), attr_to_set)
131
+ current_user_id = perun_user_id
132
+ current_user_token_types = []
133
+ current_user_token_types.append(token_type)
134
+
135
+
136
+ if __name__ == "__main__":
137
+ main(get_args())
@@ -1,8 +1,8 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: perun.proxy.utils
3
- Version: 2.2.2
4
- Summary: Module with utilities and monitoring probes
5
- Home-page: https://gitlab.ics.muni.cz/perun-proxy-aai/proxyidp-scripts.git
3
+ Version: 2.4.0
4
+ Summary: Utilities and monitoring probes for Perun ProxyIdP
5
+ Home-page: https://gitlab.ics.muni.cz/perun/perun-proxyidp/perun-proxy-utils.git
6
6
  Requires-Python: >=3.9
7
7
  Description-Content-Type: text/markdown
8
8
  License-File: LICENSE
@@ -15,6 +15,8 @@ Requires-Dist: requests~=2.31
15
15
  Requires-Dist: PyYAML<7.0,>=5.4
16
16
  Requires-Dist: check_nginx_status~=1.0
17
17
  Requires-Dist: pyotp~=2.9
18
+ Requires-Dist: perun.connector~=3.8
19
+ Requires-Dist: privacyidea~=3.9
18
20
  Provides-Extra: ldap
19
21
  Requires-Dist: ldap3~=2.9.1; extra == "ldap"
20
22
  Requires-Dist: check_syncrepl_extended~=2020.13; extra == "ldap"
@@ -96,6 +98,27 @@ For usage instructions, run:
96
98
  run_version_script --help
97
99
  ```
98
100
 
101
+ ### sync_usable_token_types.py
102
+
103
+ Collects information about the usable token types of each privacyIDEA user and sends it
104
+ to Perun. Each user with usable tokens in privacyIDEA is assigned a list of their types, for example: `['backupcode', 'totp']`. A token is considered usable when it is **active** and it is not **locked** or
105
+ **revoked** and its rollout state allows logging in.
106
+
107
+ For more usage instructions, run:
108
+
109
+ ```sh
110
+ sync_usable_token_types --help
111
+ ```
112
+
113
+ Example:
114
+
115
+ ```sh
116
+ python3 sync_usable_token_types.py
117
+ --mfa-active-tokens-attr-name "attr_name"
118
+ --perun-user-id-regex "\d+"
119
+ --perun-connector-config-path "/path/to/file"
120
+ ```
121
+
99
122
  ## Nagios probes
100
123
 
101
124
  All nagios scripts are located under `nagios` directory.
@@ -15,6 +15,7 @@ perun/proxy/utils/run_probes.py
15
15
  perun/proxy/utils/run_version_script.py
16
16
  perun/proxy/utils/separate_oidc_logs.py
17
17
  perun/proxy/utils/separate_ssp_logs.py
18
+ perun/proxy/utils/sync_usable_token_types.py
18
19
  perun/proxy/utils/nagios/__init__.py
19
20
  perun/proxy/utils/nagios/check_custom_command.py
20
21
  perun/proxy/utils/nagios/check_dockers.py
@@ -19,3 +19,4 @@ run_probes = perun.proxy.utils.run_probes:main
19
19
  run_version_script = perun.proxy.utils.run_version_script:main
20
20
  separate_oidc_logs = perun.proxy.utils.separate_oidc_logs:main
21
21
  separate_ssp_logs = perun.proxy.utils.separate_ssp_logs:main
22
+ sync_usable_token_types = perun.proxy.utils.sync_usable_token_types:main
@@ -7,6 +7,8 @@ requests~=2.31
7
7
  PyYAML<7.0,>=5.4
8
8
  check_nginx_status~=1.0
9
9
  pyotp~=2.9
10
+ perun.connector~=3.8
11
+ privacyidea~=3.9
10
12
 
11
13
  [ldap]
12
14
  ldap3~=2.9.1
@@ -1,5 +1,5 @@
1
1
  [metadata]
2
- version = 2.2.2
2
+ version = 2.4.0
3
3
  license_files = LICENSE
4
4
  long_description = file: README.md
5
5
  long_description_content_type = text/markdown
@@ -3,8 +3,8 @@ import setuptools
3
3
  setuptools.setup(
4
4
  name="perun.proxy.utils",
5
5
  python_requires=">=3.9",
6
- url="https://gitlab.ics.muni.cz/perun-proxy-aai/proxyidp-scripts.git",
7
- description="Module with utilities and monitoring probes",
6
+ url="https://gitlab.ics.muni.cz/perun/perun-proxyidp/perun-proxy-utils.git",
7
+ description="Utilities and monitoring probes for Perun ProxyIdP",
8
8
  include_package_data=True,
9
9
  packages=setuptools.find_namespace_packages(include=["perun.*"]),
10
10
  install_requires=[
@@ -17,6 +17,8 @@ setuptools.setup(
17
17
  "PyYAML>=5.4,<7.0",
18
18
  "check_nginx_status~=1.0",
19
19
  "pyotp~=2.9",
20
+ "perun.connector~=3.8",
21
+ "privacyidea~=3.9",
20
22
  ],
21
23
  extras_require={
22
24
  "ldap": [
@@ -51,6 +53,7 @@ setuptools.setup(
51
53
  "run_version_script=perun.proxy.utils.run_version_script:main",
52
54
  "separate_oidc_logs=perun.proxy.utils.separate_oidc_logs:main",
53
55
  "separate_ssp_logs=perun.proxy.utils.separate_ssp_logs:main",
56
+ "sync_usable_token_types=perun.proxy.utils.sync_usable_token_types:main",
54
57
  ]
55
58
  },
56
59
  )