示例#1
0
文件: tests.py 项目: zwfec/jumpserver
    def setUp(self):
        Organization.objects.bulk_create([
            Organization(name='org-01'),
            Organization(name='org-02'),
            Organization(name='org-03'),
        ])
        org_01, org_02, org_03 = Organization.objects.all()
        self.org_01, self.org_02, self.org_03 = org_01, org_02, org_03

        set_current_org(org_01)

        AdminUser.objects.bulk_create([
            AdminUser(name='au-01', username='******'),
            AdminUser(name='au-02', username='******'),
            AdminUser(name='au-03', username='******'),
        ])

        SystemUser.objects.bulk_create([
            SystemUser(name='su-01', username='******'),
            SystemUser(name='su-02', username='******'),
            SystemUser(name='su-03', username='******'),
        ])

        admin_users = AdminUser.objects.all()
        Asset.objects.bulk_create([
            Asset(hostname='asset-01',
                  ip='192.168.1.1',
                  public_ip='192.168.1.1',
                  admin_user=admin_users[0]),
            Asset(hostname='asset-02',
                  ip='192.168.1.2',
                  public_ip='192.168.1.2',
                  admin_user=admin_users[0]),
            Asset(hostname='asset-03',
                  ip='192.168.1.3',
                  public_ip='192.168.1.3',
                  admin_user=admin_users[0]),
        ])

        new_user = User.objects.create
        new_org_memeber = OrganizationMember.objects.create

        u = new_user(name='user-01',
                     username='******',
                     email='*****@*****.**')
        new_org_memeber(org=org_01, user=u, role=ORG_ROLE.USER)
        new_org_memeber(org=org_02, user=u, role=ORG_ROLE.USER)
        self.user_01 = u

        u = new_user(name='org-admin-01',
                     username='******',
                     email='*****@*****.**')
        new_org_memeber(org=org_01, user=u, role=ORG_ROLE.ADMIN)
        self.org_admin_01 = u

        u = new_user(name='org-admin-02',
                     username='******',
                     email='*****@*****.**')
        new_org_memeber(org=org_02, user=u, role=ORG_ROLE.ADMIN)
        self.org_admin_02 = u
示例#2
0
 def test_span_link(self):
     start = Text(text="In this sentence is a ")
     start.save()
     link_text = Text(text="link")
     link_text.save()
     link_target = UriElement(uri="https://ct.de")
     link_target.save()
     end = Text(text=" which leads to https://ct.de.")
     end.save()
     start_span = Asset(t=self.at("span-regular"),
                        content_ids={"text": start.pk})
     start_span.save()
     link_span = Asset(t=self.at("span-link"),
                       content_ids={
                           "link_text": link_text.pk,
                           "url": link_target.pk
                       })
     link_span.save()
     end_span = Asset(t=self.at("span-regular"),
                      content_ids={"text": end.pk})
     end_span.save()
     paragraph = Asset(
         t=self.at("block-paragraph"),
         content_ids={
             "spans":
             [str(start_span.pk),
              str(link_span.pk),
              str(end_span.pk)]
         })
     paragraph.save()
     self.maxDiff = None
     self.assertJSONEqual(
         json.dumps(paragraph.content),
         json.dumps({
             "type":
             "block-paragraph",
             "id":
             str(paragraph.pk),
             "spans": [
                 {
                     "type": "span-regular",
                     "id": str(start_span.pk),
                     "text": "In this sentence is a "
                 },
                 {
                     "type": "span-link",
                     "id": str(link_span.pk),
                     "link_text": "link",
                     "url": "https://ct.de"
                 },
                 {
                     "type": "span-regular",
                     "id": str(end_span.pk),
                     "text": " which leads to https://ct.de."
                 },
             ]
         }))
