1.背景
F5提供了各种配置和查询的restful API,通过这些API可以进行各种查询和配置。本文通过F5 提供的restful API 获取ssl 状态,并且筛选出即将过期的SSL 证书,发送邮件提醒。
2.思路
- 使用F5 本身的API登陆 F5,获取token;
- 利用token获取SSL 证书状态
- 对SSL状态进行筛选
- 发送邮件
3.F5官方文档参考
https://clouddocs.f5.com/api/icontrol-rest/
4.部分代码
4.1F5登陆,查询类
F5Operation 类中方法介绍:
- login() :登陆F5,返回token
- get_metric() :利用token和metric url ,获取相应的值,本次利用到的url是/mgmt/tm/sys/file/ssl-cert
本例中查询除了获取F5上SSL 证书状态外,还需要利用公司系统进行查询,不具有通用性,因此不在此贴出代码。
import requests
import json
import win32com
class F5Operation:
def __init__(self, host, username, password):
self.username = username
self.password = password
self.url = host
def login(self):
auth_url = self.url + "/mgmt/shared/authn/login"
data = {"username": self.username, "password": self.password, "loginProviderName": "tmos"}
header = {"Content-Type": "application/json"}
ret = requests.post(auth_url, data=json.dumps(data), verify=False, headers=header).json()
if ret.get("token")
return ret['token']['token']
else:
print("cannot get token of %s" %self.host)
sys.exit(1)
def get_metric(self, metric_url):
full_url = self.url + metric_url
print(full_url)
token = self.login()
print(token)
header = {"Content-Type": "application/json", "X-F5-Auth-Token": token}
metric_ret = requests.get(full_url, headers=header, verify=False,).json()
return metric_ret
4.2邮件发送类
由于是在windows下编程,邮件发送可以选择win32com 中调用本机的outlook程序发送,也可以利用smtplib 发送邮件。因为smtplib需要设置smtp服务器地址,当前没有公司的smtp服务器地址,因此使用win32com 调用outlook发送邮件
win32com是功能强大的模块,可以调用window下各种程序。
邮件发送类实现如下:
import win32com.client as com
class OutLook:
def __init__(self):
pass
@staticmethod
def send_mail(revc, subject,body, html, display, cc=None,bcc=None):
outlook = com.Dispatch("Outlook.Application")
mail = outlook.CreateItem(0)
mail.To = revc
mail.Subject = subject
if cc:
mail.CC = cc
if bcc:
mail.BCC = bcc
if html:
"""
Set the format of mail
1 - Plain Text
2 - HTML
3 - Rich Text
"""
mail.BodyFormat = 2
mail.body = body
if display:
mail.Display(True)
else:
mail.Send()
有了这两个类后,在主函数中即可实现需求。
5.遇到问题和解决过程
5.1使用url /mgmt/tm/sys/file/system-ssl-cert无法查询到证书状态
根据官方文档(https://clouddocs.f5.com/api/icontrol-rest/APIRef_tm_sys_file_system-ssl-cert.html)和web查看路径,初步认为/mgmt/tm/sys/file/system-ssl-cert 是获取ssl 证书状态的url
多次尝试,无法获取到证书状态,怀疑是权限或方法问题,尝试从/mgmt/tm/ltm/pool获取pool信息,获取成功,说明方法和权限没有问题;
退一步,使用/mgmt/tm/sys/file 获取file中的信息,获得一下信息:
{'kind': 'tm:sys:file:filecollectionstate', 'selfLink': 'https://localhost/mgmt/tm/sys/file?ver=12.1.4.1',
'items': [{'reference': {'link': 'https://localhost/mgmt/tm/sys/file/apache-ssl-cert?ver=12.1.4.1'}},
{'reference': {'link': 'https://localhost/mgmt/tm/sys/file/browser-capabilities-db?ver=12.1.4.1'}},
{'reference': {'link': 'https://localhost/mgmt/tm/sys/file/dashboard-viewset?ver=12.1.4.1'}},
{'reference': {'link': 'https://localhost/mgmt/tm/sys/file/data-group?ver=12.1.4.1'}},
{'reference': {'link': 'https://localhost/mgmt/tm/sys/file/device-capabilities-db?ver=12.1.4.1'}},
{'reference': {'link': 'https://localhost/mgmt/tm/sys/file/external-monitor?ver=12.1.4.1'}},
{'reference': {'link': 'https://localhost/mgmt/tm/sys/file/ifile?ver=12.1.4.1'}},
{'reference': {'link': 'https://localhost/mgmt/tm/sys/file/lwtunneltbl?ver=12.1.4.1'}},
{'reference': {'link': 'https://localhost/mgmt/tm/sys/file/ssl-cert?ver=12.1.4.1'}},
{'reference': {'link': 'https://localhost/mgmt/tm/sys/file/ssl-crl?ver=12.1.4.1'}},
{'reference': {'link': 'https://localhost/mgmt/tm/sys/file/ssl-csr?ver=12.1.4.1'}},
{'reference': {'link': 'https://localhost/mgmt/tm/sys/file/ssl-key?ver=12.1.4.1'}},
{'reference': {'link': 'https://localhost/mgmt/tm/sys/file/system-ssl-cert?ver=12.1.4.1'}},
{'reference': {'link': 'https://localhost/mgmt/tm/sys/file/system-ssl-key?ver=12.1.4.1'}}]}
尝试从/mgmt/tm/sys/file/ssl-cert获取,可以获取到ssl证书列表和状态信息。返回官方文档
https://clouddocs.f5.com/api/icontrol-rest/APIRef_tm_sys_file.html
可见文档中也提供了各种文件中的url。在使用url获取信息,且对该功能不熟悉时,不要盲目,先看官方文档,如果一个url无法获取,及时换另一个。
6.进一步思考
如果需要对多个F5 SSL 证书进行检查。可以利用利用多线进程方式实现。