Exemple #1
0
def render(args):
    """
    Helper that helps us render the REST Api responses.
    :param args:
    :return:
    """
    query = database.session_query(Authority)
    filt = args.pop('filter')

    if filt:
        terms = filt.split(';')
        if 'active' in filt:
            query = query.filter(Authority.active == truthiness(terms[1]))
        elif 'cn' in filt:
            query = query.join(Authority.active == truthiness(terms[1]))
        else:
            query = database.filter(query, Authority, terms)

    # we make sure that a user can only use an authority they either own are a member of - admins can see all
    if not args['user'].is_admin:
        authority_ids = []
        for authority in args['user'].authorities:
            authority_ids.append(authority.id)

        for role in args['user'].roles:
            for authority in role.authorities:
                authority_ids.append(authority.id)
        query = query.filter(Authority.id.in_(authority_ids))

    return database.sort_and_page(query, Authority, args)
Exemple #2
0
def render(args):
    """
    Helper that helps us render the REST Api responses.
    :param args:
    :return:
    """
    query = database.session_query(Authority)
    filt = args.pop("filter")

    if filt:
        terms = filt.split(";")
        if "active" in filt:
            query = query.filter(Authority.active == truthiness(terms[1]))
        elif "cn" in filt:
            term = "%{0}%".format(terms[1])
            sub_query = (database.session_query(
                Certificate.root_authority_id).filter(
                    Certificate.cn.ilike(term)).subquery())

            query = query.filter(Authority.id.in_(sub_query))
        else:
            query = database.filter(query, Authority, terms)

    # we make sure that a user can only use an authority they either own are a member of - admins can see all
    if not args["user"].is_admin:
        authority_ids = []
        for authority in args["user"].authorities:
            authority_ids.append(authority.id)

        for role in args["user"].roles:
            for authority in role.authorities:
                authority_ids.append(authority.id)
        query = query.filter(Authority.id.in_(authority_ids))

    return database.sort_and_page(query, Authority, args)
Exemple #3
0
def render(args):
    """
    Helper that helps us render the REST Api responses.
    :param args:
    :return:
    """
    query = database.session_query(Endpoint)\
        .options(joinedload(Endpoint.certificate))\
        .options(joinedload(Endpoint.source))
    filt = args.pop("filter")

    if filt:
        terms = filt.split(";")
        term = "%{0}%".format(terms[1])
        if "active" in filt:  # this is really weird but strcmp seems to not work here??
            query = query.filter(Endpoint.active == truthiness(terms[1]))
        elif "port" in filt:
            if terms[1] != "null":  # ng-table adds 'null' if a number is removed
                query = query.filter(Endpoint.port == terms[1])
        elif "ciphers" in filt:
            query = query.filter(Cipher.name == terms[1])
        elif "source" in filt:
            query = query.filter(Endpoint.source.has(Source.label.like(term.lower())))
        else:
            query = database.filter(query, Endpoint, terms)

        if terms[0] == "name":
            alias_query = Endpoint.query.filter(
                Endpoint.aliases.any(EndpointDnsAlias.alias.ilike(f"%{terms[1]}%")))
            query = query.union(alias_query)

    return database.sort_and_page(query, Endpoint, args)
Exemple #4
0
def render(args):
    filt = args.pop('filter')
    certificate_id = args.pop('certificate_id', None)

    if certificate_id:
        query = database.session_query(Notification).join(
            Certificate, Notification.certificate)
        query = query.filter(Certificate.id == certificate_id)
    else:
        query = database.session_query(Notification)

    if filt:
        terms = filt.split(';')
        if terms[0] == 'active':
            query = query.filter(Notification.active == truthiness(terms[1]))
        else:
            query = database.filter(query, Notification, terms)

    return database.sort_and_page(query, Notification, args)
Exemple #5
0
def render(args):
    """
    Helper that helps us render the REST Api responses.
    :param args:
    :return:
    """
    query = database.session_query(Endpoint)
    filt = args.pop('filter')

    if filt:
        terms = filt.split(';')
        if 'active' in filt:  # this is really weird but strcmp seems to not work here??
            query = query.filter(Endpoint.active == truthiness(terms[1]))
        elif 'port' in filt:
            if terms[1] != 'null':  # ng-table adds 'null' if a number is removed
                query = query.filter(Endpoint.port == terms[1])
        elif 'ciphers' in filt:
            query = query.filter(Cipher.name == terms[1])
        else:
            query = database.filter(query, Endpoint, terms)

    return database.sort_and_page(query, Endpoint, args)
