/
server.go
65 lines (54 loc) · 2.04 KB
/
server.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
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2021-Present The Zarf Authors
// Package http provides a http server for the webhook and proxy.
package http
import (
"fmt"
"net/http"
"time"
"github.com/defenseunicorns/zarf/src/internal/agent/hooks"
"github.com/defenseunicorns/zarf/src/pkg/message"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
// NewAdmissionServer creates a http.Server for the mutating webhook admission handler.
func NewAdmissionServer(port string) *http.Server {
message.Debugf("http.NewServer(%s)", port)
// Instances hooks
podsMutation := hooks.NewPodMutationHook()
fluxGitRepositoryMutation := hooks.NewGitRepositoryMutationHook()
argocdApplicationMutation := hooks.NewApplicationMutationHook()
argocdRepositoryMutation := hooks.NewRepositoryMutationHook()
// Routers
ah := newAdmissionHandler()
mux := http.NewServeMux()
mux.Handle("/healthz", healthz())
mux.Handle("/mutate/pod", ah.Serve(podsMutation))
mux.Handle("/mutate/flux-gitrepository", ah.Serve(fluxGitRepositoryMutation))
mux.Handle("/mutate/argocd-application", ah.Serve(argocdApplicationMutation))
mux.Handle("/mutate/argocd-repository", ah.Serve(argocdRepositoryMutation))
mux.Handle("/metrics", promhttp.Handler())
return &http.Server{
Addr: fmt.Sprintf(":%s", port),
Handler: mux,
ReadHeaderTimeout: 5 * time.Second, // Set ReadHeaderTimeout to avoid Slowloris attacks
}
}
// NewProxyServer creates and returns an http proxy server.
func NewProxyServer(port string) *http.Server {
message.Debugf("http.NewHTTPProxy(%s)", port)
mux := http.NewServeMux()
mux.Handle("/healthz", healthz())
mux.Handle("/", ProxyHandler())
mux.Handle("/metrics", promhttp.Handler())
return &http.Server{
Addr: fmt.Sprintf(":%s", port),
Handler: mux,
ReadHeaderTimeout: 5 * time.Second, // Set ReadHeaderTimeout to avoid Slowloris attacks
}
}
func healthz() http.HandlerFunc {
return func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("ok"))
}
}