From 709a9808aaef5ce5621d25eaff41adc01d6410c6 Mon Sep 17 00:00:00 2001 From: Hossein Shafagh Date: Mon, 26 Oct 2020 18:32:53 -0700 Subject: [PATCH] better structure of the query and and removing ilike --- lemur/certificates/service.py | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/lemur/certificates/service.py b/lemur/certificates/service.py index 1716ccb2..1b026f4d 100644 --- a/lemur/certificates/service.py +++ b/lemur/certificates/service.py @@ -560,18 +560,29 @@ def query_common_name(common_name, args): :return: """ owner = args.pop("owner") - if not owner: - owner = "%" - # only not expired certificates current_time = arrow.utcnow() - result = ( - Certificate.query.filter(Certificate.cn.ilike(common_name)) - .filter(Certificate.owner.ilike(owner)) - .filter(Certificate.not_after >= current_time.format("YYYY-MM-DD")) - .all() - ) + if common_name == "%" and not owner: + result = ( + Certificate.query.filter(Certificate.not_after >= current_time.format("YYYY-MM-DD")) + .all() + ) + elif common_name == "%": + # all valid certs from the owner + result = ( + Certificate.query.filter(Certificate.not_after >= current_time.format("YYYY-MM-DD")) + .filter(Certificate.owner == owner) + .all() + ) + else: + # search based on owner and cn + result = ( + Certificate.query.filter(Certificate.not_after >= current_time.format("YYYY-MM-DD")) + .filter(Certificate.cn.like(common_name)) + .filter(Certificate.owner == owner) + .all() + ) return result