Exemple #6
0
def render(args):
    """
    Helper that helps us render the REST Api responses.
    :param args:
    :return:
    """
    query = database.session_query(Endpoint)
    filt = args.pop('filter')

    if filt:
        terms = filt.split(';')
        if 'active' in filt:  # this is really weird but strcmp seems to not work here??
            query = query.filter(Endpoint.active == truthiness(terms[1]))
        elif 'port' in filt:
            if terms[1] != 'null':  # ng-table adds 'null' if a number is removed
                query = query.filter(Endpoint.port == terms[1])
        elif 'ciphers' in filt:
            query = query.filter(
                Cipher.name == terms[1]
            )
        else:
            query = database.filter(query, Endpoint, terms)

    return database.sort_and_page(query, Endpoint, args)
Exemple #7
0
def render(args):
    """
    Helper function that allows use to render our REST Api.

    :param args:
    :return:
    """
    query = database.session_query(Certificate)

    show_expired = args.pop("showExpired")
    if show_expired != 1:
        one_month_old = (
            arrow.now()
            .shift(months=current_app.config.get("HIDE_EXPIRED_CERTS_AFTER_MONTHS", -1))
            .format("YYYY-MM-DD")
        )
        query = query.filter(Certificate.not_after > one_month_old)

    time_range = args.pop("time_range")

    destination_id = args.pop("destination_id")
    notification_id = args.pop("notification_id", None)
    serial_number = args.pop("serial", None)
    show = args.pop("show")
    # owner = args.pop('owner')
    # creator = args.pop('creator')  # TODO we should enabling filtering by owner

    filt = args.pop("filter")

    if filt:
        terms = filt.split(";")
        term = "%{0}%".format(terms[1])
        # Exact matches for quotes. Only applies to name, issuer, and cn
        if terms[1].startswith('"') and terms[1].endswith('"'):
            term = terms[1][1:-1]

        if "issuer" in terms:
            # we can't rely on issuer being correct in the cert directly so we combine queries
            sub_query = (
                database.session_query(Authority.id)
                .filter(Authority.name.ilike(term))
                .subquery()
            )

            query = query.filter(
                or_(
                    Certificate.issuer.ilike(term),
                    Certificate.authority_id.in_(sub_query),
                )
            )

        elif "destination" in terms:
            query = query.filter(
                Certificate.destinations.any(Destination.id == terms[1])
            )
        elif "notify" in filt:
            query = query.filter(Certificate.notify == truthiness(terms[1]))
        elif "rotation" in filt:
            query = query.filter(Certificate.rotation == truthiness(terms[1]))
        elif "active" in filt:
            query = query.filter(Certificate.active == truthiness(terms[1]))
        elif "cn" in terms:
            query = query.filter(
                or_(
                    func.lower(Certificate.cn).like(term.lower()),
                    Certificate.id.in_(like_domain_query(term)),
                )
            )
        elif "id" in terms:
            query = query.filter(Certificate.id == cast(terms[1], Integer))
        elif "name" in terms:
            query = query.filter(
                or_(
                    func.lower(Certificate.name).like(term.lower()),
                    Certificate.id.in_(like_domain_query(term)),
                    func.lower(Certificate.cn).like(term.lower()),
                )
            )
        elif "fixedName" in terms:
            # only what matches the fixed name directly if a fixedname is provided
            query = query.filter(Certificate.name == terms[1])
        else:
            query = database.filter(query, Certificate, terms)

    if show:
        sub_query = (
            database.session_query(Role.name)
            .filter(Role.user_id == args["user"].id)
            .subquery()
        )
        query = query.filter(
            or_(
                Certificate.user_id == args["user"].id, Certificate.owner.in_(sub_query)
            )
        )

    if destination_id:
        query = query.filter(
            Certificate.destinations.any(Destination.id == destination_id)
        )

    if notification_id:
        query = query.filter(
            Certificate.notifications.any(Notification.id == notification_id)
        )

    if time_range:
        to = arrow.now().shift(weeks=+time_range).format("YYYY-MM-DD")
        now = arrow.now().format("YYYY-MM-DD")
        query = query.filter(Certificate.not_after <= to).filter(
            Certificate.not_after >= now
        )

    if current_app.config.get("ALLOW_CERT_DELETION", False):
        query = query.filter(Certificate.deleted == false())

    if serial_number:
        if serial_number.lower().startswith('0x'):
            serial_number = str(int(serial_number[2:], 16))
        elif ":" in serial_number:
            serial_number = str(int(serial_number.replace(':', ''), 16))

        query = query.filter(Certificate.serial == serial_number)

    result = database.sort_and_page(query, Certificate, args)
    return result
