def _CreateClientConfig(tmp_dir: str) -> str: """Creates and returns the path to a fleetspeak client config.""" def TmpPath(*args): return os.path.join(tmp_dir, *args) server_config_dir = package.ResourcePath( "fleetspeak-server-bin", "fleetspeak-server-bin/etc/fleetspeak-server") if not os.path.exists(server_config_dir): raise Error( f"Fleetspeak server config dir not found: {server_config_dir}. " "Please make sure `grr_config_updater initialize` has been run.") client_config_name = { "Linux": "linux_client.config", "Windows": "windows_client.config", "Darwin": "darwin_client.config", } client_config_path = os.path.join(server_config_dir, client_config_name[platform.system()]) with open(client_config_path, "r") as f: client_config = text_format.Parse(f.read(), fs_cli_config_pb2.Config()) if client_config.HasField("filesystem_handler"): client_config.filesystem_handler.configuration_directory = TmpPath() # We store the client state file in `Logging.path`. # 1) We need a writable path, where writing a file doesn't surprise the # user (as opposed to other paths in the source tree). # 2) We need the state file to be somewhat persistent, i.e. kept after # re-runs of this command. Otherwise the client ID of the client would # change at each re-run. client_config.filesystem_handler.state_file = os.path.join( config.CONFIG["Logging.path"], "fleetspeak-client.state") with open(TmpPath("config"), "w") as f: f.write(text_format.MessageToString(client_config)) return TmpPath("config")
def InitFleetspeakConfigs( grr_configs: GRRConfigs, mysql_database: str, mysql_username: Optional[str] = None, mysql_password: Optional[str] = None) -> FleetspeakConfigs: """Initializes Fleetspeak server and client configs.""" fs_frontend_port, fs_admin_port = api_helpers.GetFleetspeakPortsFromConfig( grr_configs.server_config) mysql_username = mysql_username or "" mysql_password = mysql_password or "" temp_root = tempfile.mkdtemp(suffix="_fleetspeak") def TempPath(*args): return os.path.join(temp_root, *args) cp = config_pb2.Config(configuration_name="Self-contained testing") cp.components_config.mysql_data_source_name = "%s:%s@tcp(127.0.0.1:3306)/%s" % ( mysql_username, mysql_password, mysql_database) cp.components_config.https_config.listen_address = "localhost:%d" % portpicker.pick_unused_port( ) # TODO(user): Use streaming connections by default. At the moment # a few tests are failing with MySQL errors when streaming is used. cp.components_config.https_config.disable_streaming = True cp.components_config.admin_config.listen_address = ("localhost:%d" % fs_admin_port) cp.public_host_port.append( cp.components_config.https_config.listen_address) cp.server_component_configuration_file = TempPath("server.config") cp.trusted_cert_file = TempPath("trusted_cert.pem") cp.trusted_cert_key_file = TempPath("trusted_cert_key.pem") cp.server_cert_file = TempPath("server_cert.pem") cp.server_cert_key_file = TempPath("server_cert_key.pem") cp.linux_client_configuration_file = TempPath("linux_client.config") cp.windows_client_configuration_file = TempPath("windows_client.config") cp.darwin_client_configuration_file = TempPath("darwin_client.config") built_configurator_config_path = TempPath("configurator.config") with open(built_configurator_config_path, mode="w", encoding="utf-8") as fd: fd.write(text_format.MessageToString(cp)) p = _StartBinary( "fleetspeak-config", ["--logtostderr", "--config", built_configurator_config_path]) if p.wait() != 0: raise ConfigInitializationError( "fleetspeak-config execution failed: {}".format(p.returncode)) # Adjust client config. with open(cp.linux_client_configuration_file, mode="r", encoding="utf-8") as fd: conf_content = fd.read() conf = text_format.Parse(conf_content, client_config_pb2.Config()) conf.filesystem_handler.configuration_directory = temp_root conf.filesystem_handler.state_file = TempPath("client.state") with open(cp.linux_client_configuration_file, mode="w", encoding="utf-8") as fd: fd.write(text_format.MessageToString(conf)) # Write client services configuration. service_conf = system_pb2.ClientServiceConfig(name="GRR", factory="Daemon") payload = daemonservice_config_pb2.Config() payload.argv.extend([ sys.executable, "-u", "-m", "grr_response_client.grr_fs_client", "--config", grr_configs.client_config ]) # TODO(user): remove this condition when Fleetspeak is used as a nanny # on all platforms. if platform.system() == "Windows": payload.monitor_heartbeats = True payload.heartbeat_unresponsive_grace_period_seconds = 45 payload.heartbeat_unresponsive_kill_period_seconds = 15 service_conf.config.Pack(payload) os.mkdir(TempPath("textservices")) with open(TempPath("textservices", "GRR.textproto"), mode="w", encoding="utf-8") as fd: fd.write(text_format.MessageToString(service_conf)) # Server services configuration. service_config = services_pb2.ServiceConfig(name="GRR", factory="GRPC") grpc_config = grpcservice_pb2.Config(target="localhost:%d" % fs_frontend_port, insecure=True) service_config.config.Pack(grpc_config) server_conf = server_pb2.ServerConfig(services=[service_config]) server_conf.broadcast_poll_time.seconds = 1 built_server_services_config_path = TempPath("server.services.config") with open(built_server_services_config_path, mode="w", encoding="utf-8") as fd: fd.write(text_format.MessageToString(server_conf)) return FleetspeakConfigs(cp.server_component_configuration_file, built_server_services_config_path, cp.linux_client_configuration_file)