Exemplo n.º 1
0
    def Handle(self, args, token=None):
        labels = data_store.REL_DB.ReadAllClientLabels()

        label_objects = []
        for name in set(l.name for l in labels):
            label_objects.append(rdf_objects.ClientLabel(name=name))

        return ApiListClientsLabelsResult(
            items=sorted(label_objects, key=lambda l: l.name))
Exemplo n.º 2
0
    def ReadAllClientLabels(self):
        """Lists all client labels known to the system."""
        result = set()
        for labels_dict in itervalues(self.labels):
            for owner, names in iteritems(labels_dict):
                for name in names:
                    result.add(rdf_objects.ClientLabel(owner=owner, name=name))

        return list(result)
Exemplo n.º 3
0
 def ReadAllClientLabels(self):
   """Lists all client labels known to the system."""
   results = {}
   for labels_dict in self.labels.values():
     for owner, names in labels_dict.items():
       for name in names:
         results[(owner, name)] = rdf_objects.ClientLabel(
             owner=owner, name=name)
   return list(results.values())
Exemplo n.º 4
0
    def testReadAllLabelsReturnsLabelsFromSingleClient(self):
        d = self.db

        client_id = self.InitializeClient()

        d.AddClientLabels(client_id, "owner1", [u"foo"])

        all_labels = d.ReadAllClientLabels()
        self.assertEqual(all_labels,
                         [rdf_objects.ClientLabel(name="foo", owner="owner1")])
