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)))
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")
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")
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)
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)
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")
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])
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")])
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"), [])
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)))
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")])
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)
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")])
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)
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])
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)
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)
def testForwardSlashIsAllowed(self): aff4_rdfvalues.AFF4ObjectLabel(name="b/label.42:1", owner="test")
def testColonIsAllowed(self): aff4_rdfvalues.AFF4ObjectLabel(name="label.42:1", owner="test")
def testAlphanumericCharactersAreAllowed(self): aff4_rdfvalues.AFF4ObjectLabel(name="label42", owner="test")
def GenerateSample(self, number=0): return aff4_rdfvalues.AFF4ObjectLabel( name="label%d" % number, owner="test", timestamp=rdfvalue.RDFDatetime().FromSecondsFromEpoch(42))