示例#1
0
    def testRegexForStringifiedValueMatchMatchesLabelsInList(self):
        labels_list = aff4_rdfvalues.AFF4ObjectLabelsList()

        labels_list.AddLabel(
            aff4_rdfvalues.AFF4ObjectLabel(name="ein", owner="GRR"))
        labels_list.AddLabel(
            aff4_rdfvalues.AFF4ObjectLabel(name="zwei", owner="test"))
        labels_list.AddLabel(
            aff4_rdfvalues.AFF4ObjectLabel(name="drei", owner="GRR"))
        labels_list.AddLabel(
            aff4_rdfvalues.AFF4ObjectLabel(name="vier", owner="test"))

        self.assertTrue(
            re.match(
                aff4_rdfvalues.AFF4ObjectLabelsList.
                RegexForStringifiedValueMatch("ein"), str(labels_list)))
        self.assertTrue(
            re.match(
                aff4_rdfvalues.AFF4ObjectLabelsList.
                RegexForStringifiedValueMatch("zwei"), str(labels_list)))
        self.assertTrue(
            re.match(
                aff4_rdfvalues.AFF4ObjectLabelsList.
                RegexForStringifiedValueMatch("drei"), str(labels_list)))
        self.assertTrue(
            re.match(
                aff4_rdfvalues.AFF4ObjectLabelsList.
                RegexForStringifiedValueMatch("vier"), str(labels_list)))
示例#2
0
    def testStringifiedRepresentationIsSorted(self):
        labels_list = aff4_rdfvalues.AFF4ObjectLabelsList()

        labels_list.AddLabel(
            aff4_rdfvalues.AFF4ObjectLabel(name="foo", owner="GRR"))
        labels_list.AddLabel(
            aff4_rdfvalues.AFF4ObjectLabel(name="bar", owner="test"))

        self.assertEqual(utils.SmartStr(labels_list), "bar,foo")
示例#3
0
    def testStringifiedValueIsLabelsNamesWithoutOwners(self):
        labels_list = aff4_rdfvalues.AFF4ObjectLabelsList()

        labels_list.AddLabel(
            aff4_rdfvalues.AFF4ObjectLabel(name="bar", owner="GRR"))
        labels_list.AddLabel(
            aff4_rdfvalues.AFF4ObjectLabel(name="foo", owner="test"))

        self.assertEqual(utils.SmartStr(labels_list), "bar,foo")
示例#4
0
    def testAddLabelAddsLabelWithSameNameButDifferentOwner(self):
        labels_list = aff4_rdfvalues.AFF4ObjectLabelsList()

        labels_list.AddLabel(
            aff4_rdfvalues.AFF4ObjectLabel(name="foo", owner="test"))
        self.assertEqual(len(labels_list.labels), 1)

        labels_list.AddLabel(
            aff4_rdfvalues.AFF4ObjectLabel(name="foo", owner="GRR"))
        self.assertEqual(len(labels_list.labels), 2)
示例#5
0
    def testAddLabelDoesNotAddLabelWithSameNameAndOwner(self):
        labels_list = aff4_rdfvalues.AFF4ObjectLabelsList()

        labels_list.AddLabel(
            aff4_rdfvalues.AFF4ObjectLabel(name="foo", owner="test"))
        self.assertEqual(len(labels_list.labels), 1)

        labels_list.AddLabel(
            aff4_rdfvalues.AFF4ObjectLabel(name="foo", owner="test"))
        self.assertEqual(len(labels_list.labels), 1)
示例#6
0
    def testStringifiedValueDoesNotHaveDuplicates(self):
        labels_list = aff4_rdfvalues.AFF4ObjectLabelsList()

        labels_list.AddLabel(
            aff4_rdfvalues.AFF4ObjectLabel(name="foo", owner="GRR"))
        labels_list.AddLabel(
            aff4_rdfvalues.AFF4ObjectLabel(name="bar", owner="GRR"))
        labels_list.AddLabel(
            aff4_rdfvalues.AFF4ObjectLabel(name="foo", owner="test"))

        self.assertEqual(utils.SmartStr(labels_list), "bar,foo")
