github.com/google/fleetspeak@v0.1.15-0.20240426164851-4f31f62c1aea/fleetspeak/src/config/client/config.go (about)

     1  package client
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  
     7  	"google.golang.org/protobuf/encoding/prototext"
     8  
     9  	gpb "github.com/google/fleetspeak/fleetspeak/src/client/generic/proto/fleetspeak_client_generic"
    10  	cpb "github.com/google/fleetspeak/fleetspeak/src/config/proto/fleetspeak_config"
    11  )
    12  
    13  func WriteLinuxConfig(cfg *cpb.Config, trustedPEM []byte) error {
    14  	if cfg.LinuxClientConfigurationFile == "" {
    15  		return nil
    16  	}
    17  	out := &gpb.Config{
    18  		TrustedCerts: string(trustedPEM),
    19  		Server:       cfg.PublicHostPort,
    20  		ServerName:   cfg.ServerName,
    21  		ClientLabel:  []string{cfg.ComponentsConfig.RequiredLabel},
    22  		PersistenceHandler: &gpb.Config_FilesystemHandler{
    23  			FilesystemHandler: &gpb.FilesystemHandler{
    24  				ConfigurationDirectory: "/etc/fleetspeak-client",
    25  				StateFile:              "/var/lib/misc/fleetspeak-client.state",
    26  			}},
    27  		Streaming: !cfg.ComponentsConfig.HttpsConfig.DisableStreaming,
    28  	}
    29  
    30  	b, err := prototext.Marshal(out)
    31  	if err != nil {
    32  		return fmt.Errorf("failed to marshal linux client configuration: %v", err)
    33  	}
    34  	err = os.WriteFile(cfg.LinuxClientConfigurationFile, b, 0644)
    35  	if err != nil {
    36  		return fmt.Errorf("failed to write linux client configuration file [%s]: %v", cfg.LinuxClientConfigurationFile, err)
    37  	}
    38  
    39  	return nil
    40  }
    41  
    42  func WriteDarwinConfig(cfg *cpb.Config, trustedPEM []byte) error {
    43  	if cfg.DarwinClientConfigurationFile == "" {
    44  		return nil
    45  	}
    46  	out := &gpb.Config{
    47  		TrustedCerts: string(trustedPEM),
    48  		Server:       cfg.PublicHostPort,
    49  		ServerName:   cfg.ServerName,
    50  		ClientLabel:  []string{cfg.ComponentsConfig.RequiredLabel},
    51  		PersistenceHandler: &gpb.Config_FilesystemHandler{
    52  			FilesystemHandler: &gpb.FilesystemHandler{
    53  				ConfigurationDirectory: "/etc/fleetspeak-client",
    54  				StateFile:              "/etc/fleetspeak-client/state",
    55  			}},
    56  		Streaming: !cfg.ComponentsConfig.HttpsConfig.DisableStreaming,
    57  	}
    58  
    59  	b, err := prototext.Marshal(out)
    60  	if err != nil {
    61  		return fmt.Errorf("failed to marshal darwin client configuration: %v", err)
    62  	}
    63  	err = os.WriteFile(cfg.DarwinClientConfigurationFile, b, 0644)
    64  	if err != nil {
    65  		return fmt.Errorf("failed to write darwin client configuration file [%s]: %v", cfg.DarwinClientConfigurationFile, err)
    66  	}
    67  
    68  	return nil
    69  }
    70  
    71  func WriteWindowsConfig(cfg *cpb.Config, trustedPEM []byte) error {
    72  	if cfg.WindowsClientConfigurationFile == "" {
    73  		return nil
    74  	}
    75  	out := &gpb.Config{
    76  		TrustedCerts: string(trustedPEM),
    77  		Server:       cfg.PublicHostPort,
    78  		ServerName:   cfg.ServerName,
    79  		ClientLabel:  []string{cfg.ComponentsConfig.RequiredLabel},
    80  		PersistenceHandler: &gpb.Config_RegistryHandler{
    81  			RegistryHandler: &gpb.RegistryHandler{
    82  				ConfigurationKey: `HKEY_LOCAL_MACHINE\SOFTWARE\FleetspeakClient`,
    83  			}},
    84  		Streaming: !cfg.ComponentsConfig.HttpsConfig.DisableStreaming,
    85  	}
    86  
    87  	b, err := prototext.Marshal(out)
    88  	if err != nil {
    89  		return fmt.Errorf("failed to marshal Windows client configuration: %v", err)
    90  	}
    91  	err = os.WriteFile(cfg.WindowsClientConfigurationFile, b, 0644)
    92  	if err != nil {
    93  		return fmt.Errorf("failed to write Windows client configuration file [%s]: %v", cfg.WindowsClientConfigurationFile, err)
    94  	}
    95  
    96  	return nil
    97  }