2013-12-02 17:37:36 +08:00
|
|
|
package weed_server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2019-03-04 02:17:44 +08:00
|
|
|
"strings"
|
2014-10-27 02:34:55 +08:00
|
|
|
|
2019-02-14 16:08:20 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/security"
|
2016-06-03 09:09:14 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/stats"
|
2013-12-02 17:37:36 +08:00
|
|
|
)
|
|
|
|
|
2015-02-26 15:59:07 +08:00
|
|
|
/*
|
2013-12-02 17:37:36 +08:00
|
|
|
|
2015-03-09 16:10:01 +08:00
|
|
|
If volume server is started with a separated public port, the public port will
|
|
|
|
be more "secure".
|
|
|
|
|
|
|
|
Public port currently only supports reads.
|
|
|
|
|
|
|
|
Later writes on public port can have one of the 3
|
2015-02-26 15:59:07 +08:00
|
|
|
security settings:
|
|
|
|
1. not secured
|
|
|
|
2. secured by white list
|
|
|
|
3. secured by JWT(Json Web Token)
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
func (vs *VolumeServer) privateStoreHandler(w http.ResponseWriter, r *http.Request) {
|
2013-12-02 17:37:36 +08:00
|
|
|
switch r.Method {
|
2018-07-23 01:27:10 +08:00
|
|
|
case "GET", "HEAD":
|
2014-03-26 04:46:59 +08:00
|
|
|
stats.ReadRequest()
|
2014-05-27 12:15:05 +08:00
|
|
|
vs.GetOrHeadHandler(w, r)
|
2013-12-02 17:37:36 +08:00
|
|
|
case "DELETE":
|
2014-03-26 04:46:59 +08:00
|
|
|
stats.DeleteRequest()
|
2015-02-26 15:59:07 +08:00
|
|
|
vs.guard.WhiteList(vs.DeleteHandler)(w, r)
|
2018-07-23 01:27:10 +08:00
|
|
|
case "PUT", "POST":
|
2014-03-26 04:46:59 +08:00
|
|
|
stats.WriteRequest()
|
2015-02-26 15:59:07 +08:00
|
|
|
vs.guard.WhiteList(vs.PostHandler)(w, r)
|
2013-12-02 17:37:36 +08:00
|
|
|
}
|
|
|
|
}
|
2014-04-14 15:13:18 +08:00
|
|
|
|
2015-03-09 16:10:01 +08:00
|
|
|
func (vs *VolumeServer) publicReadOnlyHandler(w http.ResponseWriter, r *http.Request) {
|
2015-02-26 15:59:07 +08:00
|
|
|
switch r.Method {
|
|
|
|
case "GET":
|
|
|
|
stats.ReadRequest()
|
|
|
|
vs.GetOrHeadHandler(w, r)
|
|
|
|
case "HEAD":
|
|
|
|
stats.ReadRequest()
|
|
|
|
vs.GetOrHeadHandler(w, r)
|
2014-04-14 16:00:09 +08:00
|
|
|
}
|
|
|
|
}
|
2019-02-14 16:08:20 +08:00
|
|
|
|
|
|
|
func (vs *VolumeServer) maybeCheckJwtAuthorization(r *http.Request, vid, fid string) bool {
|
|
|
|
|
|
|
|
if len(vs.guard.SigningKey) == 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
tokenStr := security.GetJwt(r)
|
|
|
|
if tokenStr == "" {
|
|
|
|
glog.V(1).Infof("missing jwt from %s", r.RemoteAddr)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
token, err := security.DecodeJwt(vs.guard.SigningKey, tokenStr)
|
|
|
|
if err != nil {
|
|
|
|
glog.V(1).Infof("jwt verification error from %s: %v", r.RemoteAddr, err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if !token.Valid {
|
|
|
|
glog.V(1).Infof("jwt invalid from %s: %v", r.RemoteAddr, tokenStr)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if sc, ok := token.Claims.(*security.SeaweedFileIdClaims); ok {
|
2019-03-04 02:17:44 +08:00
|
|
|
if sepIndex := strings.LastIndex(fid, "_"); sepIndex > 0 {
|
|
|
|
fid = fid[:sepIndex]
|
|
|
|
}
|
2019-02-14 16:08:20 +08:00
|
|
|
return sc.Fid == vid+","+fid
|
|
|
|
}
|
|
|
|
glog.V(1).Infof("unexpected jwt from %s: %v", r.RemoteAddr, tokenStr)
|
|
|
|
return false
|
|
|
|
}
|