def Run(self): with test_lib.FakeTime(42): self.CreateUser("approver") cron_manager = aff4_cronjobs.GetCronManager() cron_args = rdf_cronjobs.CreateCronJobFlowArgs(periodicity="1d", allow_overruns=False) cron_id = cron_manager.CreateJob(cron_args=cron_args, token=self.token) def ReplaceCronAndApprovalIds(): approvals = self.ListCronJobApprovals() return { approvals[0].id: "approval:112233", cron_id: "CronJob_123456" } with test_lib.FakeTime(126): self.Check("CreateCronJobApproval", args=user_plugin.ApiCreateCronJobApprovalArgs( cron_job_id=cron_id, approval=user_plugin.ApiCronJobApproval( reason="really important reason!", notified_users=["approver1", "approver2"], email_cc_addresses=["*****@*****.**"])), replace=ReplaceCronAndApprovalIds)
def Run(self): with test_lib.FakeTime(42): self.CreateAdminUser("requestor") cron_manager = aff4_cronjobs.GetCronManager() cron_args = rdf_cronjobs.CreateCronJobFlowArgs( periodicity="1d", allow_overruns=False) cron_id = cron_manager.CreateJob(cron_args=cron_args, token=self.token) with test_lib.FakeTime(44): approval_id = self.RequestCronJobApproval( cron_id, approver=self.token.username, requestor="requestor", reason="foo") with test_lib.FakeTime(126): self.Check("GrantCronJobApproval", args=user_plugin.ApiGrantCronJobApprovalArgs( cron_job_id=cron_id, approval_id=approval_id, username="******"), replace={ cron_id: "CronJob_123456", approval_id: "approval:111111" })
def setUp(self): super(ApiListCronJobFlowsHandlerRegressionTest, self).setUp() with test_lib.FakeTime(44): cron_args = rdf_cronjobs.CreateCronJobFlowArgs(periodicity="7d", lifetime="1d") cron_args.flow_runner_args.flow_name = self.flow_name cronjobs.GetCronManager().CreateJob(cron_args, job_id=self.flow_name, token=self.token) cronjobs.GetCronManager().RunOnce(token=self.token)
def ScheduleSystemCronFlows(names=None, token=None): """Schedule all the SystemCronFlows found.""" errors = [] for name in config.CONFIG["Cron.disabled_system_jobs"]: try: cls = registry.FlowRegistry.FlowClassByName(name) except ValueError: errors.append("No such flow: %s." % name) continue if not aff4.issubclass(cls, SystemCronFlow): errors.append( "Disabled system cron job name doesn't correspond to " "a flow inherited from SystemCronFlow: %s" % name) if names is None: names = registry.FlowRegistry.FLOW_REGISTRY.keys() for name in names: cls = registry.FlowRegistry.FlowClassByName(name) if aff4.issubclass(cls, SystemCronFlow): cron_args = rdf_cronjobs.CreateCronJobFlowArgs( periodicity=cls.frequency) cron_args.flow_runner_args.flow_name = name cron_args.lifetime = cls.lifetime cron_args.allow_overruns = cls.allow_overruns cron_args.start_time = GetStartTime(cls) if cls.disabled: disabled = True else: disabled = name in config.CONFIG["Cron.disabled_system_jobs"] manager = GetCronManager() if data_store.RelationalDBReadEnabled(): manager.CreateJob(cron_args=cron_args, job_id=name, disabled=disabled) else: manager.CreateJob(cron_args=cron_args, job_id=name, token=token, disabled=disabled) if errors: raise ValueError( "Error(s) while parsing Cron.disabled_system_jobs: %s" % errors)
def setUp(self): super(TestCronView, self).setUp() for flow_name in [ cron_system.GRRVersionBreakDown.__name__, cron_system.OSBreakDown.__name__, cron_system.LastAccessStats.__name__ ]: cron_args = rdf_cronjobs.CreateCronJobFlowArgs(periodicity="7d", lifetime="1d") cron_args.flow_runner_args.flow_name = flow_name cronjobs.GetCronManager().CreateJob(cron_args, job_id=flow_name, token=self.token) cronjobs.GetCronManager().RunOnce(token=self.token)
def testRendersRequestedCronJobApproval(self): cron_manager = aff4_cronjobs.GetCronManager() cron_args = rdf_cronjobs.CreateCronJobFlowArgs( periodicity="1d", allow_overruns=False) cron_job_id = cron_manager.CreateJob(cron_args=cron_args, token=self.token) self.RequestCronJobApproval( cron_job_id, reason=self.token.reason, approver="approver", requestor=self.token.username) args = user_plugin.ApiListCronJobApprovalsArgs() result = self.handler.Handle(args, token=self.token) self.assertEqual(len(result.items), 1)
def setUp(self): super(ApiCreateCronJobApprovalHandlerTest, self).setUp() self.SetUpApprovalTest() cron_manager = aff4_cronjobs.GetCronManager() cron_args = rdf_cronjobs.CreateCronJobFlowArgs( periodicity="1d", allow_overruns=False) cron_id = cron_manager.CreateJob(cron_args=cron_args, token=self.token) self.handler = user_plugin.ApiCreateCronJobApprovalHandler() self.args = user_plugin.ApiCreateCronJobApprovalArgs(cron_job_id=cron_id) self.args.approval.reason = self.token.reason self.args.approval.notified_users = ["approver"] self.args.approval.email_cc_addresses = ["*****@*****.**"]
def CreateCronJob(self, flow_name, periodicity="1d", lifetime="7d", description="", disabled=False, token=None): cron_args = rdf_cronjobs.CreateCronJobFlowArgs(periodicity=periodicity, lifetime=lifetime, description=description) cron_args.flow_runner_args.flow_name = flow_name return cronjobs.GetCronManager().CreateJob(cron_args, job_id=flow_name, disabled=disabled, token=token)
def Handle(self, args, token=None): args.flow_args.hunt_runner_args.hunt_name = "GenericHunt" # TODO(user): The following should be asserted in a more elegant way. # Also, it's not clear whether cron job scheduling UI is used often enough # to justify its existence. We should check with opensource users whether # they find this feature useful and if not, deprecate it altogether. if args.flow_name != standard.CreateAndRunGenericHuntFlow.__name__: raise ValueError( "Only CreateAndRunGenericHuntFlow flows are supported " "here (got: %s)." % args.flow_name) # Clear all fields marked with HIDDEN, except for output_plugins - they are # marked HIDDEN, because we have a separate UI for them, not because they # shouldn't be shown to the user at all. # # TODO(user): Refactor the code to remove the HIDDEN label from # FlowRunnerArgs.output_plugins. args.flow_runner_args.ClearFieldsWithLabel( rdf_structs.SemanticDescriptor.Labels.HIDDEN, exceptions="output_plugins") if not args.flow_runner_args.flow_name: args.flow_runner_args.flow_name = args.flow_name cron_args = rdf_cronjobs.CreateCronJobFlowArgs( description=args.description, periodicity=args.periodicity, flow_runner_args=args.flow_runner_args, flow_args=args.flow_args, allow_overruns=args.allow_overruns, lifetime=args.lifetime) name = aff4_cronjobs.GetCronManager().CreateJob(cron_args=cron_args, disabled=True, token=token) fd = aff4_cronjobs.GetCronManager().ReadJob(name) return ApiCronJob().InitFromObject(fd)
def setUp(self): super(CleanCronJobsTest, self).setUp() with test_lib.FakeTime(40): cron_args = rdf_cronjobs.CreateCronJobFlowArgs( periodicity=RetentionTestSystemCronJob.frequency) cron_args.flow_runner_args.flow_name = RetentionTestSystemCronJob.__name__ cron_args.lifetime = RetentionTestSystemCronJob.lifetime self.cron_jobs_names = [] self.cron_jobs_names.append(cronjobs.GetCronManager().CreateJob( cron_args=cron_args, job_id="Foo", token=self.token, disabled=False)) self.cron_jobs_names.append(cronjobs.GetCronManager().CreateJob( cron_args=cron_args, job_id="Bar", token=self.token, disabled=False)) for i in range(self.NUM_CRON_RUNS): with test_lib.FakeTime(40 + 60 * i): cronjobs.GetCronManager().RunOnce(token=self.token, force=True)