Exemplo n.º 5
0
    def HandleLegacy(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(rdf_objects.ClientLabel(name=label))

        return ApiListClientsLabelsResult(items=label_objects)
Exemplo n.º 6
0
 def MultiReadClientLabels(self, client_ids):
   """Reads the user labels for a list of clients."""
   res = {}
   for client_id in client_ids:
     res[client_id] = []
     owner_dict = self.labels.get(client_id, {})
     for owner, labels in iteritems(owner_dict):
       for l in labels:
         res[client_id].append(rdf_objects.ClientLabel(owner=owner, name=l))
     res[client_id].sort(key=lambda label: (label.owner, label.name))
   return res
Exemplo n.º 7
0
  def ReadAllClientLabels(self, cursor=None):
    """Reads the user labels for a list of clients."""

    cursor.execute("SELECT DISTINCT owner_username, label FROM client_labels")

    result = []
    for owner, label in cursor.fetchall():
      result.append(rdf_objects.ClientLabel(name=label, owner=owner))

    result.sort(key=lambda label: (label.owner, label.name))
    return result
Exemplo n.º 8
0
    def MultiReadClientLabels(self, client_ids, cursor=None):
        """Reads the user labels for a list of clients."""

        int_ids = [db_utils.ClientIDToInt(cid) for cid in client_ids]
        query = ("SELECT client_id, owner_username, label "
                 "FROM client_labels "
                 "WHERE client_id IN ({})").format(", ".join(["%s"] *
                                                             len(client_ids)))

        ret = {client_id: [] for client_id in client_ids}
        cursor.execute(query, int_ids)
        for client_id, owner, label in cursor.fetchall():
            ret[db_utils.IntToClientID(client_id)].append(
                rdf_objects.ClientLabel(name=label, owner=owner))

        for r in itervalues(ret):
            r.sort(key=lambda label: (label.owner, label.name))
        return ret
Exemplo n.º 9
0
  def testReadClientFullInfoReturnsCorrectResult(self):
    d = self.db

    client_id = self.InitializeClient()

    cl = rdf_objects.ClientSnapshot(
        client_id=client_id,
        knowledge_base=rdf_client.KnowledgeBase(fqdn="test1234.examples.com"),
        kernel="12.3")
    d.WriteClientSnapshot(cl)
    d.WriteClientMetadata(client_id, certificate=CERT)
    si = rdf_client.StartupInfo(boot_time=1)
    d.WriteClientStartupInfo(client_id, si)
    d.AddClientLabels(client_id, "test_owner", ["test_label"])

    full_info = d.ReadClientFullInfo(client_id)
    self.assertEqual(full_info.last_snapshot, cl)
    self.assertEqual(full_info.metadata.certificate, CERT)
    self.assertEqual(full_info.last_startup_info, si)
    self.assertEqual(
        full_info.labels,
        [rdf_objects.ClientLabel(owner="test_owner", name="test_label")])
Exemplo n.º 10
0
    def _ResponseToClientsFullInfo(self, response):
        """Creates a ClientFullInfo object from a database response."""
        c_full_info = None
        prev_cid = None
        for row in response:
            (cid, fs, crt, ping, clk, ip, foreman, first, last_client_ts,
             last_crash_ts, last_startup_ts, client_obj, client_startup_obj,
             last_startup_obj, label_owner, label_name) = row

            if cid != prev_cid:
                if c_full_info:
                    yield mysql_utils.IntToClientID(prev_cid), c_full_info

                metadata = rdf_objects.ClientMetadata(
                    certificate=crt,
                    fleetspeak_enabled=fs,
                    first_seen=mysql_utils.MysqlToRDFDatetime(first),
                    ping=mysql_utils.MysqlToRDFDatetime(ping),
                    clock=mysql_utils.MysqlToRDFDatetime(clk),
                    ip=mysql_utils.StringToRDFProto(
                        rdf_client_network.NetworkAddress, ip),
                    last_foreman_time=mysql_utils.MysqlToRDFDatetime(foreman),
                    startup_info_timestamp=mysql_utils.MysqlToRDFDatetime(
                        last_startup_ts),
                    last_crash_timestamp=mysql_utils.MysqlToRDFDatetime(
                        last_crash_ts))

                if client_obj is not None:
                    l_snapshot = rdf_objects.ClientSnapshot.FromSerializedString(
                        client_obj)
                    l_snapshot.timestamp = mysql_utils.MysqlToRDFDatetime(
                        last_client_ts)
                    l_snapshot.startup_info = rdf_client.StartupInfo.FromSerializedString(
                        client_startup_obj)
                    l_snapshot.startup_info.timestamp = l_snapshot.timestamp
                else:
                    l_snapshot = rdf_objects.ClientSnapshot(
                        client_id=mysql_utils.IntToClientID(cid))

                if last_startup_obj is not None:
                    startup_info = rdf_client.StartupInfo.FromSerializedString(
                        last_startup_obj)
                    startup_info.timestamp = mysql_utils.MysqlToRDFDatetime(
                        last_startup_ts)
                else:
                    startup_info = None

                prev_cid = cid
                c_full_info = rdf_objects.ClientFullInfo(
                    metadata=metadata,
                    labels=[],
                    last_snapshot=l_snapshot,
                    last_startup_info=startup_info)

            if label_owner and label_name:
                c_full_info.labels.append(
                    rdf_objects.ClientLabel(name=label_name,
                                            owner=label_owner))

        if c_full_info:
            yield mysql_utils.IntToClientID(prev_cid), c_full_info
Exemplo n.º 11
0
    def InitFromAff4Object(self, client_obj, include_metadata=True):
        # TODO(amoser): Deprecate all urns.
        self.urn = client_obj.urn

        self.client_id = client_obj.urn.Basename()

        self.agent_info = client_obj.Get(client_obj.Schema.CLIENT_INFO)
        self.hardware_info = client_obj.Get(client_obj.Schema.HARDWARE_INFO)
        self.os_info = rdf_client.Uname(
            system=client_obj.Get(client_obj.Schema.SYSTEM),
            release=client_obj.Get(client_obj.Schema.OS_RELEASE),
            # TODO(user): Check if ProtoString.Validate should be fixed
            # to do an isinstance() check on a value. Is simple type
            # equality check used there for performance reasons?
            version=utils.SmartStr(
                client_obj.Get(client_obj.Schema.OS_VERSION, "")),
            kernel=client_obj.Get(client_obj.Schema.KERNEL),
            machine=client_obj.Get(client_obj.Schema.ARCH),
            fqdn=(client_obj.Get(client_obj.Schema.FQDN)
                  or client_obj.Get(client_obj.Schema.HOSTNAME)),
            install_date=client_obj.Get(client_obj.Schema.INSTALL_DATE))
        self.knowledge_base = client_obj.Get(client_obj.Schema.KNOWLEDGE_BASE)
        self.memory_size = client_obj.Get(client_obj.Schema.MEMORY_SIZE)

        self.first_seen_at = client_obj.Get(client_obj.Schema.FIRST_SEEN)

        if include_metadata:
            ping = client_obj.Get(client_obj.Schema.PING)
            if ping:
                self.last_seen_at = ping

            booted = client_obj.Get(client_obj.Schema.LAST_BOOT_TIME)
            if booted:
                self.last_booted_at = booted

            clock = client_obj.Get(client_obj.Schema.CLOCK)
            if clock:
                self.last_clock = clock

            last_crash = client_obj.Get(client_obj.Schema.LAST_CRASH)
            if last_crash is not None:
                self.last_crash_at = last_crash.timestamp

            self.fleetspeak_enabled = bool(
                client_obj.Get(client_obj.Schema.FLEETSPEAK_ENABLED))

        self.labels = [
            rdf_objects.ClientLabel(name=l.name, owner=l.owner)
            for l in client_obj.GetLabels()
        ]
        self.interfaces = client_obj.Get(client_obj.Schema.INTERFACES)
        kb = client_obj.Get(client_obj.Schema.KNOWLEDGE_BASE)
        if kb and kb.users:
            self.users = sorted(kb.users, key=lambda user: user.username)
        self.volumes = client_obj.Get(client_obj.Schema.VOLUMES)

        type_obj = client_obj.Get(client_obj.Schema.TYPE)
        if type_obj:
            # Without self.Set self.age would reference "age" attribute instead of a
            # protobuf field.
            self.Set("age", type_obj.age)

        self.cloud_instance = client_obj.Get(client_obj.Schema.CLOUD_INSTANCE)
        return self