Upload files to "/"
This commit is contained in:
7
config.yaml.example
Normal file
7
config.yaml.example
Normal file
@@ -0,0 +1,7 @@
|
||||
api_key: "your_opnsense_api_key"
|
||||
api_secret: "your_opnsense_api_secret"
|
||||
host: "https://your-opnsense.local"
|
||||
certificate_search: "example.com"
|
||||
output_directory: "./certs"
|
||||
output_filename: "cert.cert"
|
||||
export_format: "crt" # crt or prv
|
||||
62
export_cert.py
Normal file
62
export_cert.py
Normal file
@@ -0,0 +1,62 @@
|
||||
import requests
|
||||
import yaml
|
||||
import os
|
||||
import sys
|
||||
from requests.auth import HTTPBasicAuth
|
||||
|
||||
def load_config(config_path):
|
||||
with open(config_path, 'r') as f:
|
||||
return yaml.safe_load(f)
|
||||
|
||||
def search_certificates(config):
|
||||
url = f"{config['host']}/api/trust/cert/search"
|
||||
payload = {
|
||||
"searchPhrase": config['certificate_search']
|
||||
}
|
||||
response = requests.post(url, json=payload, auth=HTTPBasicAuth(config['api_key'], config['api_secret']))
|
||||
response.raise_for_status()
|
||||
return response.json().get("rows", [])
|
||||
|
||||
def export_certificate(config, uuid):
|
||||
url = f"{config['host']}/api/trust/cert/generate_file/{uuid}/{config['export_format']}"
|
||||
response = requests.post(url, auth=HTTPBasicAuth(config['api_key'], config['api_secret']))
|
||||
response.raise_for_status()
|
||||
return response.json().get("payload")
|
||||
|
||||
def save_certificate(cert_data, filename, output_dir):
|
||||
os.makedirs(output_dir, exist_ok=True)
|
||||
filepath = os.path.join(output_dir, filename)
|
||||
with open(filepath, 'w') as f:
|
||||
f.write(cert_data)
|
||||
print(f"Zertifikat saved.")
|
||||
|
||||
def main():
|
||||
if len(sys.argv) != 2:
|
||||
print("Usage: python export_cert.py <config.yaml>")
|
||||
sys.exit(1)
|
||||
|
||||
### Load Config ###
|
||||
config = load_config(sys.argv[1])
|
||||
|
||||
### check cert options ###
|
||||
if config['export_format'] not in ['crt', 'prv']:
|
||||
print(f"Invalid export format. Possible options are crt or prv.")
|
||||
sys.exit(1)
|
||||
|
||||
### Search Certificates ###
|
||||
certificates = search_certificates(config)
|
||||
if len(certificates) > 1:
|
||||
print(f"Search results in more then one certificate. Please adjust your search to only return a single one.")
|
||||
sys.exit(1)
|
||||
if len(certificates) == 0:
|
||||
print(f"No certificate found with search phrase: {config['certificate_search']}")
|
||||
sys.exit(1)
|
||||
|
||||
certificate_uuid = certificates[0]['uuid']
|
||||
|
||||
### Get Certificate by uuid ###
|
||||
certificate_data = export_certificate(config, certificate_uuid)
|
||||
save_certificate(certificate_data, 'cert.crt', config['output_directory'])
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
6
requirements.txt
Normal file
6
requirements.txt
Normal file
@@ -0,0 +1,6 @@
|
||||
certifi==2025.4.26
|
||||
charset-normalizer==3.4.2
|
||||
idna==3.10
|
||||
PyYAML==6.0.2
|
||||
requests==2.32.3
|
||||
urllib3==2.4.0
|
||||
Reference in New Issue
Block a user