示例#3
0
 def test_partially_existent_caches(self):
     t1 = Text(text="Text1")
     t1.save()
     t2 = Text(text="Text2")
     t2.save()
     a1 = Asset(t=AssetType.objects.get(type_name="span-regular"),
                content_ids={"text": t1.pk})
     a1.save()
     a2 = Asset(t=AssetType.objects.get(type_name="span-emphasized"),
                content_ids={"text": t2.pk})
     a2.save()
     self.assertIsNone(a1.content_cache)
     self.assertIsNone(a1.raw_content_cache)
     self.assertIsNone(a2.content_cache)
     self.assertIsNone(a2.raw_content_cache)
     self.assertEqual(a1.content, {
         "id": str(a1.pk),
         "type": "span-regular",
         "text": "Text1"
     })
     self.assertIsNotNone(a1.content_cache)
     self.assertIsNone(a1.raw_content_cache)
     self.assertEqual(a1.content_cache, {
         "id": str(a1.pk),
         "type": "span-regular",
         "text": "Text1"
     })
     self.assertEqual(a2.render_template(), "Text2")
     self.assertIsNone(a2.content_cache)
     self.assertIsNotNone(a2.raw_content_cache)
     self.assertEqual(a2.raw_content_cache, "Text2")
     call_command("build_caches")
     r_a1 = Asset.objects.get(pk=a1.pk)
     r_a2 = Asset.objects.get(pk=a2.pk)
     self.assertIsNotNone(r_a1.content_cache)
     self.assertIsNotNone(r_a1.raw_content_cache)
     self.assertIsNotNone(r_a2.content_cache)
     self.assertIsNotNone(r_a2.raw_content_cache)
     self.assertEqual(r_a1.content_cache, {
         "id": str(r_a1.pk),
         "type": "span-regular",
         "text": "Text1"
     })
     self.assertEqual(r_a1.raw_content_cache, "Text1")
     self.assertEqual(r_a2.content_cache, {
         "id": str(r_a2.pk),
         "type": "span-emphasized",
         "text": "Text2"
     })
     self.assertEqual(r_a2.raw_content_cache, "Text2")
示例#4
0
 def test_block_paragraph_raw_template(self):
     t1 = Text(text="Foo ")
     t1.save()
     t2 = Text(text="Bar")
     t2.save()
     a1 = Asset(t=AssetType.objects.get(type_name="span-regular"),
                content_ids={"text": t1.pk})
     a1.save()
     a2 = Asset(t=AssetType.objects.get(type_name="span-emphasized"),
                content_ids={"text": t2.pk})
     a2.save()
     p = Asset(t=AssetType.objects.get(type_name="block-paragraph"),
               content_ids={"spans": [str(a1.pk), str(a2.pk)]})
     p.save()
     self.assertEqual(p.render_template(), "Foo Bar\n\n")
 def test_object_perms_user_can_change(self):
     """check that the object permission is true when
     the user is associated with the asset's existing and updated department"""
     self.request.method = 'PATCH'
     self.request.data['department'] = 'UIS'
     self.request.data['name'] = 'new name'
     self.assertTrue(self.has_object_permission(Asset(department='UIS')))
示例#6
0
def transform_asset(asset):
    data = {
        "exchange_id": "XNAS",
        "name": asset['name'],
        "symbol": asset['symbol'],
    }
    return Asset(**data)
示例#7
0
 def create_asset(tree, item_type=None):
     if item_type == 1:
         text_item = Text(text=tree)
         text_item.save()
         return text_item.pk
     if item_type == 2:
         uri_item = UriElement(uri=tree)
         uri_item.save()
         return uri_item.pk
     if type(item_type) is dict and \
             len(item_type.keys()) == 1 and \
             "3" in item_type.keys():
         enum_item = Enum(t=EnumType.objects.get(pk=item_type["3"]), item=tree)
         enum_item.save()
         return enum_item.pk
     asset_type = AssetType.objects.get(type_name=tree["type"])
     content_ids = {}
     for key in asset_type.schema.keys():
         if type(asset_type.schema[key]) is list:
             item_ids_list = []
             for list_item in tree[key]:
                 item_ids_list.append(create_or_modify_asset(list_item, item_type=asset_type.schema[key][0]))
             content_ids[key] = item_ids_list
         else:
             content_ids[key] = create_or_modify_asset(tree[key], item_type=asset_type.schema[key])
     asset = Asset(t=asset_type, content_ids=content_ids)
     asset.save()
     return str(asset.pk)
