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.

137 lines
3.4 KiB

2 years ago
2 years ago
10 years ago
10 years ago
10 years ago
9 years ago
9 years ago
10 years ago
  1. package security
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/seaweedfs/seaweedfs/weed/glog"
  6. "net"
  7. "net/http"
  8. "strings"
  9. )
  10. var (
  11. ErrUnauthorized = errors.New("unauthorized token")
  12. )
  13. /*
  14. Guard is to ensure data access security.
  15. There are 2 ways to check access:
  16. 1. white list. It's checking request ip address.
  17. 2. JSON Web Token(JWT) generated from secretKey.
  18. The jwt can come from:
  19. 1. url parameter jwt=...
  20. 2. request header "Authorization"
  21. 3. cookie with the name "jwt"
  22. The white list is checked first because it is easy.
  23. Then the JWT is checked.
  24. The Guard will also check these claims if provided:
  25. 1. "exp" Expiration Time
  26. 2. "nbf" Not Before
  27. Generating JWT:
  28. 1. use HS256 to sign
  29. 2. optionally set "exp", "nbf" fields, in Unix time,
  30. the number of seconds elapsed since January 1, 1970 UTC.
  31. Referenced:
  32. https://github.com/pkieltyka/jwtauth/blob/master/jwtauth.go
  33. */
  34. type Guard struct {
  35. whiteListIp map[string]struct{}
  36. whiteListCIDR map[string]*net.IPNet
  37. SigningKey SigningKey
  38. ExpiresAfterSec int
  39. ReadSigningKey SigningKey
  40. ReadExpiresAfterSec int
  41. isWriteActive bool
  42. isEmptyWhiteList bool
  43. }
  44. func NewGuard(whiteList []string, signingKey string, expiresAfterSec int, readSigningKey string, readExpiresAfterSec int) *Guard {
  45. g := &Guard{
  46. SigningKey: SigningKey(signingKey),
  47. ExpiresAfterSec: expiresAfterSec,
  48. ReadSigningKey: SigningKey(readSigningKey),
  49. ReadExpiresAfterSec: readExpiresAfterSec,
  50. }
  51. g.UpdateWhiteList(whiteList)
  52. return g
  53. }
  54. func (g *Guard) WhiteList(f http.HandlerFunc) http.HandlerFunc {
  55. if !g.isWriteActive {
  56. //if no security needed, just skip all checking
  57. return f
  58. }
  59. return func(w http.ResponseWriter, r *http.Request) {
  60. if err := g.checkWhiteList(w, r); err != nil {
  61. w.WriteHeader(http.StatusUnauthorized)
  62. return
  63. }
  64. f(w, r)
  65. }
  66. }
  67. func GetActualRemoteHost(r *http.Request) (host string, err error) {
  68. host = r.Header.Get("HTTP_X_FORWARDED_FOR")
  69. if host == "" {
  70. host = r.Header.Get("X-FORWARDED-FOR")
  71. }
  72. if strings.Contains(host, ",") {
  73. host = host[0:strings.Index(host, ",")]
  74. }
  75. if host == "" {
  76. host, _, err = net.SplitHostPort(r.RemoteAddr)
  77. }
  78. return
  79. }
  80. func (g *Guard) checkWhiteList(w http.ResponseWriter, r *http.Request) error {
  81. if g.isEmptyWhiteList {
  82. return nil
  83. }
  84. host, err := GetActualRemoteHost(r)
  85. if err != nil {
  86. return fmt.Errorf("get actual remote host %s in checkWhiteList failed: %v", r.RemoteAddr, err)
  87. }
  88. if _, ok := g.whiteListIp[host]; ok {
  89. return nil
  90. }
  91. for _, cidrnet := range g.whiteListCIDR {
  92. // If the whitelist entry contains a "/" it
  93. // is a CIDR range, and we should check the
  94. remote := net.ParseIP(host)
  95. if cidrnet.Contains(remote) {
  96. return nil
  97. }
  98. }
  99. glog.V(0).Infof("Not in whitelist: %s", r.RemoteAddr)
  100. return fmt.Errorf("Not in whitelist: %s", r.RemoteAddr)
  101. }
  102. func (g *Guard) UpdateWhiteList(whiteList []string) {
  103. whiteListIp := make(map[string]struct{})
  104. whiteListCIDR := make(map[string]*net.IPNet)
  105. for _, ip := range whiteList {
  106. if strings.Contains(ip, "/") {
  107. _, cidrnet, err := net.ParseCIDR(ip)
  108. if err != nil {
  109. glog.Errorf("Parse CIDR %s in whitelist failed: %v", ip, err)
  110. }
  111. whiteListCIDR[ip] = cidrnet
  112. } else {
  113. whiteListIp[ip] = struct{}{}
  114. }
  115. }
  116. g.isEmptyWhiteList = len(whiteListIp) == 0 && len(whiteListCIDR) == 0
  117. g.isWriteActive = !g.isEmptyWhiteList || len(g.SigningKey) != 0
  118. g.whiteListIp = whiteListIp
  119. g.whiteListCIDR = whiteListCIDR
  120. }