Skip to content

Commit 30ffc64

Browse files
authored
fix: address suggestions from SonarCloud (#55)
* add devsecops_pipeline.py * add devsecops-pipeline workflow * replace sonarqube with bandit * edit cron job * add comment for the cron job * add Git Guardian workflow * edit Git Guardian workflow * edit Git Guardian workflow * edit Git Guardian workflow * edit Git Guardian workflow * edit Git Guardian workflow * edit Git Guardian workflow * edit Git Guardian workflow * edit Git Guardian workflow * edit Git Guardian workflow * edit README file * edit DevSecOps pipeline * edit release badge * add .gitignore file * add PR Title Linter * add status badge for PR Linter * add SonarCloud implementation * edit sonar-project.properties * add SonarCloud badge * address suggestions from SonarCloud * add secrets module in strong_passgen_for_prod.py * edit strong_passgen_for_prod.py * edit strong_passgen_for_prod.py * edit strong_passgen_for_prod.py * edit strong_passgen_for_prod.py * edit strong_passgen_for_prod.py * edit strong_passgen_for_prod.py * edit strong_passgen_for_prod.py * edit strong_passgen_for_prod.py * edit crypto_passgen_for_prod.py script
1 parent 305cb4a commit 30ffc64

File tree

5 files changed

+76
-67
lines changed

5 files changed

+76
-67
lines changed

β€ŽTesla.pyβ€Ž

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Parameters and Arguments example
2-
def checkDriverAge(age=0):
2+
def check_driver_age(age=0):
33
if int(age) < 18:
44
print("Sorry, you are too young to drive this car. Powering off 😟")
55
elif int(age) > 18:
@@ -8,4 +8,4 @@ def checkDriverAge(age=0):
88
print("Congratulations on your first year of driving. Enjoy the ride! πŸš€")
99

1010

11-
checkDriverAge()
11+
check_driver_age()

β€Žcrypto_passgen_for_prod.pyβ€Ž

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import string
2+
import secrets
3+
from cryptography.fernet import Fernet
4+
import os
5+
6+
# Step 1: Generate or retrieve a secure encryption key
7+
key = os.environ.get('FERNET_KEY')
8+
if not key:
9+
print("Encryption key is missing. Please set the 'FERNET_KEY' environment variable.")
10+
exit(1)
11+
cipher_suite = Fernet(key)
12+
13+
# Step 2: Define all character sets for password generation
14+
s1 = list(string.ascii_lowercase) # Lowercase letters
15+
s2 = list(string.ascii_uppercase) # Uppercase letters
16+
s3 = list(string.digits) # Digits
17+
s4 = list(string.punctuation) # Special characters
18+
19+
# Step 3: Ask user for password length
20+
while True:
21+
try:
22+
characters_number = int(input("How many characters do you want in your password? "))
23+
if 8 <= characters_number <= 128:
24+
break
25+
print("Please choose a number between 8 and 128.")
26+
except ValueError:
27+
print("Invalid input. Please enter a valid number.")
28+
29+
# Step 4: Securely shuffle the character lists using secrets.SystemRandom()
30+
secure_random = secrets.SystemRandom()
31+
s1 = secure_random.sample(s1, len(s1)) # Securely shuffle lowercase letters
32+
s2 = secure_random.sample(s2, len(s2)) # Securely shuffle uppercase letters
33+
s3 = secure_random.sample(s3, len(s3)) # Securely shuffle digits
34+
s4 = secure_random.sample(s4, len(s4)) # Securely shuffle punctuation
35+
36+
# Step 5: Create the password
37+
# Ensure at least one character from each set is included
38+
result = [
39+
secrets.choice(s1),
40+
secrets.choice(s2),
41+
secrets.choice(s3),
42+
secrets.choice(s4)
43+
]
44+
45+
# Fill the remaining slots randomly
46+
remaining_characters = characters_number - len(result)
47+
result.extend(secrets.choice(s1 + s2 + s3 + s4) for _ in range(remaining_characters))
48+
49+
# Secure final shuffle
50+
result = secure_random.sample(result, len(result))
51+
52+
# Step 6: Join and encrypt the password
53+
password = "".join(result)
54+
encrypted_password = cipher_suite.encrypt(password.encode())
55+
56+
# Step 7: Store the encrypted password securely
57+
try:
58+
with open("password_storage.txt", "wb") as file:
59+
file.write(encrypted_password)
60+
print("Your password has been securely generated and encrypted.")
61+
print("The encrypted password has been saved in 'password_storage.txt'.")
62+
print("Ensure your encryption key is securely stored to decrypt the password.")
63+
except IOError as e:
64+
print(f"File operation failed: {e}")
65+
except Exception as e:
66+
print(f"An unexpected error occurred: {e}")

β€Žfind_duplicates.pyβ€Ž

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
# Shows the duplicate strings
22
my_list = ['a', 'b', 'c', 'd', 'd', 'm', 'm', 'n', 'o', 'z', 'z']
3-
43
duplicates = []
54
for value in my_list:
6-
if my_list.count(value) > 1:
7-
if value not in duplicates:
8-
duplicates.append(value)
9-
5+
if my_list.count(value) > 1 and value not in duplicates:
6+
duplicates.append(value)
107
print(duplicates)

β€Žperformance_decorator.pyβ€Ž

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
1-
# Performance decorator
21
from time import time
32

4-
3+
# Decorator to measure performance
54
def performance(fn):
65
def wrapper(*args, **kwargs):
76
t1 = time()
87
result = fn(*args, **kwargs)
98
t2 = time()
10-
print(f'took {t2-t1} seconds')
9+
print(f'took {t2 - t1} seconds')
1110
return result
1211
return wrapper
1312

14-
1513
@performance
1614
def long_time():
17-
for i in range(10000000):
18-
i*5
19-
15+
"""Benchmarking function to test performance."""
16+
for _ in range(10_000_000):
17+
pass # Empty loop for benchmarking the decorator
2018

19+
# Execute the benchmark
2120
long_time()

β€Žstrong_passgen_for_prod.pyβ€Ž

Lines changed: 0 additions & 53 deletions
This file was deleted.

0 commit comments

Comments
Β (0)