示例#8
0
    def test_create_asset(self):
        """Asset object can be created."""
        asset_uuid = uuid.uuid4()
        asset = Asset(
            id=asset_uuid,
            name="Asset Name",
            asset_class=self.asset_class,
            easy_to_borrow=True,
            exchange=self.exchange,
            marginable=True,
            shortable=True,
            status=Asset.ACTIVE,
            symbol="SYMBOL",
            tradable=True,
        )
        asset.save()

        self.assertIn(asset, Asset.objects.all())
        self.assertEqual(str(asset.id), str(asset_uuid))
        self.assertEqual(asset.name, "Asset Name")
        self.assertEqual(asset.asset_class.pk, self.asset_class.pk)
        self.assertTrue(asset.easy_to_borrow)
        self.assertEqual(asset.exchange.pk, self.exchange.pk)
        self.assertTrue(asset.marginable)
        self.assertTrue(asset.shortable)
        self.assertEqual(asset.status, Asset.ACTIVE)
        self.assertEqual(asset.symbol, "SYMBOL")
        self.assertTrue(asset.tradable)
示例#9
0
    def test_put_with_perms(self):
        """A user with the change asset permission can put an asset."""
        client = APIClient()
        asset_dict = copy.copy(COMPLETE_ASSET)
        asset_dict['department'] = 'TESTDEPT2'
        asset = Asset(**asset_dict)
        asset.save()

        # PUT not in allowed methods
        self.assert_method_is_not_listed_as_allowed('PUT', asset)

        result_put = client.put('/assets/%s/' % asset.pk, COMPLETE_ASSET)

        # Not allowed because the asset belongs to TESTDEPT2
        self.assertEqual(result_put.status_code, 403)

        # Succeeds if use has permission
        perm = Permission.objects.get(
            content_type=ContentType.objects.get_for_model(Asset),
            codename='change_asset')
        self.user.user_permissions.add(perm)
        self.user.save()

        self.refresh_user()

        self.assertTrue(self.user.has_perm('assets.change_asset'))

        # PUT is now in allowed methods
        self.assert_method_is_listed_as_allowed('PUT', asset)

        result_put = client.put('/assets/%s/' % asset.pk, COMPLETE_ASSET)
        self.assertEqual(result_put.status_code, 200)
示例#10
0
    def test_asset_put_validation(self):
        """User's only allow to PUT an asset that has a department their are part of, and the
        PUT department has to be one he belongs to"""
        client = APIClient()
        asset_dict = copy.copy(COMPLETE_ASSET)
        asset_dict['department'] = 'TESTDEPT2'
        asset = Asset(**asset_dict)
        asset.save()
        result_put = client.put('/assets/%s/' % asset.pk, COMPLETE_ASSET)
        # Not allowed because the asset belongs to TESTDEPT2
        self.assert_method_is_not_listed_as_allowed('PUT', asset)
        self.assertEqual(result_put.status_code, 403)

        # We fix the department, so now the user should be allow but we try to change the
        # department to another that the user doesn't belong to
        asset.department = 'TESTDEPT'
        set_local_user(self.user)
        asset.save()

        # User can, in principle PUT...
        self.assert_method_is_listed_as_allowed('PUT', asset)

        # ... but not this asset
        result_put = client.put('/assets/%s/' % asset.pk, asset_dict)
        self.assertEqual(result_put.status_code, 403)

        # This one should be allowed
        asset_dict['department'] = 'TESTDEPT'
        asset_dict['name'] = 'asset2'
        result_put = client.put('/assets/%s/' % asset.pk, asset_dict)
        self.assert_method_is_listed_as_allowed('PUT', asset)
        self.assertEqual(result_put.status_code, 200)
示例#11
0
    def test_asset_patch_validation(self):
        """User's only allow to PATCH an asset that has a department their are part of, and the
        PATCH department has to be one he belongs to"""
        client = APIClient()
        asset_dict = copy.copy(COMPLETE_ASSET)
        asset_dict['department'] = 'TESTDEPT2'
        asset = Asset(**asset_dict)
        asset.save()
        result_patch = client.patch('/assets/%s/' % asset.pk,
                                    {"name": "asset1"})
        # Not allowed because the asset belongs to TESTDEPT2
        self.assert_method_is_not_listed_as_allowed('PATCH', asset)
        self.assertEqual(result_patch.status_code, 403)

        # We fix the department, so now the user should be allow but we try to change the
        # department to another that the user doesn't belong to
        asset.department = 'TESTDEPT'
        set_local_user(self.user)
        asset.save()

        # User is in principle allowed ...
        self.assert_method_is_listed_as_allowed('PATCH', asset)

        # ... but not in this case
        result_patch = client.patch('/assets/%s/' % asset.pk,
                                    {"department": "TESTDEPT2"})
        self.assertEqual(result_patch.status_code, 403)

        # This one should be allowed
        result_patch = client.patch('/assets/%s/' % asset.pk,
                                    {"name": "asset2"})
        self.assert_method_is_listed_as_allowed('PATCH', asset)
        self.assertEqual(result_patch.status_code, 200)
