forked from ContentSquare/chproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
415 lines (367 loc) · 12 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
package main
import (
"context"
"crypto/tls"
"flag"
"fmt"
"net"
"net/http"
"os"
"os/signal"
"path/filepath"
"reflect"
"sort"
"strings"
"sync/atomic"
"syscall"
"time"
"github.com/Vertamedia/chproxy/config"
"github.com/Vertamedia/chproxy/log"
"github.com/prometheus/client_golang/prometheus/promhttp"
"golang.org/x/crypto/acme/autocert"
//client-go dependencies
//"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/util/homedir"
)
var (
configFile = flag.String("config", "", "Proxy configuration filename")
version = flag.Bool("version", false, "Prints current version and exits")
)
var (
proxy = newReverseProxy()
// networks allow lists
allowedNetworksHTTP atomic.Value
allowedNetworksHTTPS atomic.Value
allowedNetworksMetrics atomic.Value
)
// Clickhouse on Kubernetes means pods can come and go, i.e replicas and shards
// For this reason, CHproxy has been expanded to use the Client-go library to dynamically discover Clickhouse pods.
// Discovery is perfomed using the kubernetes APIs to find all pods in a cluster.
// Clickhouse pods are filtered out by means of comparing the pod name to strings included or excluded.
var clientset *kubernetes.Clientset // Kubernetes client API access
func main() {
flag.Parse()
if *version {
fmt.Printf("%s\n", versionString())
os.Exit(0)
}
log.Infof("%s", versionString())
log.Infof("Loading config: %s", *configFile)
cfg, err := loadConfig()
if err != nil {
log.Fatalf("error while loading config: %s", err)
}
if err = applyConfig(cfg); err != nil {
log.Fatalf("error while applying config: %s", err)
}
log.Infof("Loading config %q: successful", *configFile)
// Are we configured for dynamic kubernetes pod discovery?
if cfg.Clusters[0].KubernetesPodDiscovery {
// Are we inside or outside the Kubernetes cluster?
var kubeaccessok bool
kubeaccessok = true
// Attempt to create an in-cluster config
kconfig, err := rest.InClusterConfig()
if err == nil {
log.Infof("Failed to build cluster internal kubeconfig. No further kubernetes API access attempts will be made")
clientset, err = kubernetes.NewForConfig(kconfig)
if err != nil {
log.Infof("Failed to create Kubernetes clientset for cluster internal accesss.")
kubeaccessok = false
}
} else { // in-cluster config failed, attempt out of cluster config
var kubeconfig *string
if home := homedir.HomeDir(); home != "" {
kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file")
} else {
kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
}
flag.Parse()
// use the current context in kubeconfig
kconfig, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
if err != nil {
log.Infof("Failed to build cluster external kubeconfig. Attempting to build cluster internal kubeconfig")
kubeaccessok = false
}
clientset, err = kubernetes.NewForConfig(kconfig)
if err != nil {
log.Infof("Failed to create Kubernetes clientset for cluster internal accesss.")
kubeaccessok = false
}
}
// if we have Clickhouse deployed on kubernetes, and CHproxy has internal or external access to the Kubernetes APIs, we can automatically manage pods coming and going
if kubeaccessok != false {
kubepodscanticker := time.NewTicker(500 * time.Millisecond)
go func() {
for {
select {
case <-kubepodscanticker.C:
{
// fetch the configuration for kubenretes pod discovery
chCurrentNodes := cfg.Clusters[0].Nodes
sort.Strings(chCurrentNodes)
skubeInclude := cfg.Clusters[0].KubernetesPodNameInclude
skubeExclude := cfg.Clusters[0].KubernetesPodNameExclude
skubeNamespace := cfg.Clusters[0].KubernetesPodNamespace
// get pods in all the namespaces by omitting namespace by setting Pods("")
pods, err := clientset.CoreV1().Pods(skubeNamespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
log.Infof("Kubernetes API error: \n", err.Error())
}
//log.Infof("There are %d pods in the cluster\n", len(pods.Items))
var chDiscoveredNodes []string
// Filter out the relevant pods
for i, s := range pods.Items {
sname := s.GetName()
_ = i //workaround for unused variable...
if strings.Contains(sname, skubeInclude) && !strings.Contains(sname, skubeExclude) {
//log.Infof("Clickhouse Pod %s %s\n", i, s.GetName(), s.Status.PodIP)
if len(s.Status.PodIP) > 6 {
chDiscoveredNodes = append(chDiscoveredNodes, s.Status.PodIP+":8123")
}
}
}
sort.Strings(chDiscoveredNodes)
// if pods appear of dissapear, there will be a difference between chCurrentNodes and chDiscoveredNodes
if reflect.DeepEqual(chCurrentNodes, chDiscoveredNodes) == false {
// Assign the new node configuration
cfg.Clusters[0].Nodes = chDiscoveredNodes
// Apply the updated configuration
applyConfig(cfg)
chCurrentNodes = chDiscoveredNodes
log.Infof("Clickhouse Pod configuration changed, node list updated\n")
}
}
}
}
}() //end gofunc
}
} // end of kubernetes pod discovery
//////////////
c := make(chan os.Signal)
signal.Notify(c, syscall.SIGHUP)
go func() {
for {
switch <-c {
case syscall.SIGHUP:
log.Infof("SIGHUP received. Going to reload config %s ...", *configFile)
if err := reloadConfig(); err != nil {
log.Errorf("error while reloading config: %s", err)
continue
}
log.Infof("Reloading config %s: successful", *configFile)
}
}
}()
server := cfg.Server
if len(server.HTTP.ListenAddr) == 0 && len(server.HTTPS.ListenAddr) == 0 {
panic("BUG: broken config validation - `listen_addr` is not configured")
}
if server.HTTP.ForceAutocertHandler {
autocertManager = newAutocertManager(server.HTTPS.Autocert)
}
if len(server.HTTPS.ListenAddr) != 0 {
go serveTLS(server.HTTPS)
}
if len(server.HTTP.ListenAddr) != 0 {
go serve(server.HTTP)
}
select {}
}
var autocertManager *autocert.Manager
func newAutocertManager(cfg config.Autocert) *autocert.Manager {
if len(cfg.CacheDir) > 0 {
if err := os.MkdirAll(cfg.CacheDir, 0700); err != nil {
log.Fatalf("error while creating folder %q: %s", cfg.CacheDir, err)
}
}
var hp autocert.HostPolicy
if len(cfg.AllowedHosts) != 0 {
allowedHosts := make(map[string]struct{}, len(cfg.AllowedHosts))
for _, v := range cfg.AllowedHosts {
allowedHosts[v] = struct{}{}
}
hp = func(_ context.Context, host string) error {
if _, ok := allowedHosts[host]; ok {
return nil
}
return fmt.Errorf("host %q doesn't match `host_policy` configuration", host)
}
}
return &autocert.Manager{
Prompt: autocert.AcceptTOS,
Cache: autocert.DirCache(cfg.CacheDir),
HostPolicy: hp,
}
}
func newListener(listenAddr string) net.Listener {
ln, err := net.Listen("tcp4", listenAddr)
if err != nil {
log.Fatalf("cannot listen for %q: %s", listenAddr, err)
}
return ln
}
func serveTLS(cfg config.HTTPS) {
ln := newListener(cfg.ListenAddr)
h := http.HandlerFunc(serveHTTP)
tlsCfg := newTLSConfig(cfg)
tln := tls.NewListener(ln, tlsCfg)
log.Infof("Serving https on %q", cfg.ListenAddr)
if err := listenAndServe(tln, h, cfg.TimeoutCfg); err != nil {
log.Fatalf("TLS server error on %q: %s", cfg.ListenAddr, err)
}
}
func serve(cfg config.HTTP) {
var h http.Handler
ln := newListener(cfg.ListenAddr)
h = http.HandlerFunc(serveHTTP)
if cfg.ForceAutocertHandler {
if autocertManager == nil {
panic("BUG: autocertManager is not inited")
}
addr := ln.Addr().String()
parts := strings.Split(addr, ":")
if parts[len(parts)-1] != "80" {
log.Fatalf("`letsencrypt` specification requires http server to listen on :80 port to satisfy http-01 challenge. " +
"Otherwise, certificates will be impossible to generate")
}
h = autocertManager.HTTPHandler(h)
}
log.Infof("Serving http on %q", cfg.ListenAddr)
if err := listenAndServe(ln, h, cfg.TimeoutCfg); err != nil {
log.Fatalf("HTTP server error on %q: %s", cfg.ListenAddr, err)
}
}
func newTLSConfig(cfg config.HTTPS) *tls.Config {
tlsCfg := tls.Config{
PreferServerCipherSuites: true,
CurvePreferences: []tls.CurveID{
tls.CurveP256,
tls.X25519,
},
}
if len(cfg.KeyFile) > 0 && len(cfg.CertFile) > 0 {
cert, err := tls.LoadX509KeyPair(cfg.CertFile, cfg.KeyFile)
if err != nil {
log.Fatalf("cannot load cert for `https.cert_file`=%q, `https.key_file`=%q: %s",
cfg.CertFile, cfg.KeyFile, err)
}
tlsCfg.Certificates = []tls.Certificate{cert}
} else {
if autocertManager == nil {
panic("BUG: autocertManager is not inited")
}
tlsCfg.GetCertificate = autocertManager.GetCertificate
}
return &tlsCfg
}
func listenAndServe(ln net.Listener, h http.Handler, cfg config.TimeoutCfg) error {
s := &http.Server{
TLSNextProto: make(map[string]func(*http.Server, *tls.Conn, http.Handler)),
Handler: h,
ReadTimeout: time.Duration(cfg.ReadTimeout),
WriteTimeout: time.Duration(cfg.WriteTimeout),
IdleTimeout: time.Duration(cfg.IdleTimeout),
// Suppress error logging from the server, since chproxy
// must handle all these errors in the code.
ErrorLog: log.NilLogger,
}
return s.Serve(ln)
}
var promHandler = promhttp.Handler()
func serveHTTP(rw http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet, http.MethodPost:
// Only GET and POST methods are supported.
case http.MethodOptions:
// This is required for CORS shit :)
rw.Header().Set("Allow", "GET,POST")
return
default:
err := fmt.Errorf("%q: unsupported method %q", r.RemoteAddr, r.Method)
rw.Header().Set("Connection", "close")
respondWith(rw, err, http.StatusMethodNotAllowed)
return
}
switch r.URL.Path {
case "/favicon.ico":
case "/metrics":
an := allowedNetworksMetrics.Load().(*config.Networks)
if !an.Contains(r.RemoteAddr) {
err := fmt.Errorf("connections to /metrics are not allowed from %s", r.RemoteAddr)
rw.Header().Set("Connection", "close")
respondWith(rw, err, http.StatusForbidden)
return
}
proxy.refreshCacheMetrics()
promHandler.ServeHTTP(rw, r)
case "/", "/query":
var err error
var an *config.Networks
if r.TLS != nil {
an = allowedNetworksHTTPS.Load().(*config.Networks)
err = fmt.Errorf("https connections are not allowed from %s", r.RemoteAddr)
} else {
an = allowedNetworksHTTP.Load().(*config.Networks)
err = fmt.Errorf("http connections are not allowed from %s", r.RemoteAddr)
}
if !an.Contains(r.RemoteAddr) {
rw.Header().Set("Connection", "close")
respondWith(rw, err, http.StatusForbidden)
return
}
proxy.ServeHTTP(rw, r)
default:
badRequest.Inc()
err := fmt.Errorf("%q: unsupported path: %q", r.RemoteAddr, r.URL.Path)
rw.Header().Set("Connection", "close")
respondWith(rw, err, http.StatusBadRequest)
}
}
func loadConfig() (*config.Config, error) {
if *configFile == "" {
log.Fatalf("Missing -config flag")
}
cfg, err := config.LoadFile(*configFile)
if err != nil {
configSuccess.Set(0)
return nil, fmt.Errorf("can't load config %q: %s", *configFile, err)
}
configSuccess.Set(1)
configSuccessTime.Set(float64(time.Now().Unix()))
return cfg, nil
}
func applyConfig(cfg *config.Config) error {
if err := proxy.applyConfig(cfg); err != nil {
return err
}
allowedNetworksHTTP.Store(&cfg.Server.HTTP.AllowedNetworks)
allowedNetworksHTTPS.Store(&cfg.Server.HTTPS.AllowedNetworks)
allowedNetworksMetrics.Store(&cfg.Server.Metrics.AllowedNetworks)
log.SetDebug(cfg.LogDebug)
log.Infof("Loaded config:\n%s", cfg)
return nil
}
func reloadConfig() error {
cfg, err := loadConfig()
if err != nil {
return err
}
return applyConfig(cfg)
}
var (
buildTag = "unknown"
buildRevision = "unknown"
buildTime = "unknown"
)
func versionString() string {
ver := buildTag
if len(ver) == 0 {
ver = "unknown"
}
return fmt.Sprintf("chproxy ver. %s, rev. %s, built at %s", ver, buildRevision, buildTime)
}