You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

97 lines
2.3 KiB

4 years ago
4 years ago
4 years ago
6 years ago
  1. package weed_server
  2. import (
  3. "github.com/chrislusf/seaweedfs/weed/util"
  4. "net/http"
  5. "strings"
  6. "github.com/chrislusf/seaweedfs/weed/glog"
  7. "github.com/chrislusf/seaweedfs/weed/security"
  8. "github.com/chrislusf/seaweedfs/weed/stats"
  9. )
  10. /*
  11. If volume server is started with a separated public port, the public port will
  12. be more "secure".
  13. Public port currently only supports reads.
  14. Later writes on public port can have one of the 3
  15. security settings:
  16. 1. not secured
  17. 2. secured by white list
  18. 3. secured by JWT(Json Web Token)
  19. */
  20. func (vs *VolumeServer) privateStoreHandler(w http.ResponseWriter, r *http.Request) {
  21. w.Header().Set("Server", "SeaweedFS Volume "+util.VERSION)
  22. switch r.Method {
  23. case "GET", "HEAD":
  24. stats.ReadRequest()
  25. vs.GetOrHeadHandler(w, r)
  26. case "DELETE":
  27. stats.DeleteRequest()
  28. vs.guard.WhiteList(vs.DeleteHandler)(w, r)
  29. case "PUT", "POST":
  30. stats.WriteRequest()
  31. vs.guard.WhiteList(vs.PostHandler)(w, r)
  32. }
  33. }
  34. func (vs *VolumeServer) publicReadOnlyHandler(w http.ResponseWriter, r *http.Request) {
  35. w.Header().Set("Server", "SeaweedFS Volume "+util.VERSION)
  36. switch r.Method {
  37. case "GET":
  38. stats.ReadRequest()
  39. vs.GetOrHeadHandler(w, r)
  40. case "HEAD":
  41. stats.ReadRequest()
  42. vs.GetOrHeadHandler(w, r)
  43. }
  44. }
  45. func (vs *VolumeServer) maybeCheckJwtAuthorization(r *http.Request, vid, fid string, isWrite bool) bool {
  46. var signingKey security.SigningKey
  47. if isWrite {
  48. if len(vs.guard.SigningKey) == 0 {
  49. return true
  50. } else {
  51. signingKey = vs.guard.SigningKey
  52. }
  53. } else {
  54. if len(vs.guard.ReadSigningKey) == 0 {
  55. return true
  56. } else {
  57. signingKey = vs.guard.ReadSigningKey
  58. }
  59. }
  60. tokenStr := security.GetJwt(r)
  61. if tokenStr == "" {
  62. glog.V(1).Infof("missing jwt from %s", r.RemoteAddr)
  63. return false
  64. }
  65. token, err := security.DecodeJwt(signingKey, tokenStr)
  66. if err != nil {
  67. glog.V(1).Infof("jwt verification error from %s: %v", r.RemoteAddr, err)
  68. return false
  69. }
  70. if !token.Valid {
  71. glog.V(1).Infof("jwt invalid from %s: %v", r.RemoteAddr, tokenStr)
  72. return false
  73. }
  74. if sc, ok := token.Claims.(*security.SeaweedFileIdClaims); ok {
  75. if sepIndex := strings.LastIndex(fid, "_"); sepIndex > 0 {
  76. fid = fid[:sepIndex]
  77. }
  78. return sc.Fid == vid+","+fid
  79. }
  80. glog.V(1).Infof("unexpected jwt from %s: %v", r.RemoteAddr, tokenStr)
  81. return false
  82. }