示例#12
0
 def setUp(self):
     self.user = get_user_model().objects.create_user(username='******')
     self.user_lookup = UserLookup.objects.create(
         user=self.user, scheme='mock', identifier=self.user.username)
     set_local_user(self.user)
     self.asset = Asset(name='test-asset')
     self.asset.save()
示例#13
0
def create_asset(user_instance, company_instance):
    A = Asset(asset_tag=get_random_tag(),
              device_type=get_random_type(),
              created_by=user_instance,
              company=company_instance,
              serial_number=get_random_tag())
    A.save()
示例#14
0
def assets_add_apply(request):
    if request.method == "POST":
        asset = Asset(mnfacture=request.POST['mnfacture'],
                      model=request.POST['model'],
                      cpu=request.POST['cpu'],
                      memory=request.POST['memory'],
                      harddisk=request.POST['hardDisk'],
                      is_where=request.POST['where'],
                      is_state=request.POST['state'],
                      purchase_date=request.POST['purchase_date'],
                      comments=request.POST['comments'],
                      member_name_id=request.POST['memberId'],
                      serial=request.POST['serial'])
        asset.save()
        asset_status = int(request.POST['state'])
        if asset_status == ASSET_STATUS_RENTAL:
            asset_rent = Assetrent(stdate=request.POST['rentDate'],
                                   eddate=request.POST['returnDate'],
                                   comments=request.POST['comments'],
                                   asset_id=asset.id,
                                   member_name=request.POST['memberName'])
            asset_rent.save()
        return redirect("assets_main")
    else:
        return render(request, 'assets_main/assets.html',
                      {'permission': REPORT_PERMISSION_DEFAULT})
示例#15
0
def make_asset(product_id, path, md5_check, class_label='hirise product'):
    asset = Asset()
    asset.class_label = class_label
    asset.instrument_name = 'HiRISE'
    asset.product_id = product_id
    asset.md5_check = md5_check
    asset.relative_file_path = path.partition(DATA_ROOT)[-1]
    asset.save()
示例#16
0
def _create_asset_on_import(asset_value, scan, asset_type='unknown'):
    Event.objects.create(message="[EngineTasks/_create_asset_on_import()] create: '{}/{}'.".format(asset_value, asset_type), type="DEBUG", severity="INFO", scan=scan)

    # create assets if data_type is ip-subnet or ip-range
    if scan and asset_type == 'ip':
        assets = scan.assets.filter(type__in=['ip-subnet', 'ip-range'])

        # Search parent asset
        parent_asset = None
        for pa in assets:
            if net.is_ip_in_ipset(ip=asset_value, ipset=pa.value):
                parent_asset = pa
                break
        if parent_asset:
            name = "{} (from '{}')".format(asset_value, parent_asset.name)
            criticity = parent_asset.criticity
            owner = parent_asset.owner
        else:
            name = asset_value
            criticity = 'medium'
            owner = User.objects.filter(username='******').first()
    else:
        if net.is_valid_ip(asset_value):
            asset_type = "ip"
        elif net._is_valid_domain(asset_value):
            asset_type = "domain"
        elif net._is_valid_url(asset_value):
            asset_type = "url"
        else:
            asset_type = "fqdn"  # default :/
        name = asset_value
        criticity = 'medium'
        owner = User.objects.filter(username='******').first()

    # Create the new asset ...
    asset_args = {
        'value': asset_value,
        'name': name,
        'type': asset_type,
        'criticity': criticity,
        'description': "Asset dynamically created",
        'owner': owner
    }
    asset = Asset(**asset_args)
    asset.save()
    scan.assets.add(asset)

    # Then add the asset to every related asset groups
    for ag in AssetGroup.objects.filter(assets__type__in=['ip-subnet', 'ip-range']):
        for aga in ag.assets.all():
            if net.is_ip_in_ipset(ip=asset_value, ipset=aga.value):
                ag.assets.add(asset)
                ag.save()
                ag.calc_risk_grade()
                ag.save()

    return asset