Exemple #8
0
def render(args):
    query = database.session_query(PendingCertificate)
    time_range = args.pop("time_range")
    destination_id = args.pop("destination_id")
    notification_id = args.pop("notification_id", None)
    show = args.pop("show")
    # owner = args.pop('owner')
    # creator = args.pop('creator')  # TODO we should enabling filtering by owner

    filt = args.pop("filter")

    if filt:
        terms = filt.split(";")

        if "issuer" in terms:
            # we can't rely on issuer being correct in the cert directly so we combine queries
            sub_query = (database.session_query(Authority.id).filter(
                Authority.name.ilike("%{0}%".format(terms[1]))).subquery())

            query = query.filter(
                or_(
                    PendingCertificate.issuer.ilike("%{0}%".format(terms[1])),
                    PendingCertificate.authority_id.in_(sub_query),
                ))

        elif "destination" in terms:
            query = query.filter(
                PendingCertificate.destinations.any(
                    Destination.id == terms[1]))
        elif "notify" in filt:
            query = query.filter(
                PendingCertificate.notify == truthiness(terms[1]))
        elif "active" in filt:
            query = query.filter(
                PendingCertificate.active == truthiness(terms[1]))
        elif "cn" in terms:
            query = query.filter(
                or_(
                    PendingCertificate.cn.ilike("%{0}%".format(terms[1])),
                    PendingCertificate.domains.any(
                        Domain.name.ilike("%{0}%".format(terms[1]))),
                ))
        elif "id" in terms:
            query = query.filter(
                PendingCertificate.id == cast(terms[1], Integer))
        else:
            query = database.filter(query, PendingCertificate, terms)

    if show:
        sub_query = (database.session_query(
            Role.name).filter(Role.user_id == args["user"].id).subquery())
        query = query.filter(
            or_(
                PendingCertificate.user_id == args["user"].id,
                PendingCertificate.owner.in_(sub_query),
            ))

    if destination_id:
        query = query.filter(
            PendingCertificate.destinations.any(
                Destination.id == destination_id))

    if notification_id:
        query = query.filter(
            PendingCertificate.notifications.any(
                Notification.id == notification_id))

    if time_range:
        to = arrow.now().shift(weeks=+time_range).format("YYYY-MM-DD")
        now = arrow.now().format("YYYY-MM-DD")
        query = query.filter(PendingCertificate.not_after <= to).filter(
            PendingCertificate.not_after >= now)

    # Only show unresolved certificates in the UI
    query = query.filter(PendingCertificate.resolved.is_(False))
    return database.sort_and_page(query, PendingCertificate, args)
