示例#1
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 = 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.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 = 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 = 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 = 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(
                    objects.ClientLabel(name=label_name, owner=label_owner))

        if c_full_info:
            yield mysql_utils.IntToClientID(prev_cid), c_full_info
示例#2
0
    def HandleRelationalDB(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(objects.ClientLabel(name=name))

        return ApiListClientsLabelsResult(
            items=sorted(label_objects, key=lambda l: l.name))
示例#3
0
    def ReadAllClientLabels(self):
        """Lists all client labels known to the system."""
        result = set()
        for labels_dict in self.labels.values():
            for owner, names in labels_dict.items():
                for name in names:
                    result.add(objects.ClientLabel(owner=owner, name=name))

        return list(result)
示例#4
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(objects.ClientLabel(name=label))

        return ApiListClientsLabelsResult(items=label_objects)
示例#5
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 owner_dict.items():
             for l in labels:
                 res[client_id].append(
                     objects.ClientLabel(owner=owner, name=l))
         res[client_id].sort(key=lambda label: (label.owner, label.name))
     return res
示例#6
0
    def ReadAllClientLabels(self, cursor=None):
        """Reads the user labels for a list of clients."""

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

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

        result.sort(key=lambda label: (label.owner, label.name))
        return result
示例#7
0
    def MultiReadClientLabels(self, client_ids, cursor=None):
        """Reads the user labels for a list of clients."""

        int_ids = [mysql_utils.ClientIDToInt(cid) for cid in client_ids]
        query = ("SELECT client_id, owner, 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[mysql_utils.IntToClientID(client_id)].append(
                objects.ClientLabel(name=utils.SmartUnicode(label),
                                    owner=owner))

        for r in ret.values():
            r.sort(key=lambda label: (label.owner, label.name))
        return ret
示例#8
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 = [
            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