2024-06-29 16:05:58 +00:00
|
|
|
from src.args import args
|
|
|
|
import src.api as api
|
2024-06-30 07:46:15 +00:00
|
|
|
import src.pki as pki
|
2024-06-30 08:14:42 +00:00
|
|
|
import src.utils as utils
|
2024-06-29 16:05:58 +00:00
|
|
|
import os
|
|
|
|
|
|
|
|
print("Check storage")
|
|
|
|
if not os.path.isdir(args.storage):
|
|
|
|
print("Create storage")
|
|
|
|
os.makedirs(args.storage, exist_ok=True)
|
|
|
|
|
|
|
|
|
|
|
|
print("Check secure origin...")
|
|
|
|
if not os.path.isfile(args.secure_origin_path):
|
|
|
|
origin = api.get_secure_origin()
|
|
|
|
with open(args.secure_origin_path, "w") as f:
|
|
|
|
f.write(origin)
|
|
|
|
|
|
|
|
with open(args.secure_origin_path, "r") as f:
|
|
|
|
args.secure_origin = f.read()
|
2024-06-29 16:08:57 +00:00
|
|
|
print(f"Secure origin = {args.secure_origin}")
|
|
|
|
|
|
|
|
|
|
|
|
print("Check system root CA")
|
|
|
|
if not os.path.isfile(args.root_ca_path):
|
|
|
|
origin = api.get_root_ca()
|
|
|
|
with open(args.root_ca_path, "w") as f:
|
|
|
|
f.write(origin)
|
|
|
|
|
2024-07-03 20:05:19 +00:00
|
|
|
print("Check device ID")
|
|
|
|
if not os.path.isfile(args.dev_id_path):
|
|
|
|
print("Generate device id...")
|
|
|
|
with open(args.dev_id_path, "w") as f:
|
|
|
|
f.write(f"PyDev {utils.rand_str(10)}")
|
|
|
|
|
|
|
|
with open(args.dev_id_path, "r") as f:
|
|
|
|
args.dev_id = f.read()
|
|
|
|
|
2024-06-30 07:46:15 +00:00
|
|
|
print("Check private key")
|
2024-06-30 08:14:42 +00:00
|
|
|
if not os.path.isfile(args.dev_priv_key_path):
|
2024-06-30 07:46:15 +00:00
|
|
|
print("Generate private key...")
|
|
|
|
key = pki.gen_priv_key()
|
2024-06-30 08:14:42 +00:00
|
|
|
with open(args.dev_priv_key_path, "w") as f:
|
2024-06-30 07:46:15 +00:00
|
|
|
f.write(key)
|
2024-06-30 08:14:42 +00:00
|
|
|
|
|
|
|
print("Check CSR")
|
|
|
|
if not os.path.isfile(args.dev_csr_path):
|
|
|
|
print("Generate CSR...")
|
|
|
|
with open(args.dev_priv_key_path, "r") as f:
|
|
|
|
priv_key = "".join(f.readlines())
|
2024-07-03 20:05:19 +00:00
|
|
|
csr = pki.gen_csr(priv_key=priv_key, cn=args.dev_id)
|
2024-06-30 08:14:42 +00:00
|
|
|
with open(args.dev_csr_path, "w") as f:
|
|
|
|
f.write(csr)
|
2024-07-01 19:10:45 +00:00
|
|
|
|
|
|
|
print("Check device enrollment...")
|
2024-07-03 20:05:19 +00:00
|
|
|
status = api.device_enrollment_status()
|
|
|
|
|
2024-07-03 20:22:36 +00:00
|
|
|
if status != "Validated":
|
|
|
|
if os.path.isfile(args.dev_crt_path):
|
|
|
|
print("Delete invalid certificate")
|
|
|
|
os.unlink(args.dev_crt_path)
|
|
|
|
|
2024-07-03 20:05:19 +00:00
|
|
|
if status == "Unknown":
|
|
|
|
print("Device is unknown on the system, need to submit a CSR...")
|
2024-07-01 19:10:45 +00:00
|
|
|
with open(args.dev_csr_path, "r") as f:
|
|
|
|
csr = "".join(f.read())
|
|
|
|
|
|
|
|
print("Enrolling device...")
|
2024-08-23 19:00:18 +00:00
|
|
|
api.enroll_device(csr)
|
2024-07-03 20:05:19 +00:00
|
|
|
print("Done. Please accept the device on central system web UI")
|
|
|
|
exit(0)
|
|
|
|
|
|
|
|
if status == "Pending":
|
|
|
|
print(
|
|
|
|
"Device is enrolled, but not validated yet. Please accept the device on central system web UI"
|
|
|
|
)
|
|
|
|
exit(0)
|
2024-07-02 20:55:51 +00:00
|
|
|
|
2024-07-03 20:05:19 +00:00
|
|
|
print("Device is successfully enrolled!")
|
2024-07-03 20:19:56 +00:00
|
|
|
|
|
|
|
print("Check device certificate")
|
|
|
|
if not os.path.isfile(args.dev_crt_path):
|
|
|
|
print("Retrieve certificate...")
|
|
|
|
cert = api.device_certificate()
|
|
|
|
with open(args.dev_crt_path, "w") as f:
|
|
|
|
f.write(cert)
|
|
|
|
|
2024-07-03 20:22:36 +00:00
|
|
|
print("Done. ready to operate.")
|