def configure_server_with_puppet(self): if self.args.endpoint is None: raise exceptions.AgentOptionValueNotSetException("endpoint") # XXX it will only work with the default port. There is no way for # the user to configure anything else in the console endpoint = urllib.parse.urlparse(self.args.endpoint).hostname puppet_extras_base_path = os.path.join(self.conf.extra_base_path, "puppetconf") puppet_extras_bin = os.path.join(self.conf.extra_base_path, "bin/puppet") try: utils.install_extras(self.conf, package=self.conf.extra_package_name) except exceptions.AgentExtrasNotInstalledException as ex: _g_logger.exception("An error occurred trying to install puppet. " "Exception message is %s" % str(ex)) raise template_puppet_conf_path = os.path.join(puppet_extras_base_path, "puppet.conf.template") if not os.path.exists(template_puppet_conf_path): raise exceptions.AgentExtrasNotInstalledException( "The puppet.conf template did not install properly.") if not os.path.exists(puppet_extras_bin): raise exceptions.AgentExtrasNotInstalledException( "The puppet binary did not install properly.") puppet_conf_path = self.conf.get_temp_file("puppet.conf") self._edit_puppet_conf(template_puppet_conf_path, puppet_conf_path, endpoint) cert_file_path = self.conf.get_temp_file("cert.pem") key_file_path = self.conf.get_temp_file("key.pem") try: with open(cert_file_path, "w") as fptr: fptr.write(self.args.configCert) with open(key_file_path, "w") as fptr: fptr.write(self.args.configKey) exe = self.conf.get_script_location( "runConfigurationManagement-PUPPET") cmd = [ exe, endpoint, cert_file_path, key_file_path, self.args.configClientName, self.conf.extra_base_path, puppet_conf_path ] return plugin_utils.run_command(self.conf, cmd) finally: plugin_utils.safe_delete(cert_file_path) plugin_utils.safe_delete(key_file_path) plugin_utils.safe_delete(puppet_conf_path)
def test_install_extras_passes_with_good_return_code( self, extras_installed_cmd, mock_http_get_to_file): extras_installed_cmd.return_value = False mock_http_get_to_file.return_value = False config_files = config.get_config_files() conf = config.AgentConfig(config_files) conf.extra_location = "fake" with patch('dcm.agent.utils.run_command') as mock_run_cmd: mock_run_cmd.return_value = ('stdout', 'stderr', 0) result = agent_utils.install_extras(conf) self.assertTrue(result)
def main(argv=sys.argv[1:]): parser = setup_command_line_parser() opts = parser.parse_args(args=argv) opts.loglevel = opts.loglevel.upper() if opts.loglevel not in ["ERROR", "WARN", "INFO", "DEBUG"]: print("WARNING: %s is an invalid log level. Using INFO" % opts.loglevel) opts.loglevel = "INFO" opts.intrusion_detection_ossec = opts.intrusion_detection_ossec.lower() opts.intrusion_detection_ossec =\ opts.intrusion_detection_ossec in ['y', 'yes', 't', 'true'] conf_d = gather_values(opts) if not opts.initial: guess_default_cloud(conf_d) do_interactive(opts, conf_d) normalize_cloud_name(conf_d) pick_meta_data(conf_d) validate_cacerts(conf_d) # before writing anything make sure that all the needed values are # set if not opts.initial: if not conf_d["system"]["user"]: raise Exception("You must set the user name that will run " "this service.") if not conf_d["storage"]["base_dir"]: raise Exception("You must set the base dir for this service " "installation.") try: make_dirs(conf_d) (_, base_dir) = conf_d["storage"]["base_dir"] if not opts.reload: copy_scripts(conf_d) do_plugin_conf(conf_d) do_logging_conf(conf_d, opts) else: if not os.path.isfile(os.path.join(base_dir, "etc", "plugin.conf")) or opts.rewrite_logging_plugin: do_plugin_conf(conf_d) if not os.path.isfile(os.path.join(base_dir, "etc", "logging.yaml")) or opts.rewrite_logging_plugin: do_logging_conf(conf_d, opts) cleanup_previous_install(conf_d) conf_file_name = os.path.join(base_dir, "etc", "agent.conf") write_conf_file(conf_file_name, conf_d) do_set_owner_and_perms(conf_d) if not opts.initial: enable_start_agent(opts) conf = config.AgentConfig([conf_file_name]) if opts.install_extras: if opts.package_name: agent_utils.install_extras(conf, package=opts.package_name) else: agent_utils.install_extras(conf) if opts.intrusion_detection_ossec and not agent_utils.ossec_installed(conf): # call out to install ossec agent_utils.install_ossec(conf) except Exception as ex: print(str(ex), file=sys.stderr) if opts.verbose: raise return 1 return 0