Example #1
0
    def testRemovesUserLabelWhenSystemLabelWithSimilarNameAlsoExists(self):
        idx = client_index.ClientIndex()
        with aff4.FACTORY.Open(self.client_ids[0], mode="rw",
                               token=self.token) as grr_client:
            grr_client.AddLabel("foo")
            grr_client.AddLabel("foo", owner="GRR")
            data_store.REL_DB.WriteClientMetadata(
                self.client_ids[0].Basename(), fleetspeak_enabled=False)
            data_store.REL_DB.AddClientLabels(self.client_ids[0].Basename(),
                                              self.token.username, ["foo"])
            data_store.REL_DB.AddClientLabels(self.client_ids[0].Basename(),
                                              "GRR", ["foo"])
            idx.AddClientLabels(self.client_ids[0].Basename(), ["foo"])

        self.handler.Handle(client_plugin.ApiRemoveClientsLabelsArgs(
            client_ids=[self.client_ids[0]], labels=["foo"]),
                            token=self.token)

        # AFF4 labels.
        labels = aff4.FACTORY.Open(self.client_ids[0],
                                   token=self.token).GetLabels()
        self.assertEqual(len(labels), 1)
        self.assertEqual(labels[0].name, "foo")
        self.assertEqual(labels[0].owner, "GRR")

        # Relational labels.
        labels = data_store.REL_DB.ReadClientLabels(
            self.client_ids[0].Basename())
        self.assertEqual(len(labels), 1)
        self.assertEqual(labels[0].name, "foo")
        self.assertEqual(labels[0].owner, "GRR")
        # The label is still in the index.
        self.assertEqual(idx.LookupClients(["label:foo"]),
                         [self.client_ids[0].Basename()])
Example #2
0
    def testRemovesUserLabelFromSingleClient(self):
        with aff4.FACTORY.Open(self.client_ids[0], mode="rw",
                               token=self.token) as grr_client:
            grr_client.AddLabels(["foo", "bar"])
            data_store.REL_DB.WriteClientMetadata(
                self.client_ids[0].Basename(), fleetspeak_enabled=False)
            data_store.REL_DB.AddClientLabels(self.client_ids[0].Basename(),
                                              self.token.username,
                                              ["foo", "bar"])

        self.handler.Handle(client_plugin.ApiRemoveClientsLabelsArgs(
            client_ids=[self.client_ids[0]], labels=["foo"]),
                            token=self.token)

        # AFF4 labels.
        labels = aff4.FACTORY.Open(self.client_ids[0],
                                   token=self.token).GetLabels()
        self.assertEqual(len(labels), 1)
        self.assertEqual(labels[0].name, "bar")
        self.assertEqual(labels[0].owner, self.token.username)

        # Relational labels.
        labels = data_store.REL_DB.ReadClientLabels(
            self.client_ids[0].Basename())
        self.assertEqual(len(labels), 1)
        self.assertEqual(labels[0].name, "bar")
        self.assertEqual(labels[0].owner, self.token.username)