mirror of
https://github.com/hanzo-ml/kuberay.git
synced 2026-10-30 19:48:47 +00:00
Right now API server access is completely opened. This defines the approach to secure API server and provides a very simple implementation. Securing API server endpoint Related issue number Closes #1376
38 lines
802 B
Go
38 lines
802 B
Go
package httpproxy
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httputil"
|
|
"net/url"
|
|
"strings"
|
|
)
|
|
|
|
const (
|
|
token_header = "Authorization"
|
|
)
|
|
|
|
type tokenAuth struct {
|
|
authorization
|
|
token string
|
|
}
|
|
|
|
func NewTokenAuth(token string, proxy *httputil.ReverseProxy, prefix string, upstream *url.URL) tokenAuth {
|
|
auth := authorization{proxy: proxy, prefix: prefix, upstream: upstream}
|
|
return tokenAuth{token: token, authorization: auth}
|
|
}
|
|
|
|
func (ta tokenAuth) AuthFunc() func(http.ResponseWriter, *http.Request) {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
if strings.HasPrefix(r.URL.String(), ta.prefix) {
|
|
auth := r.Header.Get(token_header)
|
|
if auth != ta.token {
|
|
// Wrong token
|
|
WriteUnauthorisedResponse(w)
|
|
return
|
|
}
|
|
}
|
|
modifyRequest(r, ta.upstream)
|
|
ta.proxy.ServeHTTP(w, r)
|
|
}
|
|
}
|