Beispiel #1
0
    def testWriteClientSnapshotHistoryRaiseTypeError(self):
        client = rdf_objects.ClientMetadata()
        client.os_version = "16.04"
        client.timestamp = rdfvalue.RDFDatetime.FromHumanReadable("2010-04-10")

        with self.assertRaisesRegexp(TypeError, "Expected"):
            self.db.WriteClientSnapshotHistory([client])
Beispiel #2
0
 def MultiReadClientMetadata(self, client_ids, cursor=None):
     """Reads ClientMetadata records for a list of clients."""
     ids = [
         mysql_utils.ClientIDToInt(client_id) for client_id in client_ids
     ]
     query = (
         "SELECT client_id, fleetspeak_enabled, certificate, last_ping, "
         "last_clock, last_ip, last_foreman, first_seen, "
         "last_crash_timestamp, last_startup_timestamp FROM "
         "clients WHERE client_id IN ({})").format(", ".join(["%s"] *
                                                             len(ids)))
     ret = {}
     cursor.execute(query, ids)
     while True:
         row = cursor.fetchone()
         if not row:
             break
         cid, fs, crt, ping, clk, ip, foreman, first, lct, lst = row
         ret[mysql_utils.IntToClientID(cid)] = 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(lst),
             last_crash_timestamp=mysql_utils.MysqlToRDFDatetime(lct))
     return ret
Beispiel #3
0
    def MultiReadClientMetadata(self, client_ids):
        """Reads ClientMetadata records for a list of clients."""
        res = {}
        for client_id in client_ids:
            md = self.metadatas.get(client_id, None)
            if md is None:
                continue

            metadata = rdf_objects.ClientMetadata(
                certificate=md.get("certificate"),
                fleetspeak_enabled=md.get("fleetspeak_enabled"),
                first_seen=md.get("first_seen"),
                ping=md.get("ping"),
                clock=md.get("clock"),
                ip=md.get("ip"),
                last_foreman_time=md.get("last_foreman_time"),
                last_crash_timestamp=md.get("last_crash_timestamp"),
                startup_info_timestamp=md.get("startup_info_timestamp"))

            fsvi = md.get("last_fleetspeak_validation_info")
            if fsvi is not None:
                pb = rdf_client.FleetspeakValidationInfo.FromSerializedBytes(
                    fsvi)
                metadata.last_fleetspeak_validation_info = pb

            res[client_id] = metadata

        return res
Beispiel #4
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 db_utils.IntToClientID(prev_cid), c_full_info

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

        if client_obj is not None:
          l_snapshot = rdf_objects.ClientSnapshot.FromSerializedString(
              client_obj)
          l_snapshot.timestamp = mysql_utils.TimestampToRDFDatetime(
              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=db_utils.IntToClientID(cid))

        if last_startup_obj is not None:
          startup_info = rdf_client.StartupInfo.FromSerializedString(
              last_startup_obj)
          startup_info.timestamp = mysql_utils.TimestampToRDFDatetime(
              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 db_utils.IntToClientID(prev_cid), c_full_info
Beispiel #5
0
    def MultiReadClientMetadata(self, client_ids, cursor=None):
        """Reads ClientMetadata records for a list of clients."""
        ids = [db_utils.ClientIDToInt(client_id) for client_id in client_ids]
        query = """
      SELECT
        client_id,
        fleetspeak_enabled,
        certificate,
        UNIX_TIMESTAMP(last_ping),
        UNIX_TIMESTAMP(last_clock),
        last_ip,
        UNIX_TIMESTAMP(last_foreman),
        UNIX_TIMESTAMP(first_seen),
        UNIX_TIMESTAMP(last_crash_timestamp),
        UNIX_TIMESTAMP(last_startup_timestamp),
        last_fleetspeak_validation_info
      FROM
        clients
      WHERE
        client_id IN ({})""".format(", ".join(["%s"] * len(ids)))
        ret = {}
        cursor.execute(query, ids)
        while True:
            row = cursor.fetchone()
            if not row:
                break
            cid, fs, crt, ping, clk, ip, foreman, first, lct, lst, fsvi = row
            metadata = rdf_objects.ClientMetadata(
                certificate=crt,
                fleetspeak_enabled=fs,
                first_seen=mysql_utils.TimestampToRDFDatetime(first),
                ping=mysql_utils.TimestampToRDFDatetime(ping),
                clock=mysql_utils.TimestampToRDFDatetime(clk),
                ip=mysql_utils.StringToRDFProto(
                    rdf_client_network.NetworkAddress, ip),
                last_foreman_time=mysql_utils.TimestampToRDFDatetime(foreman),
                startup_info_timestamp=mysql_utils.TimestampToRDFDatetime(lst),
                last_crash_timestamp=mysql_utils.TimestampToRDFDatetime(lct))

            if fsvi:
                metadata.last_fleetspeak_validation_info = (
                    rdf_client.FleetspeakValidationInfo.FromSerializedBytes(
                        fsvi))

            ret[db_utils.IntToClientID(cid)] = metadata

        return ret
Beispiel #6
0
    def MultiReadClientMetadata(self, client_ids):
        """Reads ClientMetadata records for a list of clients."""
        res = {}
        for client_id in client_ids:
            md = self.metadatas.get(client_id, {})
            res[client_id] = rdf_objects.ClientMetadata(
                certificate=md.get("certificate"),
                fleetspeak_enabled=md.get("fleetspeak_enabled"),
                first_seen=md.get("first_seen"),
                ping=md.get("ping"),
                clock=md.get("clock"),
                ip=md.get("ip"),
                last_foreman_time=md.get("last_foreman_time"),
                last_crash_timestamp=md.get("last_crash_timestamp"),
                startup_info_timestamp=md.get("startup_info_timestamp"))

        return res