コード例 #1
0
    def test_rollback_removed_mapped_system(self):
        baseline = SystemBaseline(account=account1,
                                  display_name="baseline1",
                                  baseline_facts=baseline_facts)
        db.session.add(baseline)
        test_system_ids = []
        for i in range(4):
            test_system_id = str(uuid.uuid4())
            test_system_ids.append(test_system_id)
            baseline.add_mapped_system(test_system_id)
        db.session.commit()

        query = SystemBaseline.query.filter(SystemBaseline.account == account1)
        self.assertEqual(query.count(), 1)
        result = query.one()
        self.assertEqual(len(result.mapped_system_ids()), 4)
        removed_system_id = test_system_ids.pop(2)
        result.remove_mapped_system(removed_system_id)
        self.assertNotIn(removed_system_id, result.mapped_system_ids())
        db.session.rollback()
        self.assertIn(removed_system_id, result.mapped_system_ids())

        query = SystemBaseline.query.filter(SystemBaseline.account == account1)
        self.assertEqual(query.count(), 1)
        result = query.one()
        self.assertEqual(len(result.mapped_system_ids()), 4)
        for test_system_id in test_system_ids:
            self.assertIn(test_system_id, result.mapped_system_ids())
        self.assertIn(removed_system_id, result.mapped_system_ids())
コード例 #2
0
    def test_add_mapped_system(self):
        baseline = SystemBaseline(account=account1,
                                  display_name="baseline1",
                                  baseline_facts=baseline_facts)
        new_uuid = str(uuid.uuid4())
        baseline.add_mapped_system(new_uuid)
        db.session.add(baseline)
        db.session.commit()

        query = SystemBaseline.query.filter(SystemBaseline.account == account1)
        self.assertEqual(query.count(), 1)
        result = query.one()
        self.assertEqual(len(result.mapped_system_ids()), 1)
        self.assertEqual(result.mapped_system_ids()[0], new_uuid)
コード例 #3
0
    def test_remove_mapped_system_not_in_list(self):
        baseline = SystemBaseline(account=account1,
                                  display_name="baseline1",
                                  baseline_facts=baseline_facts)
        db.session.add(baseline)
        test_system_ids = []
        for i in range(4):
            test_system_id = str(uuid.uuid4())
            test_system_ids.append(test_system_id)
            baseline.add_mapped_system(test_system_id)
        db.session.commit()

        query = SystemBaseline.query.filter(SystemBaseline.account == account1)
        self.assertEqual(query.count(), 1)
        result = query.one()
        self.assertEqual(len(result.mapped_system_ids()), 4)
        with self.assertRaises(ValueError) as context:
            result.remove_mapped_system(str(uuid.uuid4()))
            self.assertTrue(
                "Failed to remove system id" in str(context.exception))
コード例 #4
0
    def test_another_baseline(self):
        baseline = SystemBaseline(account=account1,
                                  display_name="baseline1",
                                  baseline_facts=baseline_facts)
        db.session.add(baseline)
        db.session.commit()

        query = SystemBaseline.query.filter(SystemBaseline.account == account1)
        self.assertEqual(query.count(), 1)
        results = query.all()
        self.assertEqual(results[0].display_name, "baseline1")
        self.assertEqual(results[0].account, account1)
