示例#1
0
def add_policy(bundle):
    request_inputs = anchore_engine.services.common.do_request_prep(
        request, default_params={})
    user_auth = request_inputs['auth']
    bodycontent = request_inputs['bodycontent']
    params = request_inputs['params']

    return_object = []
    httpcode = 500
    userId, pw = user_auth

    try:
        logger.debug('Adding policy')

        jsondata = json.loads(bodycontent)

        # schema check
        try:
            import anchore.anchore_policy
            rc = anchore.anchore_policy.verify_policy_bundle(bundle=jsondata)
            if not rc:
                raise Exception(
                    "input bundle does not conform to anchore bundle schema")
        except Exception as err:
            raise Exception(
                "cannot run bundle schema verification - exception: " +
                str(err))

        if 'id' in jsondata and jsondata['id']:
            policyId = jsondata['id']
        else:
            policyId = hashlib.md5(str(userId + ":" +
                                       jsondata['name'])).hexdigest()
            jsondata['id'] = policyId

        try:
            policybundle = jsondata
            policy_record = catalog.add_policy(user_auth, policybundle)
        except Exception as err:
            raise Exception(
                "cannot store policy data to catalog - exception: " + str(err))

        if policy_record:
            return_object = make_response_policy(user_auth, policy_record,
                                                 params)
            httpcode = 200
        else:
            raise Exception('failed to add policy to catalog DB')
    except Exception as err:
        logger.debug("operation exception: " + str(err))
        return_object = anchore_engine.services.common.make_response_error(
            err, in_httpcode=httpcode)
        httpcode = return_object['httpcode']

    return (return_object, httpcode)
示例#2
0
def add_policy(bundle):
    request_inputs = anchore_engine.services.common.do_request_prep(request, default_params={})
    user_auth = request_inputs['auth']
    bodycontent = request_inputs['bodycontent']
    params = request_inputs['params']

    return_object = []
    httpcode = 500
    userId, pw = user_auth

    try:
        logger.debug('Adding policy')

        jsondata = json.loads(bodycontent)

        # schema check
        try:
            p_client = policy_engine.get_client(user=user_auth[0], password=user_auth[1])
            response = p_client.validate_bundle(policy_bundle=jsondata)

            if not response.valid:
                raise Exception('Bundle failed validation. Validation errors: {}'.format([x.to_dict() for x in response.validation_details]))

        except ApiException as err:
            raise Exception('Error response from policy service during bundle validation. Validation could not be performed: {}'.format(err))

        if 'id' in jsondata and jsondata['id']:
            policyId = jsondata['id']
        else:
            policyId = hashlib.md5(str(userId + ":" + jsondata['name'])).hexdigest()
            jsondata['id'] = policyId

        try:
            policybundle = jsondata
            policy_record = catalog.add_policy(user_auth, policybundle)
        except Exception as err:
            raise Exception("cannot store policy data to catalog - exception: " + str(err))

        if policy_record:
            return_object = make_response_policy(user_auth, policy_record, params)
            httpcode = 200
        else:
            raise Exception('failed to add policy to catalog DB')
    except Exception as err:
        logger.debug("operation exception: " + str(err))
        return_object = anchore_engine.services.common.make_response_error(err, in_httpcode=httpcode)
        httpcode = return_object['httpcode']

    return (return_object, httpcode)