Exemple #9
0
def render(args):
    """
    Helper function that allows use to render our REST Api.

    :param args:
    :return:
    """
    query = database.session_query(Certificate)

    time_range = args.pop('time_range')
    destination_id = args.pop('destination_id')
    notification_id = args.pop('notification_id', None)
    show = args.pop('show')
    # owner = args.pop('owner')
    # creator = args.pop('creator')  # TODO we should enabling filtering by owner

    filt = args.pop('filter')

    if filt:
        terms = filt.split(';')
        term = '%{0}%'.format(terms[1])
        # Exact matches for quotes. Only applies to name, issuer, and cn
        if terms[1].startswith('"') and terms[1].endswith('"'):
            term = terms[1][1:-1]

        if 'issuer' in terms:
            # we can't rely on issuer being correct in the cert directly so we combine queries
            sub_query = database.session_query(Authority.id) \
                .filter(Authority.name.ilike(term)) \
                .subquery()

            query = query.filter(
                or_(Certificate.issuer.ilike(term),
                    Certificate.authority_id.in_(sub_query)))

        elif 'destination' in terms:
            query = query.filter(
                Certificate.destinations.any(Destination.id == terms[1]))
        elif 'notify' in filt:
            query = query.filter(Certificate.notify == truthiness(terms[1]))
        elif 'active' in filt:
            query = query.filter(Certificate.active == truthiness(terms[1]))
        elif 'cn' in terms:
            query = query.filter(
                or_(Certificate.cn.ilike(term),
                    Certificate.domains.any(Domain.name.ilike(term))))
        elif 'id' in terms:
            query = query.filter(Certificate.id == cast(terms[1], Integer))
        elif 'name' in terms:
            query = query.outerjoin(certificate_associations).outerjoin(
                Domain).filter(
                    or_(
                        Certificate.name.ilike(term),
                        Domain.name.ilike(term),
                        Certificate.cn.ilike(term),
                    )).group_by(Certificate.id)
        else:
            query = database.filter(query, Certificate, terms)

    if show:
        sub_query = database.session_query(
            Role.name).filter(Role.user_id == args['user'].id).subquery()
        query = query.filter(
            or_(Certificate.user_id == args['user'].id,
                Certificate.owner.in_(sub_query)))

    if destination_id:
        query = query.filter(
            Certificate.destinations.any(Destination.id == destination_id))

    if notification_id:
        query = query.filter(
            Certificate.notifications.any(Notification.id == notification_id))

    if time_range:
        to = arrow.now().replace(weeks=+time_range).format('YYYY-MM-DD')
        now = arrow.now().format('YYYY-MM-DD')
        query = query.filter(Certificate.not_after <= to).filter(
            Certificate.not_after >= now)

    result = database.sort_and_page(query, Certificate, args)
    return result
Exemple #10
0
def render(args):
    """
    Helper function that allows use to render our REST Api.

    :param args:
    :return:
    """
    query = database.session_query(Certificate)

    time_range = args.pop("time_range")
    destination_id = args.pop("destination_id")
    notification_id = args.pop("notification_id", None)
    show = args.pop("show")
    # owner = args.pop('owner')
    # creator = args.pop('creator')  # TODO we should enabling filtering by owner

    filt = args.pop("filter")

    if filt:
        terms = filt.split(";")
        term = "%{0}%".format(terms[1])
        # Exact matches for quotes. Only applies to name, issuer, and cn
        if terms[1].startswith('"') and terms[1].endswith('"'):
            term = terms[1][1:-1]

        if "issuer" in terms:
            # we can't rely on issuer being correct in the cert directly so we combine queries
            sub_query = (database.session_query(Authority.id).filter(
                Authority.name.ilike(term)).subquery())

            query = query.filter(
                or_(
                    Certificate.issuer.ilike(term),
                    Certificate.authority_id.in_(sub_query),
                ))

        elif "destination" in terms:
            query = query.filter(
                Certificate.destinations.any(Destination.id == terms[1]))
        elif "notify" in filt:
            query = query.filter(Certificate.notify == truthiness(terms[1]))
        elif "active" in filt:
            query = query.filter(Certificate.active == truthiness(terms[1]))
        elif "cn" in terms:
            query = query.filter(
                or_(
                    Certificate.cn.ilike(term),
                    Certificate.domains.any(Domain.name.ilike(term)),
                ))
        elif "id" in terms:
            query = query.filter(Certificate.id == cast(terms[1], Integer))
        elif "name" in terms:
            query = query.filter(
                or_(
                    Certificate.name.ilike(term),
                    Certificate.domains.any(Domain.name.ilike(term)),
                    Certificate.cn.ilike(term),
                ))
        else:
            query = database.filter(query, Certificate, terms)

    if show:
        sub_query = (database.session_query(
            Role.name).filter(Role.user_id == args["user"].id).subquery())
        query = query.filter(
            or_(Certificate.user_id == args["user"].id,
                Certificate.owner.in_(sub_query)))

    if destination_id:
        query = query.filter(
            Certificate.destinations.any(Destination.id == destination_id))

    if notification_id:
        query = query.filter(
            Certificate.notifications.any(Notification.id == notification_id))

    if time_range:
        to = arrow.now().replace(weeks=+time_range).format("YYYY-MM-DD")
        now = arrow.now().format("YYYY-MM-DD")
        query = query.filter(Certificate.not_after <= to).filter(
            Certificate.not_after >= now)

    if current_app.config.get("ALLOW_CERT_DELETION", False):
        query = query.filter(Certificate.deleted == False)  # noqa

    result = database.sort_and_page(query, Certificate, args)
    return result