示例#7
0
    def GenerateSample(self, number=0):
        label1 = aff4_rdfvalues.AFF4ObjectLabel(
            name="foo_%d" % number,
            owner="test",
            timestamp=rdfvalue.RDFDatetime().FromSecondsFromEpoch(42))

        label2 = aff4_rdfvalues.AFF4ObjectLabel(
            name="bar_%d" % number,
            owner="test",
            timestamp=rdfvalue.RDFDatetime().FromSecondsFromEpoch(42))

        return aff4_rdfvalues.AFF4ObjectLabelsList(labels=[label1, label2])
示例#8
0
  def testMultipleLabelsWithDifferentOwnersAreCorrectlyListed(self):
    urn = rdfvalue.RDFURN("aff4:/foo/bar")

    with self.CreateIndex(token=self.token) as index:
      index.AddLabel(urn, "foo", owner="testuser1")
      index.AddLabel(urn, "foo", owner="testuser2")

    index = self.ReadIndex(token=self.token)
    self.assertItemsEqual(index.ListUsedLabels(),
                          [aff4_rdfvalues.AFF4ObjectLabel(name="foo",
                                                          owner="testuser1"),
                           aff4_rdfvalues.AFF4ObjectLabel(name="foo",
                                                          owner="testuser2")])
示例#9
0
  def testGetSortedLabelSet(self):
    labels_list = aff4_rdfvalues.AFF4ObjectLabelsList()

    labels_list.AddLabel(aff4_rdfvalues.AFF4ObjectLabel(name="foo",
                                                        owner="test"))
    labels_list.AddLabel(aff4_rdfvalues.AFF4ObjectLabel(name="foo2",
                                                        owner="test2"))
    labels_list.AddLabel(aff4_rdfvalues.AFF4ObjectLabel(name="foo3",
                                                        owner="test2"))

    self.assertItemsEqual(labels_list.GetLabelNames(), ["foo", "foo2", "foo3"])
    self.assertItemsEqual(
        labels_list.GetLabelNames(owner="test2"), ["foo2", "foo3"])
    self.assertEqual(labels_list.GetLabelNames(owner="test4"), [])
示例#10
0
  def testRegexForStringifiedValueDoesNotMatchLabelsNotInList(self):
    labels_list = aff4_rdfvalues.AFF4ObjectLabelsList()

    labels_list.AddLabel(aff4_rdfvalues.AFF4ObjectLabel(name="ein",
                                                        owner="GRR"))
    labels_list.AddLabel(aff4_rdfvalues.AFF4ObjectLabel(name="zwei",
                                                        owner="test"))
    self.assertFalse(re.match(
        aff4_rdfvalues.AFF4ObjectLabelsList.RegexForStringifiedValueMatch("e"),
        str(labels_list)))
    self.assertFalse(re.match(
        aff4_rdfvalues.AFF4ObjectLabelsList.RegexForStringifiedValueMatch("in"),
        str(labels_list)))
    self.assertFalse(re.match(
        aff4_rdfvalues.AFF4ObjectLabelsList.RegexForStringifiedValueMatch(
            "a.zwer"), str(labels_list)))
    self.assertFalse(re.match(
        aff4_rdfvalues.AFF4ObjectLabelsList.RegexForStringifiedValueMatch(
            "ein."), str(labels_list)))
示例#11
0
  def testAddedLabelIsCorrectlyListed(self):
    urn = rdfvalue.RDFURN("aff4:/foo/bar")

    with self.CreateIndex(token=self.token) as index:
      index.AddLabel(urn, "foo", owner="testuser")

    index = self.ReadIndex(token=self.token)
    self.assertListEqual(index.ListUsedLabels(),
                         [aff4_rdfvalues.AFF4ObjectLabel(name="foo",
                                                         owner="testuser")])
示例#12
0
    def Handle(self, args, token=None):
        labels_index = aff4.FACTORY.Create(standard.LabelSet.CLIENT_LABELS_URN,
                                           standard.LabelSet,
                                           mode="r",
                                           token=token)
        label_objects = []
        for label in labels_index.ListLabels():
            label_objects.append(aff4_rdfvalues.AFF4ObjectLabel(name=label))

        return ApiListClientsLabelsResult(items=label_objects)
