-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser_create_env_specific.py
More file actions
153 lines (128 loc) · 4.99 KB
/
Copy pathuser_create_env_specific.py
File metadata and controls
153 lines (128 loc) · 4.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import random
import string
from mysql.connector import connect, Error
# Function to read credentials from a file
def read_credentials(file_path):
credentials = {}
try:
with open(file_path, 'r') as file:
for line in file:
key, value = line.strip().split('=')
credentials[key] = value
return credentials
except FileNotFoundError:
print(f"Error: File {file_path} not found.")
exit()
except ValueError:
print(f"Error: Invalid format in {file_path}. Ensure each line is in 'key=value' format.")
exit()
# Load credentials
credentials = read_credentials("credentials.txt")
DB_HOST = credentials.get("DB_HOST")
DB_USER = credentials.get("DB_USER")
DB_PASSWORD = credentials.get("DB_PASSWORD")
DB_PROD = credentials.get("DB_PROD")
DB_PREPROD = credentials.get("DB_PREPROD")
def connect_to_mysql():
try:
connection = connect(
host=DB_HOST,
user=DB_USER,
password=DB_PASSWORD
)
if connection.is_connected():
return connection
except Error as e:
print(f"Error: {e}")
return None
def generate_random_password(length=12):
if length < 4:
raise ValueError("Password length must be at least 4 to meet the condition requirements.")
# Define the categories for the password
lowercase = random.choice(string.ascii_lowercase)
uppercase = random.choice(string.ascii_uppercase)
numeric = random.choice(string.digits)
non_alphanumeric = random.choice(string.punctuation)
# Generate the remaining characters
remaining_length = length - 4
remaining_chars = random.choices(string.ascii_letters + string.digits + string.punctuation, k=remaining_length)
# Combine all parts and shuffle to ensure randomness
password_list = [lowercase, uppercase, numeric, non_alphanumeric] + remaining_chars
random.shuffle(password_list)
# Join the list into a single string and return
return ''.join(password_list)
def write_to_file(username, password, environment):
# Define the file path
file_path = 'generated_password.txt'
# Remove existing file (if any)
try:
with open(file_path, 'w') as file:
file.write(f"Username: {username}\n")
file.write(f"Password: {password}\n")
file.write(f"Environment: {environment}\n")
print(f"Details saved in {file_path}")
except Exception as e:
print(f"Error writing to file: {e}")
def create_user_from_email(email, environment, role):
try:
connection = connect_to_mysql()
if not connection:
print("Failed to connect to the database.")
return
cursor = connection.cursor()
# Extract username from email
username = email.split('@')[0]
# Generate a secure random password
password = generate_random_password(12)
# Determine access level
if role == "dba":
if environment == "prod":
privileges = "ALL PRIVILEGES"
database = DB_PROD
elif environment == "preprod":
privileges = "ALL PRIVILEGES"
database = DB_PREPROD
else:
print("Invalid environment specified.")
return
elif role in ["dev", "qa"]:
if environment == "prod":
privileges = "SELECT"
database = DB_PROD
elif environment == "preprod":
privileges = "ALL PRIVILEGES"
database = DB_PREPROD
else:
print("Invalid environment specified.")
return
else:
print("Invalid role specified.")
return
# Create user and assign privileges
cursor.execute(f"CREATE USER '{username}'@'%' IDENTIFIED BY '{password}';")
cursor.execute(f"GRANT {privileges} ON {database}.* TO '{username}'@'%';")
connection.commit()
print(f"User '{username}' created with {privileges} access on {database}.")
# Save the details to a text file
write_to_file(username, password, environment)
except Error as e:
print(f"Error: {e}")
finally:
if connection and connection.is_connected():
cursor.close()
connection.close()
if __name__ == "__main__":
print("User Creation Utility")
email = input("Enter the email ID: ").strip()
environment = input("Enter the environment (prod/preprod): ").strip().lower()
role = input("Enter the role (dev/qa/dba): ").strip().lower()
create_user_from_email(email, environment, role)
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
cd path/to/your/script
python3 user_creation.py
User Creation Utility
Enter the email ID: example@domain.com
Enter the environment (prod/preprod): prod
Enter the role (dev/qa/dba): dba
User 'example' created with ALL PRIVILEGES access on production_db.
Details saved in generated_password.txt