def list_policies(active=None): """ GET /policies?active=true|false :return: """ # set up the filter based on input try: request_inputs = anchore_engine.apis.do_request_prep(connexion.request, default_params={}) user_id = request_inputs['userId'] with db.session_scope() as dbsession: if active is not None: records = db_policybundle.get_byfilter(user_id, session=dbsession, active=active) else: records = db_policybundle.get_byfilter(user_id, session=dbsession) if records: for record in records: record['policybundle'] = {} try: policybundle = archive.get_document( user_id, 'policy_bundles', record['policyId']) if policybundle: record['policybundle'] = policybundle record['policybundlemeta'] = {} meta = archive.get_document_meta( user_id, 'policy_bundles', record['policyId']) if meta: record['policybundlemeta'] = meta except Exception as err: logger.warn( "failed to fetch policy bundle from archive - exception: " + str(err)) raise anchore_engine.common.helpers.make_anchore_exception( err, input_message= "failed to fetch policy bundle from archive", input_httpcode=500) else: records = [] return records, 200 except Exception as err: logger.exception('Uncaught exception') return str(err), 500
def get_policy(policyId): """ GET /policies/{policyId} :param policyId: :return: """ try: request_inputs = anchore_engine.apis.do_request_prep(connexion.request, default_params={}) user_id = request_inputs['userId'] with db.session_scope() as dbsession: record = db_policybundle.get(user_id, policyId=policyId, session=dbsession) if record: record['policybundle'] = {} try: policybundle = archive.get_document(user_id, 'policy_bundles', record['policyId']) if policybundle: record['policybundle'] = policybundle record['policybundlemeta'] = {} meta = archive.get_document_meta(user_id, 'policy_bundles', record['policyId']) if meta: record['policybundlemeta'] = meta except Exception as err: logger.warn( "failed to fetch policy bundle from archive - exception: " + str(err)) raise anchore_engine.common.helpers.make_anchore_exception( err, input_message="failed to fetch policy bundle from archive", input_httpcode=500) return record, 200 else: return anchore_engine.common.helpers.make_response_error( 'Policy bundle {} not found in DB'.format(policyId), in_httpcode=404), 404 except Exception as err: logger.exception('Uncaught exception') return str(err), 500