118
118
import json
119
119
import logging
120
120
import os
121
+ import random
121
122
import re
123
+ import string
122
124
from typing import Callable
123
125
from urllib .parse import urlparse
124
126
@@ -2095,7 +2097,7 @@ def process_group_placeholders(self):
2095
2097
2096
2098
# Now we determine the ID. Either it is in the payload section from
2097
2099
# the current customizer run or we try to look it up in the system.
2098
- # The latter case may happen if the custiomuer pod got restarted.
2100
+ # The latter case may happen if the customizer pod got restarted.
2099
2101
group_id = self .determine_group_id (group )
2100
2102
if not group_id :
2101
2103
logger .warning (
@@ -2129,7 +2131,7 @@ def process_user_placeholders(self):
2129
2131
)
2130
2132
continue
2131
2133
user_name = user ["name" ]
2132
- # Check if group has been disabled in payload (enabled = false).
2134
+ # Check if user has been disabled in payload (enabled = false).
2133
2135
# In this case we skip the element:
2134
2136
if "enabled" in user and not user ["enabled" ]:
2135
2137
logger .info (
@@ -2139,15 +2141,15 @@ def process_user_placeholders(self):
2139
2141
2140
2142
# Now we determine the ID. Either it is in the payload section from
2141
2143
# the current customizer run or we try to look it up in the system.
2142
- # The latter case may happen if the custiomuer pod got restarted.
2144
+ # The latter case may happen if the customizer pod got restarted.
2143
2145
user_id = self .determine_user_id (user )
2144
2146
if not user_id :
2145
2147
logger .warning (
2146
2148
"User needs an ID for placeholder definition. Skipping..."
2147
2149
)
2148
2150
continue
2149
2151
2150
- # Add Group with its ID to the dict self._placeholder_values:
2152
+ # Add user with its ID to the dict self._placeholder_values:
2151
2153
self ._placeholder_values ["OTCS_USER_ID_%s" , user_name .upper ()] = str (
2152
2154
user_id
2153
2155
)
@@ -2428,8 +2430,8 @@ def process_users(self, section_name: str = "users") -> bool:
2428
2430
2429
2431
# If this payload section has been processed successfully before we
2430
2432
# can return True and skip processing it once more:
2431
- if self .check_status_file (section_name ):
2432
- return True
2433
+ # if self.check_status_file(section_name):
2434
+ # return True
2433
2435
2434
2436
success : bool = True
2435
2437
@@ -2452,13 +2454,28 @@ def process_users(self, section_name: str = "users") -> bool:
2452
2454
continue
2453
2455
2454
2456
# Sanity checks:
2455
- if not "password" in user :
2456
- logger .error (
2457
- "User -> %s is missing a password. Skipping to next user..." ,
2457
+ if (
2458
+ not "password" in user
2459
+ or user ["password" ] is None
2460
+ or user ["password" ] == ""
2461
+ ):
2462
+ logger .info (
2463
+ "User -> %s no password defined in payload, generating random password..." ,
2458
2464
user_name ,
2459
2465
)
2460
- success = False
2461
- continue
2466
+ user ["password" ] = self .generate_password (
2467
+ length = 10 , use_special_chars = True
2468
+ )
2469
+
2470
+ description_attribue = {
2471
+ "name" : "description" ,
2472
+ "value" : "initial password: " + user ["password" ],
2473
+ }
2474
+
2475
+ try :
2476
+ user ["extra_attributes" ].append (description_attribue )
2477
+ except KeyError :
2478
+ user ["extra_attributes" ] = [description_attribue ]
2462
2479
2463
2480
# Sanity checks:
2464
2481
if not "base_group" in user :
@@ -3533,12 +3550,12 @@ def process_admin_settings(
3533
3550
3534
3551
if not admin_settings :
3535
3552
logger .info ("Payload section -> %s is empty. Skipping..." , section_name )
3536
- return True
3553
+ return False # important to return False here as otherwise we are triggering a restart of services!!
3537
3554
3538
3555
# If this payload section has been processed successfully before we
3539
3556
# can return True and skip processing it once more:
3540
3557
if self .check_status_file (section_name ):
3541
- return True
3558
+ return False # important to return False here as otherwise we are triggering a restart of services!!
3542
3559
3543
3560
restart_required : bool = False
3544
3561
success : bool = True
@@ -5587,12 +5604,12 @@ def process_workspace_members(self, section_name: str = "workspaceMembers") -> b
5587
5604
# of automatically created workspaces - this can happen because the
5588
5605
# creator gets added to the leader role automatically:
5589
5606
leader_role_id = self ._otcs .lookup_result_value (
5590
- workspace_roles , "leader" , " True" , "id"
5607
+ workspace_roles , "leader" , True , "id"
5591
5608
)
5592
5609
5593
5610
if leader_role_id :
5594
5611
leader_role_name = self ._otcs .lookup_result_value (
5595
- workspace_roles , "leader" , str ( True ) , "name"
5612
+ workspace_roles , "leader" , True , "name"
5596
5613
)
5597
5614
response = self ._otcs .remove_member_from_workspace (
5598
5615
workspace_node_id , leader_role_id , workspace_owner_id , False
@@ -8391,7 +8408,7 @@ def process_browser_automations(
8391
8408
success = False
8392
8409
break
8393
8410
else :
8394
- browser_automation_object .implict_wait (10 .0 )
8411
+ browser_automation_object .implict_wait (15 .0 )
8395
8412
logger .info (
8396
8413
"Successfuly loaded page -> %s." , base_url + page
8397
8414
)
@@ -8415,7 +8432,7 @@ def process_browser_automations(
8415
8432
success = False
8416
8433
break
8417
8434
else :
8418
- browser_automation_object .implict_wait (10 .0 )
8435
+ browser_automation_object .implict_wait (15 .0 )
8419
8436
logger .info ("Successfuly clicked element -> %s." , elem )
8420
8437
case "set_elem" :
8421
8438
elem = automation .get ("elem" , "" )
@@ -8657,3 +8674,71 @@ def get_k8s(self) -> object:
8657
8674
def getM365 (self ) -> object :
8658
8675
"""Get M365 object"""
8659
8676
return self ._m365
8677
+
8678
+ def generate_password (
8679
+ self ,
8680
+ length : int ,
8681
+ use_special_chars : bool = False ,
8682
+ min_special : int = 1 ,
8683
+ min_numerical : int = 1 ,
8684
+ min_upper : int = 1 ,
8685
+ min_lower : int = 1 ,
8686
+ override_special : str = None ,
8687
+ ):
8688
+ """Function to generate random passwords with a given specification
8689
+
8690
+ Args:
8691
+ length (int): Define password length
8692
+ use_special_chars (bool, optional): Define if special characters should be used. Defaults to False.
8693
+ min_special (int, optional): Define min amount of special characters. Defaults to 1.
8694
+ min_numerical (int, optional): Define if numbers should be used. Defaults to 1.
8695
+ min_upper (int, optional): Define mininum number of upper case letters. Defaults to 1.
8696
+ min_lower (int, optional): Define minimum number of lower case letters. Defaults to 1.
8697
+ override_special (string | None, optional): Define special characters to be used, if not set: !@#$%^&*()_-+=<>?/{}[]. Defaults to None.
8698
+
8699
+ Raises:
8700
+ ValueError: _description_
8701
+
8702
+ Returns:
8703
+ _type_: _description_
8704
+ """
8705
+ # Define character sets
8706
+ lowercase_letters = string .ascii_lowercase
8707
+ uppercase_letters = string .ascii_uppercase
8708
+ numerical_digits = string .digits
8709
+ special_characters = "!@#$%^&*()_-+=<>?/{}[]"
8710
+
8711
+ if override_special :
8712
+ special_characters = override_special
8713
+ # Ensure minimum requirements are met
8714
+
8715
+ if min_special + min_numerical + min_upper + min_lower > length :
8716
+ raise ValueError ("Minimum requirements exceed password length" )
8717
+
8718
+ # Initialize the password
8719
+ password = []
8720
+
8721
+ # Add required characters
8722
+ password .extend (random .sample (lowercase_letters , min_lower ))
8723
+ password .extend (random .sample (uppercase_letters , min_upper ))
8724
+ password .extend (random .sample (numerical_digits , min_numerical ))
8725
+
8726
+ if use_special_chars :
8727
+ password .extend (random .sample (special_characters , min_special ))
8728
+
8729
+ # Fill the rest of the password with random characters
8730
+ remaining_length = length - len (password )
8731
+ all_chars = lowercase_letters + uppercase_letters + numerical_digits
8732
+
8733
+ if use_special_chars :
8734
+ all_chars += special_characters
8735
+
8736
+ password .extend (random .choices (all_chars , k = remaining_length ))
8737
+
8738
+ # Shuffle the password to ensure randomness
8739
+ random .shuffle (password )
8740
+
8741
+ # Convert the password list to a string
8742
+ final_password = "" .join (password )
8743
+
8744
+ return final_password
0 commit comments