示例#17
0
 def test_no_raw_template(self):
     insufficient_type = AssetType(type_name="insufficient_type",
                                   schema={"text": 1},
                                   templates={})
     insufficient_type.save()
     t = Text(text="Foobar!")
     t.save()
     asset = Asset(t=insufficient_type, content_ids={"text": t.pk})
     asset.save()
     self.assertEqual(asset.render_template(), "")
示例#18
0
 def test_load_block(self):
     title = Text(text="Text Box Title")
     title.save()
     content = Text(text="This is the content of the box.")
     content.save()
     span = Asset(t=AssetType.objects.get(type_name="span-regular"),
                  content_ids={"text": content.pk})
     span.save()
     paragraph_block = Asset(
         t=AssetType.objects.get(type_name="block-paragraph"),
         content_ids={"spans": [str(span.pk)]})
     paragraph_block.save()
     box = Asset(
         t=AssetType.objects.get(type_name="block-accompaniement-box"),
         content_ids={
             "title": title.pk,
             "content": [str(paragraph_block.pk)]
         })
     box.save()
     response = self.client.get(reverse('load_asset'), {"id": str(box.pk)})
     self.assertEqual(response.status_code, 200)
     self.assertJSONEqual(
         response.content, {
             "type":
             "block-accompaniement-box",
             "title":
             "Text Box Title",
             "id":
             str(box.pk),
             "content": [{
                 "type":
                 "block-paragraph",
                 "id":
                 str(paragraph_block.pk),
                 "spans": [{
                     "type": "span-regular",
                     "id": str(span.pk),
                     "text": "This is the content of the box."
                 }]
             }]
         })
示例#19
0
 def test_foo(self):
     t1 = Text(text="Foo")
     t1.save()
     t1_span = Asset(t=self.at("span-regular"), content_ids={"text": t1.pk})
     t1_span.save()
     self.assertJSONEqual(
         json.dumps(t1_span.content),
         json.dumps({
             'type': "span-regular",
             'id': str(t1_span.pk),
             'text': "Foo"
         }))
示例#20
0
 def create(self, data):
     print(data)
     if (data.get('asset')):
         assets_data = data.pop('asset')
         print('assets_data:', assets_data)
         asset = Asset.objects.create(**assets_data)
         print('asset_obj:', asset)
     else:
         asset = Asset()
     data['asset'] = asset
     server = Server.objects.create(**data)
     return server
示例#21
0
 def test_block_image_raw_template(self):
     img_uri = UriElement(uri="/foo/bar.jpg")
     img_uri.save()
     alt_text = Text(text="An image of a bar.")
     alt_text.save()
     caption_text1 = Text(text="This bar is an ")
     caption_text1.save()
     caption_text2 = Text(text="example")
     caption_text2.save()
     caption_text3 = Text(text=" of foo.")
     caption_text3.save()
     caption_span1 = Asset(
         t=AssetType.objects.get(type_name="caption-span-regular"),
         content_ids={"text": caption_text1.pk})
     caption_span1.save()
     caption_span2 = Asset(
         t=AssetType.objects.get(type_name="caption-span-emphasized"),
         content_ids={"text": caption_text2.pk})
     caption_span2.save()
     caption_span3 = Asset(
         t=AssetType.objects.get(type_name="caption-span-regular"),
         content_ids={"text": caption_text3.pk})
     caption_span3.save()
     img = Asset(t=AssetType.objects.get(type_name="block-image"),
                 content_ids={
                     "image_uri":
                     img_uri.pk,
                     "alt":
                     alt_text.pk,
                     "caption": [
                         str(caption_span1.pk),
                         str(caption_span2.pk),
                         str(caption_span3.pk)
                     ]
                 })
     img.save()
     self.assertEqual(
         img.render_template(),
         "img (/foo/bar.jpg): An image of a bar.\n\nThis bar is an example of foo.\n"
     )
示例#22
0
 def test_privacy(self):
     """Test that a User cannot see/access to assets that are private outside their
     department"""
     client = APIClient()
     asset_dict = copy.copy(COMPLETE_ASSET)
     asset_dict['department'] = 'TESTDEPT2'
     asset_dict['private'] = True
     asset = Asset(**asset_dict)
     asset.save()
     result_patch = client.get('/assets/%s/' % asset.pk)
     self.assertEqual(result_patch.status_code, 404)
     list_assets = client.get('/assets/', format='json')
     self.assertEqual(list_assets.json()['results'], [])
