def query_common_name(common_name, args):
    """
    Helper function that queries for not expired certificates by common name,
    owner and san. Pagination is supported.

    :param common_name:
    :param args:
    :return:
    """
    owner = args.pop("owner")
    san = args.pop("san")
    page = args.pop("page")
    count = args.pop("count")

    paginate = page and count
    query = database.session_query(Certificate) if paginate else Certificate.query

    # only not expired certificates
    current_time = arrow.utcnow()
    query = query.filter(Certificate.not_after >= current_time.format("YYYY-MM-DD"))\
        .filter(not_(Certificate.revoked))\
        .filter(not_(Certificate.replaced.any()))  # ignore rotated certificates to avoid duplicates

    if owner:
        query = query.filter(Certificate.owner.ilike(owner))

    if common_name != "%":
        # if common_name is a wildcard ('%'), no need to include it in the query
        query = query.filter(Certificate.cn.ilike(common_name))

    if san and san != "%":
        # if san is a wildcard ('%'), no need to include it in the query
        query = query.filter(Certificate.id.in_(like_domain_query(san)))

    if paginate:
        args = {"page": page, "count": count, "sort_by": "id", "sort_dir": "desc"}
        return database.sort_and_page(query, Certificate, args)

    return query.all()

    Helper function that queries for not expired certificates by common name,
    owner and san. Pagination is supported.

    :param common_name:
    :param args:
    :return:
    
lemur/certificates/service.py