Skip to content
Snippets Groups Projects
Commit dd0e9ca5 authored by brian's avatar brian
Browse files

added telegram msg

parent b05aaf89
No related branches found
No related tags found
No related merge requests found
......@@ -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}.\nSigned 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__":
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment