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.

260 lines
7.2 KiB

3 years ago
7 years ago
7 years ago
9 months ago
3 years ago
3 years ago
8 months ago
8 months ago
8 months ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
FEATURE: add JWT to HTTP endpoints of Filer and use them in S3 Client - one JWT for reading and one for writing, analogous to how the JWT between Master and Volume Server works - I did not implement IP `whiteList` parameter on the filer Additionally, because http_util.DownloadFile now sets the JWT, the `download` command should now work when `jwt.signing.read` is configured. By looking at the code, I think this case did not work before. ## Docs to be adjusted after a release Page `Amazon-S3-API`: ``` # Authentication with Filer You can use mTLS for the gRPC connection between S3-API-Proxy and the filer, as explained in [Security-Configuration](Security-Configuration) - controlled by the `grpc.*` configuration in `security.toml`. Starting with version XX, it is also possible to authenticate the HTTP operations between the S3-API-Proxy and the Filer (especially uploading new files). This is configured by setting `filer_jwt.signing.key` and `filer_jwt.signing.read.key` in `security.toml`. With both configurations (gRPC and JWT), it is possible to have Filer and S3 communicate in fully authenticated fashion; so Filer will reject any unauthenticated communication. ``` Page `Security Overview`: ``` The following items are not covered, yet: - master server http REST services Starting with version XX, the Filer HTTP REST services can be secured with a JWT, by setting `filer_jwt.signing.key` and `filer_jwt.signing.read.key` in `security.toml`. ... Before version XX: "weed filer -disableHttp", disable http operations, only gRPC operations are allowed. This works with "weed mount" by FUSE. It does **not work** with the [S3 Gateway](Amazon S3 API), as this does HTTP calls to the Filer. Starting with version XX: secured by JWT, by setting `filer_jwt.signing.key` and `filer_jwt.signing.read.key` in `security.toml`. **This now works with the [S3 Gateway](Amazon S3 API).** ... # Securing Filer HTTP with JWT To enable JWT-based access control for the Filer, 1. generate `security.toml` file by `weed scaffold -config=security` 2. set `filer_jwt.signing.key` to a secret string - and optionally filer_jwt.signing.read.key` as well to a secret string 3. copy the same `security.toml` file to the filers and all S3 proxies. If `filer_jwt.signing.key` is configured: When sending upload/update/delete HTTP operations to a filer server, the request header `Authorization` should be the JWT string (`Authorization: Bearer [JwtToken]`). The operation is authorized after the filer validates the JWT with `filer_jwt.signing.key`. If `filer_jwt.signing.read.key` is configured: When sending GET or HEAD requests to a filer server, the request header `Authorization` should be the JWT string (`Authorization: Bearer [JwtToken]`). The operation is authorized after the filer validates the JWT with `filer_jwt.signing.read.key`. The S3 API Gateway reads the above JWT keys and sends authenticated HTTP requests to the filer. ``` Page `Security Configuration`: ``` (update scaffold file) ... [filer_jwt.signing] key = "blahblahblahblah" [filer_jwt.signing.read] key = "blahblahblahblah" ``` Resolves: #158
3 years ago
3 years ago
FEATURE: add JWT to HTTP endpoints of Filer and use them in S3 Client - one JWT for reading and one for writing, analogous to how the JWT between Master and Volume Server works - I did not implement IP `whiteList` parameter on the filer Additionally, because http_util.DownloadFile now sets the JWT, the `download` command should now work when `jwt.signing.read` is configured. By looking at the code, I think this case did not work before. ## Docs to be adjusted after a release Page `Amazon-S3-API`: ``` # Authentication with Filer You can use mTLS for the gRPC connection between S3-API-Proxy and the filer, as explained in [Security-Configuration](Security-Configuration) - controlled by the `grpc.*` configuration in `security.toml`. Starting with version XX, it is also possible to authenticate the HTTP operations between the S3-API-Proxy and the Filer (especially uploading new files). This is configured by setting `filer_jwt.signing.key` and `filer_jwt.signing.read.key` in `security.toml`. With both configurations (gRPC and JWT), it is possible to have Filer and S3 communicate in fully authenticated fashion; so Filer will reject any unauthenticated communication. ``` Page `Security Overview`: ``` The following items are not covered, yet: - master server http REST services Starting with version XX, the Filer HTTP REST services can be secured with a JWT, by setting `filer_jwt.signing.key` and `filer_jwt.signing.read.key` in `security.toml`. ... Before version XX: "weed filer -disableHttp", disable http operations, only gRPC operations are allowed. This works with "weed mount" by FUSE. It does **not work** with the [S3 Gateway](Amazon S3 API), as this does HTTP calls to the Filer. Starting with version XX: secured by JWT, by setting `filer_jwt.signing.key` and `filer_jwt.signing.read.key` in `security.toml`. **This now works with the [S3 Gateway](Amazon S3 API).** ... # Securing Filer HTTP with JWT To enable JWT-based access control for the Filer, 1. generate `security.toml` file by `weed scaffold -config=security` 2. set `filer_jwt.signing.key` to a secret string - and optionally filer_jwt.signing.read.key` as well to a secret string 3. copy the same `security.toml` file to the filers and all S3 proxies. If `filer_jwt.signing.key` is configured: When sending upload/update/delete HTTP operations to a filer server, the request header `Authorization` should be the JWT string (`Authorization: Bearer [JwtToken]`). The operation is authorized after the filer validates the JWT with `filer_jwt.signing.key`. If `filer_jwt.signing.read.key` is configured: When sending GET or HEAD requests to a filer server, the request header `Authorization` should be the JWT string (`Authorization: Bearer [JwtToken]`). The operation is authorized after the filer validates the JWT with `filer_jwt.signing.read.key`. The S3 API Gateway reads the above JWT keys and sends authenticated HTTP requests to the filer. ``` Page `Security Configuration`: ``` (update scaffold file) ... [filer_jwt.signing] key = "blahblahblahblah" [filer_jwt.signing.read] key = "blahblahblahblah" ``` Resolves: #158
3 years ago
FEATURE: add JWT to HTTP endpoints of Filer and use them in S3 Client - one JWT for reading and one for writing, analogous to how the JWT between Master and Volume Server works - I did not implement IP `whiteList` parameter on the filer Additionally, because http_util.DownloadFile now sets the JWT, the `download` command should now work when `jwt.signing.read` is configured. By looking at the code, I think this case did not work before. ## Docs to be adjusted after a release Page `Amazon-S3-API`: ``` # Authentication with Filer You can use mTLS for the gRPC connection between S3-API-Proxy and the filer, as explained in [Security-Configuration](Security-Configuration) - controlled by the `grpc.*` configuration in `security.toml`. Starting with version XX, it is also possible to authenticate the HTTP operations between the S3-API-Proxy and the Filer (especially uploading new files). This is configured by setting `filer_jwt.signing.key` and `filer_jwt.signing.read.key` in `security.toml`. With both configurations (gRPC and JWT), it is possible to have Filer and S3 communicate in fully authenticated fashion; so Filer will reject any unauthenticated communication. ``` Page `Security Overview`: ``` The following items are not covered, yet: - master server http REST services Starting with version XX, the Filer HTTP REST services can be secured with a JWT, by setting `filer_jwt.signing.key` and `filer_jwt.signing.read.key` in `security.toml`. ... Before version XX: "weed filer -disableHttp", disable http operations, only gRPC operations are allowed. This works with "weed mount" by FUSE. It does **not work** with the [S3 Gateway](Amazon S3 API), as this does HTTP calls to the Filer. Starting with version XX: secured by JWT, by setting `filer_jwt.signing.key` and `filer_jwt.signing.read.key` in `security.toml`. **This now works with the [S3 Gateway](Amazon S3 API).** ... # Securing Filer HTTP with JWT To enable JWT-based access control for the Filer, 1. generate `security.toml` file by `weed scaffold -config=security` 2. set `filer_jwt.signing.key` to a secret string - and optionally filer_jwt.signing.read.key` as well to a secret string 3. copy the same `security.toml` file to the filers and all S3 proxies. If `filer_jwt.signing.key` is configured: When sending upload/update/delete HTTP operations to a filer server, the request header `Authorization` should be the JWT string (`Authorization: Bearer [JwtToken]`). The operation is authorized after the filer validates the JWT with `filer_jwt.signing.key`. If `filer_jwt.signing.read.key` is configured: When sending GET or HEAD requests to a filer server, the request header `Authorization` should be the JWT string (`Authorization: Bearer [JwtToken]`). The operation is authorized after the filer validates the JWT with `filer_jwt.signing.read.key`. The S3 API Gateway reads the above JWT keys and sends authenticated HTTP requests to the filer. ``` Page `Security Configuration`: ``` (update scaffold file) ... [filer_jwt.signing] key = "blahblahblahblah" [filer_jwt.signing.read] key = "blahblahblahblah" ``` Resolves: #158
3 years ago
3 years ago
3 years ago
4 years ago
3 years ago
5 years ago
  1. package s3api
  2. import (
  3. "bytes"
  4. "fmt"
  5. "github.com/seaweedfs/seaweedfs/weed/filer"
  6. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  7. "io"
  8. "net/http"
  9. "net/url"
  10. "strings"
  11. "time"
  12. "github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants"
  13. "github.com/seaweedfs/seaweedfs/weed/s3api/s3err"
  14. "github.com/seaweedfs/seaweedfs/weed/util/mem"
  15. "github.com/seaweedfs/seaweedfs/weed/glog"
  16. "github.com/seaweedfs/seaweedfs/weed/util"
  17. )
  18. func mimeDetect(r *http.Request, dataReader io.Reader) io.ReadCloser {
  19. mimeBuffer := make([]byte, 512)
  20. size, _ := dataReader.Read(mimeBuffer)
  21. if size > 0 {
  22. r.Header.Set("Content-Type", http.DetectContentType(mimeBuffer[:size]))
  23. return io.NopCloser(io.MultiReader(bytes.NewReader(mimeBuffer[:size]), dataReader))
  24. }
  25. return io.NopCloser(dataReader)
  26. }
  27. func urlEscapeObject(object string) string {
  28. t := urlPathEscape(removeDuplicateSlashes(object))
  29. if strings.HasPrefix(t, "/") {
  30. return t
  31. }
  32. return "/" + t
  33. }
  34. func entryUrlEncode(dir string, entry string, encodingTypeUrl bool) (dirName string, entryName string, prefix string) {
  35. if !encodingTypeUrl {
  36. return dir, entry, entry
  37. }
  38. return urlPathEscape(dir), url.QueryEscape(entry), urlPathEscape(entry)
  39. }
  40. func urlPathEscape(object string) string {
  41. var escapedParts []string
  42. for _, part := range strings.Split(object, "/") {
  43. escapedParts = append(escapedParts, strings.ReplaceAll(url.PathEscape(part), "+", "%2B"))
  44. }
  45. return strings.Join(escapedParts, "/")
  46. }
  47. func removeDuplicateSlashes(object string) string {
  48. result := strings.Builder{}
  49. result.Grow(len(object))
  50. isLastSlash := false
  51. for _, r := range object {
  52. switch r {
  53. case '/':
  54. if !isLastSlash {
  55. result.WriteRune(r)
  56. }
  57. isLastSlash = true
  58. default:
  59. result.WriteRune(r)
  60. isLastSlash = false
  61. }
  62. }
  63. return result.String()
  64. }
  65. func newListEntry(entry *filer_pb.Entry, key string, dir string, name string, bucketPrefix string, fetchOwner bool, isDirectory bool) (listEntry ListEntry) {
  66. storageClass := "STANDARD"
  67. if v, ok := entry.Extended[s3_constants.AmzStorageClass]; ok {
  68. storageClass = string(v)
  69. }
  70. keyFormat := "%s/%s"
  71. if isDirectory {
  72. keyFormat += "/"
  73. }
  74. if key == "" {
  75. key = fmt.Sprintf(keyFormat, dir, name)[len(bucketPrefix):]
  76. }
  77. listEntry = ListEntry{
  78. Key: key,
  79. LastModified: time.Unix(entry.Attributes.Mtime, 0).UTC(),
  80. ETag: "\"" + filer.ETag(entry) + "\"",
  81. Size: int64(filer.FileSize(entry)),
  82. StorageClass: StorageClass(storageClass),
  83. }
  84. if fetchOwner {
  85. listEntry.Owner = CanonicalUser{
  86. ID: fmt.Sprintf("%x", entry.Attributes.Uid),
  87. DisplayName: entry.Attributes.UserName,
  88. set: true,
  89. }
  90. }
  91. return listEntry
  92. }
  93. func (s3a *S3ApiServer) toFilerUrl(bucket, object string) string {
  94. object = urlPathEscape(removeDuplicateSlashes(object))
  95. destUrl := fmt.Sprintf("http://%s%s/%s%s",
  96. s3a.option.Filer.ToHttpAddress(), s3a.option.BucketsPath, bucket, object)
  97. return destUrl
  98. }
  99. func (s3a *S3ApiServer) GetObjectHandler(w http.ResponseWriter, r *http.Request) {
  100. bucket, object := s3_constants.GetBucketAndObject(r)
  101. glog.V(3).Infof("GetObjectHandler %s %s", bucket, object)
  102. if strings.HasSuffix(r.URL.Path, "/") {
  103. s3err.WriteErrorResponse(w, r, s3err.ErrNotImplemented)
  104. return
  105. }
  106. destUrl := s3a.toFilerUrl(bucket, object)
  107. s3a.proxyToFiler(w, r, destUrl, false, passThroughResponse)
  108. }
  109. func (s3a *S3ApiServer) HeadObjectHandler(w http.ResponseWriter, r *http.Request) {
  110. bucket, object := s3_constants.GetBucketAndObject(r)
  111. glog.V(3).Infof("HeadObjectHandler %s %s", bucket, object)
  112. destUrl := s3a.toFilerUrl(bucket, object)
  113. s3a.proxyToFiler(w, r, destUrl, false, passThroughResponse)
  114. }
  115. func (s3a *S3ApiServer) proxyToFiler(w http.ResponseWriter, r *http.Request, destUrl string, isWrite bool, responseFn func(proxyResponse *http.Response, w http.ResponseWriter) (statusCode int)) {
  116. glog.V(3).Infof("s3 proxying %s to %s", r.Method, destUrl)
  117. start := time.Now()
  118. proxyReq, err := http.NewRequest(r.Method, destUrl, r.Body)
  119. if err != nil {
  120. glog.Errorf("NewRequest %s: %v", destUrl, err)
  121. s3err.WriteErrorResponse(w, r, s3err.ErrInternalError)
  122. return
  123. }
  124. proxyReq.Header.Set("X-Forwarded-For", r.RemoteAddr)
  125. for k, v := range r.URL.Query() {
  126. if _, ok := s3_constants.PassThroughHeaders[strings.ToLower(k)]; ok {
  127. proxyReq.Header[k] = v
  128. }
  129. if k == "partNumber" {
  130. proxyReq.Header[s3_constants.SeaweedFSPartNumber] = v
  131. }
  132. }
  133. for header, values := range r.Header {
  134. proxyReq.Header[header] = values
  135. }
  136. // ensure that the Authorization header is overriding any previous
  137. // Authorization header which might be already present in proxyReq
  138. s3a.maybeAddFilerJwtAuthorization(proxyReq, isWrite)
  139. resp, postErr := s3a.client.Do(proxyReq)
  140. if postErr != nil {
  141. glog.Errorf("post to filer: %v", postErr)
  142. s3err.WriteErrorResponse(w, r, s3err.ErrInternalError)
  143. return
  144. }
  145. defer util.CloseResponse(resp)
  146. if resp.StatusCode == http.StatusPreconditionFailed {
  147. s3err.WriteErrorResponse(w, r, s3err.ErrPreconditionFailed)
  148. return
  149. }
  150. if resp.StatusCode == http.StatusRequestedRangeNotSatisfiable {
  151. s3err.WriteErrorResponse(w, r, s3err.ErrInvalidRange)
  152. return
  153. }
  154. if r.Method == "DELETE" {
  155. if resp.StatusCode == http.StatusNotFound {
  156. // this is normal
  157. responseStatusCode := responseFn(resp, w)
  158. s3err.PostLog(r, responseStatusCode, s3err.ErrNone)
  159. return
  160. }
  161. }
  162. if resp.StatusCode == http.StatusNotFound {
  163. s3err.WriteErrorResponse(w, r, s3err.ErrNoSuchKey)
  164. return
  165. }
  166. TimeToFirstByte(r.Method, start, r)
  167. if resp.Header.Get(s3_constants.SeaweedFSIsDirectoryKey) == "true" {
  168. responseStatusCode := responseFn(resp, w)
  169. s3err.PostLog(r, responseStatusCode, s3err.ErrNone)
  170. return
  171. }
  172. if resp.StatusCode == http.StatusInternalServerError {
  173. s3err.WriteErrorResponse(w, r, s3err.ErrInternalError)
  174. return
  175. }
  176. // when HEAD a directory, it should be reported as no such key
  177. // https://github.com/seaweedfs/seaweedfs/issues/3457
  178. if resp.ContentLength == -1 && resp.StatusCode != http.StatusNotModified {
  179. s3err.WriteErrorResponse(w, r, s3err.ErrNoSuchKey)
  180. return
  181. }
  182. if resp.StatusCode == http.StatusBadRequest {
  183. resp_body, _ := io.ReadAll(resp.Body)
  184. switch string(resp_body) {
  185. case "InvalidPart":
  186. s3err.WriteErrorResponse(w, r, s3err.ErrInvalidPart)
  187. default:
  188. s3err.WriteErrorResponse(w, r, s3err.ErrInvalidRequest)
  189. }
  190. resp.Body.Close()
  191. return
  192. }
  193. setUserMetadataKeyToLowercase(resp)
  194. responseStatusCode := responseFn(resp, w)
  195. s3err.PostLog(r, responseStatusCode, s3err.ErrNone)
  196. }
  197. func setUserMetadataKeyToLowercase(resp *http.Response) {
  198. for key, value := range resp.Header {
  199. if strings.HasPrefix(key, s3_constants.AmzUserMetaPrefix) {
  200. resp.Header[strings.ToLower(key)] = value
  201. delete(resp.Header, key)
  202. }
  203. }
  204. }
  205. func passThroughResponse(proxyResponse *http.Response, w http.ResponseWriter) (statusCode int) {
  206. for k, v := range proxyResponse.Header {
  207. w.Header()[k] = v
  208. }
  209. if proxyResponse.Header.Get("Content-Range") != "" && proxyResponse.StatusCode == 200 {
  210. w.WriteHeader(http.StatusPartialContent)
  211. statusCode = http.StatusPartialContent
  212. } else {
  213. statusCode = proxyResponse.StatusCode
  214. }
  215. w.WriteHeader(statusCode)
  216. buf := mem.Allocate(128 * 1024)
  217. defer mem.Free(buf)
  218. if n, err := io.CopyBuffer(w, proxyResponse.Body, buf); err != nil {
  219. glog.V(1).Infof("passthrough response read %d bytes: %v", n, err)
  220. }
  221. return statusCode
  222. }