示例#23
0
 def test_clear_cache(self):
     text = Text(text="cached text")
     text.save()
     span = Asset(t=self.at("span-regular"), content_ids={"text": text.pk})
     span.save()
     block = Asset(t=self.at("block-paragraph"),
                   content_ids={"spans": [str(span.pk)]})
     block.save()
     self.assertIsNone(span.content_cache)
     self.assertIsNone(block.content_cache)
     self.assertJSONEqual(
         json.dumps(span.content),
         json.dumps({
             'type': "span-regular",
             'id': str(span.pk),
             'text': "cached text"
         }))
     self.assertJSONEqual(
         json.dumps(block.content),
         json.dumps({
             'type':
             "block-paragraph",
             'id':
             str(block.pk),
             'spans': [{
                 'type': "span-regular",
                 'id': str(span.pk),
                 'text': "cached text"
             }]
         }))
     span = Asset.objects.get(pk=span.pk)
     block = Asset.objects.get(pk=block.pk)
     self.assertIsNotNone(span.content_cache)
     self.assertIsNotNone(block.content_cache)
     span.clear_cache()
     span = Asset.objects.get(pk=span.pk)
     block = Asset.objects.get(pk=block.pk)
     self.assertIsNone(span.content_cache)
     self.assertIsNone(block.content_cache)
示例#24
0
 def test_basic_template(self):
     statement = Text(text="Vertrauen ist gut. Kontrolle ist besser.")
     statement.save()
     attribution = Text(text="Lenin")
     attribution.save()
     asset = Asset(t=AssetType.objects.get(type_name="block-citation"),
                   content_ids={
                       "statement": statement.pk,
                       "attribution": attribution.pk
                   })
     asset.save()
     self.assertEqual(
         asset.render_template(),
         "Vertrauen ist gut. Kontrolle ist besser.\n - Lenin\n")
示例#25
0
 def test_block_info_box_raw_template(self):
     t1 = Text(text="Foo ")
     t1.save()
     t2 = Text(text="Bar")
     t2.save()
     s1 = Asset(t=AssetType.objects.get(type_name="span-regular"),
                content_ids={"text": t1.pk})
     s1.save()
     s2 = Asset(t=AssetType.objects.get(type_name="span-emphasized"),
                content_ids={"text": t2.pk})
     s2.save()
     p1 = Asset(t=AssetType.objects.get(type_name="block-paragraph"),
                content_ids={"spans": [str(s1.pk), str(s2.pk)]})
     p1.save()
     p2 = Asset(t=AssetType.objects.get(type_name="block-paragraph"),
                content_ids={"spans": [str(s2.pk)]})
     p2.save()
     box = Asset(t=AssetType.objects.get(type_name="block-info-box"),
                 content_ids={
                     "title": t2.pk,
                     "content": [str(p1.pk), str(p2.pk)]
                 })
     box.save()
     self.assertEqual(box.render_template(), "Bar\n\nFoo Bar\n\nBar\n\n")
示例#26
0
 def test_listing_block(self):
     title = Text(text="Box Title")
     title.save()
     code = Text(text="a = 2 + 4\nprint(a)")
     code.save()
     language_type = EnumType.objects.get(pk=2)
     language = Enum(t=language_type, item="python")
     language.save()
     listing_block = Asset(t=self.at("block-listing"),
                           content_ids={
                               "language": language.pk,
                               "code": code.pk
                           })
     listing_block.save()
     box = Asset(t=self.at("block-accompaniement-box"),
                 content_ids={
                     "title": title.pk,
                     "content": [str(listing_block.pk)]
                 })
     box.save()
     self.assertJSONEqual(
         json.dumps(box.content),
         json.dumps({
             "type":
             "block-accompaniement-box",
             "id":
             str(box.pk),
             "title":
             "Box Title",
             "content": [{
                 "type": "block-listing",
                 "id": str(listing_block.pk),
                 "code": "a = 2 + 4\nprint(a)",
                 "language": "python"
             }]
         }))
示例#27
0
 def test_cache_usage(self):
     text = Text(text="cached span")
     text.save()
     text_span = Asset(t=self.at("span-regular"),
                       content_ids={"text": text.pk})
     text_span.save()
     self.assertIsNone(text_span.content_cache)
     expected_content = json.dumps({
         'type': "span-regular",
         'id': str(text_span.pk),
         'text': "cached span"
     })
     self.assertJSONEqual(json.dumps(text_span.content), expected_content)
     self.assertIsNotNone(text_span.content_cache)
     self.assertJSONEqual(json.dumps(text_span.content_cache),
                          expected_content)
     self.assertJSONEqual(json.dumps(text_span.content), expected_content)
