Example #1
0
 def MultiReadClientSnapshot(self, client_ids, cursor=None):
     """Reads the latest client snapshots for a list of clients."""
     int_ids = [mysql_utils.ClientIDToInt(cid) for cid in client_ids]
     query = (
         "SELECT h.client_id, h.client_snapshot, h.timestamp, s.startup_info "
         "FROM clients as c, client_snapshot_history as h, "
         "client_startup_history as s "
         "WHERE h.client_id = c.client_id "
         "AND s.client_id = c.client_id "
         "AND h.timestamp = c.last_client_timestamp "
         "AND s.timestamp = c.last_startup_timestamp "
         "AND c.client_id IN ({})").format(", ".join(["%s"] *
                                                     len(client_ids)))
     ret = {cid: None for cid in client_ids}
     cursor.execute(query, int_ids)
     while True:
         row = cursor.fetchone()
         if not row:
             break
         cid, snapshot, timestamp, startup_info = row
         client_obj = mysql_utils.StringToRDFProto(
             rdf_objects.ClientSnapshot, snapshot)
         client_obj.startup_info = mysql_utils.StringToRDFProto(
             rdf_client.StartupInfo, startup_info)
         client_obj.timestamp = mysql_utils.MysqlToRDFDatetime(timestamp)
         ret[mysql_utils.IntToClientID(cid)] = client_obj
     return ret
Example #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
Example #3
0
def _ResponseToApprovalsWithGrants(response):
    """Converts a generator with approval rows into ApprovalRequest objects."""
    prev_triplet = None
    cur_approval_request = None
    for (approval_id_int, approval_timestamp, approval_request_bytes,
         grantor_username, grant_timestamp) in response:

        cur_triplet = (approval_id_int, approval_timestamp,
                       approval_request_bytes)

        if cur_triplet != prev_triplet:
            prev_triplet = cur_triplet

            if cur_approval_request:
                yield cur_approval_request

            cur_approval_request = mysql_utils.StringToRDFProto(
                rdf_objects.ApprovalRequest, approval_request_bytes)
            cur_approval_request.approval_id = _IntToApprovalID(
                approval_id_int)
            cur_approval_request.timestamp = mysql_utils.TimestampToRDFDatetime(
                approval_timestamp)

        if grantor_username and grant_timestamp:
            cur_approval_request.grants.append(
                rdf_objects.ApprovalGrant(
                    grantor_username=grantor_username,
                    timestamp=mysql_utils.TimestampToRDFDatetime(
                        grant_timestamp)))

    if cur_approval_request:
        yield cur_approval_request
Example #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
Example #5
0
    def ReadApprovalRequest(self,
                            requestor_username,
                            approval_id,
                            cursor=None):
        """Reads an approval request object with a given id."""

        query = ("""
        SELECT
            ar.approval_id,
            UNIX_TIMESTAMP(ar.timestamp),
            ar.approval_request,
            u.username,
            UNIX_TIMESTAMP(ag.timestamp)
        FROM approval_request ar
        LEFT JOIN approval_grant ag USING (username_hash, approval_id)
        LEFT JOIN grr_users u ON u.username_hash = ag.grantor_username_hash
        WHERE ar.approval_id = %s AND ar.username_hash = %s
        """)

        cursor.execute(query, [
            _ApprovalIDToInt(approval_id),
            mysql_utils.Hash(requestor_username)
        ])
        res = cursor.fetchall()
        if not res:
            raise db.UnknownApprovalRequestError("Approval '%s' not found." %
                                                 approval_id)

        approval_id_int, timestamp, approval_request_bytes, _, _ = res[0]

        approval_request = mysql_utils.StringToRDFProto(
            rdf_objects.ApprovalRequest, approval_request_bytes)
        approval_request.approval_id = _IntToApprovalID(approval_id_int)
        approval_request.timestamp = mysql_utils.TimestampToRDFDatetime(
            timestamp)

        for _, _, _, grantor_username, timestamp in res:
            if not grantor_username:
                continue

            # Note: serialized approval_request objects are guaranteed to not
            # have any grants.
            approval_request.grants.append(
                rdf_objects.ApprovalGrant(
                    grantor_username=grantor_username,
                    timestamp=mysql_utils.TimestampToRDFDatetime(timestamp)))

        return approval_request
Example #6
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
Example #7
0
    def ReadApprovalRequest(self,
                            requestor_username,
                            approval_id,
                            cursor=None):
        """Reads an approval request object with a given id."""

        query = (
            "SELECT approval_request.approval_id, approval_request.timestamp, "
            "approval_request.approval_request, "
            "approval_grant.grantor_username, approval_grant.timestamp "
            "FROM approval_request "
            "LEFT JOIN approval_grant USING (username, approval_id) "
            "WHERE approval_request.approval_id=%s "
            "AND approval_request.username=%s")

        cursor.execute(query,
                       [_ApprovalIDToInt(approval_id), requestor_username])
        res = cursor.fetchall()
        if not res:
            raise db.UnknownApprovalRequestError("Approval '%s' not found." %
                                                 approval_id)

        approval_id_int, timestamp, approval_request_bytes, _, _ = res[0]

        approval_request = mysql_utils.StringToRDFProto(
            rdf_objects.ApprovalRequest, approval_request_bytes)
        approval_request.approval_id = _IntToApprovalID(approval_id_int)
        approval_request.timestamp = mysql_utils.MysqlToRDFDatetime(timestamp)

        for _, _, _, grantor_username, timestamp in res:
            if not grantor_username:
                continue

            # Note: serialized approval_request objects are guaranteed to not
            # have any grants.
            approval_request.grants.append(
                rdf_objects.ApprovalGrant(
                    grantor_username=grantor_username,
                    timestamp=mysql_utils.MysqlToRDFDatetime(timestamp)))

        return approval_request