mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-05-21 17:21:34 +00:00
* Revert "volume: fail closed in admin gRPC gate when no whitelist is configured (#9440)" This reverts commit21054b6c18. The fail-closed gate broke any multi-host cluster: in compose / k8s / remote-host deployments the master's IP isn't loopback, so every master->volume admin RPC (AllocateVolume, BatchDelete, EC reroute, vacuum, scrub, ...) is rejected with PermissionDenied unless the operator manually configures -whiteList. The e2e workflow has been failing since10cc06333with `not authorized: 172.18.0.2` on AllocateVolume; downstream symptom is fio fsync EIO because zero volumes can be grown. The gate's intent was to lock down destructive admin tooling, but the same RPCs are the master's normal mechanism for growing and managing volumes. Reverting to restore cluster-internal operation; a narrower re-do should distinguish operator/admin callers from the master peer (e.g. trust IPs resolved from -master) before going back in. * security: skip invalid CIDR in UpdateWhiteList so IsWhiteListed can't panic The revert in the previous commit also rolled back an unrelated bug fix that lived inside #9440: UpdateWhiteList logged on net.ParseCIDR error but did not continue, so the nil *net.IPNet was stored in whiteListCIDR and IsWhiteListed would panic dereferencing cidrnet.Contains(remote) on the next gRPC admin check. Restore the continue. Orthogonal to the fail-closed semantics this PR is reverting.
152 lines
3.9 KiB
Go
152 lines
3.9 KiB
Go
package security
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"net"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
|
)
|
|
|
|
var (
|
|
ErrUnauthorized = errors.New("unauthorized token")
|
|
)
|
|
|
|
/*
|
|
Guard is to ensure data access security.
|
|
There are 2 ways to check access:
|
|
1. white list. It's checking request ip address.
|
|
2. JSON Web Token(JWT) generated from secretKey.
|
|
The jwt can come from:
|
|
1. url parameter jwt=...
|
|
2. request header "Authorization"
|
|
3. cookie with the name "jwt"
|
|
|
|
The white list is checked first because it is easy.
|
|
Then the JWT is checked.
|
|
|
|
The Guard will also check these claims if provided:
|
|
1. "exp" Expiration Time
|
|
2. "nbf" Not Before
|
|
|
|
Generating JWT:
|
|
1. use HS256 to sign
|
|
2. optionally set "exp", "nbf" fields, in Unix time,
|
|
the number of seconds elapsed since January 1, 1970 UTC.
|
|
|
|
Referenced:
|
|
https://github.com/pkieltyka/jwtauth/blob/master/jwtauth.go
|
|
*/
|
|
type Guard struct {
|
|
whiteListIp map[string]struct{}
|
|
whiteListCIDR map[string]*net.IPNet
|
|
SigningKey SigningKey
|
|
ExpiresAfterSec int
|
|
ReadSigningKey SigningKey
|
|
ReadExpiresAfterSec int
|
|
|
|
isWriteActive bool
|
|
isEmptyWhiteList bool
|
|
}
|
|
|
|
func NewGuard(whiteList []string, signingKey string, expiresAfterSec int, readSigningKey string, readExpiresAfterSec int) *Guard {
|
|
g := &Guard{
|
|
SigningKey: SigningKey(signingKey),
|
|
ExpiresAfterSec: expiresAfterSec,
|
|
ReadSigningKey: SigningKey(readSigningKey),
|
|
ReadExpiresAfterSec: readExpiresAfterSec,
|
|
}
|
|
g.UpdateWhiteList(whiteList)
|
|
return g
|
|
}
|
|
|
|
func (g *Guard) WhiteList(f http.HandlerFunc) http.HandlerFunc {
|
|
if !g.isWriteActive {
|
|
//if no security needed, just skip all checking
|
|
return f
|
|
}
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
if err := g.checkWhiteList(w, r); err != nil {
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
return
|
|
}
|
|
f(w, r)
|
|
}
|
|
}
|
|
|
|
func GetActualRemoteHost(r *http.Request) string {
|
|
// For security reasons, only use RemoteAddr to determine the client's IP address.
|
|
// Do not trust headers like X-Forwarded-For, as they can be easily spoofed by clients.
|
|
host, _, err := net.SplitHostPort(r.RemoteAddr)
|
|
if err == nil {
|
|
return host
|
|
}
|
|
|
|
// If SplitHostPort fails, it may be because of a missing port.
|
|
// We try to parse RemoteAddr as a raw host (IP or hostname).
|
|
host = strings.TrimSpace(r.RemoteAddr)
|
|
// It might be an IPv6 address without a port, but with brackets.
|
|
// e.g. "[::1]"
|
|
if strings.HasPrefix(host, "[") && strings.HasSuffix(host, "]") {
|
|
host = host[1 : len(host)-1]
|
|
}
|
|
|
|
// Return the host (can be IP or hostname, just like headers)
|
|
return host
|
|
}
|
|
|
|
func (g *Guard) checkWhiteList(w http.ResponseWriter, r *http.Request) error {
|
|
host := GetActualRemoteHost(r)
|
|
if g.IsWhiteListed(host) {
|
|
return nil
|
|
}
|
|
glog.V(0).Infof("Not in whitelist: %s (original RemoteAddr: %s)", host, r.RemoteAddr)
|
|
return fmt.Errorf("Not in whitelist: %s", host)
|
|
}
|
|
|
|
// IsWhiteListed returns true if the given host IP is allowed by the guard.
|
|
// When no whitelist is configured (security inactive), all hosts are allowed.
|
|
func (g *Guard) IsWhiteListed(host string) bool {
|
|
if !g.isWriteActive {
|
|
return true
|
|
}
|
|
if g.isEmptyWhiteList {
|
|
return true
|
|
}
|
|
if _, ok := g.whiteListIp[host]; ok {
|
|
return true
|
|
}
|
|
remote := net.ParseIP(host)
|
|
if remote != nil {
|
|
for _, cidrnet := range g.whiteListCIDR {
|
|
if cidrnet.Contains(remote) {
|
|
return true
|
|
}
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func (g *Guard) UpdateWhiteList(whiteList []string) {
|
|
whiteListIp := make(map[string]struct{})
|
|
whiteListCIDR := make(map[string]*net.IPNet)
|
|
for _, ip := range whiteList {
|
|
if strings.Contains(ip, "/") {
|
|
_, cidrnet, err := net.ParseCIDR(ip)
|
|
if err != nil {
|
|
glog.Errorf("Parse CIDR %s in whitelist failed: %v", ip, err)
|
|
continue
|
|
}
|
|
whiteListCIDR[ip] = cidrnet
|
|
} else {
|
|
whiteListIp[ip] = struct{}{}
|
|
}
|
|
}
|
|
g.isEmptyWhiteList = len(whiteListIp) == 0 && len(whiteListCIDR) == 0
|
|
g.isWriteActive = !g.isEmptyWhiteList || len(g.SigningKey) != 0
|
|
g.whiteListIp = whiteListIp
|
|
g.whiteListCIDR = whiteListCIDR
|
|
}
|