def Decorated(this, token, *args, **kwargs): try: result = func(this, token, *args, **kwargs) if (self.access_type == "data_store_access" and token and token.username in aff4_users.GRRUser.SYSTEM_USERS): # Logging internal system database access is noisy and useless. return result if logging.getLogger().isEnabledFor(logging.DEBUG): logging.debug( u"%s GRANTED by %s to %s%s (%s, %s) with reason: %s", utils.SmartUnicode(self.access_type), utils.GetName(this.__class__.__name__), utils.SmartUnicode(token and token.username), utils.SmartUnicode( token and token.supervisor and " (supervisor)" or ""), utils.SmartUnicode(args), utils.SmartUnicode(kwargs), utils.SmartUnicode(token and token.reason)) return result except access_control.UnauthorizedAccess: if logging.getLogger().isEnabledFor(logging.DEBUG): logging.debug( u"%s REJECTED by %s to %s%s (%s, %s) with reason: %s", utils.SmartUnicode(self.access_type), utils.GetName(this.__class__.__name__), utils.SmartUnicode(token and token.username), utils.SmartUnicode( token and token.supervisor and " (supervisor)" or ""), utils.SmartUnicode(args), utils.SmartUnicode(kwargs), utils.SmartUnicode(token and token.reason)) raise
def _ScheduleCronJob(self): if data_store.RelationalDBReadEnabled(category="cronjobs"): cron_job_id = utils.GetName(cron_system.OSBreakDownCronJob) cronjobs.ScheduleSystemCronJobs(names=[cron_job_id]) else: cron_job_id = utils.GetName(cron_system.OSBreakDown) aff4_cronjobs.ScheduleSystemCronFlows(names=[cron_job_id], token=self.token) aff4_cronjobs.GetCronManager().DisableJob(job_id=cron_job_id) return cron_job_id
def _CreateOSBreakDownCronJobApproval(self): if data_store.RelationalDBReadEnabled(): job_name = utils.GetName(cron_system.OSBreakDownCronJob) cronjobs.ScheduleSystemCronJobs(names=[job_name]) else: job_name = utils.GetName(cron_system.OSBreakDown) aff4_cronjobs.ScheduleSystemCronFlows(names=[job_name], token=self.token) aff4_cronjobs.GetCronManager().DisableJob(job_id=job_name) return job_name
def TestInit(): """Only used in tests and will rerun all the hooks to create a clean state.""" global INIT_RAN if stats.STATS is None: stats.STATS = stats.StatsCollector() threadpool.InitializeMetrics() # Tests use both the server template grr_server.yaml as a primary config file # (this file does not contain all required options, e.g. private keys), and # additional configuration in test_data/grr_test.yaml which contains typical # values for a complete installation. flags.FLAGS.config = config_lib.Resource().Filter( "install_data/etc/grr-server.yaml@grr-response-core") flags.FLAGS.secondary_configs.append(config_lib.Resource().Filter( "grr_response_test/test_data/grr_test.yaml@grr-response-test")) # This config contains non-public settings that should be applied during # tests. extra_test_config = config.CONFIG["Test.additional_test_config"] if os.path.exists(extra_test_config): flags.FLAGS.secondary_configs.append(extra_test_config) # Tests additionally add a test configuration file. config_lib.SetPlatformArchContext() config_lib.ParseConfigCommandLine() # We are running a test so let the config system know that. config.CONFIG.AddContext(contexts.TEST_CONTEXT, "Context applied when we run tests.") test_ds = flags.FLAGS.test_data_store if test_ds is None: test_ds = utils.GetName(fake_data_store.FakeDataStore) config.CONFIG.Set("Datastore.implementation", test_ds) config.CONFIG.Set("Blobstore.implementation", utils.GetName(db_blob_store.DbBlobstore)) if not INIT_RAN: server_logging.ServerLoggingStartupInit() server_logging.SetTestVerbosity() registry.TestInit() db = data_store.DB.SetupTestDB() if db: data_store.DB = db data_store.DB.Initialize() aff4.AFF4InitHook().Run() INIT_RAN = True
def testShowsCronJobDetailsOnClick(self): self.Open("/") self.Click("css=a[grrtarget=crons]") self.Click("css=td:contains('OSBreakDown')") # Tabs should appear in the bottom pane self.WaitUntil(self.IsElementPresent, "css=#main_bottomPane #Details") self.WaitUntil(self.IsElementPresent, "css=#main_bottomPane #Runs") self.WaitUntil(self.IsTextPresent, "Allow Overruns") self.WaitUntil(self.IsTextPresent, "Cron Arguments") # Click on "Runs" tab self.Click("css=#main_bottomPane #Runs") # Click on the first flow and wait for flow details panel to appear. runs = cronjobs.GetCronManager().ReadJobRuns( utils.GetName(cron_system.OSBreakDown)) try: run_id = runs[0].run_id except AttributeError: run_id = runs[0].urn.Basename() self.assertEqual(len(runs), 1) self.WaitUntil(self.IsElementPresent, "css=td:contains('%s')" % run_id)
def Decorator(cls): """Decorator producing 2 classes: legacy style one and a new style one.""" if not legacy_name: raise ValueError("legacy_name has to be provided") # Legacy cron jobs have different base classes depending on whether they're # stateful or not. if stateful: aff4_base_cls = StatefulSystemCronFlow else: aff4_base_cls = SystemCronFlow # Make sure that we're dealing with a true mixin to avoid subtle errors. if issubclass(cls, cronjobs.SystemCronJobBase): raise ValueError( "Mixin class shouldn't inherit from SystemCronJobBase") if issubclass(cls, aff4_base_cls): raise ValueError("Mixin class shouldn't inherit from %s" % aff4_base_cls.__name__) # Generate legacy class. Register it within the module as it's not going # to be returned from the decorator. aff4_cls = utils.MakeType( legacy_name, (cls, LegacyCronJobAdapterMixin, aff4_base_cls), {}) module = sys.modules[cls.__module__] setattr(module, legacy_name, aff4_cls) # Generate new class. No need to register it in the module (like the legacy # one) since it will replace the original decorated class. reldb_cls = utils.MakeType(utils.GetName(cls), (cls, cronjobs.SystemCronJobBase), {}) return reldb_cls
def ReadFlowResults(self, client_id, flow_id, offset, count, with_tag=None, with_type=None, with_substring=None): """Reads flow results of a given flow using given query options.""" results = sorted([ x.Copy() for x in self.flow_results.get((client_id, flow_id), []) ], key=lambda r: r.timestamp) # This is done in order to pass the tests that try to deserialize # value of an unrecognized type. for r in results: if utils.GetName( r.payload.__class__) not in rdfvalue.RDFValue.classes: r.payload = rdf_objects.SerializedValueOfUnrecognizedType( type_name=utils.GetName(r.payload.__class__), value=r.payload.SerializeToString()) if with_tag is not None: results = [i for i in results if i.tag == with_tag] if with_type is not None: results = [ i for i in results if utils.GetName(i.payload.__class__) == with_type ] if with_substring is not None: encoded_substring = with_substring.encode("utf8") results = [ i for i in results if encoded_substring in i.payload.SerializeToString() ] return results[offset:offset + count]
def _SetupAndRunVersionBreakDownCronjob(token=None): with test_lib.FakeTime(44): manager = aff4_cronjobs.GetCronManager() if data_store.RelationalDBReadEnabled(): cron_job_name = utils.GetName( cron_system.GRRVersionBreakDownCronJob) cronjobs.ScheduleSystemCronJobs(names=[cron_job_name]) manager.RunOnce() manager._GetThreadPool().Join() else: cron_job_name = utils.GetName(cron_system.GRRVersionBreakDown) aff4_cronjobs.ScheduleSystemCronFlows(names=[cron_job_name], token=token) manager.RunOnce(token=token) run_id = _GetRunId(cron_job_name, token=token) flow_test_lib.TestFlowHelper(rdfvalue.RDFURN( "aff4:/cron/%s/%s" % (cron_job_name, run_id)), token=token) manager.RunOnce(token=token) return cron_job_name