mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-04 18:17:16 +01:00
* security: add a constant-time HTTP Basic Auth check Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * filer: require HTTP Basic Auth on the HTTP ports when configured With filer.basic_auth.password set in security.toml (or WEED_FILER_BASIC_AUTH_PASSWORD), a request on the filer HTTP ports, TUS included, must carry those credentials or a valid filer JWT, and an access level with no JWT signing key is no longer open. Valid Basic credentials grant read and write. 401 responses then carry a Basic challenge so browsers prompt. The credentials reload on SIGHUP with the signing keys. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * scaffold: document filer.basic_auth in security.toml Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * filer: enable reloaded Basic Auth before swapping the JWT keys Reload cleared the old signing keys before storing the new Basic credentials, so a switch from JWT to Basic Auth briefly left both off and admitted anonymous requests. Store new credentials first and clear old ones last. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * filer: refuse Basic credentials on cross-origin browser requests Browsers resend cached Basic credentials on requests other websites trigger, such as a form POST with mv.from, and the default cors.allowed_origins "*" lets them through. Accept Basic credentials only on requests without an Origin, from the filer's own host, or from an origin explicitly listed in cors.allowed_origins. A JWT is still accepted from any origin. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * filer: recheck Basic Auth after reading an empty signing key A request that loaded Basic Auth as off, then read the signing key after a JWT-to-Basic Reload had cleared it, was let through anonymously. Open access now also needs Basic Auth to be off after the key read; with Reload enabling the new credential before disabling the old one, the two loads cannot both miss a credential that was on throughout. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * security: compare Basic Auth credentials as MACs under a per-process key A plain SHA-256 of the password is what password-hash scanners flag; keying the digests with a random per-process HMAC key keeps the length-independent constant-time compare without holding one. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * util: read a configuration file without merging into the shared viper LoadConfiguration merges each read into the shared viper, so a key removed from the file keeps its old value after a reload. ReadConfiguration reads the file into a fresh viper with the same search path and WEED_ environment overrides. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * filer: turn Basic Auth off when security.toml no longer sets it The shared viper merges each SIGHUP reload into what it held, so removing [filer.basic_auth] kept the old password valid. Read the credentials from security.toml alone at startup and on reload; an unreadable file keeps the current setting. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * filer: keep Basic Auth when security.toml cannot be read A malformed file at startup dropped a WEED_FILER_BASIC_AUTH_PASSWORD from the environment, and viper reports an unsearchable config directory as a missing file, which turned Basic Auth off on reload. ReadConfiguration now always returns the environment overrides and reports whether the file was read; a reload that could not read it keeps the current credentials. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * filer: keep reloading Basic Auth from the security.toml first read A search directory that turned unsearchable made viper fall through to a lower-priority security.toml, and a reload from a file without [filer.basic_auth] turned Basic Auth off. ReadConfiguration now takes the file to read, and the filer reads the file it first found on every reload. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
243 lines
6.1 KiB
Go
243 lines
6.1 KiB
Go
package util
|
|
|
|
import (
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
|
)
|
|
|
|
var (
|
|
ConfigurationFileDirectory DirectoryValueType
|
|
loadSecurityConfigOnce sync.Once
|
|
)
|
|
|
|
type DirectoryValueType string
|
|
|
|
func (s *DirectoryValueType) Set(value string) error {
|
|
*s = DirectoryValueType(value)
|
|
return nil
|
|
}
|
|
func (s *DirectoryValueType) String() string {
|
|
return string(*s)
|
|
}
|
|
|
|
type Configuration interface {
|
|
GetString(key string) string
|
|
GetBool(key string) bool
|
|
GetInt(key string) int
|
|
GetStringSlice(key string) []string
|
|
SetDefault(key string, value interface{})
|
|
}
|
|
|
|
func LoadSecurityConfiguration() {
|
|
loadSecurityConfigOnce.Do(func() {
|
|
LoadConfiguration("security", false)
|
|
})
|
|
}
|
|
|
|
func LoadConfiguration(configFileName string, required bool) (loaded bool) {
|
|
// MergeInConfig mutates the shared viper that ViperProxy serializes;
|
|
// take the same lock so a merge cannot race a reader or SetDefault.
|
|
vp.Lock()
|
|
defer vp.Unlock()
|
|
|
|
setConfigurationPaths(viper.GetViper(), configFileName)
|
|
if err := viper.MergeInConfig(); err != nil { // Handle errors reading the config file
|
|
if strings.Contains(err.Error(), "Not Found") {
|
|
glog.V(1).Infof("Reading %s: %v", viper.ConfigFileUsed(), err)
|
|
} else {
|
|
// If the config is required, fail immediately
|
|
if required {
|
|
glog.Fatalf("Reading %s: %v", viper.ConfigFileUsed(), err)
|
|
}
|
|
// If the config is optional, log a warning but don't crash
|
|
glog.Warningf("Reading %s: %v. Skipping optional configuration.", viper.ConfigFileUsed(), err)
|
|
}
|
|
if required {
|
|
glog.Fatalf("Failed to load %s.toml file from current directory, or $HOME/.seaweedfs/, or /etc/seaweedfs/"+
|
|
"\n\nPlease use this command to generate the default %s.toml file\n"+
|
|
" weed scaffold -config=%s -output=.\n\n\n",
|
|
configFileName, configFileName, configFileName)
|
|
}
|
|
return false
|
|
}
|
|
glog.V(1).Infof("Reading %s.toml from %s", configFileName, viper.ConfigFileUsed())
|
|
|
|
return true
|
|
}
|
|
|
|
// ReadConfiguration reads configFileName into a new viper with the WEED_
|
|
// environment overrides. Unlike LoadConfiguration it does not merge, so a key
|
|
// the file no longer sets reads as unset. A non-empty configFile is read in
|
|
// place of searching the configuration directories. used is the file read, or
|
|
// empty when none could be; the environment still applies then.
|
|
func ReadConfiguration(configFileName, configFile string) (v *ViperProxy, used string) {
|
|
fresh := viper.New()
|
|
setEnvOverrides(fresh)
|
|
if configFile != "" {
|
|
fresh.SetConfigFile(configFile)
|
|
} else {
|
|
setConfigurationPaths(fresh, configFileName)
|
|
}
|
|
if err := fresh.ReadInConfig(); err != nil {
|
|
glog.V(1).Infof("Reading %s: %v", configFileName, err)
|
|
return NewViperProxy(fresh), ""
|
|
}
|
|
return NewViperProxy(fresh), fresh.ConfigFileUsed()
|
|
}
|
|
|
|
func setConfigurationPaths(v *viper.Viper, configFileName string) {
|
|
v.SetConfigName(configFileName)
|
|
v.AddConfigPath(ResolvePath(ConfigurationFileDirectory.String()))
|
|
v.AddConfigPath(".")
|
|
v.AddConfigPath("$HOME/.seaweedfs")
|
|
v.AddConfigPath("/usr/local/etc/seaweedfs/")
|
|
v.AddConfigPath("/etc/seaweedfs/")
|
|
}
|
|
|
|
func setEnvOverrides(v *viper.Viper) {
|
|
v.AutomaticEnv()
|
|
v.SetEnvPrefix("weed")
|
|
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
|
|
}
|
|
|
|
// ViperProxy serializes access to the global viper. The wrapped Viper is a
|
|
// named field, not embedded, so every method must be declared here under the
|
|
// mutex — a promoted method would take no lock and race, e.g. GetStringMap
|
|
// against a concurrent SetDefault during `weed server` startup.
|
|
type ViperProxy struct {
|
|
v *viper.Viper
|
|
sync.Mutex
|
|
}
|
|
|
|
var (
|
|
vp = &ViperProxy{}
|
|
)
|
|
|
|
func (vp *ViperProxy) SetDefault(key string, value interface{}) {
|
|
vp.Lock()
|
|
defer vp.Unlock()
|
|
vp.v.SetDefault(key, value)
|
|
}
|
|
|
|
func (vp *ViperProxy) AutomaticEnv() {
|
|
vp.Lock()
|
|
defer vp.Unlock()
|
|
vp.v.AutomaticEnv()
|
|
}
|
|
|
|
func (vp *ViperProxy) GetString(key string) string {
|
|
vp.Lock()
|
|
defer vp.Unlock()
|
|
return vp.v.GetString(key)
|
|
}
|
|
|
|
func (vp *ViperProxy) GetBool(key string) bool {
|
|
vp.Lock()
|
|
defer vp.Unlock()
|
|
return vp.v.GetBool(key)
|
|
}
|
|
|
|
func (vp *ViperProxy) GetInt(key string) int {
|
|
vp.Lock()
|
|
defer vp.Unlock()
|
|
return vp.v.GetInt(key)
|
|
}
|
|
|
|
func (vp *ViperProxy) GetStringSlice(key string) []string {
|
|
vp.Lock()
|
|
defer vp.Unlock()
|
|
// a []string value comes back uncast — the same shared subtree issue
|
|
// GetStringMap has, so hand the caller a copy
|
|
return append([]string(nil), vp.v.GetStringSlice(key)...)
|
|
}
|
|
|
|
func (vp *ViperProxy) GetStringMap(key string) map[string]interface{} {
|
|
vp.Lock()
|
|
defer vp.Unlock()
|
|
// viper hands back its internal subtree, so a caller iterating it after
|
|
// the lock is released would race the next SetDefault — copy it out.
|
|
return deepCopyStringMap(vp.v.GetStringMap(key))
|
|
}
|
|
|
|
func deepCopyStringMap(m map[string]interface{}) map[string]interface{} {
|
|
out := make(map[string]interface{}, len(m))
|
|
for k, v := range m {
|
|
out[k] = deepCopyValue(v)
|
|
}
|
|
return out
|
|
}
|
|
|
|
func deepCopyValue(v interface{}) interface{} {
|
|
switch nested := v.(type) {
|
|
case map[string]interface{}:
|
|
return deepCopyStringMap(nested)
|
|
case []interface{}:
|
|
out := make([]interface{}, len(nested))
|
|
for i, item := range nested {
|
|
out[i] = deepCopyValue(item)
|
|
}
|
|
return out
|
|
default:
|
|
return v
|
|
}
|
|
}
|
|
|
|
func (vp *ViperProxy) GetUint32(key string) uint32 {
|
|
vp.Lock()
|
|
defer vp.Unlock()
|
|
return vp.v.GetUint32(key)
|
|
}
|
|
|
|
func (vp *ViperProxy) GetFloat64(key string) float64 {
|
|
vp.Lock()
|
|
defer vp.Unlock()
|
|
return vp.v.GetFloat64(key)
|
|
}
|
|
|
|
func (vp *ViperProxy) GetDuration(key string) time.Duration {
|
|
vp.Lock()
|
|
defer vp.Unlock()
|
|
return vp.v.GetDuration(key)
|
|
}
|
|
|
|
func (vp *ViperProxy) Set(key string, value interface{}) {
|
|
vp.Lock()
|
|
defer vp.Unlock()
|
|
vp.v.Set(key, value)
|
|
}
|
|
|
|
func (vp *ViperProxy) IsSet(key string) bool {
|
|
vp.Lock()
|
|
defer vp.Unlock()
|
|
return vp.v.IsSet(key)
|
|
}
|
|
|
|
func (vp *ViperProxy) AllKeys() []string {
|
|
vp.Lock()
|
|
defer vp.Unlock()
|
|
return vp.v.AllKeys()
|
|
}
|
|
|
|
// NewViperProxy wraps a specific viper — for configuration loaded from a
|
|
// source other than the shared instance GetViper returns.
|
|
func NewViperProxy(v *viper.Viper) *ViperProxy {
|
|
return &ViperProxy{v: v}
|
|
}
|
|
|
|
func GetViper() *ViperProxy {
|
|
vp.Lock()
|
|
defer vp.Unlock()
|
|
|
|
if vp.v == nil {
|
|
vp.v = viper.GetViper()
|
|
setEnvOverrides(vp.v)
|
|
}
|
|
|
|
return vp
|
|
}
|