示例#13
0
  def testUrnWithAddedLabelCanBeFoundViaLabelRegex(self):
    with self.CreateIndex(token=self.token) as index:
      index.AddLabel(rdfvalue.RDFURN("aff4:/foo/bar1"),
                     "foo", owner="testuser1")
      index.AddLabel(rdfvalue.RDFURN("aff4:/foo/bar2"),
                     "bar", owner="testuser2")
      index.AddLabel(rdfvalue.RDFURN("aff4:/foo/bar3"),
                     "foo", owner="testuser3")

    index = self.ReadIndex(token=self.token)
    found_urns = index.FindUrnsByLabelNameRegex("f.*o")
    self.assertEqual(len(found_urns), 2)
    self.assertListEqual(
        found_urns[aff4_rdfvalues.AFF4ObjectLabel(name="foo",
                                                  owner="testuser1")],
        [rdfvalue.RDFURN("aff4:/foo/bar1")])
    self.assertListEqual(
        found_urns[aff4_rdfvalues.AFF4ObjectLabel(name="foo",
                                                  owner="testuser3")],
        [rdfvalue.RDFURN("aff4:/foo/bar3")])
示例#14
0
 def Render(self, args, token=None):
     labels_index = aff4.FACTORY.Create(standard.LabelSet.CLIENT_LABELS_URN,
                                        "LabelSet",
                                        mode="r",
                                        token=token)
     rendered_labels = []
     for label in labels_index.ListLabels():
         label_object = aff4_rdfvalues.AFF4ObjectLabel(name=label)
         rendered_labels.append(
             api_value_renderers.RenderValue(label_object))
     return dict(labels=rendered_labels)
示例#15
0
  def testDeletedLabelIsNotRemovedFromUsedLabelsList(self):
    label = aff4_rdfvalues.AFF4ObjectLabel(name="foo", owner="testuser")
    urn = rdfvalue.RDFURN("aff4:/foo/bar")

    with self.CreateIndex(token=self.token) as index:
      index.AddLabel(urn, "foo", owner="testuser")

    index = self.ReadIndex(token=self.token)
    self.assertListEqual(index.ListUsedLabels(), [label])

    with self.CreateIndex(token=self.token) as index:
      index.RemoveLabel(urn, "foo", owner="testuser")

    index = self.ReadIndex(token=self.token)
    self.assertListEqual(index.ListUsedLabels(), [label])
示例#16
0
    def testUserAccess(self):
        """Tests access to user objects."""
        token = access_control.ACLToken(username="******", reason="For testing")
        urn = aff4.ROOT_URN.Add("users")
        # We cannot open any user account.
        self.assertRaises(access_control.UnauthorizedAccess, aff4.FACTORY.Open,
                          urn.Add("some_user"), None, "rw", False, token)

        # But we can open our own.
        aff4.FACTORY.Open(urn.Add("test"), mode="rw", token=token)

        # And we can also access our labels.
        label_urn = urn.Add("test").Add("labels")
        labels = aff4.FACTORY.Open(label_urn, mode="rw", token=token)

        # But we cannot write to them.
        l = labels.Schema.LABELS()
        l.AddLabel(aff4_rdfvalues.AFF4ObjectLabel(name="admin", owner="GRR"))
        labels.Set(labels.Schema.LABELS, l)
        self.assertRaises(access_control.UnauthorizedAccess, labels.Close)
示例#17
0
文件: standard.py 项目: wprelic/grr
 def LabelForIndexName(self, index_name):
   label_owner, label_name = utils.SmartStr(index_name).split(
       self.SEPARATOR, 1)
   return aff4_rdfvalues.AFF4ObjectLabel(name=label_name, owner=label_owner)
示例#18
0
 def testForwardSlashIsAllowed(self):
     aff4_rdfvalues.AFF4ObjectLabel(name="b/label.42:1", owner="test")
示例#19
0
 def testColonIsAllowed(self):
     aff4_rdfvalues.AFF4ObjectLabel(name="label.42:1", owner="test")
示例#20
0
 def testAlphanumericCharactersAreAllowed(self):
     aff4_rdfvalues.AFF4ObjectLabel(name="label42", owner="test")
示例#21
0
 def GenerateSample(self, number=0):
     return aff4_rdfvalues.AFF4ObjectLabel(
         name="label%d" % number,
         owner="test",
         timestamp=rdfvalue.RDFDatetime().FromSecondsFromEpoch(42))