Esempio n. 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(u"foo")
            grr_client.AddLabel(u"foo", owner=u"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, [u"foo"])
            data_store.REL_DB.AddClientLabels(self.client_ids[0].Basename(),
                                              u"GRR", [u"foo"])
            idx.AddClientLabels(self.client_ids[0].Basename(), [u"foo"])

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

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

        # Relational labels.
        labels = data_store.REL_DB.ReadClientLabels(
            self.client_ids[0].Basename())
        self.assertLen(labels, 1)
        self.assertEqual(labels[0].name, u"foo")
        self.assertEqual(labels[0].owner, u"GRR")
        # The label is still in the index.
        self.assertEqual(idx.LookupClients(["label:foo"]),
                         [self.client_ids[0].Basename()])
Esempio n. 2
0
    def testRemovesUserLabelFromSingleClient(self):
        with aff4.FACTORY.Open(self.client_ids[0], mode="rw",
                               token=self.token) as grr_client:
            grr_client.AddLabels([u"foo", u"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,
                                              [u"foo", u"bar"])

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

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

        # Relational labels.
        labels = data_store.REL_DB.ReadClientLabels(
            self.client_ids[0].Basename())
        self.assertLen(labels, 1)
        self.assertEqual(labels[0].name, u"bar")
        self.assertEqual(labels[0].owner, self.token.username)
Esempio n. 3
0
  def testDoesNotRemoveSystemLabelFromSingleClient(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", 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(), "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)

    # Relational labels.
    labels = data_store.REL_DB.ReadClientLabels(self.client_ids[0].Basename())
    self.assertEqual(len(labels), 1)
    # The label is still in the index.
    self.assertEqual(
        idx.LookupClients(["label:foo"]), [self.client_ids[0].Basename()])
Esempio n. 4
0
  def testRemovesUserLabelFromSingleClient(self):
    data_store.REL_DB.WriteClientMetadata(
        self.client_ids[0], fleetspeak_enabled=False)
    data_store.REL_DB.AddClientLabels(self.client_ids[0], self.context.username,
                                      [u"foo", u"bar"])

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

    labels = data_store.REL_DB.ReadClientLabels(self.client_ids[0])
    self.assertLen(labels, 1)
    self.assertEqual(labels[0].name, u"bar")
    self.assertEqual(labels[0].owner, self.context.username)
Esempio n. 5
0
  def testDoesNotRemoveSystemLabelFromSingleClient(self):
    data_store.REL_DB.WriteClientMetadata(
        self.client_ids[0], fleetspeak_enabled=False)
    data_store.REL_DB.AddClientLabels(self.client_ids[0], u"GRR", [u"foo"])
    idx = client_index.ClientIndex()
    idx.AddClientLabels(self.client_ids[0], [u"foo"])

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

    labels = data_store.REL_DB.ReadClientLabels(self.client_ids[0])
    self.assertLen(labels, 1)
    # The label is still in the index.
    self.assertEqual(idx.LookupClients(["label:foo"]), [self.client_ids[0]])
Esempio n. 6
0
  def testRemovesUserLabelWhenSystemLabelWithSimilarNameAlsoExists(self):
    data_store.REL_DB.WriteClientMetadata(
        self.client_ids[0], fleetspeak_enabled=False)
    data_store.REL_DB.AddClientLabels(self.client_ids[0], self.context.username,
                                      [u"foo"])
    data_store.REL_DB.AddClientLabels(self.client_ids[0], u"GRR", [u"foo"])
    idx = client_index.ClientIndex()
    idx.AddClientLabels(self.client_ids[0], [u"foo"])

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

    labels = data_store.REL_DB.ReadClientLabels(self.client_ids[0])
    self.assertLen(labels, 1)
    self.assertEqual(labels[0].name, u"foo")
    self.assertEqual(labels[0].owner, u"GRR")
    # The label is still in the index.
    self.assertEqual(idx.LookupClients(["label:foo"]), [self.client_ids[0]])