示例#28
0
def _create_asset_on_import(asset_value, scan, asset_type='ip'):
    Event.objects.create(
        message="[EngineTasks/_create_asset_on_import()] create: '{}'.".format(
            asset_value),
        type="DEBUG",
        severity="INFO",
        scan=scan)

    # Search parent asset
    parent_asset = None
    for pa in scan.assets.filter(type__in=['ip-subnet', 'ip-range']):
        if net.is_ip_in_ipset(ip=asset_value, ipset=pa.value):
            parent_asset = pa
            break
    if parent_asset == None: return False

    # Create the new asset ...
    asset_args = {
        'value':
        asset_value,
        'name':
        "{} (from '{}')".format(asset_value, parent_asset.name),
        'type':
        asset_type,
        'criticity':
        parent_asset.criticity,
        'description':
        "Asset dynamically created. Imported desc: {}".format(
            parent_asset.description),
        'owner':
        parent_asset.owner
    }
    asset = Asset(**asset_args)
    asset.save()
    scan.assets.add(asset)

    # Then the asset to every related asset groups
    for ag in AssetGroup.objects.filter(
            assets__type__in=['ip-subnet', 'ip-range']):
        for aga in ag.assets.all():
            if net.is_ip_in_ipset(ip=asset_value, ipset=aga.value):
                ag.assets.add(asset)
                ag.save()
                ag.calc_risk_grade()
                ag.save()
    return asset
示例#29
0
def create_video(user, session, region='eu-west-3'):
    """Create a video for a user and a given session."""
    asset = Asset(owner=user, external=False)
    default_bucket = 'kincube-development'
    asset.info['s3'] = {}
    asset.info['s3']['bucket'] = input(  # Noqa B322
        'Video\'s bucket in Amazon S3 [{}]: '.format(default_bucket)) or \
        default_bucket
    default_key = 'australian open 2017 finale federer_nadal best points HD ' \
        'french_français (720p_30fps_H264-192kbit_AAC).mp4'
    asset.info['s3']['key'] = \
        input('Video\'s key in Amazon S3 [{}]: '.format(  # Noqa B322
        default_key,
    )).replace(' ', '').strip("'") or default_key
    #  asset.url = 'http://{}.s3-aws-{}.amazonaws.com/{}'.format(
    #      asset.info['s3']['bucket'],
    #      region,
    #      asset.info['s3']['key'],
    #  )
    # duration = datetime.timedelta(seconds=get_length(video.key))
    # video.duration = duration
    s3 = boto3.resource('s3')
    data = open(asset.info['s3']['key'], 'rb')
    mime_type = get_mime_type(asset.info['s3']['key'])
    asset.info['Content-Type'] = mime_type
    s3.Bucket(asset.info['s3']['bucket']).put_object(
        Key=asset.info['s3']['key'],
        Body=data,
        ACL='public-read',
        ContentType=mime_type,
    )
    config = boto3.client('s3')._client_config
    config.signature_version = botocore.UNSIGNED
    asset.url = boto3.resource(
        's3',
        config=config,
    ).meta.client.generate_presigned_url(
        'get_object',
        ExpiresIn=0,
        Params={
            'Bucket': asset.info['s3']['bucket'],
            'Key': asset.info['s3']['key'],
        },
    )
    video = Video(asset=asset, session=session)
    return video
示例#30
0
 def test_single_asset(self):
     t = Text(text="Foohoo!")
     t.save()
     asset = Asset(t=AssetType.objects.get(type_name="span-regular"),
                   content_ids={"text": t.pk})
     asset.save()
     self.assertIsNone(asset.content_cache)
     self.assertIsNone(asset.raw_content_cache)
     call_command("build_caches")
     reloaded_asset = Asset.objects.get(pk=asset.pk)
     self.assertIsNotNone(reloaded_asset.content_cache)
     self.assertIsNotNone(reloaded_asset.raw_content_cache)
     self.assertEqual(reloaded_asset.content_cache, {
         "id": str(asset.pk),
         "type": "span-regular",
         "text": "Foohoo!"
     })
     self.assertEqual(reloaded_asset.raw_content_cache, "Foohoo!")