Hello,
I have a quick Python script to delete the expired certificates. It uses Opalstack's API, see code below:
import os
from datetime import datetime
import requests
# create an API token at https://my.opalstack.com/tokens
OPAL_TOKEN = os.environ.get("OPAL_TOKEN")
OPAL_HEADERS = {
'Authorization': f'Token {OPAL_TOKEN}',
'Content-Type':'application/json'
}
# Obtain all certificates
certs = requests.get('https://my.opalstack.com/api/v1/cert/list/', headers=OPAL_HEADERS).json()
now = datetime.utcnow()
# For each cert, check if it is experided, and if so delete it
for cert in certs:
raw_date = cert["exp_date"]
date = datetime.strptime(raw_date, "%Y-%m-%dT%H:%M:%SZ")
if date < now:
print("EXPIRED", cert["name"], raw_date, date)
response= requests.post('https://my.opalstack.com/api/v1/cert/delete/',json=[{"id": cert["id"]}],
headers=OPAL_HEADERS)
print(response.json())
In order to use it (you need Python installed):
- First save the script in your local computer or in the server, and save it, for example, as
delete_certs.py
.
- Create a virtual environment, activate and install requests:
python -m venv venv
source venv/bin/activate
pip install requests
- Once you have an Opalstack's API token, you can create one at https://my.opalstack.com/tokens/, you can execute it the script with:
OPAL_TOKEN=[yourtoken] python delete_certs.py
- On future executions, skip virtualenv creation, but don't forget to activate it:
source venv/bin/activate
I hope this helps you clean up your certificate page!