Exemple #11
0
def render(args):
    query = database.session_query(PendingCertificate)
    time_range = args.pop('time_range')
    destination_id = args.pop('destination_id')
    notification_id = args.pop('notification_id', None)
    show = args.pop('show')
    # owner = args.pop('owner')
    # creator = args.pop('creator')  # TODO we should enabling filtering by owner

    filt = args.pop('filter')

    if filt:
        terms = filt.split(';')

        if 'issuer' in terms:
            # we can't rely on issuer being correct in the cert directly so we combine queries
            sub_query = database.session_query(Authority.id) \
                .filter(Authority.name.ilike('%{0}%'.format(terms[1]))) \
                .subquery()

            query = query.filter(
                or_(PendingCertificate.issuer.ilike('%{0}%'.format(terms[1])),
                    PendingCertificate.authority_id.in_(sub_query)))

        elif 'destination' in terms:
            query = query.filter(
                PendingCertificate.destinations.any(
                    Destination.id == terms[1]))
        elif 'notify' in filt:
            query = query.filter(
                PendingCertificate.notify == truthiness(terms[1]))
        elif 'active' in filt:
            query = query.filter(
                PendingCertificate.active == truthiness(terms[1]))
        elif 'cn' in terms:
            query = query.filter(
                or_(
                    PendingCertificate.cn.ilike('%{0}%'.format(terms[1])),
                    PendingCertificate.domains.any(
                        Domain.name.ilike('%{0}%'.format(terms[1])))))
        elif 'id' in terms:
            query = query.filter(
                PendingCertificate.id == cast(terms[1], Integer))
        else:
            query = database.filter(query, PendingCertificate, terms)

    if show:
        sub_query = database.session_query(
            Role.name).filter(Role.user_id == args['user'].id).subquery()
        query = query.filter(
            or_(PendingCertificate.user_id == args['user'].id,
                PendingCertificate.owner.in_(sub_query)))

    if destination_id:
        query = query.filter(
            PendingCertificate.destinations.any(
                Destination.id == destination_id))

    if notification_id:
        query = query.filter(
            PendingCertificate.notifications.any(
                Notification.id == notification_id))

    if time_range:
        to = arrow.now().replace(weeks=+time_range).format('YYYY-MM-DD')
        now = arrow.now().format('YYYY-MM-DD')
        query = query.filter(PendingCertificate.not_after <= to).filter(
            PendingCertificate.not_after >= now)

    # Only show unresolved certificates in the UI
    query = query.filter(PendingCertificate.resolved.is_(False))
    return database.sort_and_page(query, PendingCertificate, args)
