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.

219 lines
6.2 KiB

5 years ago
7 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
  1. package command
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/chrislusf/seaweedfs/weed/s3api/s3err"
  6. "net/http"
  7. "time"
  8. "github.com/chrislusf/seaweedfs/weed/pb"
  9. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  10. "github.com/chrislusf/seaweedfs/weed/security"
  11. "github.com/gorilla/mux"
  12. "github.com/chrislusf/seaweedfs/weed/glog"
  13. "github.com/chrislusf/seaweedfs/weed/s3api"
  14. stats_collect "github.com/chrislusf/seaweedfs/weed/stats"
  15. "github.com/chrislusf/seaweedfs/weed/util"
  16. )
  17. var (
  18. s3StandaloneOptions S3Options
  19. )
  20. type S3Options struct {
  21. filer *string
  22. port *int
  23. config *string
  24. domainName *string
  25. tlsPrivateKey *string
  26. tlsCertificate *string
  27. metricsHttpPort *int
  28. allowEmptyFolder *bool
  29. auditLogConfig *string
  30. }
  31. func init() {
  32. cmdS3.Run = runS3 // break init cycle
  33. s3StandaloneOptions.filer = cmdS3.Flag.String("filer", "localhost:8888", "filer server address")
  34. s3StandaloneOptions.port = cmdS3.Flag.Int("port", 8333, "s3 server http listen port")
  35. s3StandaloneOptions.domainName = cmdS3.Flag.String("domainName", "", "suffix of the host name in comma separated list, {bucket}.{domainName}")
  36. s3StandaloneOptions.config = cmdS3.Flag.String("config", "", "path to the config file")
  37. s3StandaloneOptions.auditLogConfig = cmdS3.Flag.String("auditLogConfig", "", "path to the audit log config file")
  38. s3StandaloneOptions.tlsPrivateKey = cmdS3.Flag.String("key.file", "", "path to the TLS private key file")
  39. s3StandaloneOptions.tlsCertificate = cmdS3.Flag.String("cert.file", "", "path to the TLS certificate file")
  40. s3StandaloneOptions.metricsHttpPort = cmdS3.Flag.Int("metricsPort", 0, "Prometheus metrics listen port")
  41. s3StandaloneOptions.allowEmptyFolder = cmdS3.Flag.Bool("allowEmptyFolder", true, "allow empty folders")
  42. }
  43. var cmdS3 = &Command{
  44. UsageLine: "s3 [-port=8333] [-filer=<ip:port>] [-config=</path/to/config.json>]",
  45. Short: "start a s3 API compatible server that is backed by a filer",
  46. Long: `start a s3 API compatible server that is backed by a filer.
  47. By default, you can use any access key and secret key to access the S3 APIs.
  48. To enable credential based access, create a config.json file similar to this:
  49. {
  50. "identities": [
  51. {
  52. "name": "anonymous",
  53. "actions": [
  54. "Read"
  55. ]
  56. },
  57. {
  58. "name": "some_admin_user",
  59. "credentials": [
  60. {
  61. "accessKey": "some_access_key1",
  62. "secretKey": "some_secret_key1"
  63. }
  64. ],
  65. "actions": [
  66. "Admin",
  67. "Read",
  68. "List",
  69. "Tagging",
  70. "Write"
  71. ]
  72. },
  73. {
  74. "name": "some_read_only_user",
  75. "credentials": [
  76. {
  77. "accessKey": "some_access_key2",
  78. "secretKey": "some_secret_key2"
  79. }
  80. ],
  81. "actions": [
  82. "Read"
  83. ]
  84. },
  85. {
  86. "name": "some_normal_user",
  87. "credentials": [
  88. {
  89. "accessKey": "some_access_key3",
  90. "secretKey": "some_secret_key3"
  91. }
  92. ],
  93. "actions": [
  94. "Read",
  95. "List",
  96. "Tagging",
  97. "Write"
  98. ]
  99. },
  100. {
  101. "name": "user_limited_to_bucket1",
  102. "credentials": [
  103. {
  104. "accessKey": "some_access_key4",
  105. "secretKey": "some_secret_key4"
  106. }
  107. ],
  108. "actions": [
  109. "Read:bucket1",
  110. "List:bucket1",
  111. "Tagging:bucket1",
  112. "Write:bucket1"
  113. ]
  114. }
  115. ]
  116. }
  117. `,
  118. }
  119. func runS3(cmd *Command, args []string) bool {
  120. util.LoadConfiguration("security", false)
  121. go stats_collect.StartMetricsServer(*s3StandaloneOptions.metricsHttpPort)
  122. return s3StandaloneOptions.startS3Server()
  123. }
  124. func (s3opt *S3Options) startS3Server() bool {
  125. filerAddress := pb.ServerAddress(*s3opt.filer)
  126. filerBucketsPath := "/buckets"
  127. grpcDialOption := security.LoadClientTLS(util.GetViper(), "grpc.client")
  128. // metrics read from the filer
  129. var metricsAddress string
  130. var metricsIntervalSec int
  131. for {
  132. err := pb.WithGrpcFilerClient(filerAddress, grpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
  133. resp, err := client.GetFilerConfiguration(context.Background(), &filer_pb.GetFilerConfigurationRequest{})
  134. if err != nil {
  135. return fmt.Errorf("get filer %s configuration: %v", filerAddress, err)
  136. }
  137. filerBucketsPath = resp.DirBuckets
  138. metricsAddress, metricsIntervalSec = resp.MetricsAddress, int(resp.MetricsIntervalSec)
  139. glog.V(0).Infof("S3 read filer buckets dir: %s", filerBucketsPath)
  140. return nil
  141. })
  142. if err != nil {
  143. glog.V(0).Infof("wait to connect to filer %s grpc address %s", *s3opt.filer, filerAddress.ToGrpcAddress())
  144. time.Sleep(time.Second)
  145. } else {
  146. glog.V(0).Infof("connected to filer %s grpc address %s", *s3opt.filer, filerAddress.ToGrpcAddress())
  147. break
  148. }
  149. }
  150. go stats_collect.LoopPushingMetric("s3", stats_collect.SourceName(uint32(*s3opt.port)), metricsAddress, metricsIntervalSec)
  151. router := mux.NewRouter().SkipClean(true)
  152. _, s3ApiServer_err := s3api.NewS3ApiServer(router, &s3api.S3ApiServerOption{
  153. Filer: filerAddress,
  154. Port: *s3opt.port,
  155. Config: *s3opt.config,
  156. DomainName: *s3opt.domainName,
  157. BucketsPath: filerBucketsPath,
  158. GrpcDialOption: grpcDialOption,
  159. AllowEmptyFolder: *s3opt.allowEmptyFolder,
  160. })
  161. if s3ApiServer_err != nil {
  162. glog.Fatalf("S3 API Server startup error: %v", s3ApiServer_err)
  163. }
  164. httpS := &http.Server{Handler: router}
  165. listenAddress := fmt.Sprintf(":%d", *s3opt.port)
  166. s3ApiListener, err := util.NewListener(listenAddress, time.Duration(10)*time.Second)
  167. if err != nil {
  168. glog.Fatalf("S3 API Server listener on %s error: %v", listenAddress, err)
  169. }
  170. if len(*s3opt.auditLogConfig) > 0 {
  171. s3err.InitAuditLog(*s3opt.auditLogConfig)
  172. if s3err.Logger != nil {
  173. defer s3err.Logger.Close()
  174. }
  175. }
  176. if *s3opt.tlsPrivateKey != "" {
  177. glog.V(0).Infof("Start Seaweed S3 API Server %s at https port %d", util.Version(), *s3opt.port)
  178. if err = httpS.ServeTLS(s3ApiListener, *s3opt.tlsCertificate, *s3opt.tlsPrivateKey); err != nil {
  179. glog.Fatalf("S3 API Server Fail to serve: %v", err)
  180. }
  181. } else {
  182. glog.V(0).Infof("Start Seaweed S3 API Server %s at http port %d", util.Version(), *s3opt.port)
  183. if err = httpS.Serve(s3ApiListener); err != nil {
  184. glog.Fatalf("S3 API Server Fail to serve: %v", err)
  185. }
  186. }
  187. return true
  188. }