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.

114 lines
2.9 KiB

4 years ago
4 years ago
6 years ago
  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. }