Exemplo n.º 1
0
  def CreateJob(self, cron_args=None, job_id=None, token=None, enabled=True):
    """Creates a cron job that runs given flow with a given frequency.

    Args:
      cron_args: A protobuf of type rdf_cronjobs.CreateCronJobArgs.

      job_id: Use this job_id instead of an autogenerated unique name (used
              for system cron jobs - we want them to have well-defined
              persistent name).

      token: Security token used for data store access.

      enabled: If False, the job object will be created, but will be disabled.

    Returns:
      Name of the cron job created.
    """
    if not job_id:
      uid = random.UInt16()
      job_id = "%s_%s" % (cron_args.flow_name, uid)

    flow_runner_args = rdf_flow_runner.FlowRunnerArgs(
        flow_name="CreateAndRunGenericHuntFlow")

    flow_args = rdf_hunts.CreateGenericHuntFlowArgs()
    flow_args.hunt_args.flow_args = cron_args.flow_args
    flow_args.hunt_args.flow_runner_args.flow_name = cron_args.flow_name
    flow_args.hunt_runner_args = cron_args.hunt_runner_args
    flow_args.hunt_runner_args.hunt_name = "GenericHunt"

    create_cron_args = rdf_cronjobs.CreateCronJobFlowArgs(
        description=cron_args.description,
        periodicity=cron_args.frequency,
        flow_runner_args=flow_runner_args,
        flow_args=flow_args,
        allow_overruns=cron_args.allow_overruns,
        lifetime=cron_args.lifetime)

    cron_job_urn = self.CRON_JOBS_PATH.Add(job_id)
    with aff4.FACTORY.Create(
        cron_job_urn,
        aff4_type=CronJob,
        mode="rw",
        token=token,
        force_new_version=False) as cron_job:

      # If the cronjob was already present we don't want to overwrite the
      # original start_time.
      existing_cron_args = cron_job.Get(cron_job.Schema.CRON_ARGS)
      if existing_cron_args and existing_cron_args.start_time:
        create_cron_args.start_time = existing_cron_args.start_time

      if create_cron_args != existing_cron_args:
        cron_job.Set(cron_job.Schema.CRON_ARGS(create_cron_args))

      cron_job.Set(cron_job.Schema.DISABLED(not enabled))

    return job_id
Exemplo n.º 2
0
  def CreateJob(self, cron_args=None, job_id=None, enabled=True):
    """Creates a cron job that runs given flow with a given frequency.

    Args:
      cron_args: A protobuf of type rdf_cronjobs.CreateCronJobArgs.
      job_id: Use this job_id instead of an autogenerated unique name (used for
        system cron jobs - we want them to have well-defined persistent name).
      enabled: If False, the job object will be created, but will be disabled.

    Returns:
      URN of the cron job created.

    Raises:
      ValueError: This function expects an arg protobuf that starts a
                  CreateAndRunGenericHuntFlow flow. If the args specify
                  something else, ValueError is raised.
    """
    if not cron_args.flow_name:
      raise ValueError("Unspecified flow name")

    if not job_id:
      # TODO: UInt16 is too small for randomly generated IDs.
      uid = random.UInt16()
      job_id = "%s_%s" % (cron_args.flow_name, uid)

    args = rdf_cronjobs.CronJobAction(
        action_type=rdf_cronjobs.CronJobAction.ActionType.HUNT_CRON_ACTION,
        hunt_cron_action=rdf_cronjobs.HuntCronAction(
            flow_name=cron_args.flow_name,
            flow_args=cron_args.flow_args,
            hunt_runner_args=cron_args.hunt_runner_args))

    job = rdf_cronjobs.CronJob(
        cron_job_id=job_id,
        description=cron_args.description,
        frequency=cron_args.frequency,
        lifetime=cron_args.lifetime,
        allow_overruns=cron_args.allow_overruns,
        args=args,
        enabled=enabled)
    data_store.REL_DB.WriteCronJob(job)

    return job_id
Exemplo n.º 3
0
 def testSpecific(self):
     self.assertEqual(random.UInt16(), 0xDEAD)
     self.assertEqual(random.UInt16(), 0xBEEF)
Exemplo n.º 4
0
    def testMax(self, urandom):
        del urandom  # Unused.

        for _ in range(10000):
            self.assertEqual(random.UInt16(), 2**16 - 1)
Exemplo n.º 5
0
    def testMin(self, urandom):
        del urandom  # Unused.

        for _ in range(10000):
            if random.UInt16() != 0:
                self.assertEqual(random.UInt16(), 0)
Exemplo n.º 6
0
 def _CreateCronJob(self):
     return rdf_cronjobs.CronJob(cron_job_id="job_%s" % random.UInt16(),
                                 enabled=True)