@@ -189,74 +189,85 @@ def remove_acl_role(role_template: str, id: int):
189189 log .warning (f"Rolle '{ role_data ['rolename' ]} ' existiert nicht und kann daher nicht gelöscht werden." )
190190
191191
192- def update_acls ():
192+ def update_acls () -> bool :
193+ """ Vergleicht die aktuell in Mosquitto DynSec konfigurierten ACLs mit den Vorlagen aus der neuen Version
194+ und aktualisiert diese entsprechend. Dabei werden überflüssige ACLs entfernt und fehlende ACLs ergänzt.
195+ Rollen, welche nicht mehr in der Konfiguration vorhanden sind, werden gelöscht. Es dürfen nur ACLs editiert
196+ werden, damit die Zuordnung zu Benutzern und Gruppen erhalten bleibt!
197+ Gibt True zurück, wenn eine Aktualisierung durchgeführt wurde, False wenn die ACLs bereits auf dem aktuellen
198+ Stand waren.
199+ """
193200 try :
194201 current_version , template_version = get_acl_versions ()
195- if current_version != template_version :
196- log .info ("Aktualisiere ACLs entsprechend der neuen Version..." )
197- roles = list_acl_roles ()
198- for role_name in roles :
199- try :
200- role_data = get_configured_role_data (role_name )
201- template_role_data = get_template_role_data (role_name )
202- # entferne Rollen, die in der Konfigurationsdatei nicht vorhanden sind
203- # bei allen anderen Rollen dürfen nur die ACLs editiert werden,
204- # damit die Zuordnung zu Benutzern und Gruppen erhalten bleibt!
205- if template_role_data is None :
206- log .info (f"Rolle '{ role_data ['rolename' ]} ' existiert nicht in den Vorlagen und wird gelöscht." )
207- run_command (["mosquitto_ctrl" , "dynsec" , "deleteRole" , role_data ["rolename" ]])
208- continue
209- # entferne ACLs aus der Rolle, wenn diese im Template nicht vorhanden sind
210- for acl in role_data ["acls" ]:
211- for template_acl in template_role_data ["acls" ]:
212- if acl_equal_with_placeholder (template_acl , acl ):
213- break
214- else :
215- log .info (f"Überflüssige ACL { acl ['acltype' ]} :{ 'allow' if acl ['allow' ] else 'deny' } :"
216- f"{ acl ['topic' ]} :{ acl ['priority' ]} in Rolle { role_data ['rolename' ]} "
217- "wird entfernt." )
218- run_command ([
219- "mosquitto_ctrl" , "dynsec" , "removeRoleAcl" , role_data ["rolename" ],
220- acl ["acltype" ], acl ["topic" ]
221- ])
222- # ergänze zusätzliche ACLs aus dem Template in der Rollen
223- for acl in template_role_data ["acls" ]:
224- for role_acl in role_data ["acls" ]:
225- if acl_equal_with_placeholder (acl , role_acl ):
226- break
227- else :
228- rolename_id = extract_id_from_role_name (role_data ["rolename" ])
229- if rolename_id is not None :
230- acl ["topic" ] = acl ["topic" ].replace ("<id>" , str (rolename_id ))
231- log .info (f"Zusätzliche ACL { acl ['acltype' ]} :{ 'allow' if acl ['allow' ] else 'deny' } :"
232- f"{ acl ['topic' ]} :{ acl ['priority' ]} in Rolle { role_data ['rolename' ]} "
233- "wird hinzugefügt." )
234- run_command ([
235- "mosquitto_ctrl" , "dynsec" , "addRoleAcl" , role_data ["rolename" ],
236- acl ["acltype" ], acl ["topic" ],
237- "allow" if acl ["allow" ] else "deny" ,
238- str (acl ["priority" ])
239- ])
240- except Exception :
241- log .exception (f"Fehler beim Aktualisieren der Rolle '{ role_name } '" )
242- # Rollen ergänzen, welche in der neuen Version hinzugekommen sind,
243- # aber noch nicht in der Konfiguration existieren
244- dynsec_roles = _get_default_roles ()
245- for config_role in dynsec_roles :
246- if (config_role ["rolename" ] not in roles and
247- VERSION_STRING not in config_role ["rolename" ]):
248- log .info (f"Füge neue Rolle '{ config_role ['rolename' ]} ' aus der neuen Version hinzu." )
249- run_command (["mosquitto_ctrl" , "dynsec" , "createRole" , config_role ["rolename" ]])
250- for acl in config_role ["acls" ]:
202+ if current_version == template_version :
203+ log .info (f"ACLs sind bereits auf dem aktuellen Stand (Version: '{ current_version } ')." )
204+ return False
205+ log .info ("Aktualisiere ACLs entsprechend der neuen Version..." )
206+ roles = list_acl_roles ()
207+ for role_name in roles :
208+ try :
209+ role_data = get_configured_role_data (role_name )
210+ template_role_data = get_template_role_data (role_name )
211+ # entferne Rollen, die in der Konfigurationsdatei nicht vorhanden sind
212+ # bei allen anderen Rollen dürfen nur die ACLs editiert werden,
213+ # damit die Zuordnung zu Benutzern und Gruppen erhalten bleibt!
214+ if template_role_data is None :
215+ log .info (f"Rolle '{ role_data ['rolename' ]} ' existiert nicht in den Vorlagen und wird gelöscht." )
216+ run_command (["mosquitto_ctrl" , "dynsec" , "deleteRole" , role_data ["rolename" ]])
217+ continue
218+ # entferne ACLs aus der Rolle, wenn diese im Template nicht vorhanden sind
219+ for acl in role_data ["acls" ]:
220+ for template_acl in template_role_data ["acls" ]:
221+ if acl_equal_with_placeholder (template_acl , acl ):
222+ break
223+ else :
224+ log .info (f"Überflüssige ACL { acl ['acltype' ]} :{ 'allow' if acl ['allow' ] else 'deny' } :"
225+ f"{ acl ['topic' ]} :{ acl ['priority' ]} in Rolle { role_data ['rolename' ]} wird entfernt." )
251226 run_command ([
252- "mosquitto_ctrl" , "dynsec" , "addRoleAcl" , config_role ["rolename" ],
227+ "mosquitto_ctrl" , "dynsec" , "removeRoleAcl" , role_data ["rolename" ],
228+ acl ["acltype" ], acl ["topic" ]
229+ ])
230+ # ergänze zusätzliche ACLs aus dem Template in der Rollen
231+ for acl in template_role_data ["acls" ]:
232+ for role_acl in role_data ["acls" ]:
233+ if acl_equal_with_placeholder (acl , role_acl ):
234+ break
235+ else :
236+ rolename_id = extract_id_from_role_name (role_data ["rolename" ])
237+ if rolename_id is not None :
238+ acl ["topic" ] = acl ["topic" ].replace ("<id>" , str (rolename_id ))
239+ log .info (f"Zusätzliche ACL { acl ['acltype' ]} :{ 'allow' if acl ['allow' ] else 'deny' } :"
240+ f"{ acl ['topic' ]} :{ acl ['priority' ]} in Rolle { role_data ['rolename' ]} wird hinzugefügt." )
241+ run_command ([
242+ "mosquitto_ctrl" , "dynsec" , "addRoleAcl" , role_data ["rolename" ],
253243 acl ["acltype" ], acl ["topic" ],
254244 "allow" if acl ["allow" ] else "deny" ,
255245 str (acl ["priority" ])
256246 ])
257- # aktualisiere die openwb-version Rolle
258- run_command (["mosquitto_ctrl" , "dynsec" , "deleteRole" , f"{ VERSION_STRING } { current_version } " ])
259- run_command (["mosquitto_ctrl" , "dynsec" , "createRole" , f"{ VERSION_STRING } { template_version } " ])
260- log .info ("ACL-Aktualisierung abgeschlossen." )
247+ except Exception :
248+ log .exception (f"Fehler beim Aktualisieren der Rolle '{ role_name } '" )
249+ # Rollen ergänzen, welche in der neuen Version hinzugekommen sind,
250+ # aber noch nicht in der Konfiguration existieren
251+ dynsec_roles = _get_default_roles ()
252+ for config_role in dynsec_roles :
253+ if (config_role ["rolename" ] not in roles and
254+ VERSION_STRING not in config_role ["rolename" ]):
255+ log .info (f"Füge neue Rolle '{ config_role ['rolename' ]} ' aus der neuen Version hinzu." )
256+ run_command (["mosquitto_ctrl" , "dynsec" , "createRole" , config_role ["rolename" ]])
257+ for acl in config_role ["acls" ]:
258+ run_command ([
259+ "mosquitto_ctrl" , "dynsec" , "addRoleAcl" , config_role ["rolename" ],
260+ acl ["acltype" ], acl ["topic" ],
261+ "allow" if acl ["allow" ] else "deny" ,
262+ str (acl ["priority" ])
263+ ])
264+ # aktualisiere die openwb-version Rolle
265+ run_command (["mosquitto_ctrl" , "dynsec" , "deleteRole" , f"{ VERSION_STRING } { current_version } " ])
266+ run_command (["mosquitto_ctrl" , "dynsec" , "createRole" , f"{ VERSION_STRING } { template_version } " ])
267+ log .info ("ACL-Aktualisierung abgeschlossen." )
268+ return True
261269 except Exception :
262270 log .exception ("Fehler beim Aktualisieren der ACLs" )
271+ # Im Fehlerfall nehmen wir an, dass zumindest einige ACLs aktualisiert wurden.
272+ # Also geben wir True zurück, damit die notwendigen weiteren Schritte ausgeführt werden.
273+ return True
0 commit comments