|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/tls" |
| 5 | + "crypto/x509" |
| 6 | + "errors" |
| 7 | + "io/ioutil" |
| 8 | + "strconv" |
| 9 | + "strings" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/calyptia/plugin" |
| 13 | +) |
| 14 | + |
| 15 | +func loadTLSConfig(cfg TLSSettings) (*tls.Config, error) { |
| 16 | + tlsConfig := &tls.Config{InsecureSkipVerify: cfg.Insecure, ServerName: cfg.ServerName} |
| 17 | + if cfg.CAFile != "" { |
| 18 | + caBytes, err := ioutil.ReadFile(cfg.CAFile) |
| 19 | + if err != nil { |
| 20 | + return nil, err |
| 21 | + } |
| 22 | + caPool := x509.NewCertPool() |
| 23 | + caPool.AppendCertsFromPEM(caBytes) |
| 24 | + tlsConfig.RootCAs = caPool |
| 25 | + } |
| 26 | + if cfg.CertFile != "" && cfg.KeyFile != "" { |
| 27 | + cert, err := tls.LoadX509KeyPair(cfg.CertFile, cfg.KeyFile) |
| 28 | + if err != nil { |
| 29 | + return nil, err |
| 30 | + } |
| 31 | + tlsConfig.Certificates = []tls.Certificate{cert} |
| 32 | + } |
| 33 | + return tlsConfig, nil |
| 34 | +} |
| 35 | + |
| 36 | +func parseCorsConfig(cfg *Config, conf plugin.ConfigLoader) (*Config, error) { |
| 37 | + if cfg == nil { |
| 38 | + return nil, errors.New("cfg must not nil") |
| 39 | + } |
| 40 | + if corsStr := conf.String("server.cors.allowed_origins"); corsStr != "" { |
| 41 | + for _, origin := range strings.Split(corsStr, ",") { |
| 42 | + trimmedOrigin := strings.TrimSpace(origin) |
| 43 | + if trimmedOrigin != "" { // Only append non-empty origins |
| 44 | + cfg.ServerCors.AllowedOrigins = append(cfg.ServerCors.AllowedOrigins, trimmedOrigin) |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + return cfg, nil |
| 50 | +} |
| 51 | + |
| 52 | +func loadConfig(fbit *plugin.Fluentbit) (*Config, error) { |
| 53 | + log := fbit.Logger |
| 54 | + cfg := &Config{ |
| 55 | + Mode: fbit.Conf.String("mode"), |
| 56 | + ClientServerURL: fbit.Conf.String("client.server_url"), |
| 57 | + ClientSamplingURL: fbit.Conf.String("client.sampling_url"), |
| 58 | + ServerEndpoint: fbit.Conf.String("server.endpoint"), |
| 59 | + ServerStrategyFile: fbit.Conf.String("server.strategy_file"), |
| 60 | + ServerHttpListenAddr: fbit.Conf.String("server.http.listen_addr"), |
| 61 | + ServerGrpcListenAddr: fbit.Conf.String("server.grpc.listen_addr"), |
| 62 | + ServerHeaders: parseHeaders(fbit.Conf.String("server.headers")), |
| 63 | + } |
| 64 | + |
| 65 | + cfg, err := parseCorsConfig(cfg, fbit.Conf) |
| 66 | + if err != nil { |
| 67 | + return nil, err |
| 68 | + } |
| 69 | + |
| 70 | + if cfg.Mode == "" { |
| 71 | + cfg.Mode = "all" |
| 72 | + } |
| 73 | + |
| 74 | + if cfg.Mode == "client" || cfg.Mode == "all" { |
| 75 | + if cfg.ClientServerURL == "" { |
| 76 | + return nil, errors.New("'client.server_url' is required for client/all mode") |
| 77 | + } |
| 78 | + if cfg.ClientSamplingURL == "" { |
| 79 | + return nil, errors.New("'client.sampling_url' is required for client/all mode") |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + if cfg.Mode == "server" || cfg.Mode == "all" { |
| 84 | + if cfg.ServerEndpoint == "" && cfg.ServerStrategyFile == "" { |
| 85 | + return nil, errors.New("for server mode, either 'server.endpoint' or 'server.strategy_file' must be configured") |
| 86 | + } |
| 87 | + if cfg.ServerEndpoint != "" && cfg.ServerStrategyFile != "" { |
| 88 | + return nil, errors.New("'server.endpoint' and 'server.strategy_file' are mutually exclusive") |
| 89 | + } |
| 90 | + |
| 91 | + // Remote-only server settings |
| 92 | + if cfg.ServerEndpoint != "" { |
| 93 | + if valStr := fbit.Conf.String("server.reload_interval"); valStr != "" { |
| 94 | + val, err := time.ParseDuration(valStr) |
| 95 | + if err != nil { |
| 96 | + log.Warn("could not parse 'server.reload_interval', using default of 5m. error: %v", err) |
| 97 | + cfg.ServerReloadInterval = 5 * time.Minute |
| 98 | + } else { |
| 99 | + cfg.ServerReloadInterval = val |
| 100 | + } |
| 101 | + } else { |
| 102 | + cfg.ServerReloadInterval = 5 * time.Minute // Default TTL |
| 103 | + } |
| 104 | + |
| 105 | + cfg.ServerTLS = TLSSettings{ |
| 106 | + ServerName: fbit.Conf.String("server.tls.server_name_override"), |
| 107 | + CAFile: fbit.Conf.String("server.tls.ca_file"), |
| 108 | + CertFile: fbit.Conf.String("server.tls.cert_file"), |
| 109 | + KeyFile: fbit.Conf.String("server.tls.key_file"), |
| 110 | + } |
| 111 | + if insecureStr := fbit.Conf.String("server.tls.insecure"); insecureStr != "" { |
| 112 | + cfg.ServerTLS.Insecure, _ = strconv.ParseBool(insecureStr) |
| 113 | + } |
| 114 | + |
| 115 | + if kaTimeStr := fbit.Conf.String("server.keepalive.time"); kaTimeStr != "" { |
| 116 | + cfg.ServerKeepalive = &KeepaliveConfig{} |
| 117 | + kaTime, err := time.ParseDuration(kaTimeStr) |
| 118 | + if err != nil { |
| 119 | + log.Warn("could not parse 'server.keepalive.time', skipping keepalive config. error: %v", err) |
| 120 | + cfg.ServerKeepalive = nil |
| 121 | + } else { |
| 122 | + cfg.ServerKeepalive.Time = kaTime |
| 123 | + kaTimeoutStr := fbit.Conf.String("server.keepalive.timeout") |
| 124 | + if kaTimeoutStr == "" { |
| 125 | + cfg.ServerKeepalive.Timeout = 20 * time.Second |
| 126 | + } else { |
| 127 | + kaTimeout, err := time.ParseDuration(kaTimeoutStr) |
| 128 | + if err != nil { |
| 129 | + log.Warn("could not parse 'server.keepalive.timeout', using default of 20s. error: %v", err) |
| 130 | + cfg.ServerKeepalive.Timeout = 20 * time.Second |
| 131 | + } else { |
| 132 | + cfg.ServerKeepalive.Timeout = kaTimeout |
| 133 | + } |
| 134 | + } |
| 135 | + kaPermitStr := fbit.Conf.String("server.keepalive.permit_without_stream") |
| 136 | + if kaPermitStr == "" { |
| 137 | + cfg.ServerKeepalive.PermitWithoutStream = true |
| 138 | + } else { |
| 139 | + kaPermit, err := strconv.ParseBool(kaPermitStr) |
| 140 | + if err != nil { |
| 141 | + log.Warn("could not parse 'server.keepalive.permit_without_stream', using default of true. error: %v", err) |
| 142 | + cfg.ServerKeepalive.PermitWithoutStream = true |
| 143 | + } else { |
| 144 | + cfg.ServerKeepalive.PermitWithoutStream = kaPermit |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + cfg.ServerRetry = &RetryConfig{} |
| 151 | + if valStr := fbit.Conf.String("server.retry.initial_interval"); valStr != "" { |
| 152 | + val, err := time.ParseDuration(valStr) |
| 153 | + if err != nil { |
| 154 | + log.Warn("could not parse 'server.retry.initial_interval', using default of 5s. error: %v", err) |
| 155 | + cfg.ServerRetry.InitialInterval = 5 * time.Second |
| 156 | + } else { |
| 157 | + cfg.ServerRetry.InitialInterval = val |
| 158 | + } |
| 159 | + } else { |
| 160 | + cfg.ServerRetry.InitialInterval = 5 * time.Second |
| 161 | + } |
| 162 | + if valStr := fbit.Conf.String("server.retry.max_interval"); valStr != "" { |
| 163 | + val, err := time.ParseDuration(valStr) |
| 164 | + if err != nil { |
| 165 | + log.Warn("could not parse 'server.retry.max_interval', using default of 5m. error: %v", err) |
| 166 | + cfg.ServerRetry.MaxInterval = 5 * time.Minute |
| 167 | + } else { |
| 168 | + cfg.ServerRetry.MaxInterval = val |
| 169 | + } |
| 170 | + } else { |
| 171 | + cfg.ServerRetry.MaxInterval = 5 * time.Minute |
| 172 | + } |
| 173 | + |
| 174 | + if valStr := fbit.Conf.String("server.retry.max_retry"); valStr != "" { |
| 175 | + val, err := strconv.ParseInt(valStr, 10, 0) |
| 176 | + if err != nil { |
| 177 | + log.Warn("could not parse 'server.retry.max_retry', using default of 5m. error: %v", err) |
| 178 | + cfg.ServerRetry.MaxRetry = 10 |
| 179 | + } else { |
| 180 | + cfg.ServerRetry.MaxRetry = val |
| 181 | + } |
| 182 | + } else { |
| 183 | + cfg.ServerRetry.MaxRetry = 10 |
| 184 | + } |
| 185 | + if cfg.ServerRetry.MaxRetry <= 0 { |
| 186 | + log.Warn("'server.retry.multiplier' must be > 0, using default of 10") |
| 187 | + cfg.ServerRetry.MaxRetry = 10 |
| 188 | + } |
| 189 | + if valStr := fbit.Conf.String("server.retry.multiplier"); valStr != "" { |
| 190 | + val, err := strconv.ParseFloat(valStr, 64) |
| 191 | + if err != nil { |
| 192 | + log.Warn("could not parse 'server.retry.multiplier', using default of 1.5. error: %v", err) |
| 193 | + cfg.ServerRetry.Multiplier = 1.5 |
| 194 | + } else { |
| 195 | + cfg.ServerRetry.Multiplier = val |
| 196 | + } |
| 197 | + } else { |
| 198 | + cfg.ServerRetry.Multiplier = 1.5 |
| 199 | + } |
| 200 | + if cfg.ServerRetry.Multiplier <= 1.0 { |
| 201 | + log.Warn("'server.retry.multiplier' must be > 1.0, using default of 1.5") |
| 202 | + cfg.ServerRetry.Multiplier = 1.5 |
| 203 | + } |
| 204 | + |
| 205 | + serviceNamesStr := fbit.Conf.String("server.service_names") |
| 206 | + if serviceNamesStr == "" { |
| 207 | + return nil, errors.New("'server.service_names' (comma-separated) is required for server/all mode with a remote endpoint") |
| 208 | + } |
| 209 | + for _, s := range strings.Split(serviceNamesStr, ",") { |
| 210 | + cfg.ServerServiceNames = append(cfg.ServerServiceNames, strings.TrimSpace(s)) |
| 211 | + } |
| 212 | + } |
| 213 | + } |
| 214 | + return cfg, nil |
| 215 | +} |
0 commit comments