コード例 #5
0
def create_baseline(system_baseline_in):
    """
    create a baseline
    """
    ensure_rbac_write()
    account_number = view_helpers.get_account_number(request)

    if "values" in system_baseline_in and "value" in system_baseline_in:
        message = "'values' and 'value' cannot both be defined for system baseline"
        current_app.logger.audit(message, request=request, success=False)
        raise HTTPError(
            HTTPStatus.BAD_REQUEST,
            message=message,
        )

    _check_for_existing_display_name(system_baseline_in["display_name"],
                                     account_number)
    _check_for_whitespace_in_display_name(system_baseline_in["display_name"])

    message = "counted baselines"
    current_app.logger.audit(message, request=request)

    baseline_facts = []
    if "baseline_facts" in system_baseline_in:
        if "inventory_uuid" in system_baseline_in:
            message = (
                "Both baseline facts and inventory id provided, can clone only one."
            )
            current_app.logger.audit(message, request=request, success=False)
            raise HTTPError(
                HTTPStatus.BAD_REQUEST,
                message=message,
            )
        if "hsp_uuid" in system_baseline_in:
            message = "Both baseline facts and hsp id provided, can clone only one."
            current_app.logger.audit(message, request=request, success=False)
            raise HTTPError(
                HTTPStatus.BAD_REQUEST,
                message=message,
            )
        baseline_facts = system_baseline_in["baseline_facts"]
    elif "hsp_uuid" in system_baseline_in:
        if "inventory_uuid" in system_baseline_in:
            message = "Both hsp id and system id provided, can clone only one."
            current_app.logger.audit(message, request=request, success=False)
            raise HTTPError(
                HTTPStatus.BAD_REQUEST,
                message=message,
            )
        validate_uuids([system_baseline_in["hsp_uuid"]])
        auth_key = get_key_from_headers(request.headers)
        try:
            hsp = fetch_historical_sys_profiles(
                [system_baseline_in["hsp_uuid"]],
                auth_key,
                current_app.logger,
                get_event_counters(),
            )[0]
            message = "read historical system profiles"
            current_app.logger.audit(message, request=request)
        except ItemNotReturned:
            message = "hsp UUID %s not available" % system_baseline_in[
                "hsp_uuid"]
            current_app.logger.audit(message, request=request, success=False)
            raise HTTPError(
                HTTPStatus.NOT_FOUND,
                message=message,
            )
        except RBACDenied as error:
            message = error.message
            current_app.logger.audit(message, request=request, success=False)
            raise HTTPError(HTTPStatus.FORBIDDEN, message=message)

        system_name = "clone_from_hsp_unused"
        baseline_facts = _parse_from_sysprofile(hsp["system_profile"],
                                                system_name,
                                                current_app.logger)
    elif "inventory_uuid" in system_baseline_in:
        validate_uuids([system_baseline_in["inventory_uuid"]])
        auth_key = get_key_from_headers(request.headers)
        try:
            system_with_profile = fetch_systems_with_profiles(
                [system_baseline_in["inventory_uuid"]],
                auth_key,
                current_app.logger,
                get_event_counters(),
            )[0]
            message = "read system with profiles"
            current_app.logger.audit(message, request=request)
        except ItemNotReturned:
            message = ("inventory UUID %s not available" %
                       system_baseline_in["inventory_uuid"])
            current_app.logger.audit(message, request=request, success=False)
            raise HTTPError(
                HTTPStatus.NOT_FOUND,
                message=message,
            )
        except RBACDenied as error:
            message = error.message
            current_app.logger.audit(message, request=request, success=False)
            raise HTTPError(HTTPStatus.FORBIDDEN, message=message)

        system_name = profile_parser.get_name(system_with_profile)
        baseline_facts = _parse_from_sysprofile(
            system_with_profile["system_profile"], system_name,
            current_app.logger)

    try:
        _validate_facts(baseline_facts)
    except FactValidationError as error:
        message = error.message
        current_app.logger.audit(message, request=request, success=False)
        raise HTTPError(HTTPStatus.BAD_REQUEST, message=message)

    baseline = SystemBaseline(
        account=account_number,
        display_name=system_baseline_in["display_name"],
        baseline_facts=baseline_facts,
    )
    baseline.baseline_facts = _sort_baseline_facts(baseline.baseline_facts)
    db.session.add(baseline)

    db.session.commit(
    )  # commit now so we get a created/updated time before json conversion

    message = "creat baselines"
    current_app.logger.audit(message, request=request)

    return baseline.to_json()
コード例 #6
0
 def populate_db_with_stuff(self):
     rows = [
         SystemBaseline(
             account=account1,
             display_name="baseline1",
             baseline_facts=baseline_facts,
             mapped_systems=[
                 SystemBaselineMappedSystem(
                     account=account1,
                     system_id=system_id1,
                 ),
                 SystemBaselineMappedSystem(
                     account=account1,
                     system_id=system_id2,
                 ),
                 SystemBaselineMappedSystem(
                     account=account1,
                     system_id=system_id3,
                 ),
             ],
         ),
         SystemBaseline(
             account=account1,
             display_name="baseline2",
             baseline_facts=baseline_facts,
             mapped_systems=[
                 SystemBaselineMappedSystem(
                     account=account1,
                     system_id=system_id4,
                 ),
                 SystemBaselineMappedSystem(
                     account=account1,
                     system_id=system_id5,
                 ),
             ],
         ),
         SystemBaseline(
             account=account2,
             display_name="baseline3",
             baseline_facts=baseline_facts,
             mapped_systems=[
                 SystemBaselineMappedSystem(
                     account=account2,
                     system_id=system_id6,
                 ),
                 SystemBaselineMappedSystem(
                     account=account2,
                     system_id=system_id7,
                 ),
             ],
         ),
         SystemBaseline(
             account=account2,
             display_name="baseline4",
             baseline_facts=baseline_facts,
             mapped_systems=[
                 SystemBaselineMappedSystem(
                     account=account2,
                     system_id=system_id6,
                 ),
                 SystemBaselineMappedSystem(
                     account=account2,
                     system_id=system_id8,
                 ),
                 SystemBaselineMappedSystem(
                     account=account2,
                     system_id=system_id9,
                 ),
             ],
         ),
     ]
     db.session.add_all(rows)
     db.session.commit()
