diff --git a/weed/command/scaffold/security.toml b/weed/command/scaffold/security.toml index 173f1929a1..e9e7b09fb9 100644 --- a/weed/command/scaffold/security.toml +++ b/weed/command/scaffold/security.toml @@ -74,6 +74,14 @@ enabled = true user = "" password = "" +# HTTP Basic Auth on the WebDAV gateway (weed webdav, weed server -webdav, +# weed mini). Without a password, anyone who can reach the WebDAV port reads, +# writes and deletes everything under its -filer.path. +# Env: WEED_WEBDAV_BASIC_AUTH_USER, WEED_WEBDAV_BASIC_AUTH_PASSWORD +[webdav.basic_auth] +user = "" +password = "" + # this jwt signing key is used for read operations on volume servers: # - the Master server generates the JWT, which can be used to read a certain file on a volume server # - every process that reads chunks straight from volume servers generates its diff --git a/weed/command/webdav.go b/weed/command/webdav.go index 90e9e4400e..66ca2de7d1 100644 --- a/weed/command/webdav.go +++ b/weed/command/webdav.go @@ -93,6 +93,11 @@ func (wo *WebDavOption) startWebDav() bool { util.SetOutboundLocalIP(*wo.ipBind) + basicAuth := webDavBasicAuth() + if basicAuth == nil && !isLoopbackIp(*wo.ipBind) { + glog.Warningf("WebDAV has no authentication: anyone who can reach %s:%d can read, write and delete under %s. Set [webdav.basic_auth] in security.toml.", *wo.ipBind, *wo.port, *wo.filerRootPath) + } + // detect current user uid, gid := uint32(0), uint32(0) if u, err := user.Current(); err == nil { @@ -142,6 +147,7 @@ func (wo *WebDavOption) startWebDav() bool { CacheDir: *wo.cacheDir, CacheSizeMB: *wo.cacheSizeMB, MaxMB: *wo.maxMB, + BasicAuth: basicAuth, }) if webdavServer_err != nil { glog.Fatalf("WebDav Server startup error: %v", webdavServer_err) @@ -185,3 +191,10 @@ func (wo *WebDavOption) startWebDav() bool { return true } + +// webDavBasicAuth reads [webdav.basic_auth] from security.toml and the WEED_ +// environment; nil means WebDAV is open. +func webDavBasicAuth() *security.BasicAuth { + v := util.GetViper() + return security.NewBasicAuth(v.GetString("webdav.basic_auth.user"), v.GetString("webdav.basic_auth.password")) +} diff --git a/weed/server/webdav_server.go b/weed/server/webdav_server.go index 90576944e8..4373aa9d5c 100644 --- a/weed/server/webdav_server.go +++ b/weed/server/webdav_server.go @@ -44,6 +44,7 @@ type WebDavOption struct { CacheDir string CacheSizeMB int64 MaxMB int + BasicAuth *security.BasicAuth } type WebDavServer struct { @@ -72,23 +73,42 @@ func NewWebDavServer(option *WebDavOption) (ws *WebDavServer, err error) { fs = NewWrappedFs(fs, path.Clean(option.FilerRootPath)) } - ws = &WebDavServer{ - option: option, - grpcDialOption: security.LoadClientTLS(util.GetViper(), "grpc.filer"), - Handler: listedEntriesHandler{ - next: copyMoveGuardHandler{ - fs: fs, - next: &webdav.Handler{ - FileSystem: fs, - LockSystem: webdav.NewMemLS(), - }, + var handler http.Handler = listedEntriesHandler{ + next: copyMoveGuardHandler{ + fs: fs, + next: &webdav.Handler{ + FileSystem: fs, + LockSystem: webdav.NewMemLS(), }, }, } + if option.BasicAuth != nil { + handler = basicAuthHandler{auth: option.BasicAuth, next: handler} + } + + ws = &WebDavServer{ + option: option, + grpcDialOption: security.LoadClientTLS(util.GetViper(), "grpc.filer"), + Handler: handler, + } return ws, nil } +type basicAuthHandler struct { + auth *security.BasicAuth + next http.Handler +} + +func (h basicAuthHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { + if !h.auth.Allows(r) { + w.Header().Set("WWW-Authenticate", `Basic realm="SeaweedFS WebDAV", charset="UTF-8"`) + http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized) + return + } + h.next.ServeHTTP(w, r) +} + // putOutcomeKey carries the flag WebDavFileSystem.OpenFile sets when a PUT // replaced an existing entry, so the PUT can answer 204 instead of 201 type putOutcomeKey struct{} diff --git a/weed/server/webdav_server_test.go b/weed/server/webdav_server_test.go index be4f4dfbf7..6f928cd667 100644 --- a/weed/server/webdav_server_test.go +++ b/weed/server/webdav_server_test.go @@ -18,6 +18,7 @@ import ( "github.com/seaweedfs/seaweedfs/weed/pb" "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb" + "github.com/seaweedfs/seaweedfs/weed/security" "github.com/seaweedfs/seaweedfs/weed/util" "github.com/seaweedfs/seaweedfs/weed/wdclient" "github.com/stretchr/testify/assert" @@ -994,3 +995,46 @@ func TestWebDavFileSystem_Rename_MissingDestParent(t *testing.T) { assert.NoError(t, fs.Rename(context.Background(), "/dir/file.txt", "/dir/moved.txt")) assert.NotNil(t, filer.req) } + +func TestWebDavBasicAuth(t *testing.T) { + ws, err := NewWebDavServer(&WebDavOption{Filer: "127.0.0.1:1", CacheDir: t.TempDir(), BasicAuth: security.NewBasicAuth("alice", "secret")}) + if err != nil { + t.Fatal(err) + } + auth, ok := ws.Handler.(basicAuthHandler) + if !ok { + t.Fatalf("handler %T does not check Basic Auth", ws.Handler) + } + reached := 0 + auth.next = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { reached++ }) + + for _, creds := range [][2]string{{}, {"alice", "wrong"}, {"bob", "secret"}} { + req := httptest.NewRequest("PROPFIND", "/buckets/", nil) + if creds[0] != "" { + req.SetBasicAuth(creds[0], creds[1]) + } + rec := httptest.NewRecorder() + auth.ServeHTTP(rec, req) + if rec.Code != http.StatusUnauthorized || rec.Header().Get("WWW-Authenticate") == "" { + t.Fatalf("credentials %q: got %d %q, want 401 with a Basic challenge", creds, rec.Code, rec.Header().Get("WWW-Authenticate")) + } + } + if reached != 0 { + t.Fatalf("an unauthenticated request reached WebDAV") + } + + req := httptest.NewRequest("PROPFIND", "/buckets/", nil) + req.SetBasicAuth("alice", "secret") + auth.ServeHTTP(httptest.NewRecorder(), req) + if reached != 1 { + t.Fatalf("valid credentials did not reach WebDAV") + } + + open, err := NewWebDavServer(&WebDavOption{Filer: "127.0.0.1:1", CacheDir: t.TempDir()}) + if err != nil { + t.Fatal(err) + } + if _, ok := open.Handler.(basicAuthHandler); ok { + t.Fatalf("Basic Auth is on without a password") + } +}