volume_server_handlers.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package weed_server
  2. import (
  3. "net/http"
  4. "strings"
  5. "github.com/chrislusf/seaweedfs/weed/util"
  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. if r.Header.Get("Origin") != "" {
  23. w.Header().Set("Access-Control-Allow-Origin", "*")
  24. w.Header().Set("Access-Control-Allow-Credentials", "true")
  25. }
  26. switch r.Method {
  27. case "GET", "HEAD":
  28. stats.ReadRequest()
  29. vs.GetOrHeadHandler(w, r)
  30. case "DELETE":
  31. stats.DeleteRequest()
  32. vs.guard.WhiteList(vs.DeleteHandler)(w, r)
  33. case "PUT", "POST":
  34. stats.WriteRequest()
  35. vs.guard.WhiteList(vs.PostHandler)(w, r)
  36. case "OPTIONS":
  37. stats.ReadRequest()
  38. w.Header().Add("Access-Control-Allow-Methods", "PUT, POST, GET, DELETE, OPTIONS")
  39. w.Header().Add("Access-Control-Allow-Headers", "*")
  40. }
  41. }
  42. func (vs *VolumeServer) publicReadOnlyHandler(w http.ResponseWriter, r *http.Request) {
  43. w.Header().Set("Server", "SeaweedFS Volume "+util.VERSION)
  44. if r.Header.Get("Origin") != "" {
  45. w.Header().Set("Access-Control-Allow-Origin", "*")
  46. w.Header().Set("Access-Control-Allow-Credentials", "true")
  47. }
  48. switch r.Method {
  49. case "GET":
  50. stats.ReadRequest()
  51. vs.GetOrHeadHandler(w, r)
  52. case "HEAD":
  53. stats.ReadRequest()
  54. vs.GetOrHeadHandler(w, r)
  55. case "OPTIONS":
  56. stats.ReadRequest()
  57. w.Header().Add("Access-Control-Allow-Methods", "GET, OPTIONS")
  58. w.Header().Add("Access-Control-Allow-Headers", "*")
  59. }
  60. }
  61. func (vs *VolumeServer) maybeCheckJwtAuthorization(r *http.Request, vid, fid string, isWrite bool) bool {
  62. var signingKey security.SigningKey
  63. if isWrite {
  64. if len(vs.guard.SigningKey) == 0 {
  65. return true
  66. } else {
  67. signingKey = vs.guard.SigningKey
  68. }
  69. } else {
  70. if len(vs.guard.ReadSigningKey) == 0 {
  71. return true
  72. } else {
  73. signingKey = vs.guard.ReadSigningKey
  74. }
  75. }
  76. tokenStr := security.GetJwt(r)
  77. if tokenStr == "" {
  78. glog.V(1).Infof("missing jwt from %s", r.RemoteAddr)
  79. return false
  80. }
  81. token, err := security.DecodeJwt(signingKey, tokenStr)
  82. if err != nil {
  83. glog.V(1).Infof("jwt verification error from %s: %v", r.RemoteAddr, err)
  84. return false
  85. }
  86. if !token.Valid {
  87. glog.V(1).Infof("jwt invalid from %s: %v", r.RemoteAddr, tokenStr)
  88. return false
  89. }
  90. if sc, ok := token.Claims.(*security.SeaweedFileIdClaims); ok {
  91. if sepIndex := strings.LastIndex(fid, "_"); sepIndex > 0 {
  92. fid = fid[:sepIndex]
  93. }
  94. return sc.Fid == vid+","+fid
  95. }
  96. glog.V(1).Infof("unexpected jwt from %s: %v", r.RemoteAddr, tokenStr)
  97. return false
  98. }