Exemple #12
0
def render(args):
    """
    Helper function that allows use to render our REST Api.

    :param args:
    :return:
    """
    query = database.session_query(Certificate)

    time_range = args.pop('time_range')
    destination_id = args.pop('destination_id')
    notification_id = args.pop('notification_id', None)
    show = args.pop('show')
    # owner = args.pop('owner')
    # creator = args.pop('creator')  # TODO we should enabling filtering by owner

    filt = args.pop('filter')

    if filt:
        terms = filt.split(';')
        term = '%{0}%'.format(terms[1])
        # Exact matches for quotes. Only applies to name, issuer, and cn
        if terms[1].startswith('"') and terms[1].endswith('"'):
            term = terms[1][1:-1]

        if 'issuer' in terms:
            # we can't rely on issuer being correct in the cert directly so we combine queries
            sub_query = database.session_query(Authority.id) \
                .filter(Authority.name.ilike(term)) \
                .subquery()

            query = query.filter(
                or_(
                    Certificate.issuer.ilike(term),
                    Certificate.authority_id.in_(sub_query)
                )
            )

        elif 'destination' in terms:
            query = query.filter(Certificate.destinations.any(Destination.id == terms[1]))
        elif 'notify' in filt:
            query = query.filter(Certificate.notify == truthiness(terms[1]))
        elif 'active' in filt:
            query = query.filter(Certificate.active == truthiness(terms[1]))
        elif 'cn' in terms:
            query = query.filter(
                or_(
                    Certificate.cn.ilike(term),
                    Certificate.domains.any(Domain.name.ilike(term))
                )
            )
        elif 'id' in terms:
            query = query.filter(Certificate.id == cast(terms[1], Integer))
        elif 'name' in terms:
            query = query.filter(
                or_(
                    Certificate.name.ilike(term),
                    Certificate.domains.any(Domain.name.ilike(term)),
                    Certificate.cn.ilike(term),
                )
            )
        else:
            query = database.filter(query, Certificate, terms)

    if show:
        sub_query = database.session_query(Role.name).filter(Role.user_id == args['user'].id).subquery()
        query = query.filter(
            or_(
                Certificate.user_id == args['user'].id,
                Certificate.owner.in_(sub_query)
            )
        )

    if destination_id:
        query = query.filter(Certificate.destinations.any(Destination.id == destination_id))

    if notification_id:
        query = query.filter(Certificate.notifications.any(Notification.id == notification_id))

    if time_range:
        to = arrow.now().replace(weeks=+time_range).format('YYYY-MM-DD')
        now = arrow.now().format('YYYY-MM-DD')
        query = query.filter(Certificate.not_after <= to).filter(Certificate.not_after >= now)

    if current_app.config.get('ALLOW_CERT_DELETION', False):
        query = query.filter(Certificate.deleted == False)  # noqa

    result = database.sort_and_page(query, Certificate, args)
    return result
Exemple #13
0
def render(args):
    query = database.session_query(PendingCertificate)
    time_range = args.pop('time_range')
    destination_id = args.pop('destination_id')
    notification_id = args.pop('notification_id', None)
    show = args.pop('show')
    # owner = args.pop('owner')
    # creator = args.pop('creator')  # TODO we should enabling filtering by owner

    filt = args.pop('filter')

    if filt:
        terms = filt.split(';')

        if 'issuer' in terms:
            # we can't rely on issuer being correct in the cert directly so we combine queries
            sub_query = database.session_query(Authority.id) \
                .filter(Authority.name.ilike('%{0}%'.format(terms[1]))) \
                .subquery()

            query = query.filter(
                or_(
                    PendingCertificate.issuer.ilike('%{0}%'.format(terms[1])),
                    PendingCertificate.authority_id.in_(sub_query)
                )
            )

        elif 'destination' in terms:
            query = query.filter(PendingCertificate.destinations.any(Destination.id == terms[1]))
        elif 'notify' in filt:
            query = query.filter(PendingCertificate.notify == truthiness(terms[1]))
        elif 'active' in filt:
            query = query.filter(PendingCertificate.active == truthiness(terms[1]))
        elif 'cn' in terms:
            query = query.filter(
                or_(
                    PendingCertificate.cn.ilike('%{0}%'.format(terms[1])),
                    PendingCertificate.domains.any(Domain.name.ilike('%{0}%'.format(terms[1])))
                )
            )
        elif 'id' in terms:
            query = query.filter(PendingCertificate.id == cast(terms[1], Integer))
        else:
            query = database.filter(query, PendingCertificate, terms)

    if show:
        sub_query = database.session_query(Role.name).filter(Role.user_id == args['user'].id).subquery()
        query = query.filter(
            or_(
                PendingCertificate.user_id == args['user'].id,
                PendingCertificate.owner.in_(sub_query)
            )
        )

    if destination_id:
        query = query.filter(PendingCertificate.destinations.any(Destination.id == destination_id))

    if notification_id:
        query = query.filter(PendingCertificate.notifications.any(Notification.id == notification_id))

    if time_range:
        to = arrow.now().replace(weeks=+time_range).format('YYYY-MM-DD')
        now = arrow.now().format('YYYY-MM-DD')
        query = query.filter(PendingCertificate.not_after <= to).filter(PendingCertificate.not_after >= now)

    # Only show unresolved certificates in the UI
    query = query.filter(PendingCertificate.resolved.is_(False))
    return database.sort_and_page(query, PendingCertificate, args)