Skip to content
GitLab
Explore
Projects
Groups
Topics
Snippets
Projects
Groups
Topics
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Register
Sign in
Toggle navigation
Menu
UPSMF
uphrh-workflow
Commits
54064925
Commit
54064925
authored
1 year ago
by
SanjaySinghRajpoot
Browse files
Options
Download
Patches
Plain Diff
feat: rotate script added
parent
075a2652
main
dev
feature/formlistapi
revert-67-contributeByGitpod
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
utils/rotate_secrets.py
+37
-0
utils/rotate_secrets.py
with
37 additions
and
0 deletions
+37
-0
utils/rotate_secrets.py
0 → 100644
+
37
−
0
View file @
54064925
import
os
import
argparse
def
rotate_secret
(
env_file_path
:
str
,
variables
:
list
[
str
]):
# Open the specified .env file in read mode
with
open
(
env_file_path
,
'r'
)
as
f
:
env_lines
=
f
.
readlines
()
# Iterate over each line in the .env file
for
i
in
range
(
len
(
env_lines
)):
env_line
=
env_lines
[
i
].
strip
()
# Check if the line is not a comment and contains a variable and a value
if
not
env_line
.
startswith
(
'#'
)
and
'='
in
env_line
:
env_var
,
env_val
=
env_line
.
split
(
'='
,
1
)
# Check if the variable is in the list of variables to be rotated
if
env_var
in
variables
:
# Replace the value of the variable with a new random value
env_val
=
os
.
urandom
(
16
).
hex
()
# replace with your preferred method of generating new secrets
env_lines
[
i
]
=
f
"
{
env_var
}
=
{
env_val
}
\n
"
# Write the updated lines back to the .env file
with
open
(
env_file_path
,
'w'
)
as
f
:
f
.
writelines
(
env_lines
)
if
__name__
==
'__main__'
:
# Define the command-line arguments
parser
=
argparse
.
ArgumentParser
(
description
=
'Rotate secrets in a .env file'
)
parser
.
add_argument
(
'env_file'
,
type
=
str
,
help
=
'Path to .env file'
)
parser
.
add_argument
(
'--variables'
,
type
=
str
,
nargs
=
'+'
,
help
=
'List of variables to rotate secrets for'
)
# Parse the command-line arguments
args
=
parser
.
parse_args
()
# Call the rotate_secret function with the specified arguments
rotate_secret
(
args
.
env_file
,
args
.
variables
)
This diff is collapsed.
Click to expand it.
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment
Menu
Explore
Projects
Groups
Topics
Snippets