Using Freenas API to delete old certificates

Armitage

Cadet
Joined
Feb 2, 2020
Messages
5
I'm using Let's Encrypt to generate certificates for me Freenas instance according to these instructions: https://www.ixsystems.com/community/resources/lets-encrypt-with-freenas-11-1-and-later.82/ .
It works well, i had some problems in the beginning but i manages to learn a bit more and now it works without my involvement. One thing i've noticed it that the old certificates are not removed but they are still there in Freenas when i check System/Certificates after new have been generated. I asked on reddit if there was a good way to remove older certificates if there were more than one, and another user shared a script with me that he used to use. I tried it but i can not get it to work.
Code:
#!/usr/bin/env python

import requests, json, time, sys

password='password'
host='hostname'

login = ('root', password)
headers = {'Content-Type': "application/json"}
url = 'https://'+host+'/api/v1.0/system/certificate/'

result = requests.get(
    url,
    headers=headers,
    auth=login
)

if not result.json():
    sys.exit()

for record in result.json()[:-1]:
    print ('deleting id: %(id)2i, name: %(cert_common)s, valid until: %(cert_until)s' % record)
    result = requests.delete(
        url + str(record['id']) + '/',
        headers=headers,
        auth=login
    )
    if result.status_code != 204:
        print ('error: ' + str(result.status_code))
        break

print ('keeping  id: %(id)2i, name: %(cert_common)s, valid until: %(cert_until)s' % result.json()[-1])

#for cert in ids_to_delete:
#    print (str(cert['id']) + ': ' + cert['cert_until'])


I filled in my details in hostname and password, but it did not work for me, but instead gives me this message.

Code:
root@freenas[~/scripts]# ./clean-certs.py
Traceback (most recent call last):
  File "./clean-certs.py", line 18, in <module>
    if not result.json():
  File "/usr/local/lib/python3.7/site-packages/requests/models.py", line 897, in json
    return complexjson.loads(self.text, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/simplejson/__init__.py", line 538, in loads
    return _default_decoder.decode(s)
  File "/usr/local/lib/python3.7/site-packages/simplejson/decoder.py", line 370, in decode
    obj, end = self.raw_decode(s)
  File "/usr/local/lib/python3.7/site-packages/simplejson/decoder.py", line 400, in raw_decode
    return self.scan_once(s, idx=_w(s, idx).end())
simplejson.errors.JSONDecodeError: Expecting value: line 1 column 1 (char 0)


I do not understand the error message and i do not know what to change to get it working. Does anyone have any good ideas on how to get this working?

Also, when i try to check the API documentations inside my own Freenas instance from the Settings button in the top right then i get a mostly blank page that says "404: Not Found". The adress is https://HOSTNAME/api/docs/ for me.

Thanks in beforehand if anyone has time to help me with this.
 

Armitage

Cadet
Joined
Feb 2, 2020
Messages
5
I solved it some way by replacing the hostname with my local ip adress, it pretty much works now.
 
Top