コード例 #1
0
ファイル: policies.py プロジェクト: bjwschaap/anchore-engine
def get_policy(policyId):
    """
    GET /policies/{policyId}

    :param policyId:
    :return:
    """
    try:
        request_inputs = anchore_engine.services.common.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.services.common.make_anchore_exception(
                    err,
                    input_message="failed to fetch policy bundle from archive",
                    input_httpcode=500)
            return record, 200
        else:
            return common.make_anchore_exception(
                Exception(
                    'Policy bundle {} not found in DB'.format(policyId))), 404
    except Exception as err:
        return str(err), 500
コード例 #2
0
ファイル: policies.py プロジェクト: bjwschaap/anchore-engine
def update_policy(policyId, bodycontent):
    """
    PUT /policies/{policyId}

    Updates a policy

    :param user_context:
    :param policyId:
    :param policy_content:
    :return:
    """
    try:
        request_inputs = anchore_engine.services.common.do_request_prep(
            connexion.request, default_params={})
        user_id = request_inputs['userId']
        bundle_policyId = bodycontent.get('policyId')
        active = bodycontent.get('active', False)

        if not bundle_policyId:
            raise Exception(
                "must include 'policyId' in the json payload for this operation"
            )

        if policyId != bundle_policyId:
            raise Exception(
                'Id mismatch between route and bundle content. {} != {}'.
                format(policyId, bundle_policyId))

        policybundle = bodycontent.get('policybundle')

        with db.session_scope() as dbsession:
            record = db_policybundle.get(user_id, policyId, session=dbsession)
            if not record:
                return common.make_anchore_exception(
                    Exception("existing policyId not found to update"), 400)

            return save_policy(user_id, policyId, active, policybundle,
                               dbsession), 200

    except Exception as err:
        return str(err), 500