def query(fqdns, issuer, owner, expired):
    """Prints certificates that match the query params."""
    table = []

    q = database.session_query(Certificate)
    if issuer:
        sub_query = (
            database.session_query(Authority.id)
            .filter(Authority.name.ilike(f"%{issuer}%"))
            .subquery()
        )

        q = q.filter(
            or_(
                Certificate.issuer.ilike(f"%{issuer}%"),
                Certificate.authority_id.in_(sub_query),
            )
        )
    if owner:
        q = q.filter(Certificate.owner.ilike(f"%{owner}%"))

    if not expired:
        q = q.filter(Certificate.expired == False)  # noqa

    if fqdns:
        for f in fqdns.split(","):
            q = q.filter(
                or_(
                    Certificate.cn.ilike(f"%{f}%"),
                    Certificate.domains.any(Domain.name.ilike(f"%{f}%")),
                )
            )

    for c in q.all():
        table.append([c.id, c.name, c.owner, c.issuer])

    click.echo(tabulate(table, headers=["Id", "Name", "Owner", "Issuer"], tablefmt="csv"))
Prints certificates that match the query params.
lemur/certificates/cli.py