コード例 #7
0
def create_baseline(system_baseline_in):
    """
    create a baseline
    """
    account_number = view_helpers.get_account_number(request)

    if "values" in system_baseline_in and "value" in system_baseline_in:
        raise HTTPError(
            HTTPStatus.BAD_REQUEST,
            message=
            "'values' and 'value' cannot both be defined for system baseline",
        )

    query = SystemBaseline.query.filter(
        SystemBaseline.account == account_number,
        SystemBaseline.display_name == system_baseline_in["display_name"],
    )

    if query.count() > 0:
        raise HTTPError(
            HTTPStatus.BAD_REQUEST,
            message="display_name '%s' already used for this account" %
            system_baseline_in["display_name"],
        )

    baseline_facts = []
    if "baseline_facts" in system_baseline_in:
        baseline_facts = system_baseline_in["baseline_facts"]
    elif "inventory_uuid" in system_baseline_in:
        auth_key = get_key_from_headers(request.headers)
        system_with_profile = fetch_systems_with_profiles(
            [system_baseline_in["inventory_uuid"]],
            auth_key,
            current_app.logger,
            get_event_counters(),
        )[0]

        system_name = profile_parser.get_name(system_with_profile)
        parsed_profile = profile_parser.parse_profile(
            system_with_profile["system_profile"], system_name,
            current_app.logger)
        facts = []
        for fact in parsed_profile:
            if fact not in ["id", "name"] and parsed_profile[fact] not in [
                    "N/A",
                    "None",
                    None,
            ]:
                facts.append({"name": fact, "value": parsed_profile[fact]})

        baseline_facts = group_baselines(facts)

    baseline = SystemBaseline(
        account=account_number,
        display_name=system_baseline_in["display_name"],
        baseline_facts=baseline_facts,
    )
    db.session.add(baseline)
    db.session.commit(
    )  # commit now so we get a created/updated time before json conversion

    return baseline.to_json()
コード例 #8
0
def create_baseline(system_baseline_in):
    """
    create a baseline
    """
    account_number = view_helpers.get_account_number(request)

    if "values" in system_baseline_in and "value" in system_baseline_in:
        raise HTTPError(
            HTTPStatus.BAD_REQUEST,
            message=
            "'values' and 'value' cannot both be defined for system baseline",
        )

    _check_for_existing_display_name(system_baseline_in["display_name"],
                                     account_number)

    baseline_facts = []
    if "baseline_facts" in system_baseline_in:
        baseline_facts = system_baseline_in["baseline_facts"]
    elif "inventory_uuid" in system_baseline_in:
        _validate_uuids([system_baseline_in["inventory_uuid"]])
        auth_key = get_key_from_headers(request.headers)
        try:
            system_with_profile = fetch_systems_with_profiles(
                [system_baseline_in["inventory_uuid"]],
                auth_key,
                current_app.logger,
                get_event_counters(),
            )[0]
        except ItemNotReturned:
            raise HTTPError(
                HTTPStatus.BAD_REQUEST,
                message="inventory UUID %s not available" %
                system_baseline_in["inventory_uuid"],
            )

        system_name = profile_parser.get_name(system_with_profile)
        parsed_profile = profile_parser.parse_profile(
            system_with_profile["system_profile"], system_name,
            current_app.logger)
        facts = []
        for fact in parsed_profile:
            if fact not in ["id", "name"] and parsed_profile[fact] not in [
                    "N/A",
                    "None",
                    None,
            ]:
                facts.append({"name": fact, "value": parsed_profile[fact]})

        baseline_facts = group_baselines(facts)

    try:
        _validate_facts(baseline_facts)
    except FactValidationError as e:
        raise HTTPError(HTTPStatus.BAD_REQUEST, message=e.message)

    baseline = SystemBaseline(
        account=account_number,
        display_name=system_baseline_in["display_name"],
        baseline_facts=baseline_facts,
    )
    baseline.baseline_facts = _sort_baseline_facts(baseline.baseline_facts)
    db.session.add(baseline)
    db.session.commit(
    )  # commit now so we get a created/updated time before json conversion

    return baseline.to_json()