-
Notifications
You must be signed in to change notification settings - Fork 0
/
webproxy.go
166 lines (158 loc) · 5.3 KB
/
webproxy.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
package main
import (
"bytes"
"crypto/tls"
"fmt"
"io"
"log"
"net/http"
"os"
)
type Proxy struct {
LDAPAuth *LDAPAuth
KubeClient *KubeClient
Config *MainConfig
certificaeStorage *CertificateStorage
}
func (proxy *Proxy) StartProxy(config ProxyConfig) error {
// Start up the proxy.
proxy.certificaeStorage = NewCertificateStorage(proxy.KubeClient)
// Assing order of Handler Functions
http.HandleFunc("/", proxy.BasicAuth(proxy.ProxyHander()))
hostString := fmt.Sprintf("%s:%s", config.Host, config.Port)
// If we have TLS Certificates tart in TLS Mode
if _, err := os.Stat(config.TLS.Certificate); err == nil {
if _, err := os.Stat(config.TLS.Key); err == nil {
log.Printf("Proxy TLS on %v", hostString)
return http.ListenAndServeTLS(hostString, config.TLS.Certificate, config.TLS.Key, nil)
}
}
log.Printf("Proxy on %v", hostString)
return http.ListenAndServe(hostString, nil)
}
// Good Documentation:
// https://www.alexedwards.net/blog/basic-authentication-in-go
// https://medium.com/@matryer/the-http-handler-wrapper-technique-in-golang-updated-bc7fbcffa702
func (proxy *Proxy) BasicAuth(next http.HandlerFunc) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// http Handler function for Basic Auth
username, password, ok := r.BasicAuth()
if ok {
// Test Login with LDAP
success, err := proxy.LDAPAuth.TestLogin(username, password)
if err != nil {
log.Printf("LDAP Error %+v", err)
http.Error(w, "Internal Server Error ", http.StatusInternalServerError)
return
}
if !success {
log.Printf("LDAP Login Failed for user %+v", username)
w.Header().Set("WWW-Authenticate", `Basic realm="restricted", charset="UTF-8"`)
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
// If login ok Serve.
next.ServeHTTP(w, r)
} else {
log.Printf("Basic Auth not read correctly %+v", r.Header)
w.Header().Set("WWW-Authenticate", `Basic realm="restricted", charset="UTF-8"`)
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
})
}
var removeRequestHeaderKeys = [...]string{
"Authorization",
"Accept-Encoding",
}
// BAD NON FUNCTIONAL DOCS... but a start
// https://github.com/davidfstr/nanoproxy/blob/master/nanoproxy.go
// Good examples:
// https://stackoverflow.com/questions/34724160/go-http-send-incoming-http-request-to-an-other-server-using-client-do
func (proxy *Proxy) ProxyHander() http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
username, _, ok := r.BasicAuth()
// Since we already logged in we expect everything to be okay. Just need the Username
// TODO: Parse the username from previous authentication + Groups
if ok {
// Get an auth certificate either from Secret og new Certitificate
cert, err := proxy.certificaeStorage.GetCertificate(username)
//cert, err := NewClientAuth(proxy.KubeClient, username)
if err != nil {
log.Printf("Error creating certificate : %+v\n", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
tlsCert, err := cert.GetTLSCert()
if err != nil {
log.Printf("Error creating TLS certificate : %+v\n", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Setup HTTP Client from Certificate and CA
httpClient := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
RootCAs: proxy.KubeClient.caCertPool,
Certificates: []tls.Certificate{tlsCert},
},
},
}
// Read body to proxy
body, err := io.ReadAll(r.Body)
if proxy.Config.Verbose {
log.Printf("> %v %v %+v %v\n- %+v", username, r.Method, r.URL.Path, len(body), r.Header)
}
if err != nil {
log.Printf("Error reading origin body: %+v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Create a URL from request
url := fmt.Sprintf("%s://%s%s", "https", proxy.Config.Kubernetes.Host, r.RequestURI)
// Create new Request
proxyReq, err := http.NewRequest(r.Method, url, bytes.NewReader(body))
// Adding headers
proxyReq.Header = make(http.Header)
for headerKey, headerValue := range r.Header {
skip := false
for _, testKey := range removeRequestHeaderKeys {
if headerKey == testKey {
skip = true
}
}
if !skip {
// f headerValue
proxyReq.Header[headerKey] = headerValue
}
}
// Do Request
proxyResp, err := httpClient.Do(proxyReq)
if err != nil {
log.Printf("I %v %v %v %+v", username, r.Method, r.URL.Path, err)
http.Error(w, err.Error(), http.StatusBadGateway)
return
}
// Read body to return
defer proxyResp.Body.Close()
proxyBody, err := io.ReadAll(proxyResp.Body)
if proxy.Config.Verbose {
log.Printf("< %v %v %v %v %+v %v\n- %+v", username, r.Method, r.URL.Path, proxyResp.StatusCode, proxyResp.Status, len(proxyBody), proxyResp.Header)
}
if err != nil {
log.Printf("Error reading proxy body: %+v", err)
http.Error(w, err.Error(), http.StatusBadGateway)
return
}
// Copy response headers
respH := w.Header()
// log.Printf("Response Headers: %+v", proxyResp.Header)
for key, value := range proxyResp.Header {
respH[key] = value
}
// Write body and statuscode.
w.WriteHeader(proxyResp.StatusCode)
w.Write(proxyBody)
}
})
}