Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
scripts
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
ISC2
atelier_sec
scripts
Commits
dd0e9ca5
Commit
dd0e9ca5
authored
11 months ago
by
brian
Browse files
Options
Downloads
Patches
Plain Diff
added telegram msg
parent
b05aaf89
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
gen_cert.py
+52
-6
52 additions, 6 deletions
gen_cert.py
with
52 additions
and
6 deletions
gen_cert.py
+
52
−
6
View file @
dd0e9ca5
...
...
@@ -3,7 +3,8 @@ import datetime
import
argparse
from
cryptography
import
x509
from
cryptography.hazmat.primitives
import
hashes
,
serialization
from
cryptography.hazmat.primitives.asymmetric
import
rsa
import
requests
as
req
from
dotenv
import
dotenv_values
def
load_ca
(
ca_cert_path
,
ca_key_path
,
ca_key_password
):
...
...
@@ -18,6 +19,14 @@ def load_ca(ca_cert_path, ca_key_path, ca_key_password):
def
load_csr
(
csr_path
):
"""
Load CSR file
Args:
csr_path (): string representing the path to the CSR
Returns:
"""
with
open
(
csr_path
,
'
rb
'
)
as
csr_file
:
csr
=
x509
.
load_pem_x509_csr
(
csr_file
.
read
())
return
csr
...
...
@@ -60,16 +69,36 @@ def save_certificate(cert, filepath):
with
open
(
filepath
,
"
wb
"
)
as
f
:
f
.
write
(
cert
.
public_bytes
(
serialization
.
Encoding
.
PEM
))
def
get_certificate_details
(
cert
,
cert_name
):
txt
=
f
"
Details of
{
cert_name
}
\n
"
subject
=
cert
.
subject
for
attr
in
subject
:
txt
+=
f
"
{
attr
.
oid
.
_name
}
:
{
attr
.
value
}
\n
"
txt
+=
f
"
Validity for
{
cert_name
}
\n
"
txt
+=
f
"
\t
Not valid before:
{
cert
.
not_valid_before
}
\n
"
txt
+=
f
"
\t
Not valid after:
{
cert
.
not_valid_after
}
"
return
txt
def
main
():
parser
=
argparse
.
ArgumentParser
()
parser
.
add_argument
(
"
ca_cert_path
"
,
help
=
"
Path to the CA certificate
"
)
parser
.
add_argument
(
"
ca_key_path
"
,
help
=
"
Path to the CA private key
"
)
parser
.
add_argument
(
"
csr_path
"
,
help
=
"
Path to the Certificate Signing Request (CSR)
"
)
parser
.
add_argument
(
"
output_cert_path
"
,
help
=
"
Output path for the signed certificate
"
)
parser
.
add_argument
(
"
csr_path
"
,
help
=
"
Path to the Certificate Signing Request (CSR)
"
)
parser
.
add_argument
(
"
output_cert_path
"
,
help
=
"
Output path for the signed certificate
"
)
args
=
parser
.
parse_args
()
config
=
dotenv_values
(
"
.env
"
)
bot_token
=
config
[
"
API_TELEGRAM
"
]
channel_id
=
config
[
"
CHANNEL_ID
"
]
ca_cert_path
=
args
.
ca_cert_path
ca_key_path
=
args
.
ca_key_path
csr_path
=
args
.
csr_path
...
...
@@ -81,14 +110,16 @@ def main():
print
(
"
\n
"
)
try
:
ca_cert
,
ca_key
=
load_ca
(
ca_cert_path
,
ca_key_path
,
ca_key_password
=
ca_pass
.
encode
())
ca_cert
,
ca_key
=
load_ca
(
ca_cert_path
,
ca_key_path
,
ca_key_password
=
ca_pass
.
encode
())
except
:
print
(
"
Bad password
"
)
exit
(
1
)
csr
=
load_csr
(
csr_path
)
signed_cert
=
create_certificate
(
csr
,
ca_cert
,
ca_key
,
is_intermediate
=
False
)
signed_cert
=
create_certificate
(
csr
,
ca_cert
,
ca_key
,
is_intermediate
=
False
)
save_certificate
(
signed_cert
,
output_cert_path
)
...
...
@@ -96,7 +127,22 @@ def main():
f
.
write
(
signed_cert
.
public_bytes
(
serialization
.
Encoding
.
PEM
))
f
.
write
(
ca_cert
.
public_bytes
(
serialization
.
Encoding
.
PEM
))
print
(
f
"
Certificate
{
csr_path
}
saved at
{
output_cert_path
}
. Signed by
{
ca_cert_path
}
"
)
msg
=
f
"
Certificate
{
csr_path
}
saved at
{
output_cert_path
}
.
\n
Signed by
{
ca_cert_path
}
\n\n
{
get_certificate_details
(
ca_cert
,
"
Signer certificate
"
)
}
\n\n
{
get_certificate_details
(
signed_cert
,
"
Signed cert
"
)
}
"
url
=
f
"
https://api.telegram.org/bot
{
bot_token
}
/sendMessage
"
payload
=
{
'
chat_id
'
:
channel_id
,
'
text
'
:
msg
}
print
(
msg
)
response
=
req
.
post
(
url
,
payload
)
print
(
response
.
json
())
if
__name__
==
"
__main__
"
:
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
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